-
Notifications
You must be signed in to change notification settings - Fork 1
/
payment.go
216 lines (175 loc) · 5.85 KB
/
payment.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gopaypal
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type paymentCreateResponse struct {
ID string `json:"id"`
CreateTime time.Time `json:"create_time"`
UpdateTime time.Time `json:"update_time"`
State string `json:"state"`
Intent string `json:"intent,omitempty"`
Payer Payer `json:"payer,omitempty"`
Transactions []Transaction `json:"transactions,omitempty"`
FailureReason string `json:"failure_reason,omitempty"`
RedirectURL RedirectURL `json:"redirect_urls,omitempty"`
Links []Link `json:"links"`
}
type Link struct {
Href string `json:"href"`
Rel string `json:"rel"`
Method string `json:"method"`
}
type Payment struct {
Intent string `json:"intent,omitempty"`
Payer Payer `json:"payer,omitempty"`
Transactions []Transaction `json:"transactions,omitempty"`
RedirectURL RedirectURL `json:"redirect_urls,omitempty"`
}
type Payer struct {
PaymentMethod string `json:"payment_method,omitempty"`
Status string `json:"status,omitempty"`
Info PayerInfo `json:"payer_info,omitempty"`
}
type PayerInfo struct {
ID string `json:"payer_id,omitempty"`
}
type Transaction struct {
Amount Amount `json:"amount,omitempty"`
Description string `json:"description,omitempty"`
NoteToPayee string `json:"note_to_payee,omitempty"`
Custom string `json:"custom,omitempty"`
InvoiceNumber string `json:"invoice_number,omitempty"`
SoftDescriptor string `json:"soft_descriptor,omitempty"`
PaymentOptions PaymentOptions `json:"payment_options,omitempty"`
ItemList ItemList `json:"item_list,omitempty"`
NotifyURL string `json:"notify_url,omitempty"`
OrderURL string `json:"order_url,omitempty"`
RelatedResources []*RelatedResources `json:"related_resources,omitempty"`
}
type RelatedResources struct {
Sale Sale `json:"sale,omitempty"`
}
type Sale struct {
ID string `json:"id,omitempty"`
PurchaseUnitReferenceID string `json:"purchase_unit_reference_id,omitempty"`
Amount Amount `json:"amount,omitempty"`
PaymentMode string `json:"payment_mode,omitempty"`
State string `json:"state,omitempty"`
ReasonCode string `json:"reason_code,omitempty"`
ClearingTime string `json:"clearing_time,omitempty"`
ReceiptID string `json:"receipt_id,omitempty"`
}
type Amount struct {
Currency string `json:"currency,omitempty"`
Total string `json:"total,omitempty"`
Details Details `json:"details,omitempty"`
}
type Details struct {
SubTotal string `json:"subtotal,omitempty"`
Shipping string `json:"shipping,omitempty"`
Tax string `json:"tax,omitempty"`
HandlingFee string `json:"handling_fee,omitempty"`
ShippingDiscount string `json:"shipping_discount,omitempty"`
Insurance string `json:"insurance,omitempty"`
GiftWrap string `json:"gift_wrap,omitempty"`
}
type PaymentOptions struct {
AllowedPaymentMethod string `json:"allowed_payment_method,omitempty"`
}
type ItemList struct {
Items []Item `json:"items,omitempty"`
ShippingMethod string `json:"shipping_method,omitempty"`
ShippingPhoneNumber string `json:"shipping_phone_number,omitempty"`
}
type Item struct {
Sku string `json:"sku,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Quantity int `json:"quantity,omitempty"`
Price string `json:"price,omitempty"`
Currency string `json:"currency,omitempty"`
Tax string `json:"tax,omitempty"`
URL string `json:"url,omitempty"`
}
type RedirectURL struct {
ReturnURL string `json:"return_url,omitempty"`
CancelURL string `json:"cancel_url,omitempty"`
}
func (c Client) PaymentInformation(paymentID string) (*paymentCreateResponse, error) {
// Create auth request
req, err := c.AuthRequest(fmt.Sprintf(
PaymentInfoURL,
paymentID,
), nil, http.MethodGet)
if err != nil {
return nil, err
}
// Set content type
req.Header.Set("Content-Type", "application/json")
// Execute request
res, err := c.Execute(req)
if err != nil {
return nil, err
}
// Hold payment creation response
d := paymentCreateResponse{}
// Marshal response
if err := json.Unmarshal(res, &d); err != nil {
return nil, err
}
return &d, err
}
func (c Client) ExecutePayment(paymentID, payerID string) (*paymentCreateResponse, error) {
// Create auth request
req, err := c.AuthRequest(fmt.Sprintf(
PaymentExecuteURL,
paymentID,
), []byte("{\"payer_id\": \""+payerID+"\"}"), http.MethodPost)
if err != nil {
return nil, err
}
// Set content type
req.Header.Set("Content-Type", "application/json")
// Execute request
res, err := c.Execute(req)
if err != nil {
return nil, err
}
// Hold payment creation response
d := paymentCreateResponse{}
// Marshal response
if err := json.Unmarshal(res, &d); err != nil {
return nil, err
}
return &d, err
}
// CreatePayment creates a PayPal payment with the given payment object
func (c Client) CreatePayment(payment Payment) (*paymentCreateResponse, error) {
// Marshal payment object to byte array
buff, err := json.Marshal(&payment)
if err != nil {
return nil, err
}
// Create auth request
req, err := c.AuthRequest(PaymentCreateURL, buff, http.MethodPost)
if err != nil {
return nil, err
}
// Set content type
req.Header.Set("Content-Type", "application/json")
// Execute request
res, err := c.Execute(req)
if err != nil {
return nil, err
}
// Hold payment creation response
d := paymentCreateResponse{}
// Marshal response
if err := json.Unmarshal(res, &d); err != nil {
return nil, err
}
return &d, err
}