-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
CardFormView.swift
114 lines (96 loc) · 4.07 KB
/
CardFormView.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
import Foundation
import UIKit
import Stripe
class CardFormView: UIView, STPCardFormViewDelegate {
public var cardForm: STPCardFormView?
public var cardParams: STPPaymentMethodCardParams? = nil
@objc var dangerouslyGetFullCardDetails: Bool = false
@objc var onFormComplete: RCTDirectEventBlock?
@objc var autofocus: Bool = false
@objc var isUserInteractionEnabledValue: Bool = true
override func didSetProps(_ changedProps: [String]!) {
if let cardForm = self.cardForm {
cardForm.removeFromSuperview()
}
let style = self.cardStyle["type"] as? String == "borderless" ? STPCardFormViewStyle.borderless : STPCardFormViewStyle.standard
let _cardForm = STPCardFormView(style: style)
_cardForm.delegate = self
// _cardForm.isUserInteractionEnabled = isUserInteractionEnabledValue
if autofocus == true {
let _ = _cardForm.becomeFirstResponder()
}
self.cardForm = _cardForm
self.addSubview(_cardForm)
setStyles()
}
@objc var cardStyle: NSDictionary = NSDictionary() {
didSet {
setStyles()
}
}
func cardFormView(_ form: STPCardFormView, didChangeToStateComplete complete: Bool) {
if onFormComplete != nil {
let brand = STPCardValidator.brand(forNumber: cardForm?.cardParams?.card?.number ?? "")
var cardData: [String: Any?] = [
"expiryMonth": cardForm?.cardParams?.card?.expMonth ?? NSNull(),
"expiryYear": cardForm?.cardParams?.card?.expYear ?? NSNull(),
"complete": complete,
"brand": Mappers.mapFromCardBrand(brand) ?? NSNull(),
"last4": cardForm?.cardParams?.card?.last4 ?? "",
"postalCode": cardForm?.cardParams?.billingDetails?.address?.postalCode ?? "",
"country": cardForm?.cardParams?.billingDetails?.address?.country
]
if (dangerouslyGetFullCardDetails) {
cardData["number"] = cardForm?.cardParams?.card?.number ?? ""
cardData["cvc"] = cardForm?.cardParams?.card?.cvc ?? ""
}
if (complete) {
self.cardParams = cardForm?.cardParams?.card
} else {
self.cardParams = nil
}
onFormComplete!(cardData as [AnyHashable : Any])
}
}
func focus() {
let _ = cardForm?.becomeFirstResponder()
}
func blur() {
let _ = cardForm?.resignFirstResponder()
}
func setStyles() {
if let backgroundColor = cardStyle["backgroundColor"] as? String {
cardForm?.backgroundColor = UIColor(hexString: backgroundColor)
}
/**
The following reveals a bug in STPCardFormView where there's a extra space in the layer,
and thus must remain commented out for now.
if let borderWidth = cardStyle["borderWidth"] as? Int {
cardForm?.layer.borderWidth = CGFloat(borderWidth)
} else {
cardForm?.layer.borderWidth = CGFloat(0)
}
*/
if let borderColor = cardStyle["borderColor"] as? String {
cardForm?.layer.borderColor = UIColor(hexString: borderColor).cgColor
}
if let borderRadius = cardStyle["borderRadius"] as? Int {
cardForm?.layer.cornerRadius = CGFloat(borderRadius)
}
if let cursorColor = cardStyle["cursorColor"] as? String {
cardForm?.tintColor = UIColor(hexString: cursorColor)
}
// if let disabledBackgroundColor = cardStyle["disabledBackgroundColor"] as? String {
// cardForm?.disabledBackgroundColor = UIColor(hexString: disabledBackgroundColor)
// }
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func layoutSubviews() {
cardForm?.frame = self.bounds
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}