-
Notifications
You must be signed in to change notification settings - Fork 14
/
checkout_sdk.go
62 lines (54 loc) · 1.54 KB
/
checkout_sdk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package checkout
import (
"context"
"github.com/checkout/checkout-sdk-go"
"github.com/checkout/checkout-sdk-go/payments"
)
type Checkout struct {
ctx context.Context
environment string
secretKey string
publicKey *string
webhookSecrets map[string]string
}
func NewCheckout(secretKey string, publicKey *string, environment string, webhookSecrets map[string]string) *Checkout {
return &Checkout{
ctx: context.Background(),
environment: environment,
secretKey: secretKey,
publicKey: publicKey,
webhookSecrets: webhookSecrets,
}
}
func (c *Checkout) RequestPayment(source interface{}, amount uint64, currency string, reference string, customer *payments.Customer, metadata map[string]string) *payments.Response {
config, err := checkout.Create(c.secretKey, c.publicKey)
idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
IdempotencyKey: &idempotencyKey,
}
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
var client = payments.NewClient(*config)
var request = &payments.Request{
Amount: amount,
Source: source,
Currency: currency,
Reference: reference,
Customer: customer,
Metadata: metadata,
}
response, err := client.Request(request, ¶ms)
if err != nil {
panic("checkout.com new payment request error: " + err.Error())
}
return response
}
func (c *Checkout) CheckWebhookKey(keyCode, key string) bool {
if value, found := c.webhookSecrets[keyCode]; found {
if value == key {
return true
}
}
return false
}