-
Notifications
You must be signed in to change notification settings - Fork 5
/
authorization.go
83 lines (71 loc) · 3.52 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
package zooz
import (
"context"
"encoding/json"
"fmt"
)
// AuthorizationClient is a client for work with Authorization entity.
// https://developers.paymentsos.com/docs/api#/reference/authorizations
type AuthorizationClient struct {
Caller Caller
}
// Authorization is a model of entity.
type Authorization 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"`
}
// AuthorizationParams is a set of params for creating entity.
type AuthorizationParams 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 Authorization entity.
func (c *AuthorizationClient) New(ctx context.Context, idempotencyKey string, paymentID string, params *AuthorizationParams, clientInfo *ClientInfo) (*Authorization, error) {
authorization := &Authorization{}
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.authorizationsPath(paymentID), headers, params, authorization); err != nil {
return nil, err
}
return authorization, nil
}
// Get returns Authorization entity.
func (c *AuthorizationClient) Get(ctx context.Context, paymentID string, authorizationID string) (*Authorization, error) {
authorization := &Authorization{}
if err := c.Caller.Call(ctx, "GET", c.authorizationPath(paymentID, authorizationID), nil, nil, authorization); err != nil {
return nil, err
}
return authorization, nil
}
// GetList returns list of all Authorizations for given payment ID.
func (c *AuthorizationClient) GetList(ctx context.Context, paymentID string) ([]Authorization, error) {
var authorizations []Authorization
if err := c.Caller.Call(ctx, "GET", c.authorizationsPath(paymentID), nil, nil, &authorizations); err != nil {
return nil, err
}
return authorizations, nil
}
func (c *AuthorizationClient) authorizationsPath(paymentID string) string {
return fmt.Sprintf("%s/%s/authorizations", paymentsPath, paymentID)
}
func (c *AuthorizationClient) authorizationPath(paymentID string, authorizationID string) string {
return fmt.Sprintf("%s/%s", c.authorizationsPath(paymentID), authorizationID)
}