-
Notifications
You must be signed in to change notification settings - Fork 2
/
paddle.go
70 lines (57 loc) · 1.64 KB
/
paddle.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
63
64
65
66
67
68
69
70
// Package paddle provides the official SDK for using the Paddle Billing API.
package paddle
import (
"context"
"net/url"
"github.com/PaddleHQ/paddle-go-sdk/v3/internal/client"
"github.com/hashicorp/go-cleanhttp"
)
// Doer is an interface that is used to make requests to the Paddle API.
// This is of the custom client used by the SDK, instead of the standard
// http.Client Do method.
type Doer interface {
Do(ctx context.Context, method, path string, src, dst any) error
}
const (
// ProductionBaseURL is the base URL for the production Paddle API.
ProductionBaseURL = "https://api.paddle.com"
// SandboxBaseURL is the base URL for the sandbox Paddle API.
SandboxBaseURL = "https://sandbox-api.paddle.com"
)
// New creates a new Paddle SDK with the given API key.
func New(apiKey string, opts ...Option) (*SDK, error) {
return bootstrapSDK(
append([]Option{
WithAPIKey(apiKey),
WithBaseURL(ProductionBaseURL),
}, opts...)...,
)
}
// NewSandbox creates a new Paddle SDK with the given API key, using the sandbox
// environment.
func NewSandbox(apiKey string, opts ...Option) (*SDK, error) {
return bootstrapSDK(
append([]Option{
WithAPIKey(apiKey),
WithBaseURL(SandboxBaseURL),
}, opts...)...,
)
}
func bootstrapSDK(opts ...Option) (*SDK, error) {
o := &options{
Client: cleanhttp.DefaultPooledClient(),
}
for _, fn := range opts {
fn(o)
}
parsedBaseURL, err := url.Parse(o.BaseURL)
if err != nil {
return nil, err
}
c, err := client.New(cleanhttp.DefaultPooledClient(), o.APIKey, parsedBaseURL)
if err != nil {
return nil, err
}
// newSDK is generated by the SDK generator, do not modify.
return newSDK(c), nil
}