-
Notifications
You must be signed in to change notification settings - Fork 78
/
fake_gateway.go
238 lines (215 loc) · 6.39 KB
/
fake_gateway.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package payment
import (
"context"
"errors"
"net/url"
"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
"flamingo.me/flamingo-commerce/v3/cart/domain/placeorder"
"flamingo.me/flamingo-commerce/v3/payment/domain"
"flamingo.me/flamingo-commerce/v3/payment/interfaces"
)
const (
// FakePaymentGateway gateway code
FakePaymentGateway = "fake_payment_gateway"
)
type (
// FakeGateway used for testing all payment states
FakeGateway struct {
CartIsCompleted map[string]bool
}
// Method ...
Method struct {
Title string
Status *domain.FlowStatus
}
)
var (
_ interfaces.WebCartPaymentGateway = &FakeGateway{}
methods = map[string]Method{
domain.PaymentFlowStatusCompleted: {
Title: "Payment completed",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusCompleted,
},
},
domain.PaymentFlowStatusFailed: {
Title: "Payment failed",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusFailed,
},
},
domain.PaymentErrorAbortedByCustomer: {
Title: "Payment aborted by customer",
Status: &domain.FlowStatus{
Status: domain.PaymentErrorAbortedByCustomer,
},
},
domain.PaymentFlowStatusAborted: {
Title: "Payment aborted",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusAborted,
},
},
domain.PaymentFlowStatusCancelled: {
Title: "Payment canceled",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusCancelled,
},
},
domain.PaymentFlowStatusApproved: {
Title: "Payment approved",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusApproved,
},
},
domain.PaymentFlowWaitingForCustomer: {
Title: "Payment waiting for customer",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowWaitingForCustomer,
},
},
domain.PaymentFlowActionShowIframe: {
Title: "Payment unapproved, iframe",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
Action: domain.PaymentFlowActionShowIframe,
ActionData: domain.FlowActionData{
URL: &url.URL{Scheme: "https", Host: "url.com"},
},
},
},
domain.PaymentFlowActionRedirect: {
Title: "Payment unapproved, redirect",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
Action: domain.PaymentFlowActionRedirect,
ActionData: domain.FlowActionData{
URL: &url.URL{Scheme: "https", Host: "url.com"},
},
},
},
domain.PaymentFlowActionPostRedirect: {
Title: "Payment unapproved, post-redirect",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
Action: domain.PaymentFlowActionPostRedirect,
ActionData: domain.FlowActionData{
URL: &url.URL{Scheme: "https", Host: "url.com"},
},
},
},
domain.PaymentFlowActionShowHTML: {
Title: "Payment unapproved, html",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
Action: domain.PaymentFlowActionShowHTML,
ActionData: domain.FlowActionData{
DisplayData: "<h2>test</h2>",
},
},
},
domain.PaymentFlowActionShowWalletPayment: {
Title: "Payment unapproved, wallet",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
Action: domain.PaymentFlowActionShowWalletPayment,
ActionData: domain.FlowActionData{
WalletDetails: &domain.WalletDetails{
UsedPaymentMethod: "ApplePay",
PaymentRequestAPI: domain.PaymentRequestAPI{
Methods: `{"a": "b"}`,
},
},
},
},
},
"unknown": {
Title: "Payment unapproved, unknown",
Status: &domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
Action: "unknown",
},
},
}
)
// Inject dependencies
func (g *FakeGateway) Inject() *FakeGateway {
g.CartIsCompleted = make(map[string]bool)
return g
}
// Methods returns all payment gateway methods
func (g *FakeGateway) Methods() []domain.Method {
result := make([]domain.Method, 0, len(methods))
for key, val := range methods {
result = append(result, domain.Method{Code: key, Title: val.Title})
}
return result
}
func (g *FakeGateway) isSupportedPaymentMethod(method string) bool {
for _, supportedMethod := range g.Methods() {
if supportedMethod.Code == method {
return true
}
}
return false
}
// StartFlow starts a new Payment flow
func (g *FakeGateway) StartFlow(ctx context.Context, cart *cart.Cart, correlationID string, returnURL *url.URL) (*domain.FlowResult, error) {
method := ""
// just grab the first method we find and use it to decide between the different use cases
for qualifier := range cart.PaymentSelection.CartSplit() {
method = qualifier.Method
break
}
if !g.isSupportedPaymentMethod(method) {
return nil, errors.New("specified method not supported by payment gateway: " + method)
}
return &domain.FlowResult{
Status: domain.FlowStatus{
Status: domain.PaymentFlowStatusUnapproved,
},
}, nil
}
// FlowStatus returns a payment with a state depending on the supplied payment method
func (g *FakeGateway) FlowStatus(ctx context.Context, cart *cart.Cart, correlationID string) (*domain.FlowStatus, error) {
methodCode := ""
// just grab the first method we find and use it to decide between the different use cases
for qualifier := range cart.PaymentSelection.CartSplit() {
methodCode = qualifier.Method
break
}
if !g.isSupportedPaymentMethod(methodCode) {
return nil, errors.New("specified method not supported by payment gateway: " + methodCode)
}
if g.CartIsCompleted[cart.ID] {
return &domain.FlowStatus{
Status: domain.PaymentFlowStatusCompleted,
}, nil
}
return methods[methodCode].Status, nil
}
// ConfirmResult mark payment as completed
func (g *FakeGateway) ConfirmResult(ctx context.Context, cart *cart.Cart, cartPayment *placeorder.Payment) error {
g.CartIsCompleted[cart.ID] = true
return nil
}
// OrderPaymentFromFlow return fake payment
func (g *FakeGateway) OrderPaymentFromFlow(ctx context.Context, cart *cart.Cart, correlationID string) (*placeorder.Payment, error) {
return &placeorder.Payment{
Gateway: FakePaymentGateway,
Transactions: []placeorder.Transaction{
{
TransactionID: correlationID,
AdditionalData: nil,
AmountPayed: cart.GrandTotal,
ValuedAmountPayed: cart.GrandTotal,
},
},
RawTransactionData: nil,
PaymentID: "",
}, nil
}
// CancelOrderPayment does nothing
func (g *FakeGateway) CancelOrderPayment(ctx context.Context, cartPayment *placeorder.Payment) error {
return nil
}