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
/
Copy pathauthorization.go
89 lines (70 loc) · 2.25 KB
/
authorization.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
package paypal
import "fmt"
// https://developer.paypal.com/webapps/developer/docs/api/#authorizations
type (
AuthWithAmountReq struct {
Amount *Amount `json:"amount"`
}
)
// GetAuthorization returns an authorization by ID
func (c *Client) GetAuthorization(authID string) (*Authorization, error) {
req, err := NewRequest("GET", fmt.Sprintf("%s/payments/authorization/%s", c.APIBase, authID), nil)
if err != nil {
return nil, err
}
v := &Authorization{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}
// CaptureAuthorization captures and process an existing authorization.
// To use this method, the original payment must have Intent set to PaymentIntentAuthorize
func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture bool) (*Capture, error) {
req, err := NewRequest("POST", fmt.Sprintf("%s/payments/authorization/%s/capture", c.APIBase, authID), struct {
Amount *Amount `json:"amount"`
IsFinalCapture bool `json:"is_final_capture"`
}{a, isFinalCapture})
if err != nil {
return nil, err
}
v := &Capture{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}
// VoidAuthorization voids a previously authorized payment. A fully
// captured authorization cannot be voided
func (c *Client) VoidAuthorization(authID string) (*Authorization, error) {
req, err := NewRequest("POST", fmt.Sprintf("%s/payments/authorization/%s/void", c.APIBase, authID), nil)
if err != nil {
return nil, err
}
v := &Authorization{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}
// ReauthorizeAuthorization reauthorize a Paypal account payment. Paypal recommends
// that a payment should be reauthorized after the initial 3-day honor period to
// ensure that funds are still available. Only paypal account payments can be re-
// authorized
func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) {
req, err := NewRequest("POST", fmt.Sprintf("%s/payments/authorization/%s/reauthorize", c.APIBase, authID), struct {
Amount *Amount `json:"amount"`
}{a})
if err != nil {
return nil, err
}
v := &Authorization{}
err = c.SendWithAuth(req, v)
if err != nil {
return nil, err
}
return v, nil
}