@stripe/stripe-react-native 0.42.0 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (308) hide show
  1. package/.husky/pre-commit +0 -3
  2. package/CHANGELOG.md +14 -0
  3. package/android/.gradle/8.11.1/checksums/checksums.lock +0 -0
  4. package/android/.gradle/8.11.1/checksums/md5-checksums.bin +0 -0
  5. package/android/.gradle/8.11.1/checksums/sha1-checksums.bin +0 -0
  6. package/android/.gradle/8.11.1/executionHistory/executionHistory.lock +0 -0
  7. package/android/.gradle/8.11.1/fileHashes/fileHashes.lock +0 -0
  8. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  9. package/android/.gradle/buildOutputCleanup/cache.properties +2 -2
  10. package/android/.project +0 -11
  11. package/android/.settings/org.eclipse.buildship.core.prefs +3 -3
  12. package/android/build.gradle +7 -1
  13. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  14. package/android/gradle/wrapper/gradle-wrapper.properties +7 -0
  15. package/android/gradle.properties +4 -1
  16. package/android/gradlew +249 -0
  17. package/android/gradlew.bat +92 -0
  18. package/android/spotless.gradle +19 -0
  19. package/android/src/main/java/com/reactnativestripesdk/AuBECSDebitFormView.kt +30 -34
  20. package/android/src/main/java/com/reactnativestripesdk/AuBECSDebitFormViewManager.kt +20 -10
  21. package/android/src/main/java/com/reactnativestripesdk/CardChangedEvent.kt +12 -13
  22. package/android/src/main/java/com/reactnativestripesdk/CardFieldView.kt +242 -105
  23. package/android/src/main/java/com/reactnativestripesdk/CardFieldViewManager.kt +58 -20
  24. package/android/src/main/java/com/reactnativestripesdk/CardFocusEvent.kt +8 -11
  25. package/android/src/main/java/com/reactnativestripesdk/CardFormCompleteEvent.kt +10 -12
  26. package/android/src/main/java/com/reactnativestripesdk/CardFormView.kt +135 -87
  27. package/android/src/main/java/com/reactnativestripesdk/CardFormViewManager.kt +57 -22
  28. package/android/src/main/java/com/reactnativestripesdk/CollectBankAccountLauncherFragment.kt +33 -24
  29. package/android/src/main/java/com/reactnativestripesdk/FinancialConnectionsSheetFragment.kt +107 -85
  30. package/android/src/main/java/com/reactnativestripesdk/FormCompleteEvent.kt +9 -11
  31. package/android/src/main/java/com/reactnativestripesdk/GooglePayButtonManager.kt +16 -9
  32. package/android/src/main/java/com/reactnativestripesdk/GooglePayButtonView.kt +36 -28
  33. package/android/src/main/java/com/reactnativestripesdk/GooglePayLauncherFragment.kt +67 -43
  34. package/android/src/main/java/com/reactnativestripesdk/GooglePayPaymentMethodLauncherFragment.kt +21 -16
  35. package/android/src/main/java/com/reactnativestripesdk/GooglePayRequestHelper.kt +101 -56
  36. package/android/src/main/java/com/reactnativestripesdk/PaymentLauncherFragment.kt +244 -166
  37. package/android/src/main/java/com/reactnativestripesdk/PaymentMethodCreateParamsFactory.kt +137 -156
  38. package/android/src/main/java/com/reactnativestripesdk/PaymentSheetAppearance.kt +181 -57
  39. package/android/src/main/java/com/reactnativestripesdk/PaymentSheetFragment.kt +402 -258
  40. package/android/src/main/java/com/reactnativestripesdk/StripeContainerManager.kt +11 -4
  41. package/android/src/main/java/com/reactnativestripesdk/StripeContainerView.kt +5 -1
  42. package/android/src/main/java/com/reactnativestripesdk/StripeSdkModule.kt +654 -299
  43. package/android/src/main/java/com/reactnativestripesdk/StripeSdkPackage.kt +12 -14
  44. package/android/src/main/java/com/reactnativestripesdk/addresssheet/AddressLauncherFragment.kt +42 -37
  45. package/android/src/main/java/com/reactnativestripesdk/addresssheet/AddressSheetEvent.kt +12 -11
  46. package/android/src/main/java/com/reactnativestripesdk/addresssheet/AddressSheetView.kt +29 -29
  47. package/android/src/main/java/com/reactnativestripesdk/addresssheet/AddressSheetViewManager.kt +50 -18
  48. package/android/src/main/java/com/reactnativestripesdk/customersheet/CustomerSheetFragment.kt +227 -147
  49. package/android/src/main/java/com/reactnativestripesdk/customersheet/ReactNativeCustomerAdapter.kt +15 -18
  50. package/android/src/main/java/com/reactnativestripesdk/pushprovisioning/AddToWalletButtonManager.kt +33 -15
  51. package/android/src/main/java/com/reactnativestripesdk/pushprovisioning/AddToWalletButtonView.kt +63 -37
  52. package/android/src/main/java/com/reactnativestripesdk/pushprovisioning/AddToWalletCompleteEvent.kt +9 -13
  53. package/android/src/main/java/com/reactnativestripesdk/pushprovisioning/EphemeralKeyProvider.kt +15 -17
  54. package/android/src/main/java/com/reactnativestripesdk/pushprovisioning/PushProvisioningProxy.kt +76 -60
  55. package/android/src/main/java/com/reactnativestripesdk/pushprovisioning/TapAndPayProxy.kt +71 -47
  56. package/android/src/main/java/com/reactnativestripesdk/utils/Errors.kt +117 -51
  57. package/android/src/main/java/com/reactnativestripesdk/utils/Extensions.kt +4 -3
  58. package/android/src/main/java/com/reactnativestripesdk/utils/KeepJsAwakeTask.kt +39 -0
  59. package/android/src/main/java/com/reactnativestripesdk/utils/Mappers.kt +295 -258
  60. package/android/src/main/java/com/reactnativestripesdk/utils/PostalCodeUtilities.kt +2 -11
  61. package/ios/CustomerSheet/CustomerSheetUtils.swift +3 -1
  62. package/ios/FinancialConnections.swift +6 -2
  63. package/ios/StripeSdk+CustomerSheet.swift +2 -1
  64. package/ios/StripeSdk+PaymentSheet.swift +42 -0
  65. package/ios/StripeSdk.m +2 -0
  66. package/ios/StripeSdk.swift +72 -11
  67. package/lib/commonjs/NativeStripeSdk.js +1 -1
  68. package/lib/commonjs/NativeStripeSdk.js.map +1 -1
  69. package/lib/commonjs/components/AddToWalletButton.js +1 -1
  70. package/lib/commonjs/components/AddToWalletButton.js.map +1 -1
  71. package/lib/commonjs/components/AddressSheet.js +1 -1
  72. package/lib/commonjs/components/AddressSheet.js.map +1 -1
  73. package/lib/commonjs/components/ApplePayButtonNative.js +1 -1
  74. package/lib/commonjs/components/ApplePayButtonNative.js.map +1 -1
  75. package/lib/commonjs/components/AuBECSDebitForm.js +1 -1
  76. package/lib/commonjs/components/AuBECSDebitForm.js.map +1 -1
  77. package/lib/commonjs/components/CardField.js +1 -1
  78. package/lib/commonjs/components/CardField.js.map +1 -1
  79. package/lib/commonjs/components/CardForm.js +1 -1
  80. package/lib/commonjs/components/CardForm.js.map +1 -1
  81. package/lib/commonjs/components/CustomerSheet.js +1 -1
  82. package/lib/commonjs/components/CustomerSheet.js.map +1 -1
  83. package/lib/commonjs/components/GooglePayButtonNative.js +1 -1
  84. package/lib/commonjs/components/GooglePayButtonNative.js.map +1 -1
  85. package/lib/commonjs/components/PlatformPayButton.js +1 -1
  86. package/lib/commonjs/components/PlatformPayButton.js.map +1 -1
  87. package/lib/commonjs/components/StripeContainer.js +1 -1
  88. package/lib/commonjs/components/StripeContainer.js.map +1 -1
  89. package/lib/commonjs/components/StripeProvider.js +1 -1
  90. package/lib/commonjs/components/StripeProvider.js.map +1 -1
  91. package/lib/commonjs/functions.js +1 -1
  92. package/lib/commonjs/functions.js.map +1 -1
  93. package/lib/commonjs/helpers.js +1 -1
  94. package/lib/commonjs/helpers.js.map +1 -1
  95. package/lib/commonjs/hooks/useConfirmPayment.js.map +1 -1
  96. package/lib/commonjs/hooks/useConfirmSetupIntent.js.map +1 -1
  97. package/lib/commonjs/hooks/useFinancialConnectionsSheet.js.map +1 -1
  98. package/lib/commonjs/hooks/usePaymentSheet.js.map +1 -1
  99. package/lib/commonjs/hooks/usePlatformPay.js.map +1 -1
  100. package/lib/commonjs/hooks/useStripe.js.map +1 -1
  101. package/lib/commonjs/index.js.map +1 -1
  102. package/lib/commonjs/package.json +1 -0
  103. package/lib/commonjs/plugin/withStripe.js +1 -1
  104. package/lib/commonjs/plugin/withStripe.js.map +1 -1
  105. package/lib/commonjs/types/ApplePay.js.map +1 -1
  106. package/lib/commonjs/types/Common.js +1 -1
  107. package/lib/commonjs/types/Common.js.map +1 -1
  108. package/lib/commonjs/types/CustomerSheet.js.map +1 -1
  109. package/lib/commonjs/types/Errors.js +1 -1
  110. package/lib/commonjs/types/Errors.js.map +1 -1
  111. package/lib/commonjs/types/FinancialConnections.js +1 -1
  112. package/lib/commonjs/types/FinancialConnections.js.map +1 -1
  113. package/lib/commonjs/types/NextAction.js.map +1 -1
  114. package/lib/commonjs/types/PaymentIntent.js +1 -1
  115. package/lib/commonjs/types/PaymentIntent.js.map +1 -1
  116. package/lib/commonjs/types/PaymentMethod.js.map +1 -1
  117. package/lib/commonjs/types/PaymentSheet.js +1 -1
  118. package/lib/commonjs/types/PaymentSheet.js.map +1 -1
  119. package/lib/commonjs/types/PlatformPay.js +1 -1
  120. package/lib/commonjs/types/PlatformPay.js.map +1 -1
  121. package/lib/commonjs/types/PushProvisioning.js +1 -1
  122. package/lib/commonjs/types/PushProvisioning.js.map +1 -1
  123. package/lib/commonjs/types/SetupIntent.js +1 -1
  124. package/lib/commonjs/types/SetupIntent.js.map +1 -1
  125. package/lib/commonjs/types/ThreeDSecure.js +1 -1
  126. package/lib/commonjs/types/ThreeDSecure.js.map +1 -1
  127. package/lib/commonjs/types/Token.js.map +1 -1
  128. package/lib/commonjs/types/components/ApplePayButtonComponent.js.map +1 -1
  129. package/lib/commonjs/types/components/AuBECSDebitFormComponent.js.map +1 -1
  130. package/lib/commonjs/types/components/CardFieldInput.js +1 -1
  131. package/lib/commonjs/types/components/CardFieldInput.js.map +1 -1
  132. package/lib/commonjs/types/components/CardFormView.js.map +1 -1
  133. package/lib/commonjs/types/components/GooglePayButtonComponent.js.map +1 -1
  134. package/lib/commonjs/types/index.js +1 -1
  135. package/lib/commonjs/types/index.js.map +1 -1
  136. package/lib/module/NativeStripeSdk.js +1 -1
  137. package/lib/module/NativeStripeSdk.js.map +1 -1
  138. package/lib/module/components/AddToWalletButton.js +1 -1
  139. package/lib/module/components/AddToWalletButton.js.map +1 -1
  140. package/lib/module/components/AddressSheet.js +1 -1
  141. package/lib/module/components/AddressSheet.js.map +1 -1
  142. package/lib/module/components/ApplePayButtonNative.js +1 -1
  143. package/lib/module/components/ApplePayButtonNative.js.map +1 -1
  144. package/lib/module/components/AuBECSDebitForm.js +1 -1
  145. package/lib/module/components/AuBECSDebitForm.js.map +1 -1
  146. package/lib/module/components/CardField.js +1 -1
  147. package/lib/module/components/CardField.js.map +1 -1
  148. package/lib/module/components/CardForm.js +1 -1
  149. package/lib/module/components/CardForm.js.map +1 -1
  150. package/lib/module/components/CustomerSheet.js +1 -1
  151. package/lib/module/components/CustomerSheet.js.map +1 -1
  152. package/lib/module/components/GooglePayButtonNative.js +1 -1
  153. package/lib/module/components/GooglePayButtonNative.js.map +1 -1
  154. package/lib/module/components/PlatformPayButton.js +1 -1
  155. package/lib/module/components/PlatformPayButton.js.map +1 -1
  156. package/lib/module/components/StripeContainer.js +1 -1
  157. package/lib/module/components/StripeContainer.js.map +1 -1
  158. package/lib/module/components/StripeProvider.js +1 -1
  159. package/lib/module/components/StripeProvider.js.map +1 -1
  160. package/lib/module/functions.js +1 -1
  161. package/lib/module/functions.js.map +1 -1
  162. package/lib/module/helpers.js +1 -1
  163. package/lib/module/helpers.js.map +1 -1
  164. package/lib/module/hooks/useConfirmPayment.js.map +1 -1
  165. package/lib/module/hooks/useConfirmSetupIntent.js.map +1 -1
  166. package/lib/module/hooks/useFinancialConnectionsSheet.js.map +1 -1
  167. package/lib/module/hooks/usePaymentSheet.js.map +1 -1
  168. package/lib/module/hooks/usePlatformPay.js.map +1 -1
  169. package/lib/module/hooks/useStripe.js.map +1 -1
  170. package/lib/module/index.js.map +1 -1
  171. package/lib/module/package.json +1 -0
  172. package/lib/module/plugin/withStripe.js +1 -1
  173. package/lib/module/plugin/withStripe.js.map +1 -1
  174. package/lib/module/types/ApplePay.js.map +1 -1
  175. package/lib/module/types/Common.js +1 -1
  176. package/lib/module/types/Common.js.map +1 -1
  177. package/lib/module/types/CustomerSheet.js.map +1 -1
  178. package/lib/module/types/Errors.js +1 -1
  179. package/lib/module/types/Errors.js.map +1 -1
  180. package/lib/module/types/FinancialConnections.js +1 -1
  181. package/lib/module/types/FinancialConnections.js.map +1 -1
  182. package/lib/module/types/NextAction.js.map +1 -1
  183. package/lib/module/types/PaymentIntent.js +1 -1
  184. package/lib/module/types/PaymentIntent.js.map +1 -1
  185. package/lib/module/types/PaymentMethod.js.map +1 -1
  186. package/lib/module/types/PaymentSheet.js +1 -1
  187. package/lib/module/types/PaymentSheet.js.map +1 -1
  188. package/lib/module/types/PlatformPay.js +1 -1
  189. package/lib/module/types/PlatformPay.js.map +1 -1
  190. package/lib/module/types/PushProvisioning.js +1 -1
  191. package/lib/module/types/PushProvisioning.js.map +1 -1
  192. package/lib/module/types/SetupIntent.js +1 -1
  193. package/lib/module/types/SetupIntent.js.map +1 -1
  194. package/lib/module/types/ThreeDSecure.js +1 -1
  195. package/lib/module/types/ThreeDSecure.js.map +1 -1
  196. package/lib/module/types/Token.js.map +1 -1
  197. package/lib/module/types/components/ApplePayButtonComponent.js.map +1 -1
  198. package/lib/module/types/components/AuBECSDebitFormComponent.js.map +1 -1
  199. package/lib/module/types/components/CardFieldInput.js +1 -1
  200. package/lib/module/types/components/CardFieldInput.js.map +1 -1
  201. package/lib/module/types/components/CardFormView.js.map +1 -1
  202. package/lib/module/types/components/GooglePayButtonComponent.js.map +1 -1
  203. package/lib/module/types/index.js +1 -1
  204. package/lib/module/types/index.js.map +1 -1
  205. package/lib/typescript/src/NativeStripeSdk.d.ts +4 -3
  206. package/lib/typescript/src/NativeStripeSdk.d.ts.map +1 -0
  207. package/lib/typescript/src/components/AddToWalletButton.d.ts +3 -1
  208. package/lib/typescript/src/components/AddToWalletButton.d.ts.map +1 -0
  209. package/lib/typescript/src/components/AddressSheet.d.ts +4 -2
  210. package/lib/typescript/src/components/AddressSheet.d.ts.map +1 -0
  211. package/lib/typescript/src/components/ApplePayButtonNative.d.ts +1 -0
  212. package/lib/typescript/src/components/ApplePayButtonNative.d.ts.map +1 -0
  213. package/lib/typescript/src/components/AuBECSDebitForm.d.ts +3 -1
  214. package/lib/typescript/src/components/AuBECSDebitForm.d.ts.map +1 -0
  215. package/lib/typescript/src/components/CardField.d.ts +1 -0
  216. package/lib/typescript/src/components/CardField.d.ts.map +1 -0
  217. package/lib/typescript/src/components/CardForm.d.ts +1 -0
  218. package/lib/typescript/src/components/CardForm.d.ts.map +1 -0
  219. package/lib/typescript/src/components/CustomerSheet.d.ts +2 -1
  220. package/lib/typescript/src/components/CustomerSheet.d.ts.map +1 -0
  221. package/lib/typescript/src/components/GooglePayButtonNative.d.ts +1 -0
  222. package/lib/typescript/src/components/GooglePayButtonNative.d.ts.map +1 -0
  223. package/lib/typescript/src/components/PlatformPayButton.d.ts +3 -1
  224. package/lib/typescript/src/components/PlatformPayButton.d.ts.map +1 -0
  225. package/lib/typescript/src/components/StripeContainer.d.ts +2 -1
  226. package/lib/typescript/src/components/StripeContainer.d.ts.map +1 -0
  227. package/lib/typescript/src/components/StripeProvider.d.ts +3 -2
  228. package/lib/typescript/src/components/StripeProvider.d.ts.map +1 -0
  229. package/lib/typescript/src/functions.d.ts +6 -5
  230. package/lib/typescript/src/functions.d.ts.map +1 -0
  231. package/lib/typescript/src/helpers.d.ts +1 -0
  232. package/lib/typescript/src/helpers.d.ts.map +1 -0
  233. package/lib/typescript/src/hooks/useConfirmPayment.d.ts +2 -1
  234. package/lib/typescript/src/hooks/useConfirmPayment.d.ts.map +1 -0
  235. package/lib/typescript/src/hooks/useConfirmSetupIntent.d.ts +1 -0
  236. package/lib/typescript/src/hooks/useConfirmSetupIntent.d.ts.map +1 -0
  237. package/lib/typescript/src/hooks/useFinancialConnectionsSheet.d.ts +3 -2
  238. package/lib/typescript/src/hooks/useFinancialConnectionsSheet.d.ts.map +1 -0
  239. package/lib/typescript/src/hooks/usePaymentSheet.d.ts +2 -1
  240. package/lib/typescript/src/hooks/usePaymentSheet.d.ts.map +1 -0
  241. package/lib/typescript/src/hooks/usePlatformPay.d.ts +4 -3
  242. package/lib/typescript/src/hooks/usePlatformPay.d.ts.map +1 -0
  243. package/lib/typescript/src/hooks/useStripe.d.ts +11 -8
  244. package/lib/typescript/src/hooks/useStripe.d.ts.map +1 -0
  245. package/lib/typescript/src/index.d.ts +1 -0
  246. package/lib/typescript/src/index.d.ts.map +1 -0
  247. package/lib/typescript/src/plugin/withStripe.d.ts +2 -1
  248. package/lib/typescript/src/plugin/withStripe.d.ts.map +1 -0
  249. package/lib/typescript/src/types/ApplePay.d.ts +6 -5
  250. package/lib/typescript/src/types/ApplePay.d.ts.map +1 -0
  251. package/lib/typescript/src/types/Common.d.ts +4 -1
  252. package/lib/typescript/src/types/Common.d.ts.map +1 -0
  253. package/lib/typescript/src/types/CustomerSheet.d.ts +12 -4
  254. package/lib/typescript/src/types/CustomerSheet.d.ts.map +1 -0
  255. package/lib/typescript/src/types/Errors.d.ts +2 -1
  256. package/lib/typescript/src/types/Errors.d.ts.map +1 -0
  257. package/lib/typescript/src/types/FinancialConnections.d.ts +21 -17
  258. package/lib/typescript/src/types/FinancialConnections.d.ts.map +1 -0
  259. package/lib/typescript/src/types/NextAction.d.ts +9 -8
  260. package/lib/typescript/src/types/NextAction.d.ts.map +1 -0
  261. package/lib/typescript/src/types/PaymentIntent.d.ts +16 -15
  262. package/lib/typescript/src/types/PaymentIntent.d.ts.map +1 -0
  263. package/lib/typescript/src/types/PaymentMethod.d.ts +21 -16
  264. package/lib/typescript/src/types/PaymentMethod.d.ts.map +1 -0
  265. package/lib/typescript/src/types/PaymentSheet.d.ts +72 -22
  266. package/lib/typescript/src/types/PaymentSheet.d.ts.map +1 -0
  267. package/lib/typescript/src/types/PlatformPay.d.ts +22 -21
  268. package/lib/typescript/src/types/PlatformPay.d.ts.map +1 -0
  269. package/lib/typescript/src/types/PushProvisioning.d.ts +5 -4
  270. package/lib/typescript/src/types/PushProvisioning.d.ts.map +1 -0
  271. package/lib/typescript/src/types/SetupIntent.d.ts +4 -3
  272. package/lib/typescript/src/types/SetupIntent.d.ts.map +1 -0
  273. package/lib/typescript/src/types/ThreeDSecure.d.ts +1 -0
  274. package/lib/typescript/src/types/ThreeDSecure.d.ts.map +1 -0
  275. package/lib/typescript/src/types/Token.d.ts +10 -9
  276. package/lib/typescript/src/types/Token.d.ts.map +1 -0
  277. package/lib/typescript/src/types/components/ApplePayButtonComponent.d.ts +3 -2
  278. package/lib/typescript/src/types/components/ApplePayButtonComponent.d.ts.map +1 -0
  279. package/lib/typescript/src/types/components/AuBECSDebitFormComponent.d.ts +1 -0
  280. package/lib/typescript/src/types/components/AuBECSDebitFormComponent.d.ts.map +1 -0
  281. package/lib/typescript/src/types/components/CardFieldInput.d.ts +8 -5
  282. package/lib/typescript/src/types/components/CardFieldInput.d.ts.map +1 -0
  283. package/lib/typescript/src/types/components/CardFormView.d.ts +9 -6
  284. package/lib/typescript/src/types/components/CardFormView.d.ts.map +1 -0
  285. package/lib/typescript/src/types/components/GooglePayButtonComponent.d.ts +1 -0
  286. package/lib/typescript/src/types/components/GooglePayButtonComponent.d.ts.map +1 -0
  287. package/lib/typescript/src/types/index.d.ts +24 -23
  288. package/lib/typescript/src/types/index.d.ts.map +1 -0
  289. package/package.json +32 -40
  290. package/src/NativeStripeSdk.tsx +4 -2
  291. package/src/components/CardField.tsx +1 -1
  292. package/src/components/CardForm.tsx +12 -9
  293. package/src/components/CustomerSheet.tsx +4 -6
  294. package/src/components/StripeProvider.tsx +17 -0
  295. package/src/functions.ts +13 -14
  296. package/src/types/Common.ts +3 -0
  297. package/src/types/CustomerSheet.ts +7 -0
  298. package/src/types/FinancialConnections.ts +3 -0
  299. package/src/types/PaymentMethod.ts +5 -1
  300. package/src/types/PaymentSheet.ts +64 -4
  301. package/src/types/components/CardFieldInput.ts +8 -4
  302. package/src/types/components/CardFormView.ts +8 -4
  303. package/stripe-react-native.podspec +2 -1
  304. package/android/.gradle/8.9/checksums/checksums.lock +0 -0
  305. package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
  306. package/android/.gradle/8.9/gc.properties +0 -0
  307. /package/android/.gradle/{8.9 → 8.11.1}/fileChanges/last-build.bin +0 -0
  308. /package/android/.gradle/{8.9/dependencies-accessors → 8.11.1}/gc.properties +0 -0
@@ -18,13 +18,31 @@ import android.widget.FrameLayout
18
18
  import androidx.appcompat.content.res.AppCompatResources
19
19
  import androidx.core.graphics.drawable.DrawableCompat
20
20
  import androidx.fragment.app.Fragment
21
- import com.facebook.react.bridge.*
21
+ import com.facebook.react.bridge.Arguments
22
+ import com.facebook.react.bridge.Promise
23
+ import com.facebook.react.bridge.ReactApplicationContext
24
+ import com.facebook.react.bridge.ReadableMap
25
+ import com.facebook.react.bridge.WritableMap
26
+ import com.facebook.react.bridge.WritableNativeMap
22
27
  import com.reactnativestripesdk.addresssheet.AddressSheetView
23
- import com.reactnativestripesdk.utils.*
28
+ import com.reactnativestripesdk.utils.ErrorType
29
+ import com.reactnativestripesdk.utils.KeepJsAwakeTask
30
+ import com.reactnativestripesdk.utils.PaymentSheetAppearanceException
31
+ import com.reactnativestripesdk.utils.PaymentSheetErrorType
32
+ import com.reactnativestripesdk.utils.PaymentSheetException
24
33
  import com.reactnativestripesdk.utils.createError
25
34
  import com.reactnativestripesdk.utils.createResult
35
+ import com.reactnativestripesdk.utils.mapFromPaymentMethod
36
+ import com.reactnativestripesdk.utils.mapToPreferredNetworks
37
+ import com.reactnativestripesdk.utils.removeFragment
26
38
  import com.stripe.android.ExperimentalAllowsRemovalOfLastSavedPaymentMethodApi
27
- import com.stripe.android.paymentsheet.*
39
+ import com.stripe.android.paymentsheet.CreateIntentCallback
40
+ import com.stripe.android.paymentsheet.CreateIntentResult
41
+ import com.stripe.android.paymentsheet.ExperimentalCustomerSessionApi
42
+ import com.stripe.android.paymentsheet.PaymentOptionCallback
43
+ import com.stripe.android.paymentsheet.PaymentSheet
44
+ import com.stripe.android.paymentsheet.PaymentSheetResult
45
+ import com.stripe.android.paymentsheet.PaymentSheetResultCallback
28
46
  import kotlinx.coroutines.CompletableDeferred
29
47
  import java.io.ByteArrayOutputStream
30
48
  import kotlin.Exception
@@ -32,7 +50,7 @@ import kotlin.Exception
32
50
  @OptIn(ExperimentalAllowsRemovalOfLastSavedPaymentMethodApi::class)
33
51
  class PaymentSheetFragment(
34
52
  private val context: ReactApplicationContext,
35
- private val initPromise: Promise
53
+ private val initPromise: Promise,
36
54
  ) : Fragment() {
37
55
  private var paymentSheet: PaymentSheet? = null
38
56
  private var flowController: PaymentSheet.FlowController? = null
@@ -44,23 +62,24 @@ class PaymentSheetFragment(
44
62
  private var presentPromise: Promise? = null
45
63
  private var paymentSheetTimedOut = false
46
64
  internal var paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
65
+ private var keepJsAwake: KeepJsAwakeTask? = null
47
66
 
48
67
  override fun onCreateView(
49
68
  inflater: LayoutInflater,
50
69
  container: ViewGroup?,
51
- savedInstanceState: Bundle?
52
- ): View {
53
- return FrameLayout(requireActivity()).also {
54
- it.visibility = View.GONE
55
- }
56
- }
70
+ savedInstanceState: Bundle?,
71
+ ): View = FrameLayout(requireActivity()).also { it.visibility = View.GONE }
57
72
 
58
- @OptIn(ExperimentalPaymentMethodLayoutApi::class)
59
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
73
+ override fun onViewCreated(
74
+ view: View,
75
+ savedInstanceState: Bundle?,
76
+ ) {
60
77
  super.onViewCreated(view, savedInstanceState)
61
78
  val merchantDisplayName = arguments?.getString("merchantDisplayName").orEmpty()
62
79
  if (merchantDisplayName.isEmpty()) {
63
- initPromise.resolve(createError(ErrorType.Failed.toString(), "merchantDisplayName cannot be empty or null."))
80
+ initPromise.resolve(
81
+ createError(ErrorType.Failed.toString(), "merchantDisplayName cannot be empty or null."),
82
+ )
64
83
  return
65
84
  }
66
85
  val primaryButtonLabel = arguments?.getString("primaryButtonLabel")
@@ -69,238 +88,291 @@ class PaymentSheetFragment(
69
88
  val billingDetailsBundle = arguments?.getBundle("defaultBillingDetails")
70
89
  val billingConfigParams = arguments?.getBundle("billingDetailsCollectionConfiguration")
71
90
  val paymentMethodOrder = arguments?.getStringArrayList("paymentMethodOrder")
72
- val allowsRemovalOfLastSavedPaymentMethod = arguments?.getBoolean("allowsRemovalOfLastSavedPaymentMethod", true) ?: true
91
+ val allowsRemovalOfLastSavedPaymentMethod =
92
+ arguments?.getBoolean("allowsRemovalOfLastSavedPaymentMethod", true) ?: true
73
93
  paymentIntentClientSecret = arguments?.getString("paymentIntentClientSecret").orEmpty()
74
94
  setupIntentClientSecret = arguments?.getString("setupIntentClientSecret").orEmpty()
75
- intentConfiguration = try {
76
- buildIntentConfiguration(arguments?.getBundle("intentConfiguration"))
77
- } catch (error: PaymentSheetException) {
78
- initPromise.resolve(createError(ErrorType.Failed.toString(), error))
79
- return
80
- }
81
- val appearance = try {
82
- buildPaymentSheetAppearance(arguments?.getBundle("appearance"), context)
83
- } catch (error: PaymentSheetAppearanceException) {
84
- initPromise.resolve(createError(ErrorType.Failed.toString(), error))
85
- return
86
- }
95
+ intentConfiguration =
96
+ try {
97
+ buildIntentConfiguration(arguments?.getBundle("intentConfiguration"))
98
+ } catch (error: PaymentSheetException) {
99
+ initPromise.resolve(createError(ErrorType.Failed.toString(), error))
100
+ return
101
+ }
102
+ val appearance =
103
+ try {
104
+ buildPaymentSheetAppearance(arguments?.getBundle("appearance"), context)
105
+ } catch (error: PaymentSheetAppearanceException) {
106
+ initPromise.resolve(createError(ErrorType.Failed.toString(), error))
107
+ return
108
+ }
87
109
 
88
- val customerConfiguration = try {
89
- buildCustomerConfiguration(arguments)
90
- } catch (error: PaymentSheetException) {
91
- initPromise.resolve(createError(ErrorType.Failed.toString(), error))
92
- return
93
- }
110
+ val customerConfiguration =
111
+ try {
112
+ buildCustomerConfiguration(arguments)
113
+ } catch (error: PaymentSheetException) {
114
+ initPromise.resolve(createError(ErrorType.Failed.toString(), error))
115
+ return
116
+ }
94
117
 
95
- val shippingDetails = arguments?.getBundle("defaultShippingDetails")?.let {
96
- AddressSheetView.buildAddressDetails(it)
97
- }
118
+ val shippingDetails =
119
+ arguments?.getBundle("defaultShippingDetails")?.let {
120
+ AddressSheetView.buildAddressDetails(it)
121
+ }
122
+
123
+ val paymentOptionCallback =
124
+ PaymentOptionCallback { paymentOption ->
125
+ val result =
126
+ paymentOption?.let {
127
+ val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
128
+ val imageString = getBase64FromBitmap(bitmap)
129
+ val option: WritableMap = WritableNativeMap()
130
+ option.putString("label", it.label)
131
+ option.putString("image", imageString)
132
+ createResult("paymentOption", option)
133
+ }
134
+ ?: run {
135
+ if (paymentSheetTimedOut) {
136
+ paymentSheetTimedOut = false
137
+ createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out")
138
+ } else {
139
+ createError(
140
+ PaymentSheetErrorType.Canceled.toString(),
141
+ "The payment option selection flow has been canceled",
142
+ )
143
+ }
144
+ }
145
+ resolvePresentPromise(result)
146
+ }
98
147
 
99
- val paymentOptionCallback = PaymentOptionCallback { paymentOption ->
100
- val result = paymentOption?.let {
101
- val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
102
- val imageString = getBase64FromBitmap(bitmap)
103
- val option: WritableMap = WritableNativeMap()
104
- option.putString("label", it.label)
105
- option.putString("image", imageString)
106
- createResult("paymentOption", option)
107
- } ?: run {
148
+ val paymentResultCallback =
149
+ PaymentSheetResultCallback { paymentResult ->
108
150
  if (paymentSheetTimedOut) {
109
151
  paymentSheetTimedOut = false
110
- createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out")
152
+ resolvePaymentResult(
153
+ createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out"),
154
+ )
111
155
  } else {
112
- createError(PaymentSheetErrorType.Canceled.toString(), "The payment option selection flow has been canceled")
113
- }
114
- }
115
- presentPromise?.resolve(result)
116
- }
117
-
118
- val paymentResultCallback = PaymentSheetResultCallback { paymentResult ->
119
- if (paymentSheetTimedOut) {
120
- paymentSheetTimedOut = false
121
- resolvePaymentResult(createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out"))
122
- } else {
123
- when (paymentResult) {
124
- is PaymentSheetResult.Canceled -> {
125
- resolvePaymentResult(createError(PaymentSheetErrorType.Canceled.toString(), "The payment flow has been canceled"))
126
- }
127
- is PaymentSheetResult.Failed -> {
128
- resolvePaymentResult(createError(PaymentSheetErrorType.Failed.toString(), paymentResult.error))
129
- }
130
- is PaymentSheetResult.Completed -> {
131
- resolvePaymentResult(WritableNativeMap())
132
- // Remove the fragment now, we can be sure it won't be needed again if an intent is successful
133
- removeFragment(context)
134
- paymentSheet = null
135
- flowController = null
156
+ when (paymentResult) {
157
+ is PaymentSheetResult.Canceled -> {
158
+ resolvePaymentResult(
159
+ createError(
160
+ PaymentSheetErrorType.Canceled.toString(),
161
+ "The payment flow has been canceled",
162
+ ),
163
+ )
164
+ }
165
+ is PaymentSheetResult.Failed -> {
166
+ resolvePaymentResult(
167
+ createError(PaymentSheetErrorType.Failed.toString(), paymentResult.error),
168
+ )
169
+ }
170
+ is PaymentSheetResult.Completed -> {
171
+ resolvePaymentResult(WritableNativeMap())
172
+ // Remove the fragment now, we can be sure it won't be needed again if an intent is
173
+ // successful
174
+ removeFragment(context)
175
+ paymentSheet = null
176
+ flowController = null
177
+ }
136
178
  }
137
179
  }
138
180
  }
139
- }
140
181
 
141
- val createIntentCallback = CreateIntentCallback { paymentMethod, shouldSavePaymentMethod ->
142
- val stripeSdkModule: StripeSdkModule? = context.getNativeModule(StripeSdkModule::class.java)
143
- if (stripeSdkModule == null || stripeSdkModule.eventListenerCount == 0) {
144
- return@CreateIntentCallback CreateIntentResult.Failure(
145
- cause = Exception("Tried to call confirmHandler, but no callback was found. Please file an issue: https://github.com/stripe/stripe-react-native/issues"),
146
- displayMessage = "An unexpected error occurred"
182
+ val createIntentCallback =
183
+ CreateIntentCallback { paymentMethod, shouldSavePaymentMethod ->
184
+ val stripeSdkModule: StripeSdkModule? = context.getNativeModule(StripeSdkModule::class.java)
185
+ if (stripeSdkModule == null || stripeSdkModule.eventListenerCount == 0) {
186
+ return@CreateIntentCallback CreateIntentResult.Failure(
187
+ cause =
188
+ Exception(
189
+ "Tried to call confirmHandler, but no callback was found. Please file an issue: https://github.com/stripe/stripe-react-native/issues",
190
+ ),
191
+ displayMessage = "An unexpected error occurred",
147
192
  )
148
- }
149
- val params = Arguments.createMap().apply {
150
- putMap("paymentMethod", mapFromPaymentMethod(paymentMethod))
151
- putBoolean("shouldSavePaymentMethod", shouldSavePaymentMethod)
152
- }
193
+ }
194
+ val params =
195
+ Arguments.createMap().apply {
196
+ putMap("paymentMethod", mapFromPaymentMethod(paymentMethod))
197
+ putBoolean("shouldSavePaymentMethod", shouldSavePaymentMethod)
198
+ }
153
199
 
154
- stripeSdkModule.sendEvent(context, "onConfirmHandlerCallback", params)
200
+ stripeSdkModule.sendEvent(context, "onConfirmHandlerCallback", params)
155
201
 
156
- val resultFromJavascript = paymentSheetIntentCreationCallback.await()
157
- // reset the completable
158
- paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
202
+ val resultFromJavascript = paymentSheetIntentCreationCallback.await()
203
+ // reset the completable
204
+ paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
159
205
 
160
- return@CreateIntentCallback resultFromJavascript.getString("clientSecret")?.let {
161
- CreateIntentResult.Success(clientSecret = it)
162
- } ?: run {
163
- val errorMap = resultFromJavascript.getMap("error")
164
- CreateIntentResult.Failure(
165
- cause = Exception(errorMap?.getString("message")),
166
- displayMessage = errorMap?.getString("localizedMessage")
167
- )
206
+ return@CreateIntentCallback resultFromJavascript.getString("clientSecret")?.let {
207
+ CreateIntentResult.Success(clientSecret = it)
208
+ }
209
+ ?: run {
210
+ val errorMap = resultFromJavascript.getMap("error")
211
+ CreateIntentResult.Failure(
212
+ cause = Exception(errorMap?.getString("message")),
213
+ displayMessage = errorMap?.getString("localizedMessage"),
214
+ )
215
+ }
168
216
  }
169
- }
170
217
 
171
- val billingDetailsConfig = PaymentSheet.BillingDetailsCollectionConfiguration(
172
- name = mapToCollectionMode(billingConfigParams?.getString("name")),
173
- phone = mapToCollectionMode(billingConfigParams?.getString("phone")),
174
- email = mapToCollectionMode(billingConfigParams?.getString("email")),
175
- address = mapToAddressCollectionMode(billingConfigParams?.getString("address")),
176
- attachDefaultsToPaymentMethod = billingConfigParams?.getBoolean("attachDefaultsToPaymentMethod")
177
- ?: false
178
- )
218
+ val billingDetailsConfig =
219
+ PaymentSheet.BillingDetailsCollectionConfiguration(
220
+ name = mapToCollectionMode(billingConfigParams?.getString("name")),
221
+ phone = mapToCollectionMode(billingConfigParams?.getString("phone")),
222
+ email = mapToCollectionMode(billingConfigParams?.getString("email")),
223
+ address = mapToAddressCollectionMode(billingConfigParams?.getString("address")),
224
+ attachDefaultsToPaymentMethod =
225
+ billingConfigParams?.getBoolean("attachDefaultsToPaymentMethod") ?: false,
226
+ )
179
227
 
180
228
  var defaultBillingDetails: PaymentSheet.BillingDetails? = null
181
229
  if (billingDetailsBundle != null) {
182
230
  val addressBundle = billingDetailsBundle.getBundle("address")
183
- val address = PaymentSheet.Address(
184
- addressBundle?.getString("city"),
185
- addressBundle?.getString("country"),
186
- addressBundle?.getString("line1"),
187
- addressBundle?.getString("line2"),
188
- addressBundle?.getString("postalCode"),
189
- addressBundle?.getString("state"))
190
- defaultBillingDetails = PaymentSheet.BillingDetails(
191
- address,
192
- billingDetailsBundle.getString("email"),
193
- billingDetailsBundle.getString("name"),
194
- billingDetailsBundle.getString("phone"))
195
- }
196
- val configurationBuilder = PaymentSheet.Configuration.Builder(merchantDisplayName)
197
- .allowsDelayedPaymentMethods(allowsDelayedPaymentMethods ?: false)
198
- .defaultBillingDetails(defaultBillingDetails)
199
- .customer(customerConfiguration)
200
- .googlePay(googlePayConfig)
201
- .appearance(appearance)
202
- .shippingDetails(shippingDetails)
203
- .billingDetailsCollectionConfiguration(billingDetailsConfig)
204
- .preferredNetworks(mapToPreferredNetworks(arguments?.getIntegerArrayList("preferredNetworks")))
205
- .allowsRemovalOfLastSavedPaymentMethod(allowsRemovalOfLastSavedPaymentMethod)
206
- primaryButtonLabel?.let {
207
- configurationBuilder.primaryButtonLabel(it)
208
- }
209
- paymentMethodOrder?.let {
210
- configurationBuilder.paymentMethodOrder(it)
231
+ val address =
232
+ PaymentSheet.Address(
233
+ addressBundle?.getString("city"),
234
+ addressBundle?.getString("country"),
235
+ addressBundle?.getString("line1"),
236
+ addressBundle?.getString("line2"),
237
+ addressBundle?.getString("postalCode"),
238
+ addressBundle?.getString("state"),
239
+ )
240
+ defaultBillingDetails =
241
+ PaymentSheet.BillingDetails(
242
+ address,
243
+ billingDetailsBundle.getString("email"),
244
+ billingDetailsBundle.getString("name"),
245
+ billingDetailsBundle.getString("phone"),
246
+ )
211
247
  }
248
+ val configurationBuilder =
249
+ PaymentSheet.Configuration
250
+ .Builder(merchantDisplayName)
251
+ .allowsDelayedPaymentMethods(allowsDelayedPaymentMethods ?: false)
252
+ .defaultBillingDetails(defaultBillingDetails)
253
+ .customer(customerConfiguration)
254
+ .googlePay(googlePayConfig)
255
+ .appearance(appearance)
256
+ .shippingDetails(shippingDetails)
257
+ .billingDetailsCollectionConfiguration(billingDetailsConfig)
258
+ .preferredNetworks(
259
+ mapToPreferredNetworks(arguments?.getIntegerArrayList("preferredNetworks")),
260
+ ).allowsRemovalOfLastSavedPaymentMethod(allowsRemovalOfLastSavedPaymentMethod)
261
+ .cardBrandAcceptance(mapToCardBrandAcceptance(arguments))
262
+
263
+ primaryButtonLabel?.let { configurationBuilder.primaryButtonLabel(it) }
264
+ paymentMethodOrder?.let { configurationBuilder.paymentMethodOrder(it) }
212
265
 
213
266
  configurationBuilder.paymentMethodLayout(
214
- mapToPaymentMethodLayout(arguments?.getString("paymentMethodLayout"))
267
+ mapToPaymentMethodLayout(arguments?.getString("paymentMethodLayout")),
215
268
  )
216
269
 
217
270
  paymentSheetConfiguration = configurationBuilder.build()
218
271
 
219
272
  if (arguments?.getBoolean("customFlow") == true) {
220
- flowController = if (intentConfiguration != null) {
221
- PaymentSheet.FlowController.create(
222
- this,
223
- paymentOptionCallback = paymentOptionCallback,
224
- createIntentCallback = createIntentCallback,
225
- paymentResultCallback = paymentResultCallback
226
- )
227
- } else {
228
- PaymentSheet.FlowController.create(
229
- this,
230
- paymentOptionCallback = paymentOptionCallback,
231
- paymentResultCallback = paymentResultCallback
232
- )
233
- }
273
+ flowController =
274
+ if (intentConfiguration != null) {
275
+ PaymentSheet.FlowController.create(
276
+ this,
277
+ paymentOptionCallback = paymentOptionCallback,
278
+ createIntentCallback = createIntentCallback,
279
+ paymentResultCallback = paymentResultCallback,
280
+ )
281
+ } else {
282
+ PaymentSheet.FlowController.create(
283
+ this,
284
+ paymentOptionCallback = paymentOptionCallback,
285
+ paymentResultCallback = paymentResultCallback,
286
+ )
287
+ }
234
288
  configureFlowController()
235
289
  } else {
236
- paymentSheet = if (intentConfiguration != null) {
237
- PaymentSheet(
238
- this,
239
- createIntentCallback = createIntentCallback,
240
- paymentResultCallback = paymentResultCallback
241
- )
242
- } else {
243
- PaymentSheet(
244
- this,
245
- callback = paymentResultCallback
246
- )
247
- }
290
+ paymentSheet =
291
+ if (intentConfiguration != null) {
292
+ PaymentSheet(
293
+ this,
294
+ createIntentCallback = createIntentCallback,
295
+ paymentResultCallback = paymentResultCallback,
296
+ )
297
+ } else {
298
+ PaymentSheet(this, callback = paymentResultCallback)
299
+ }
248
300
  initPromise.resolve(WritableNativeMap())
249
301
  }
250
302
  }
251
303
 
252
304
  fun present(promise: Promise) {
253
- this.presentPromise = promise
254
- if(paymentSheet != null) {
305
+ keepJsAwake = KeepJsAwakeTask(context).apply { start() }
306
+ presentPromise = promise
307
+ if (paymentSheet != null) {
255
308
  if (!paymentIntentClientSecret.isNullOrEmpty()) {
256
- paymentSheet?.presentWithPaymentIntent(paymentIntentClientSecret!!, paymentSheetConfiguration)
309
+ paymentSheet?.presentWithPaymentIntent(
310
+ paymentIntentClientSecret!!,
311
+ paymentSheetConfiguration,
312
+ )
257
313
  } else if (!setupIntentClientSecret.isNullOrEmpty()) {
258
314
  paymentSheet?.presentWithSetupIntent(setupIntentClientSecret!!, paymentSheetConfiguration)
259
315
  } else if (intentConfiguration != null) {
260
316
  paymentSheet?.presentWithIntentConfiguration(
261
317
  intentConfiguration = intentConfiguration!!,
262
- configuration = paymentSheetConfiguration
318
+ configuration = paymentSheetConfiguration,
263
319
  )
264
320
  }
265
- } else if(flowController != null) {
321
+ } else if (flowController != null) {
266
322
  flowController?.presentPaymentOptions()
267
323
  } else {
268
324
  promise.resolve(createMissingInitError())
269
325
  }
270
326
  }
271
327
 
272
- fun presentWithTimeout(timeout: Long, promise: Promise) {
328
+ fun presentWithTimeout(
329
+ timeout: Long,
330
+ promise: Promise,
331
+ ) {
273
332
  var paymentSheetActivity: Activity? = null
274
333
 
275
- val activityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
276
- override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
277
- paymentSheetActivity = activity
278
- }
334
+ val activityLifecycleCallbacks =
335
+ object : Application.ActivityLifecycleCallbacks {
336
+ override fun onActivityCreated(
337
+ activity: Activity,
338
+ savedInstanceState: Bundle?,
339
+ ) {
340
+ paymentSheetActivity = activity
341
+ }
279
342
 
280
- override fun onActivityStarted(activity: Activity) {}
343
+ override fun onActivityStarted(activity: Activity) {}
281
344
 
282
- override fun onActivityResumed(activity: Activity) {}
345
+ override fun onActivityResumed(activity: Activity) {}
283
346
 
284
- override fun onActivityPaused(activity: Activity) {}
347
+ override fun onActivityPaused(activity: Activity) {}
285
348
 
286
- override fun onActivityStopped(activity: Activity) {}
349
+ override fun onActivityStopped(activity: Activity) {}
287
350
 
288
- override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
351
+ override fun onActivitySaveInstanceState(
352
+ activity: Activity,
353
+ outState: Bundle,
354
+ ) {}
289
355
 
290
- override fun onActivityDestroyed(activity: Activity) {
291
- paymentSheetActivity = null
292
- context.currentActivity?.application?.unregisterActivityLifecycleCallbacks(this)
356
+ override fun onActivityDestroyed(activity: Activity) {
357
+ paymentSheetActivity = null
358
+ context.currentActivity?.application?.unregisterActivityLifecycleCallbacks(this)
359
+ }
293
360
  }
294
- }
295
361
 
296
- Handler(Looper.getMainLooper()).postDelayed({
297
- paymentSheetActivity?.let {
298
- it.finish()
299
- paymentSheetTimedOut = true
300
- }
301
- }, timeout)
362
+ Handler(Looper.getMainLooper())
363
+ .postDelayed(
364
+ {
365
+ paymentSheetActivity?.let {
366
+ it.finish()
367
+ paymentSheetTimedOut = true
368
+ }
369
+ },
370
+ timeout,
371
+ )
302
372
 
303
- context.currentActivity?.application?.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
373
+ context.currentActivity
374
+ ?.application
375
+ ?.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
304
376
 
305
377
  this.present(promise)
306
378
  }
@@ -311,70 +383,81 @@ class PaymentSheetFragment(
311
383
  }
312
384
 
313
385
  private fun configureFlowController() {
314
- val onFlowControllerConfigure = PaymentSheet.FlowController.ConfigCallback { _, _ ->
315
- val result = flowController?.getPaymentOption()?.let {
316
- val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
317
- val imageString = getBase64FromBitmap(bitmap)
318
- val option: WritableMap = WritableNativeMap()
319
- option.putString("label", it.label)
320
- option.putString("image", imageString)
321
- createResult("paymentOption", option)
322
- } ?: run {
323
- WritableNativeMap()
386
+ val onFlowControllerConfigure =
387
+ PaymentSheet.FlowController.ConfigCallback { _, _ ->
388
+ val result =
389
+ flowController?.getPaymentOption()?.let {
390
+ val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
391
+ val imageString = getBase64FromBitmap(bitmap)
392
+ val option: WritableMap = WritableNativeMap()
393
+ option.putString("label", it.label)
394
+ option.putString("image", imageString)
395
+ createResult("paymentOption", option)
396
+ } ?: run { WritableNativeMap() }
397
+ initPromise.resolve(result)
324
398
  }
325
- initPromise.resolve(result)
326
- }
327
399
 
328
400
  if (!paymentIntentClientSecret.isNullOrEmpty()) {
329
401
  flowController?.configureWithPaymentIntent(
330
402
  paymentIntentClientSecret = paymentIntentClientSecret!!,
331
403
  configuration = paymentSheetConfiguration,
332
- callback = onFlowControllerConfigure
404
+ callback = onFlowControllerConfigure,
333
405
  )
334
406
  } else if (!setupIntentClientSecret.isNullOrEmpty()) {
335
407
  flowController?.configureWithSetupIntent(
336
408
  setupIntentClientSecret = setupIntentClientSecret!!,
337
409
  configuration = paymentSheetConfiguration,
338
- callback = onFlowControllerConfigure
410
+ callback = onFlowControllerConfigure,
339
411
  )
340
412
  } else if (intentConfiguration != null) {
341
413
  flowController?.configureWithIntentConfiguration(
342
414
  intentConfiguration = intentConfiguration!!,
343
415
  configuration = paymentSheetConfiguration,
344
- callback = onFlowControllerConfigure
416
+ callback = onFlowControllerConfigure,
345
417
  )
346
418
  } else {
347
- initPromise.resolve(createError(ErrorType.Failed.toString(), "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration` is required"))
419
+ initPromise.resolve(
420
+ createError(
421
+ ErrorType.Failed.toString(),
422
+ "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration` is required",
423
+ ),
424
+ )
348
425
  return
349
426
  }
350
427
  }
351
428
 
429
+ private fun resolvePresentPromise(value: Any?) {
430
+ keepJsAwake?.stop()
431
+ presentPromise?.resolve(value)
432
+ }
433
+
352
434
  private fun resolvePaymentResult(map: WritableMap) {
353
435
  confirmPromise?.let {
354
436
  it.resolve(map)
355
437
  confirmPromise = null
356
- } ?: run {
357
- presentPromise?.resolve(map)
358
- }
438
+ } ?: run { resolvePresentPromise(map) }
359
439
  }
360
440
 
361
441
  companion object {
362
442
  internal const val TAG = "payment_sheet_launch_fragment"
363
443
 
364
- private val mapIntToButtonType = mapOf(
365
- 1 to PaymentSheet.GooglePayConfiguration.ButtonType.Buy,
366
- 6 to PaymentSheet.GooglePayConfiguration.ButtonType.Book,
367
- 5 to PaymentSheet.GooglePayConfiguration.ButtonType.Checkout,
368
- 4 to PaymentSheet.GooglePayConfiguration.ButtonType.Donate,
369
- 11 to PaymentSheet.GooglePayConfiguration.ButtonType.Order,
370
- 1000 to PaymentSheet.GooglePayConfiguration.ButtonType.Pay,
371
- 7 to PaymentSheet.GooglePayConfiguration.ButtonType.Subscribe,
372
- 1001 to PaymentSheet.GooglePayConfiguration.ButtonType.Plain,
373
- )
444
+ private val mapIntToButtonType =
445
+ mapOf(
446
+ 1 to PaymentSheet.GooglePayConfiguration.ButtonType.Buy,
447
+ 6 to PaymentSheet.GooglePayConfiguration.ButtonType.Book,
448
+ 5 to PaymentSheet.GooglePayConfiguration.ButtonType.Checkout,
449
+ 4 to PaymentSheet.GooglePayConfiguration.ButtonType.Donate,
450
+ 11 to PaymentSheet.GooglePayConfiguration.ButtonType.Order,
451
+ 1000 to PaymentSheet.GooglePayConfiguration.ButtonType.Pay,
452
+ 7 to PaymentSheet.GooglePayConfiguration.ButtonType.Subscribe,
453
+ 1001 to PaymentSheet.GooglePayConfiguration.ButtonType.Plain,
454
+ )
374
455
 
375
- internal fun createMissingInitError(): WritableMap {
376
- return createError(PaymentSheetErrorType.Failed.toString(), "No payment sheet has been initialized yet. You must call `initPaymentSheet` before `presentPaymentSheet`.")
377
- }
456
+ internal fun createMissingInitError(): WritableMap =
457
+ createError(
458
+ PaymentSheetErrorType.Failed.toString(),
459
+ "No payment sheet has been initialized yet. You must call `initPaymentSheet` before `presentPaymentSheet`.",
460
+ )
378
461
 
379
462
  internal fun buildGooglePayConfig(params: Bundle?): PaymentSheet.GooglePayConfiguration? {
380
463
  if (params == null) {
@@ -386,16 +469,22 @@ class PaymentSheetFragment(
386
469
  val testEnv = params.getBoolean("testEnv")
387
470
  val amount = params.getString("amount")?.toLongOrNull()
388
471
  val label = params.getString("label")
389
- val buttonType = mapIntToButtonType.get(params.getInt("buttonType")) ?: PaymentSheet.GooglePayConfiguration.ButtonType.Pay
390
-
472
+ val buttonType =
473
+ mapIntToButtonType.get(params.getInt("buttonType"))
474
+ ?: PaymentSheet.GooglePayConfiguration.ButtonType.Pay
391
475
 
392
476
  return PaymentSheet.GooglePayConfiguration(
393
- environment = if (testEnv) PaymentSheet.GooglePayConfiguration.Environment.Test else PaymentSheet.GooglePayConfiguration.Environment.Production,
477
+ environment =
478
+ if (testEnv) {
479
+ PaymentSheet.GooglePayConfiguration.Environment.Test
480
+ } else {
481
+ PaymentSheet.GooglePayConfiguration.Environment.Production
482
+ },
394
483
  countryCode = countryCode,
395
484
  currencyCode = currencyCode,
396
485
  amount = amount,
397
486
  label = label,
398
- buttonType = buttonType
487
+ buttonType = buttonType,
399
488
  )
400
489
  }
401
490
 
@@ -404,18 +493,26 @@ class PaymentSheetFragment(
404
493
  if (intentConfigurationParams == null) {
405
494
  return null
406
495
  }
407
- val modeParams = intentConfigurationParams.getBundle("mode")
408
- ?: throw PaymentSheetException("If `intentConfiguration` is provided, `intentConfiguration.mode` is required")
496
+ val modeParams =
497
+ intentConfigurationParams.getBundle("mode")
498
+ ?: throw PaymentSheetException(
499
+ "If `intentConfiguration` is provided, `intentConfiguration.mode` is required",
500
+ )
409
501
 
410
502
  return PaymentSheet.IntentConfiguration(
411
503
  mode = buildIntentConfigurationMode(modeParams),
412
- paymentMethodTypes = intentConfigurationParams.getStringArrayList("paymentMethodTypes")?.toList() ?: emptyList(),
504
+ paymentMethodTypes =
505
+ intentConfigurationParams.getStringArrayList("paymentMethodTypes")?.toList()
506
+ ?: emptyList(),
413
507
  )
414
508
  }
415
509
 
416
510
  private fun buildIntentConfigurationMode(modeParams: Bundle): PaymentSheet.IntentConfiguration.Mode {
417
- val currencyCode = modeParams.getString("currencyCode")
418
- ?: throw PaymentSheetException("You must provide a value to intentConfiguration.mode.currencyCode")
511
+ val currencyCode =
512
+ modeParams.getString("currencyCode")
513
+ ?: throw PaymentSheetException(
514
+ "You must provide a value to intentConfiguration.mode.currencyCode",
515
+ )
419
516
 
420
517
  return if (modeParams.containsKey("amount")) {
421
518
  PaymentSheet.IntentConfiguration.Mode.Payment(
@@ -425,11 +522,14 @@ class PaymentSheetFragment(
425
522
  captureMethod = mapToCaptureMethod(modeParams.getString("captureMethod")),
426
523
  )
427
524
  } else {
428
- val setupFutureUsage = mapToSetupFutureUse(modeParams.getString("setupFutureUsage"))
429
- ?: throw PaymentSheetException("You must provide a value to intentConfiguration.mode.setupFutureUsage")
525
+ val setupFutureUsage =
526
+ mapToSetupFutureUse(modeParams.getString("setupFutureUsage"))
527
+ ?: throw PaymentSheetException(
528
+ "You must provide a value to intentConfiguration.mode.setupFutureUsage",
529
+ )
430
530
  PaymentSheet.IntentConfiguration.Mode.Setup(
431
531
  currency = currencyCode,
432
- setupFutureUse = setupFutureUsage
532
+ setupFutureUse = setupFutureUsage,
433
533
  )
434
534
  }
435
535
  }
@@ -440,25 +540,33 @@ class PaymentSheetFragment(
440
540
  val customerId = bundle?.getString("customerId").orEmpty()
441
541
  val customerEphemeralKeySecret = bundle?.getString("customerEphemeralKeySecret").orEmpty()
442
542
  val customerSessionClientSecret = bundle?.getString("customerSessionClientSecret").orEmpty()
443
- return if (customerSessionClientSecret.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
444
- throw PaymentSheetException("`customerEphemeralKeySecret` and `customerSessionClientSecret` cannot both be set")
543
+ return if (customerSessionClientSecret.isNotEmpty() &&
544
+ customerEphemeralKeySecret.isNotEmpty()
545
+ ) {
546
+ throw PaymentSheetException(
547
+ "`customerEphemeralKeySecret` and `customerSessionClientSecret` cannot both be set",
548
+ )
445
549
  } else if (customerId.isNotEmpty() && customerSessionClientSecret.isNotEmpty()) {
446
550
  PaymentSheet.CustomerConfiguration.createWithCustomerSession(
447
551
  id = customerId,
448
- clientSecret = customerSessionClientSecret
552
+ clientSecret = customerSessionClientSecret,
449
553
  )
450
- }
451
- else if (customerId.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
554
+ } else if (customerId.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
452
555
  PaymentSheet.CustomerConfiguration(
453
556
  id = customerId,
454
- ephemeralKeySecret = customerEphemeralKeySecret
557
+ ephemeralKeySecret = customerEphemeralKeySecret,
455
558
  )
456
- } else null
559
+ } else {
560
+ null
561
+ }
457
562
  }
458
563
  }
459
564
  }
460
565
 
461
- fun getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap? {
566
+ fun getBitmapFromVectorDrawable(
567
+ context: Context?,
568
+ drawableId: Int,
569
+ ): Bitmap? {
462
570
  val drawable = AppCompatResources.getDrawable(context!!, drawableId) ?: return null
463
571
  return getBitmapFromDrawable(drawable)
464
572
  }
@@ -468,7 +576,12 @@ fun getBitmapFromDrawable(drawable: Drawable): Bitmap? {
468
576
  if (drawableCompat.intrinsicWidth <= 0 || drawableCompat.intrinsicHeight <= 0) {
469
577
  return null
470
578
  }
471
- val bitmap = Bitmap.createBitmap(drawableCompat.intrinsicWidth, drawableCompat.intrinsicHeight, Bitmap.Config.ARGB_8888)
579
+ val bitmap =
580
+ Bitmap.createBitmap(
581
+ drawableCompat.intrinsicWidth,
582
+ drawableCompat.intrinsicHeight,
583
+ Bitmap.Config.ARGB_8888,
584
+ )
472
585
  bitmap.eraseColor(Color.WHITE)
473
586
  val canvas = Canvas(bitmap)
474
587
  drawable.setBounds(0, 0, canvas.width, canvas.height)
@@ -486,45 +599,76 @@ fun getBase64FromBitmap(bitmap: Bitmap?): String? {
486
599
  return Base64.encodeToString(imageBytes, Base64.DEFAULT)
487
600
  }
488
601
 
489
- fun mapToCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode {
490
- return when (str) {
602
+ fun mapToCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode =
603
+ when (str) {
491
604
  "automatic" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Automatic
492
605
  "never" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Never
493
606
  "always" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
494
607
  else -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Automatic
495
608
  }
496
- }
497
609
 
498
- fun mapToPaymentMethodLayout(str: String?): PaymentSheet.PaymentMethodLayout {
499
- return when (str) {
610
+ fun mapToPaymentMethodLayout(str: String?): PaymentSheet.PaymentMethodLayout =
611
+ when (str) {
500
612
  "Horizontal" -> PaymentSheet.PaymentMethodLayout.Horizontal
501
613
  "Vertical" -> PaymentSheet.PaymentMethodLayout.Vertical
502
614
  else -> PaymentSheet.PaymentMethodLayout.Automatic
503
615
  }
504
- }
505
616
 
506
- fun mapToAddressCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode {
507
- return when (str) {
508
- "automatic" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
617
+ fun mapToAddressCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode =
618
+ when (str) {
619
+ "automatic" ->
620
+ PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
509
621
  "never" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Never
510
622
  "full" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Full
511
623
  else -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
512
624
  }
513
- }
514
625
 
515
- fun mapToSetupFutureUse(type: String?): PaymentSheet.IntentConfiguration.SetupFutureUse? {
516
- return when (type) {
517
- "OffSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OffSession
518
- "OnSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OnSession
519
- else -> null
626
+ fun mapToSetupFutureUse(type: String?): PaymentSheet.IntentConfiguration.SetupFutureUse? =
627
+ when (type) {
628
+ "OffSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OffSession
629
+ "OnSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OnSession
630
+ else -> null
520
631
  }
521
- }
522
632
 
523
- fun mapToCaptureMethod(type: String?): PaymentSheet.IntentConfiguration.CaptureMethod {
524
- return when (type) {
525
- "Automatic" -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
526
- "Manual" -> PaymentSheet.IntentConfiguration.CaptureMethod.Manual
527
- "AutomaticAsync" -> PaymentSheet.IntentConfiguration.CaptureMethod.AutomaticAsync
528
- else -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
633
+ fun mapToCaptureMethod(type: String?): PaymentSheet.IntentConfiguration.CaptureMethod =
634
+ when (type) {
635
+ "Automatic" -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
636
+ "Manual" -> PaymentSheet.IntentConfiguration.CaptureMethod.Manual
637
+ "AutomaticAsync" -> PaymentSheet.IntentConfiguration.CaptureMethod.AutomaticAsync
638
+ else -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
639
+ }
640
+
641
+ fun mapToCardBrandAcceptance(params: Bundle?): PaymentSheet.CardBrandAcceptance {
642
+ val cardBrandAcceptanceParams = params?.getBundle("cardBrandAcceptance") ?: return PaymentSheet.CardBrandAcceptance.all()
643
+ val filter = cardBrandAcceptanceParams.getString("filter") ?: return PaymentSheet.CardBrandAcceptance.all()
644
+
645
+ return when (filter) {
646
+ "all" -> PaymentSheet.CardBrandAcceptance.all()
647
+ "allowed" -> {
648
+ val brands = cardBrandAcceptanceParams.getStringArrayList("brands") ?: return PaymentSheet.CardBrandAcceptance.all()
649
+ val brandCategories = brands.mapNotNull { mapToCardBrandCategory(it) }
650
+ if (brandCategories.isEmpty()) {
651
+ return PaymentSheet.CardBrandAcceptance.all()
652
+ }
653
+ PaymentSheet.CardBrandAcceptance.allowed(brandCategories)
654
+ }
655
+ "disallowed" -> {
656
+ val brands = cardBrandAcceptanceParams.getStringArrayList("brands") ?: return PaymentSheet.CardBrandAcceptance.all()
657
+ val brandCategories = brands.mapNotNull { mapToCardBrandCategory(it) }
658
+ if (brandCategories.isEmpty()) {
659
+ return PaymentSheet.CardBrandAcceptance.all()
660
+ }
661
+ PaymentSheet.CardBrandAcceptance.disallowed(brandCategories)
662
+ }
663
+ else -> PaymentSheet.CardBrandAcceptance.all()
529
664
  }
530
665
  }
666
+
667
+ fun mapToCardBrandCategory(brand: String): PaymentSheet.CardBrandAcceptance.BrandCategory? =
668
+ when (brand) {
669
+ "visa" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Visa
670
+ "mastercard" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Mastercard
671
+ "amex" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Amex
672
+ "discover" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Discover
673
+ else -> null
674
+ }