-
Notifications
You must be signed in to change notification settings - Fork 264
/
PaymentMethodFactory.swift
431 lines (366 loc) · 19.1 KB
/
PaymentMethodFactory.swift
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import Foundation
import Stripe
class PaymentMethodFactory {
var billingDetailsParams: STPPaymentMethodBillingDetails? = nil
var paymentMethodData: NSDictionary? = nil
var paymentMethodOptions: NSDictionary? = nil
var cardFieldView: CardFieldView? = nil
var cardFormView: CardFormView? = nil
init(paymentMethodData: NSDictionary?, options: NSDictionary, cardFieldView: CardFieldView?, cardFormView: CardFormView?) {
self.paymentMethodData = paymentMethodData
self.billingDetailsParams = Mappers.mapToBillingDetails(billingDetails: paymentMethodData?["billingDetails"] as? NSDictionary)
self.paymentMethodOptions = options
self.cardFieldView = cardFieldView
self.cardFormView = cardFormView
}
func createParams(paymentMethodType: STPPaymentMethodType) throws -> STPPaymentMethodParams? {
do {
switch paymentMethodType {
case STPPaymentMethodType.iDEAL:
return try createIDEALPaymentMethodParams()
case STPPaymentMethodType.OXXO:
return try createOXXOPaymentMethodParams()
case STPPaymentMethodType.card:
return try createCardPaymentMethodParams()
case STPPaymentMethodType.FPX:
return try createFPXPaymentMethodParams()
case STPPaymentMethodType.alipay:
return try createAlipayPaymentMethodParams()
case STPPaymentMethodType.sofort:
return try createSofortPaymentMethodParams()
case STPPaymentMethodType.bancontact:
return try createBancontactPaymentMethodParams()
case STPPaymentMethodType.SEPADebit:
return try createSepaPaymentMethodParams()
case STPPaymentMethodType.giropay:
return try createGiropayPaymentMethodParams()
case STPPaymentMethodType.EPS:
return try createEPSPaymentMethodParams()
case STPPaymentMethodType.grabPay:
return createGrabpayPaymentMethodParams()
case STPPaymentMethodType.przelewy24:
return try createP24PaymentMethodParams()
case STPPaymentMethodType.AUBECSDebit:
return try createBECSDebitPaymentMethodParams()
case STPPaymentMethodType.afterpayClearpay:
return try createAfterpayClearpayPaymentMethodParams()
case STPPaymentMethodType.klarna:
return try createKlarnaPaymentMethodParams()
case STPPaymentMethodType.USBankAccount:
return try createUSBankAccountPaymentMethodParams()
case STPPaymentMethodType.payPal:
return try createPayPalPaymentMethodParams()
case STPPaymentMethodType.affirm:
return try createAffirmPaymentMethodParams()
// case STPPaymentMethodType.weChatPay:
// return try createWeChatPayPaymentMethodParams()
default:
throw PaymentMethodError.paymentNotSupported
}
} catch {
throw error
}
}
func createOptions(paymentMethodType: STPPaymentMethodType) throws -> STPConfirmPaymentMethodOptions? {
do {
switch paymentMethodType {
case STPPaymentMethodType.iDEAL:
return nil
case STPPaymentMethodType.EPS:
return nil
case STPPaymentMethodType.card:
return createCardPaymentMethodOptions()
case STPPaymentMethodType.FPX:
return nil
case STPPaymentMethodType.sofort:
return nil
case STPPaymentMethodType.alipay:
return try createAlipayPaymentMethodOptions()
case STPPaymentMethodType.bancontact:
return nil
case STPPaymentMethodType.SEPADebit:
return nil
case STPPaymentMethodType.OXXO:
return nil
case STPPaymentMethodType.giropay:
return nil
case STPPaymentMethodType.grabPay:
return nil
case STPPaymentMethodType.przelewy24:
return nil
case STPPaymentMethodType.AUBECSDebit:
return nil
case STPPaymentMethodType.afterpayClearpay:
return nil
case STPPaymentMethodType.klarna:
return nil
case STPPaymentMethodType.weChatPay:
return try createWeChatPayPaymentMethodOptions()
case STPPaymentMethodType.USBankAccount:
return try createUSBankAccountPaymentMethodOptions()
case STPPaymentMethodType.payPal:
return nil
case STPPaymentMethodType.affirm:
return nil
default:
throw PaymentMethodError.paymentNotSupported
}
} catch {
throw error
}
}
// private func createWeChatPayPaymentMethodParams() throws -> STPPaymentMethodParams {
// let params = STPPaymentMethodWeChatPayParams()
// return STPPaymentMethodParams(weChatPay: params, billingDetails: billingDetailsParams, metadata: nil)
// }
//
private func createUSBankAccountPaymentMethodOptions() throws -> STPConfirmPaymentMethodOptions {
let paymentOptions = STPConfirmPaymentMethodOptions()
if let usage = self.paymentMethodOptions?["setupFutureUsage"] as? String {
paymentOptions.usBankAccountOptions = STPConfirmUSBankAccountOptions(setupFutureUsage: Mappers.mapToPaymentIntentFutureUsage(usage: usage))
}
return paymentOptions
}
private func createWeChatPayPaymentMethodOptions() throws -> STPConfirmPaymentMethodOptions {
guard let appId = self.paymentMethodData?["appId"] as? String else {
throw PaymentMethodError.weChatPayPaymentMissingParams
}
let paymentOptions = STPConfirmPaymentMethodOptions()
paymentOptions.weChatPayOptions = STPConfirmWeChatPayOptions(appId: appId)
return paymentOptions
}
private func createIDEALPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodiDEALParams()
if let bankName = self.paymentMethodData?["bankName"] as? String {
params.bankName = bankName
}
return STPPaymentMethodParams(iDEAL: params, billingDetails: billingDetailsParams, metadata: nil)
}
private func createGrabpayPaymentMethodParams() -> STPPaymentMethodParams {
let params = STPPaymentMethodGrabPayParams()
return STPPaymentMethodParams(grabPay: params, billingDetails: billingDetailsParams, metadata: nil)
}
private func createCardPaymentMethodParams() throws -> STPPaymentMethodParams {
if let token = paymentMethodData?["token"] as? String {
let methodParams = STPPaymentMethodCardParams()
methodParams.token = RCTConvert.nsString(token)
return STPPaymentMethodParams(card: methodParams, billingDetails: billingDetailsParams, metadata: nil)
}
guard let cardParams = cardFieldView?.cardParams ?? cardFormView?.cardParams else {
throw PaymentMethodError.cardPaymentMissingParams
}
if cardFieldView?.cardParams != nil {
if let postalCode = cardFieldView?.cardPostalCode{
if (billingDetailsParams == nil) {
let bd = STPPaymentMethodBillingDetails()
bd.address = STPPaymentMethodAddress()
bd.address?.postalCode = postalCode
billingDetailsParams = bd
} else {
billingDetailsParams?.address?.postalCode = postalCode
}
}
}
if cardFormView?.cardParams != nil {
if let address = cardFormView?.cardForm?.cardParams?.billingDetails?.address {
if (billingDetailsParams == nil) {
let bd = STPPaymentMethodBillingDetails()
bd.address = STPPaymentMethodAddress()
bd.address?.postalCode = address.postalCode
bd.address?.country = address.country
billingDetailsParams = bd
} else {
billingDetailsParams?.address?.postalCode = address.postalCode
billingDetailsParams?.address?.country = address.country
}
}
}
return STPPaymentMethodParams(card: cardParams, billingDetails: billingDetailsParams, metadata: nil)
}
private func createCardPaymentMethodOptions() -> STPConfirmPaymentMethodOptions? {
let cvc = paymentMethodData?["cvc"] as? String
guard cvc != nil else {
return nil
}
let cardOptions = STPConfirmCardOptions()
cardOptions.cvc = cvc;
let paymentMethodOptions = STPConfirmPaymentMethodOptions()
paymentMethodOptions.cardOptions = cardOptions
return paymentMethodOptions
}
private func createFPXPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodFPXParams()
if self.paymentMethodData?["testOfflineBank"] as? Bool == true {
params.rawBankString = "test_offline_bank"
}
return STPPaymentMethodParams(fpx: params, billingDetails: billingDetailsParams, metadata: nil)
}
private func createAlipayPaymentMethodParams() throws -> STPPaymentMethodParams {
return STPPaymentMethodParams(alipay: STPPaymentMethodAlipayParams(), billingDetails: billingDetailsParams, metadata: nil)
}
private func createP24PaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodPrzelewy24Params()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.p24PaymentMissingParams
}
return STPPaymentMethodParams(przelewy24: params, billingDetails: billingDetails, metadata: nil)
}
private func createAlipayPaymentMethodOptions() throws -> STPConfirmPaymentMethodOptions {
let options = STPConfirmPaymentMethodOptions()
options.alipayOptions = STPConfirmAlipayOptions()
return options
}
private func createSofortPaymentMethodParams() throws -> STPPaymentMethodParams {
guard let country = self.paymentMethodData?["country"] as? String else {
throw PaymentMethodError.sofortPaymentMissingParams
}
let params = STPPaymentMethodSofortParams()
params.country = country
return STPPaymentMethodParams(sofort: params, billingDetails: billingDetailsParams, metadata: nil)
}
private func createBancontactPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodBancontactParams()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.bancontactPaymentMissingParams
}
return STPPaymentMethodParams(bancontact: params, billingDetails: billingDetails, metadata: nil)
}
private func createSepaPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodSEPADebitParams()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.sepaPaymentMissingParams
}
guard let iban = self.paymentMethodData?["iban"] as? String else {
throw PaymentMethodError.sepaPaymentMissingParams
}
params.iban = iban
return STPPaymentMethodParams(sepaDebit: params, billingDetails: billingDetails, metadata: nil)
}
private func createOXXOPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodOXXOParams()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.bancontactPaymentMissingParams
}
return STPPaymentMethodParams(oxxo: params, billingDetails: billingDetails, metadata: nil)
}
private func createGiropayPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodGiropayParams()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.giropayPaymentMissingParams
}
return STPPaymentMethodParams(giropay: params, billingDetails: billingDetails, metadata: nil)
}
private func createEPSPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodEPSParams()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.epsPaymentMissingParams
}
return STPPaymentMethodParams(eps: params, billingDetails: billingDetails, metadata: nil)
}
private func createBECSDebitPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodAUBECSDebitParams()
let billingDetails = STPPaymentMethodBillingDetails()
let formDetails = self.paymentMethodData?["formDetails"] as? NSDictionary
billingDetails.name = formDetails?["name"] as? String
billingDetails.email = formDetails?["email"] as? String
params.accountNumber = formDetails?["accountNumber"] as? String
params.bsbNumber = formDetails?["bsbNumber"] as? String
return STPPaymentMethodParams(aubecsDebit: params, billingDetails: billingDetails, metadata: nil)
}
private func createAfterpayClearpayPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodAfterpayClearpayParams()
guard let billingDetails = billingDetailsParams else {
throw PaymentMethodError.afterpayClearpayPaymentMissingParams
}
return STPPaymentMethodParams(afterpayClearpay: params, billingDetails: billingDetails, metadata: nil)
}
private func createKlarnaPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodKlarnaParams()
if let billingDetails = billingDetailsParams, billingDetails.address?.country != nil, billingDetails.email != nil {
return STPPaymentMethodParams(klarna: params, billingDetails: billingDetails, metadata: nil)
} else {
throw PaymentMethodError.klarnaPaymentMissingParams
}
}
private func createUSBankAccountPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodUSBankAccountParams()
guard let accountNumber = self.paymentMethodData?["accountNumber"] as? String else {
throw PaymentMethodError.usBankAccountPaymentMissingAccountNumber
}
guard let routingNumber = self.paymentMethodData?["routingNumber"] as? String else {
throw PaymentMethodError.usBankAccountPaymentMissingRoutingNumber
}
params.accountNumber = accountNumber
params.routingNumber = routingNumber
params.accountHolderType = Mappers.mapToUSBankAccountHolderType(type: self.paymentMethodData?["accountHolderType"] as? String)
params.accountType = Mappers.mapToUSBankAccountType(type: self.paymentMethodData?["accountType"] as? String)
if let billingDetails = billingDetailsParams, billingDetails.name != nil {
return STPPaymentMethodParams(usBankAccount: params, billingDetails: billingDetails, metadata: nil)
} else {
throw PaymentMethodError.usBankAccountPaymentMissingParams
}
}
private func createPayPalPaymentMethodParams() throws -> STPPaymentMethodParams {
return STPPaymentMethodParams(payPal: STPPaymentMethodPayPalParams(), billingDetails: billingDetailsParams, metadata: nil)
}
private func createAffirmPaymentMethodParams() throws -> STPPaymentMethodParams {
let params = STPPaymentMethodAffirmParams()
return STPPaymentMethodParams(affirm: params, metadata: nil)
}
}
enum PaymentMethodError: Error {
case cardPaymentMissingParams
case epsPaymentMissingParams
case idealPaymentMissingParams
case paymentNotSupported
case sofortPaymentMissingParams
case cardPaymentOptionsMissingParams
case bancontactPaymentMissingParams
case sepaPaymentMissingParams
case giropayPaymentMissingParams
case p24PaymentMissingParams
case afterpayClearpayPaymentMissingParams
case klarnaPaymentMissingParams
case weChatPayPaymentMissingParams
case usBankAccountPaymentMissingParams
case usBankAccountPaymentMissingAccountNumber
case usBankAccountPaymentMissingRoutingNumber
}
extension PaymentMethodError: LocalizedError {
public var errorDescription: String? {
switch self {
case .cardPaymentMissingParams:
return NSLocalizedString("Card details not complete", comment: "Create payment error")
case .giropayPaymentMissingParams:
return NSLocalizedString("You must provide billing details", comment: "Create payment error")
case .idealPaymentMissingParams:
return NSLocalizedString("You must provide bank name", comment: "Create payment error")
case .sofortPaymentMissingParams:
return NSLocalizedString("You must provide bank account country", comment: "Create payment error")
case .p24PaymentMissingParams:
return NSLocalizedString("You must provide billing details", comment: "Create payment error")
case .bancontactPaymentMissingParams:
return NSLocalizedString("You must provide billing details", comment: "Create payment error")
case .sepaPaymentMissingParams:
return NSLocalizedString("You must provide billing details and IBAN", comment: "Create payment error")
case .epsPaymentMissingParams:
return NSLocalizedString("You must provide billing details", comment: "Create payment error")
case .afterpayClearpayPaymentMissingParams:
return NSLocalizedString("You must provide billing details", comment: "Create payment error")
case .paymentNotSupported:
return NSLocalizedString("This payment type is not supported yet", comment: "Create payment error")
case .cardPaymentOptionsMissingParams:
return NSLocalizedString("You must provide CVC number", comment: "Create payment error")
case .weChatPayPaymentMissingParams:
return NSLocalizedString("You must provide appId", comment: "Create payment error")
case .klarnaPaymentMissingParams:
return NSLocalizedString("Klarna requires that you provide the following billing details: email, country", comment: "Create payment error")
case .usBankAccountPaymentMissingParams:
return NSLocalizedString("When creating a US bank account payment method, you must provide the following billing details: name", comment: "Create payment error")
case .usBankAccountPaymentMissingAccountNumber:
return NSLocalizedString("When creating a US bank account payment method, you must provide the bank account number", comment: "Create payment error")
case .usBankAccountPaymentMissingRoutingNumber:
return NSLocalizedString("When creating a US bank account payment method, you must provide the bank routing number", comment: "Create payment error")
}
}
}