This Go package provides a client to interact with the Chargily API, allowing you to manage balances, customers, products, prices, checkouts, and payment links.
Chargily Pay™ packages/plugins are a collection of open source projects published by Chargily to facilitate the integration of our payment gateway into different programming languages and frameworks. Our goal is to empower developers and businesses by providing easy-to-use tools to seamlessly accept payments.
For detailed instructions on how to integrate with our API and utilize Chargily Pay™ in your projects, please refer to our API Documentation.
To install the package, use the following command:
go get github.com/Chargily/chargily-pay-go
To create a new Chargily API client, you need to provide an API key and the mode (prod for production or test for development):
client, err := chargily.NewClient("your-api-key", "prod")
if err != nil {
log.Fatalf("Failed to initialize client: %v", err)
}
You can retrieve your account balance using:
balance, err := client.GetBalance()
if err != nil {
log.Fatalf("Error fetching balance: %v", err)
}
fmt.Printf("Account Balance: %v\n", balance)
// Create a new customer
customerParams := &chargily.CreateCustomerParams{
Name: "John Doe",
Email: "john.doe@example.com",
Phone: "+1 123 456 7890",
Address: &chargily.Address{
Address: "123 Main St",
City: "New York",
Country: "US",
},
Metadata: map[string]any{
"custom_field": "value",
},
}
customer, err := client.CreateCustomer(customerParams)
if err!= nil {
fmt.Printf("Error creating customer: %v\n", err)
}
// update an existing customer
customerParams := &chargily.CreateCustomerParams{
Name: "John Doe Doe",
Email: "john.doe-updated@example.com",
Phone: "+1 123 456 7891",
Address: &chargily.Address{
Address: "123 Main St",
City: "New York",
Country: "US",
},
Metadata: map[string]any{
"custom_field": "value",
},
}
// Get a customer by ID
customerID := string("your-customer-id") // you should always provide a valid customer ID that already Exists
customer, err := client.UpdateCustomer(customerID,customerParams)
if err!= nil {
fmt.Printf("Error getting customer: %v\n", err)
return
}
customer, err := client.GetCustomer(string("customer_id"))
if err != nil {
fmt.Println("Error retrieving customer:", err)
return
}
fmt.Println("Customer:", customer)
customers, err := client.GetAllCustomers()
if err != nil {
fmt.Println("Error fetching customers:", err)
return
}
fmt.Println("Customers:", customers)
// Create a new product
product := &chargily.CreateProductParams{
Name: "New Product",
Description: "This is a new product",
Images: []string{"link1","link2","link3"},
Metadata: map[string]any{"key0": "value0","key1":"value1"},
}
newProduct, err := client.CreateProduct(product)
if err!= nil {
fmt.Printf("Error creating product: %v\n", err)
return
}
// new data
product := &chargily.CreateProductParams{
Name: "updated Product",
Description: "This is an updates product",
Images: []string{"link1","link2","link3","link4"},
Metadata: map[string]any{"key0": "value0"},
}
// the id of the product to update
id := "your-product-id" // make sure to provide a valid id for the product
// Update the product
updatedProduct, err := client.UpdateProduct(string(id), product)
if err!= nil {
fmt.Printf("Error updating product: %v\n", err)
return
}
product, err := client.GetProduct("product_id")
if err != nil {
fmt.Println("Error retrieving product:", err)
return
}
// use the product data
fmt.Println("Product:", product)
products, err := client.GetAllProducts()
if err != nil {
fmt.Println("Error fetching products:", err)
return
}
fmt.Println("Products:", products)
//string() method used here to force the id to be always string
err := client.DeleteProduct(string("product_id"))
if err != nil {
fmt.Println("Error deleting product:", err)
return
}
fmt.Println("Product deleted successfully.")
prices, err := client.GetProductPrices("product_id")
if err != nil {
fmt.Println("Error retrieving product prices:", err)
return
}
fmt.Println("Product Prices:", prices)
// Create a new customer
price := &chargily.ProductPriceParams{
ProductID: "your-product-id",
Currency: "USD",
Amount: 100,
Metadata: map[string]any{"key":"value"},
}
// create the product price and get back the price object
productPrice, err := client.CreatePrice(price)
if err!= nil {
fmt.Printf("Error creating price: %v\n", err)
}
// Create a new customer
price := &chargily.UpdatePriceMetaDataParams{
Metadata: map[string]any{"key":"val"},
}
//product ID
prodId := "your-product-id"
// update price metadata
productPrice, err := client.UpdatePrice(prodId,price)
if err!= nil {
fmt.Printf("Error updating price: %v\n", err)
}
price, err := client.GetPrice("product_id")
if err != nil {
fmt.Println("Error retrieving price:", err)
return
}
fmt.Println("Price:", price)
prices, err := client.GetAllPrices()
if err != nil {
fmt.Println("Error fetching prices:", err)
return
}
fmt.Println("Prices:", prices)
// Create example items to be added to the checkout.
items := []chargily.CItems{
{
Price: string(priceID),
Quantity: 2,
},
}
// Initialize the CheckoutParams struct with adjusted fields.
checkoutParams := &chargily.CheckoutParams{
Items: items,
PaymentMethod: "edahabia",
SuccessURL: "https://your-site.com/success",
FailureURL: "https://your-site.com/failure",
WebhookEndpoint: "https://your-site.com/webhook",
Description: "Checkout for Order #12345",
Locale: "en",
PercentageDiscount: 10,
}
// Create the checkout
checkoutdata, err := client.CreateCheckout(checkoutParams)
if err!= nil {
fmt.Printf("Error creating checkout: %v\n", err)
return
}
checkout, err := client.GetCheckout("checkout_id")
if err != nil {
fmt.Println("Error retrieving checkout:", err)
return
}
fmt.Println("Checkout:", checkout)
checkouts, err := client.GetAllCheckouts()
if err != nil {
fmt.Println("Error fetching checkouts:", err)
return
}
fmt.Println("Checkouts:", checkouts)
items, err := client.GetCheckoutItems("checkout_id")
if err != nil {
fmt.Println("Error retrieving checkout items:", err)
return
}
fmt.Println("Checkout Items:", items)
expiredCheckout, err := client.ExpireCheckout("checkout_id")
if err != nil {
fmt.Println("Error expiring checkout:", err)
return
}
fmt.Println("Checkout expired:", expiredCheckout)
// Create items to be added to the payment link
items := []chargily.PItems{
{
Price: "01j9k9m78jp18bdky07s0rxxtg", //id example
Quantity: 2,
AdjustableQuantity: true,
},
}
// Create the payment link parameters
paymentLinkParams := &chargily.CreatePaymentLinkParams{
Name: "Test Order for Payment Link",
Items: items,
AfterCompletionMessage: "Thank you for your order!",
Locale: "en",
PassFeesToCustomer: false,
CollectShippingAddress: 1,
Metadata: map[string]any{
"order_id": "order_54321",
"notes": "This is a test order for payment link.",
},
}
// Create the payment link
paymentLink, err := client.CreatePaymentLink(paymentLinkParams)
if err!= nil {
fmt.Printf("Error creating payment link: %v\n", err)
return
}
// the items to add to a payment link
items := []chargily.PItems{
{
Price: "01j9k9m78jp18bdky07s0rxxtg",
Quantity: 2,
AdjustableQuantity: true,
},
}
// updated payment link data
paymentLinkParams := &chargily.CreatePaymentLinkParams{
Name: "Test Order for Payment Link",
Items: items,
AfterCompletionMessage: "Thank you for your order!",
Locale: "en",
PassFeesToCustomer: true,
CollectShippingAddress: 0,
Metadata: map[string]any{
"order_id": "order_54321",
"notes": "This is a test order for payment link.",
},
}
// the id of payment id to be updated
paymentLinkId:= "your-payment-link-id"
// update
paymentLink, err := client.UpdatePaymentLink(paymentLinkId,paymentLinkParams)
if err!= nil {
fmt.Printf("Error updating payment link: %v\n", err)
return
}
// payment link id
paymentLinkId := "your-payment-link-id" // make sure to be a valid payment link id
// retrieve the payment link
paymentLink, err := client.GetPaymentLink(paymentLinkId)
if err!= nil {
fmt.Printf("Error retrieving payment link by ID: %v\n", err)
return
}
// Create a new client instance
client, err := chargily.NewClient(apiKey, mode)
if err!= nil {
fmt.Printf("Error creating client: %v\n", err)
return
}
// retrieve all payment links
paymentLinks, err := client.GetAllPaymentLinks()
if err!= nil {
fmt.Printf("Error retrieving all payment links: %v\n", err)
return
}
//payment link ID
paymentLinkId := "existing-payment-link-id" //must be unique and existing
// retrieve a payment link's items
paymentLinkItems, err := client.GetPaymentLinkItems(string(paymentLinkId))
if err!= nil {
fmt.Printf("Error getting payment link items: %v\n", err)
return
}
Join our developer community on Telegram to connect with fellow developers, ask questions, and stay updated on the latest news and developments related to Chargily Pay™ : Telegram Community
We welcome contributions of all kinds, whether it's bug fixes, feature enhancements, documentation improvements, or new plugin/package developments. Here's how you can get started:
Fork the Repository: Click the "Fork" button in the top-right corner of this page to create your own copy of the repository.
Clone the Repository: Clone your forked repository to your local machine using the following command:
git clone https://github.com/Chargily/chargily-pay-go.git
Make Changes: Make your desired changes or additions to the codebase. Be sure to follow our coding standards and guidelines.
Test Your Changes: Test your changes thoroughly to ensure they work as expected.
Submit a Pull Request: Once you're satisfied with your changes, submit a pull request back to the main repository. Our team will review your contributions and provide feedback if needed.
Have questions or need assistance? Join our developer community on Telegram and connect with fellow developers and our team.
We appreciate your interest in contributing to Chargily Pay™! Together, we can build something amazing.
Happy coding!