This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
payment.go
105 lines (82 loc) · 2.15 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
package paypal
import "fmt"
type (
CreatePaymentResp struct {
*Payment
Links []Links `json:"links"`
}
ExecutePaymentResp struct {
Intent PaymentIntent `json:"intent"`
Payer *Payer `json:"payer"`
Transactions []Transaction `json:"transactions"`
Links []Links `json:"links"`
}
ListPaymentsResp struct {
Payments []Payment `json:"payments"`
}
)
// CreatePayment creates a payment in Paypal
func (c *Client) CreatePayment(p Payment) (*CreatePaymentResp, error) {
req, err := NewRequest("POST", fmt.Sprintf("%s/payments/payment", c.APIBase), p)
if err != nil {
return nil, err
}
v := &CreatePaymentResp{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}
// ExecutePayment completes an approved Paypal payment that has been approved by the payer
func (c *Client) ExecutePayment(paymentID, payerID string, transactions []Transaction) (*ExecutePaymentResp, error) {
req, err := NewRequest("POST", fmt.Sprintf("%s/payments/payment/%s/execute", c.APIBase, paymentID), struct {
PayerID string `json:"payer_id"`
Transactions []Transaction `json:"transactions"`
}{
payerID,
transactions,
})
if err != nil {
return nil, err
}
v := &ExecutePaymentResp{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}
// GetPayment fetches a payment in Paypal
func (c *Client) GetPayment(id string) (*Payment, error) {
req, err := NewRequest("GET", fmt.Sprintf("%s/payments/payment/%s", c.APIBase, id), nil)
if err != nil {
return nil, err
}
v := &Payment{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}
// ListPayments retrieve payments resources from Paypal
func (c *Client) ListPayments(filter map[string]string) ([]Payment, error) {
req, err := NewRequest("GET", fmt.Sprintf("%s/payments/payment/", c.APIBase), nil)
if err != nil {
return nil, err
}
if filter != nil {
q := req.URL.Query()
for k, v := range filter {
q.Set(k, v)
}
req.URL.RawQuery = q.Encode()
}
var v ListPaymentsResp
err = c.SendWithAuth(req, &v)
if err != nil {
return nil, err
}
return v.Payments, nil
}