Go Library (omise-go)
The omise-go library provides an idiomatic Go interface to the Omise API with support for goroutines, channels, context-aware operations, and modern Go best practices.
Installationโ
Using go getโ
go get github.com/omise/omise-go/v2
Using go.modโ
require github.com/omise/omise-go/v2 v2.0.0
Requirementsโ
- Go 1.16 or higher (including Go 1.20+)
- Go modules for dependency management
Quick Startโ
Basic Configurationโ
package main
import (
"github.com/omise/omise-go/v2"
"github.com/omise/omise-go/v2/operations"
)
func main() {
client, err := omise.NewClient(
"pkey_test_123456789",
"skey_test_123456789",
)
if err != nil {
panic(err)
}
}
With Environment Variablesโ
import (
"os"
"github.com/omise/omise-go/v2"
)
func initClient() (*omise.Client, error) {
return omise.NewClient(
os.Getenv("OMISE_PUBLIC_KEY"),
os.Getenv("OMISE_SECRET_KEY"),
)
}
With Configuration Structโ
type Config struct {
OmisePublicKey string
OmiseSecretKey string
APIVersion string
}
func NewOmiseClient(config Config) (*omise.Client, error) {
client, err := omise.NewClient(
config.OmisePublicKey,
config.OmiseSecretKey,
)
if err != nil {
return nil, err
}
client.SetAPIVersion(config.APIVersion)
return client, nil
}
Environment Variablesโ
# Development/Test
export OMISE_SECRET_KEY=skey_test_123456789
export OMISE_PUBLIC_KEY=pkey_test_123456789
# Production
# export OMISE_SECRET_KEY=skey_live_123456789
# export OMISE_PUBLIC_KEY=pkey_live_123456789
Common Operationsโ
Creating a Chargeโ
package main
import (
"context"
"fmt"
"github.com/omise/omise-go/v2"
"github.com/omise/omise-go/v2/operations"
)
func createCharge(client *omise.Client, token string, amount int64) (*omise.Charge, error) {
charge, createCharge := &omise.Charge{}, &operations.CreateCharge{
Amount: amount, // 1,000.00 THB = 100000 satang
Currency: "THB",
Card: token,
Description: "Order #1234",
Metadata: map[string]interface{}{
"order_id": "1234",
"customer_name": "John Doe",
},
}
if err := client.Do(charge, createCharge); err != nil {
return nil, fmt.Errorf("charge creation failed: %w", err)
}
if charge.Paid {
fmt.Printf("Charge successful: %s\n", charge.ID)
} else {
fmt.Printf("Charge failed: %s\n", charge.FailureMessage)
}
return charge, nil
}