-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCardDataConverter.kt
More file actions
150 lines (125 loc) · 5.3 KB
/
CardDataConverter.kt
File metadata and controls
150 lines (125 loc) · 5.3 KB
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
package com.expobraintree
import com.braintreepayments.api.card.Card
import com.braintreepayments.api.card.CardNonce
import com.braintreepayments.api.threedsecure.ThreeDSecureAdditionalInformation
import com.braintreepayments.api.threedsecure.ThreeDSecureNonce
import com.braintreepayments.api.threedsecure.ThreeDSecurePostalAddress
import com.braintreepayments.api.threedsecure.ThreeDSecureRequest
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
class CardDataConverter {
companion object {
/**
* Converts a basic CardNonce into a WritableMap for React Native.
*/
fun createTokenizeCardDataNonce(cardNonce: CardNonce): WritableMap {
val result: WritableMap = Arguments.createMap()
result.putString("nonce", cardNonce.string)
// Handling unknown card types for consistent JS reporting
if (cardNonce.cardType == "Unknown") {
result.putString("cardNetwork", "")
} else {
result.putString("cardNetwork", cardNonce.cardType)
}
result.putString("lastFour", cardNonce.lastFour)
result.putString("lastTwo", cardNonce.lastTwo)
result.putString("expirationMonth", cardNonce.expirationMonth)
result.putString("expirationYear", cardNonce.expirationYear)
return result
}
/**
* Converts the 3D Secure result nonce into a WritableMap to be sent back to JavaScript.
* Includes liability shift details required for security checks.
*/
fun createThreeDSecureDataNonce(cardNonce: ThreeDSecureNonce): WritableMap {
val result: WritableMap = Arguments.createMap()
result.putString("nonce", cardNonce.string)
if (cardNonce.cardType == "Unknown") {
result.putString("cardNetwork", "")
} else {
result.putString("cardNetwork", cardNonce.cardType)
}
result.putString("lastFour", cardNonce.lastFour)
result.putString("lastTwo", cardNonce.lastTwo)
result.putString("expirationMonth", cardNonce.expirationMonth)
result.putString("expirationYear", cardNonce.expirationYear)
val infoMap: WritableMap = Arguments.createMap()
val info = cardNonce.threeDSecureInfo
// REMOVED: if (info != null) check because threeDSecureInfo is NonNull in SDK v5+
infoMap.putBoolean("liabilityShifted", info.liabilityShifted)
infoMap.putBoolean("liabilityShiftPossible", info.liabilityShiftPossible)
infoMap.putString("status", info.status)
infoMap.putBoolean("wasVerified", info.wasVerified)
result.putMap("threeDSecureInfo", infoMap)
return result
}
/**
* Creates a Card object from JS options for basic tokenization.
*/
fun createTokenizeCardRequest(options: ReadableMap): Card {
val card: Card = Card()
if (options.hasKey("number")) {
card.number = options.getString("number")
}
if (options.hasKey("expirationMonth")) {
card.expirationMonth = options.getString("expirationMonth")
}
if (options.hasKey("expirationYear")) {
card.expirationYear = options.getString("expirationYear")
}
if (options.hasKey("cvv")) {
card.cvv = (options.getString("cvv"))
}
if (options.hasKey("postalCode")) {
card.postalCode = options.getString("postalCode")
}
return card
}
/**
* Maps JS options to a ThreeDSecureRequest.
* Includes address mapping to satisfy 3DS 2.0 risk assessment requirements.
*/
fun create3DSecureRequest(options: ReadableMap): ThreeDSecureRequest {
val address = ThreeDSecurePostalAddress()
// Personal details mapping
if (options.hasKey("givenName")) {
address.givenName = options.getString("givenName")
}
if (options.hasKey("surName")) {
address.surname = options.getString("surName")
}
if (options.hasKey("phoneNumber")) {
address.phoneNumber = options.getString("phoneNumber")
}
// Required by Braintree to avoid 422 errors if region is provided
if (options.hasKey("countryCodeAlpha2")) {
address.countryCodeAlpha2 = options.getString("countryCodeAlpha2")
}
if (options.hasKey("city")) {
address.locality = options.getString("city")
}
if (options.hasKey("postalCode")) {
address.postalCode = options.getString("postalCode")
}
if (options.hasKey("region")) {
address.region = options.getString("region")
}
if (options.hasKey("streetAddress")) {
address.streetAddress = options.getString("streetAddress")
}
if (options.hasKey("streetAddress2")) {
address.extendedAddress = options.getString("streetAddress2")
}
val additionalInformation = ThreeDSecureAdditionalInformation()
additionalInformation.shippingAddress = address
val threeDSecureRequest = ThreeDSecureRequest()
threeDSecureRequest.nonce = options.getString("nonce") ?: ""
threeDSecureRequest.email = options.getString("email") ?: ""
threeDSecureRequest.amount = options.getString("amount") ?: "0.00"
threeDSecureRequest.billingAddress = address
threeDSecureRequest.additionalInformation = additionalInformation
return threeDSecureRequest
}
}
}