-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Errors.swift
99 lines (84 loc) · 4.03 KB
/
Errors.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
import Stripe
@_spi(STP) import StripeCore
enum ErrorType {
static let Failed = "Failed"
static let Canceled = "Canceled"
static let Unknown = "Unknown"
}
class Errors {
static internal let isPIClientSecretValidRegex: NSRegularExpression? = try? NSRegularExpression(
pattern: "^pi_[^_]+_secret_[^_]+$", options: [])
static internal let isSetiClientSecretValidRegex: NSRegularExpression? = try? NSRegularExpression(
pattern: "^seti_[^_]+_secret_[^_]+$", options: [])
static internal let isEKClientSecretValidRegex: NSRegularExpression? = try? NSRegularExpression(
pattern: "^ek_[^_](.)+$", options: [])
class func isPIClientSecretValid(clientSecret: String) -> Bool {
return (Errors.isPIClientSecretValidRegex?.numberOfMatches(
in: clientSecret,
options: .anchored,
range: NSRange(location: 0, length: clientSecret.count))) == 1
}
class func isSetiClientSecretValid(clientSecret: String) -> Bool {
return (Errors.isSetiClientSecretValidRegex?.numberOfMatches(
in: clientSecret,
options: .anchored,
range: NSRange(location: 0, length: clientSecret.count))) == 1
}
class func isEKClientSecretValid(clientSecret: String) -> Bool {
return (Errors.isEKClientSecretValidRegex?.numberOfMatches(
in: clientSecret,
options: .anchored,
range: NSRange(location: 0, length: clientSecret.count))) == 1
}
class func createError (_ code: String, _ message: String?) -> NSDictionary {
let value: NSDictionary = [
"code": code,
"message": message ?? NSNull(),
"localizedMessage": message ?? NSNull(),
"declineCode": NSNull(),
"stripeErrorCode": NSNull(),
"type": NSNull()
]
return ["error": value]
}
class func createError (_ code: String, _ error: NSError?) -> NSDictionary {
let value: NSDictionary = [
"code": code,
"message": error?.userInfo[STPError.errorMessageKey] ?? error?.localizedDescription ?? NSNull(),
"localizedMessage": error?.localizedDescription ?? NSNull(),
"declineCode": error?.userInfo[STPError.stripeDeclineCodeKey] ?? NSNull(),
"stripeErrorCode": error?.userInfo[STPError.stripeErrorCodeKey] ?? NSNull(),
"type": error?.userInfo[STPError.stripeErrorTypeKey] ?? NSNull(),
]
return ["error": value]
}
class func createError (_ code: String, _ error: STPSetupIntentLastSetupError?) -> NSDictionary {
let value: NSDictionary = [
"code": code,
"message": error?.message ?? NSNull(),
"localizedMessage": error?.message ?? NSNull(),
"declineCode": error?.declineCode ?? NSNull(),
"stripeErrorCode": error?.code ?? NSNull(),
"type": Mappers.mapFromSetupIntentLastPaymentErrorType(error?.type) ?? NSNull()
]
return ["error": value]
}
class func createError (_ code: String, _ error: STPPaymentIntentLastPaymentError?) -> NSDictionary {
let value: NSDictionary = [
"code": code,
"message": error?.message ?? NSNull(),
"localizedMessage": error?.message ?? NSNull(),
"declineCode": error?.declineCode ?? NSNull(),
"stripeErrorCode": error?.code ?? NSNull(),
"type": Mappers.mapFromPaymentIntentLastPaymentErrorType(error?.type) ?? NSNull()
]
return ["error": value]
}
class func createError(_ code: String, _ error: Error) -> NSDictionary {
if let stripeError = error as? StripeError {
return createError(code, NSError.stp_error(from: stripeError))
}
return createError(code, error as NSError)
}
static let MISSING_INIT_ERROR = Errors.createError(ErrorType.Failed, "Stripe has not been initialized. Initialize Stripe in your app with the StripeProvider component or the initStripe method.")
}