Go SDK

Go SDK v0.0.29

The OpenPay Go SDK is a client library that streamlines integration with the OpenPay payment platform. It offers straightforward access to OpenPay’s APIs for processing payments, managing customers, and handling various payment-related tasks.

This reference provides comprehensive documentation for the OpenPay Go SDK beyond what’s available in the repository README. For the source code and further details, please visit the project on GitHub.

To get started with the Go SDK, follow the steps below:

Setup

Install the OpenPay Go SDK using go get:

go get github.com/stretchr/testify/assert
go get golang.org/x/net/context
go get github.com/getopenpay/getopenpay-go

Next, import the SDK package to your project:

import "github.com/getopenpay/getopenpay-go"

The SDK requires Go modules, you can run go get github.com/getopenpay/getopenpay-go@latest to pull the library.

Authentication

OpenPay uses HTTP Bearer token authentication. To authenticate, set your secret API token into the request context using the getopenpay.ContextAccessToken key. For example:


ctx := context.WithValue(context.Background(), getopenpay.ContextAccessToken, "YOUR_BEARER_TOKEN")

All API calls should use this ctx. For instance:

client := getopenpay.NewAPIClient(getopenpay.NewConfiguration())
customer, resp, err := client.CustomersApi.GetCustomer(ctx, "cust_123")

This ensures the Authorization: Bearer YOUR_BEARER_TOKEN header is sent.

The example in the docs shows setting the context with ContextAccessToken and then calling a client method with that context.

Usage examples

First, create a client and set your API token in context:

cfg := getopenpay.NewConfiguration()
client := getopenpay.NewAPIClient(cfg)
ctx := context.WithValue(context.Background(), getopenpay.ContextAccessToken, "sk_test_xxx")  // your secret key

You can then call any service. For example, creating a customer:

createReq := getopenpay.CreateCustomerRequest{
    Name:       getopenpay.PtrString("Opemipo Disu"),
    Email:      getopenpay.PtrString("opemipo@gmail.com"),
    // ... other fields (all fields are pointers; use Ptr helpers) ...
}
customer, _, err := client.CustomersApi.CreateCustomer(ctx, createReq)
if err != nil {
    fmt.Println("Error creating customer:", err)
} else {
    fmt.Println("Created customer ID:", *customer.Id)
}

Here we use the PtrString helper (see Other Considerations) to supply pointer values. To retrieve a customer, use:

customerID := "cust_123"
cust, _, err := client.CustomersApi.GetCustomer(ctx, customerID)

For subscriptions, you might create one like:

subReq := getopenpay.CreateSubscriptionRequest{
    Customer: getopenpay.PtrString("cust_123"),
    PlanId:   getopenpay.PtrString("plan_gold"),
    // ...
}
subscription, _, err := client.SubscriptionsApi.CreateSubscriptions(ctx, subReq)

And to attach a payment method (token) to a customer:

attachReq := getopenpay.AttachPaymentMethodRequest{
    CustomerId:      getopenpay.PtrString("cust_123"),
    PaymentMethodId: getopenpay.PtrString("pm_abc123"),
}
pm, _, err := client.PaymentMethodsApi.AttachPaymentMethod(ctx, "pm_abc123", attachReq)

Each method returns a response object and an error. Handle errors appropriately (see Error Handling below) to inspect API failures.

Error handling

All SDK methods return an error alongside the response. On success, err will be nil; on failure, err will contain details. Typically, API errors return a GenericOpenAPIError which includes the raw HTTP body and status. To handle errors, check the error first. For example:

cust, resp, err := client.CustomersApi.GetCustomer(ctx, "cust_123")
if err != nil {
    // You can inspect the error message or parse details from resp
    fmt.Println("API error:", err)
    return
}

If needed, you can type-assert err to getopenpay.GenericOpenAPIError to access the response body:

if apiErr, ok := err.(getopenpay.GenericOpenAPIError); ok {
    fmt.Println("Error status:", apiErr.Error(), "Body:", string(apiErr.Body()))
}

Validation errors (model ValidationError) may be returned if request data is invalid. Always handle err before using the returned object.The SDK example usage shows r, err := client.Service.Operation(...) with error check.

Testing

The repository includes example tests. To run them, edit test/sanity_test.go and replace the placeholder TODO_ADD_BEARER_KEY with your own secret API token. Then run:

go build
go test -v

This will execute the tests against the API (by default, the OpenPay sandbox). For writing new tests, use Go’s testing framework (testing package) and the assert library (already listed as a dependency) for convenience. The SDK’s models and methods can be invoked in tests just like in code examples above.

Reference

OpenPay SDKs

In this directory, you'll find OpenPay's official SDKs designed to simplify your integration with our APIs.