-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment.go
193 lines (150 loc) · 4.84 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
package mobilepay
import (
"context"
"fmt"
"log"
"net/http"
)
const paymentsBasePath = "v1/payments"
type ListOptions struct {
PageSize int `url:"pageSize"`
PageNumber int `url:"pageNumber"`
}
type RefundsListOptions struct {
ListOptions
PaymentId string `url:"paymentId"`
PaymentPointId string `url:"paymentPointId,omitempty"`
CreatedBefore string `url:"createdBefore,omitempty"`
CreatedAfter string `url:"createdAfter,omitempty"`
}
type PaymentService interface {
Get(context.Context, ListOptions) (*PaymentsRoot, error)
Find(context.Context, string) (*Payment, error)
Create(context.Context, *PaymentParams) (*CreatePaymentResponse, error)
Cancel(ctx context.Context, paymentId string) error
Capture(ctx context.Context, paymentId string, amount int) error
}
type PaymentServiceOp struct {
Refund RefundService
client *Client
}
var _ PaymentService = &PaymentServiceOp{}
type Payment struct {
PaymentId string `json:"paymentId,omitempty"`
Amount int `json:"amount,omitempty"`
Description string `json:"description,omitempty"`
PaymentPointId string `json:"paymentPointId,omitempty"`
Reference string `json:"reference,omitempty"`
MobilePayAppRedirectUri string `json:"mobilePayAppRedirectUri,omitempty"`
State string `json:"state,omitempty"`
InitiatedOn string `json:"initiatedOn,omitempty"`
LastUpdatedOn string `json:"lastUpdatedOn,omitempty"`
MerchantId string `json:"merchantId,omitempty"`
IsoCurrencyCode string `json:"isoCurrencyCode,omitempty"`
PaymentPointName string `json:"paymentPointName,omitempty"`
}
type PaymentsRoot struct {
Payments []Payment `json:"payments"`
PageSize int `json:"pageSize"`
NextPageNumber int `json:"nextPageNumber"`
}
type CreatePaymentResponse struct {
PaymentId string `json:"paymentId"`
MobilePayAppRedirectUri string `json:"mobilePayAppRedirectUri"`
}
type PaymentParams struct {
Amount int `json:"amount"`
IdempotencyKey string `json:"idempotencyKey"`
PaymentPointId string `json:"paymentPointId"`
RedirectUri string `json:"redirectUri"`
Reference string `json:"reference"`
Description string `json:"description"`
}
func (ps PaymentServiceOp) Get(ctx context.Context, opts ListOptions) (*PaymentsRoot, error) {
path := paymentsBasePath
path, err := addOptions(path, opts)
if err != nil {
log.Println("err", err)
return nil, err
}
req, err := ps.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
root := new(PaymentsRoot)
_, err = ps.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, err
}
func (ps *PaymentServiceOp) Find(ctx context.Context, paymentId string) (*Payment, error) {
if paymentId == "" {
ps.client.Logger.Errorf("paymentParams cannot be empty")
return nil, newArgError("paymentId", "cannot be empty")
}
path := fmt.Sprintf("%s/%s", paymentsBasePath, paymentId)
req, err := ps.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
root := new(Payment)
_, err = ps.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, err
}
func (ps *PaymentServiceOp) Create(ctx context.Context, paymentParams *PaymentParams) (*CreatePaymentResponse, error) {
if paymentParams == nil {
ps.client.Logger.Errorf("paymentParams cannot be nil %v", paymentParams)
return nil, newArgError("paymentParams", "cannot be nil")
}
path := paymentsBasePath
req, err := ps.client.NewRequest(ctx, http.MethodPost, path, paymentParams)
if err != nil {
return nil, err
}
root := new(CreatePaymentResponse)
_, err = ps.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, err
}
func (ps *PaymentServiceOp) Cancel(ctx context.Context, paymentId string) error {
if paymentId == "" {
ps.client.Logger.Errorf("paymentParams cannot be empty")
return newArgError("paymentId", "cannot be empty")
}
path := fmt.Sprintf("%s/%s/cancel", paymentsBasePath, paymentId)
req, err := ps.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return err
}
_, err = ps.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
func (ps *PaymentServiceOp) Capture(ctx context.Context, paymentId string, amount int) error {
if paymentId == "" {
ps.client.Logger.Errorf("paymentId cannot be empty", paymentId)
return newArgError("paymentId", "cannot be empty")
}
path := fmt.Sprintf("%s/%s/capture", paymentsBasePath, paymentId)
type captureRequest struct {
Amount int `json:"amount"`
}
requestData := &captureRequest{Amount: amount}
req, err := ps.client.NewRequest(ctx, http.MethodPost, path, requestData)
if err != nil {
return err
}
_, err = ps.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}