-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcharge.go
83 lines (71 loc) · 3.23 KB
/
charge.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
package zooz
import (
"context"
"encoding/json"
"fmt"
)
// ChargeClient is a client for work with Charge entity.
// https://developers.paymentsos.com/docs/api#/reference/charges
type ChargeClient struct {
Caller Caller
}
// Charge is a model of entity.
type Charge struct {
ID string `json:"id"`
Result Result `json:"result"`
Amount int64 `json:"amount"`
Created json.Number `json:"created"`
ReconciliationID string `json:"reconciliation_id"`
PaymentMethod PaymentMethodHref `json:"payment_method"`
ThreeDSecureAttributes *ThreeDSecureAttributes `json:"three_d_secure_attributes"`
Installments *Installments `json:"installments"`
ProviderData ProviderData `json:"provider_data"`
ProviderSpecificData map[string]interface{} `json:"provider_specific_data"`
OriginatingPurchaseCountry string `json:"originating_purchase_country"`
IPAddress string `json:"ip_address"`
Redirection *Redirection `json:"redirection"`
}
// ChargeParams is a set of params for creating entity.
type ChargeParams struct {
PaymentMethod PaymentMethodDetails `json:"payment_method"`
MerchantSiteURL string `json:"merchant_site_url,omitempty"`
ReconciliationID string `json:"reconciliation_id,omitempty"`
ThreeDSecureAttributes *ThreeDSecureAttributes `json:"three_d_secure_attributes,omitempty"`
Installments *Installments `json:"installments,omitempty"`
ProviderSpecificData map[string]interface{} `json:"provider_specific_data,omitempty"`
}
// New creates new Charge entity.
func (c *ChargeClient) New(ctx context.Context, idempotencyKey string, paymentID string, params *ChargeParams, clientInfo *ClientInfo) (*Charge, error) {
charge := &Charge{}
headers := map[string]string{headerIdempotencyKey: idempotencyKey}
if clientInfo != nil {
headers[headerClientIPAddress] = clientInfo.IPAddress
headers[headerClientUserAgent] = clientInfo.UserAgent
}
if err := c.Caller.Call(ctx, "POST", c.chargesPath(paymentID), headers, params, charge); err != nil {
return nil, err
}
return charge, nil
}
// Get returns Charge entity.
func (c *ChargeClient) Get(ctx context.Context, paymentID string, chargeID string) (*Charge, error) {
charge := &Charge{}
if err := c.Caller.Call(ctx, "GET", c.chargePath(paymentID, chargeID), nil, nil, charge); err != nil {
return nil, err
}
return charge, nil
}
// GetList returns a list of Charges for given payment ID.
func (c *ChargeClient) GetList(ctx context.Context, paymentID string) ([]Charge, error) {
var charges []Charge
if err := c.Caller.Call(ctx, "GET", c.chargesPath(paymentID), nil, nil, &charges); err != nil {
return nil, err
}
return charges, nil
}
func (c *ChargeClient) chargesPath(paymentID string) string {
return fmt.Sprintf("%s/%s/charges", paymentsPath, paymentID)
}
func (c *ChargeClient) chargePath(paymentID string, chargeID string) string {
return fmt.Sprintf("%s/%s", c.chargesPath(paymentID), chargeID)
}