-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckout.go
More file actions
155 lines (133 loc) · 4.64 KB
/
Copy pathcheckout.go
File metadata and controls
155 lines (133 loc) · 4.64 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package creemio
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"time"
)
type CheckoutCustomer struct {
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
}
type Checkout struct {
ID string `json:"id"`
Mode Mode `json:"mode"`
Object string `json:"object"`
Status string `json:"status"`
RequestID string `json:"request_id"`
Product *Product `json:"product"`
Units int `json:"units"`
Order *CheckoutOrder `json:"order"`
Subscription *Subscription `json:"subscription"`
Customer *Customer `json:"customer"`
CustomFields []CustomField `json:"custom_fields"`
CheckoutURL string `json:"checkout_url"`
SuccessURL string `json:"success_url"`
Feature []CheckoutFeature `json:"feature"`
Metadata map[string]any `json:"metadata"`
}
type CheckoutOrder struct {
ID string `json:"id"`
Mode Mode `json:"mode"`
Object string `json:"object"`
Customer string `json:"customer"`
Product string `json:"product"`
Transaction string `json:"transaction"`
Discount string `json:"discount"`
Amount int `json:"amount"`
SubTotal int `json:"sub_total"`
TaxAmount int `json:"tax_amount"`
DiscountAmount int `json:"discount_amount"`
AmountDue int `json:"amount_due"`
AmountPaid int `json:"amount_paid"`
Currency string `json:"currency"`
FxAmount int `json:"fx_amount"`
FxCurrency string `json:"fx_currency"`
FxRate float64 `json:"fx_rate"`
Status string `json:"status"`
Type string `json:"type"`
Affiliate string `json:"affiliate"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type CheckoutFeature struct {
License *License `json:"license"`
}
// product_id is required
type CheckoutCreateRequest struct {
RequestID string `json:"request_id,omitempty"`
ProductID string `json:"product_id"`
Units int `json:"units,omitempty"`
DiscountCode string `json:"discount_code,omitempty"`
Customer *CheckoutCustomer `json:"customer,omitempty"`
CustomField []CustomField `json:"custom_field,omitempty"`
SuccessURL string `json:"success_url,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
type CheckoutService struct {
client *Client
}
func (s *CheckoutService) Get(ctx context.Context, id string) (*Checkout, *Response, error) {
targetUrl := makeUrl(s.client.baseURL, "/checkouts")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, targetUrl, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("x-api-key", s.client.apiKey)
req.Header.Set("Content-Type", "application/json")
q := req.URL.Query()
q.Set("checkout_id", id)
req.URL.RawQuery = q.Encode()
res, err := s.client.httpClient.Do(req)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, newResponse(res, body), err
}
if res.StatusCode >= 400 {
return nil, newResponse(res, body), newAPIError(body)
}
var checkout Checkout
if err := json.Unmarshal(body, &checkout); err != nil {
return nil, newResponse(res, body), err
}
return &checkout, newResponse(res, body), nil
}
func (s *CheckoutService) Create(ctx context.Context, data *CheckoutCreateRequest) (*Checkout, *Response, error) {
targetUrl := makeUrl(s.client.baseURL, "/checkouts")
if len(data.ProductID) == 0 {
return nil, nil, errRequiredFieldProductID
}
payload, err := json.Marshal(data)
if err != nil {
return nil, nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, targetUrl, bytes.NewReader(payload))
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", s.client.apiKey)
res, err := s.client.httpClient.Do(req)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, newResponse(res, body), err
}
if res.StatusCode >= 400 {
return nil, newResponse(res, body), newAPIError(body)
}
var checkout Checkout
if err := json.Unmarshal(body, &checkout); err != nil {
return nil, newResponse(res, body), err
}
return &checkout, newResponse(res, body), nil
}