@stripe/stripe-react-native 0.42.0 → 0.44.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 (309) hide show
  1. package/.husky/pre-commit +0 -3
  2. package/CHANGELOG.md +26 -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 +8 -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 +422 -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 +656 -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 +13 -7
  62. package/ios/FinancialConnections.swift +6 -2
  63. package/ios/StripeSdk+CustomerSheet.swift +3 -1
  64. package/ios/StripeSdk+PaymentSheet.swift +86 -21
  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 +16 -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 +91 -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/helpers.ts +2 -1
  297. package/src/types/Common.ts +3 -0
  298. package/src/types/CustomerSheet.ts +11 -0
  299. package/src/types/FinancialConnections.ts +3 -0
  300. package/src/types/PaymentMethod.ts +5 -1
  301. package/src/types/PaymentSheet.ts +85 -4
  302. package/src/types/components/CardFieldInput.ts +8 -4
  303. package/src/types/components/CardFormView.ts +8 -4
  304. package/stripe-react-native.podspec +2 -1
  305. package/android/.gradle/8.9/checksums/checksums.lock +0 -0
  306. package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
  307. package/android/.gradle/8.9/gc.properties +0 -0
  308. /package/android/.gradle/{8.9 → 8.11.1}/fileChanges/last-build.bin +0 -0
  309. /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,263 +62,318 @@ 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")
67
86
  val googlePayConfig = buildGooglePayConfig(arguments?.getBundle("googlePay"))
87
+ val linkConfig = buildLinkConfig(arguments?.getBundle("link"))
68
88
  val allowsDelayedPaymentMethods = arguments?.getBoolean("allowsDelayedPaymentMethods")
69
89
  val billingDetailsBundle = arguments?.getBundle("defaultBillingDetails")
70
90
  val billingConfigParams = arguments?.getBundle("billingDetailsCollectionConfiguration")
71
91
  val paymentMethodOrder = arguments?.getStringArrayList("paymentMethodOrder")
72
- val allowsRemovalOfLastSavedPaymentMethod = arguments?.getBoolean("allowsRemovalOfLastSavedPaymentMethod", true) ?: true
92
+ val allowsRemovalOfLastSavedPaymentMethod =
93
+ arguments?.getBoolean("allowsRemovalOfLastSavedPaymentMethod", true) ?: true
73
94
  paymentIntentClientSecret = arguments?.getString("paymentIntentClientSecret").orEmpty()
74
95
  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
- }
96
+ intentConfiguration =
97
+ try {
98
+ buildIntentConfiguration(arguments?.getBundle("intentConfiguration"))
99
+ } catch (error: PaymentSheetException) {
100
+ initPromise.resolve(createError(ErrorType.Failed.toString(), error))
101
+ return
102
+ }
103
+ val appearance =
104
+ try {
105
+ buildPaymentSheetAppearance(arguments?.getBundle("appearance"), context)
106
+ } catch (error: PaymentSheetAppearanceException) {
107
+ initPromise.resolve(createError(ErrorType.Failed.toString(), error))
108
+ return
109
+ }
87
110
 
88
- val customerConfiguration = try {
89
- buildCustomerConfiguration(arguments)
90
- } catch (error: PaymentSheetException) {
91
- initPromise.resolve(createError(ErrorType.Failed.toString(), error))
92
- return
93
- }
111
+ val customerConfiguration =
112
+ try {
113
+ buildCustomerConfiguration(arguments)
114
+ } catch (error: PaymentSheetException) {
115
+ initPromise.resolve(createError(ErrorType.Failed.toString(), error))
116
+ return
117
+ }
94
118
 
95
- val shippingDetails = arguments?.getBundle("defaultShippingDetails")?.let {
96
- AddressSheetView.buildAddressDetails(it)
97
- }
119
+ val shippingDetails =
120
+ arguments?.getBundle("defaultShippingDetails")?.let {
121
+ AddressSheetView.buildAddressDetails(it)
122
+ }
123
+
124
+ val paymentOptionCallback =
125
+ PaymentOptionCallback { paymentOption ->
126
+ val result =
127
+ paymentOption?.let {
128
+ val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
129
+ val imageString = getBase64FromBitmap(bitmap)
130
+ val option: WritableMap = WritableNativeMap()
131
+ option.putString("label", it.label)
132
+ option.putString("image", imageString)
133
+ createResult("paymentOption", option)
134
+ }
135
+ ?: run {
136
+ if (paymentSheetTimedOut) {
137
+ paymentSheetTimedOut = false
138
+ createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out")
139
+ } else {
140
+ createError(
141
+ PaymentSheetErrorType.Canceled.toString(),
142
+ "The payment option selection flow has been canceled",
143
+ )
144
+ }
145
+ }
146
+ resolvePresentPromise(result)
147
+ }
98
148
 
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 {
149
+ val paymentResultCallback =
150
+ PaymentSheetResultCallback { paymentResult ->
108
151
  if (paymentSheetTimedOut) {
109
152
  paymentSheetTimedOut = false
110
- createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out")
153
+ resolvePaymentResult(
154
+ createError(PaymentSheetErrorType.Timeout.toString(), "The payment has timed out"),
155
+ )
111
156
  } 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
157
+ when (paymentResult) {
158
+ is PaymentSheetResult.Canceled -> {
159
+ resolvePaymentResult(
160
+ createError(
161
+ PaymentSheetErrorType.Canceled.toString(),
162
+ "The payment flow has been canceled",
163
+ ),
164
+ )
165
+ }
166
+ is PaymentSheetResult.Failed -> {
167
+ resolvePaymentResult(
168
+ createError(PaymentSheetErrorType.Failed.toString(), paymentResult.error),
169
+ )
170
+ }
171
+ is PaymentSheetResult.Completed -> {
172
+ resolvePaymentResult(WritableNativeMap())
173
+ // Remove the fragment now, we can be sure it won't be needed again if an intent is
174
+ // successful
175
+ removeFragment(context)
176
+ paymentSheet = null
177
+ flowController = null
178
+ }
136
179
  }
137
180
  }
138
181
  }
139
- }
140
182
 
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"
183
+ val createIntentCallback =
184
+ CreateIntentCallback { paymentMethod, shouldSavePaymentMethod ->
185
+ val stripeSdkModule: StripeSdkModule? = context.getNativeModule(StripeSdkModule::class.java)
186
+ if (stripeSdkModule == null || stripeSdkModule.eventListenerCount == 0) {
187
+ return@CreateIntentCallback CreateIntentResult.Failure(
188
+ cause =
189
+ Exception(
190
+ "Tried to call confirmHandler, but no callback was found. Please file an issue: https://github.com/stripe/stripe-react-native/issues",
191
+ ),
192
+ displayMessage = "An unexpected error occurred",
147
193
  )
148
- }
149
- val params = Arguments.createMap().apply {
150
- putMap("paymentMethod", mapFromPaymentMethod(paymentMethod))
151
- putBoolean("shouldSavePaymentMethod", shouldSavePaymentMethod)
152
- }
194
+ }
195
+ val params =
196
+ Arguments.createMap().apply {
197
+ putMap("paymentMethod", mapFromPaymentMethod(paymentMethod))
198
+ putBoolean("shouldSavePaymentMethod", shouldSavePaymentMethod)
199
+ }
153
200
 
154
- stripeSdkModule.sendEvent(context, "onConfirmHandlerCallback", params)
201
+ stripeSdkModule.sendEvent(context, "onConfirmHandlerCallback", params)
155
202
 
156
- val resultFromJavascript = paymentSheetIntentCreationCallback.await()
157
- // reset the completable
158
- paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
203
+ val resultFromJavascript = paymentSheetIntentCreationCallback.await()
204
+ // reset the completable
205
+ paymentSheetIntentCreationCallback = CompletableDeferred<ReadableMap>()
159
206
 
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
- )
207
+ return@CreateIntentCallback resultFromJavascript.getString("clientSecret")?.let {
208
+ CreateIntentResult.Success(clientSecret = it)
209
+ }
210
+ ?: run {
211
+ val errorMap = resultFromJavascript.getMap("error")
212
+ CreateIntentResult.Failure(
213
+ cause = Exception(errorMap?.getString("message")),
214
+ displayMessage = errorMap?.getString("localizedMessage"),
215
+ )
216
+ }
168
217
  }
169
- }
170
218
 
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
- )
219
+ val billingDetailsConfig =
220
+ PaymentSheet.BillingDetailsCollectionConfiguration(
221
+ name = mapToCollectionMode(billingConfigParams?.getString("name")),
222
+ phone = mapToCollectionMode(billingConfigParams?.getString("phone")),
223
+ email = mapToCollectionMode(billingConfigParams?.getString("email")),
224
+ address = mapToAddressCollectionMode(billingConfigParams?.getString("address")),
225
+ attachDefaultsToPaymentMethod =
226
+ billingConfigParams?.getBoolean("attachDefaultsToPaymentMethod") ?: false,
227
+ )
179
228
 
180
229
  var defaultBillingDetails: PaymentSheet.BillingDetails? = null
181
230
  if (billingDetailsBundle != null) {
182
231
  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)
232
+ val address =
233
+ PaymentSheet.Address(
234
+ addressBundle?.getString("city"),
235
+ addressBundle?.getString("country"),
236
+ addressBundle?.getString("line1"),
237
+ addressBundle?.getString("line2"),
238
+ addressBundle?.getString("postalCode"),
239
+ addressBundle?.getString("state"),
240
+ )
241
+ defaultBillingDetails =
242
+ PaymentSheet.BillingDetails(
243
+ address,
244
+ billingDetailsBundle.getString("email"),
245
+ billingDetailsBundle.getString("name"),
246
+ billingDetailsBundle.getString("phone"),
247
+ )
211
248
  }
249
+ val configurationBuilder =
250
+ PaymentSheet.Configuration
251
+ .Builder(merchantDisplayName)
252
+ .allowsDelayedPaymentMethods(allowsDelayedPaymentMethods ?: false)
253
+ .defaultBillingDetails(defaultBillingDetails)
254
+ .customer(customerConfiguration)
255
+ .googlePay(googlePayConfig)
256
+ .appearance(appearance)
257
+ .shippingDetails(shippingDetails)
258
+ .billingDetailsCollectionConfiguration(billingDetailsConfig)
259
+ .preferredNetworks(
260
+ mapToPreferredNetworks(arguments?.getIntegerArrayList("preferredNetworks")),
261
+ ).allowsRemovalOfLastSavedPaymentMethod(allowsRemovalOfLastSavedPaymentMethod)
262
+ .cardBrandAcceptance(mapToCardBrandAcceptance(arguments))
263
+
264
+ primaryButtonLabel?.let { configurationBuilder.primaryButtonLabel(it) }
265
+ paymentMethodOrder?.let { configurationBuilder.paymentMethodOrder(it) }
212
266
 
213
267
  configurationBuilder.paymentMethodLayout(
214
- mapToPaymentMethodLayout(arguments?.getString("paymentMethodLayout"))
268
+ mapToPaymentMethodLayout(arguments?.getString("paymentMethodLayout")),
215
269
  )
216
270
 
217
271
  paymentSheetConfiguration = configurationBuilder.build()
218
272
 
219
273
  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
- }
274
+ flowController =
275
+ if (intentConfiguration != null) {
276
+ PaymentSheet.FlowController.create(
277
+ this,
278
+ paymentOptionCallback = paymentOptionCallback,
279
+ createIntentCallback = createIntentCallback,
280
+ paymentResultCallback = paymentResultCallback,
281
+ )
282
+ } else {
283
+ PaymentSheet.FlowController.create(
284
+ this,
285
+ paymentOptionCallback = paymentOptionCallback,
286
+ paymentResultCallback = paymentResultCallback,
287
+ )
288
+ }
234
289
  configureFlowController()
235
290
  } 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
- }
291
+ paymentSheet =
292
+ if (intentConfiguration != null) {
293
+ PaymentSheet(
294
+ this,
295
+ createIntentCallback = createIntentCallback,
296
+ paymentResultCallback = paymentResultCallback,
297
+ )
298
+ } else {
299
+ PaymentSheet(this, callback = paymentResultCallback)
300
+ }
248
301
  initPromise.resolve(WritableNativeMap())
249
302
  }
250
303
  }
251
304
 
252
305
  fun present(promise: Promise) {
253
- this.presentPromise = promise
254
- if(paymentSheet != null) {
306
+ keepJsAwake = KeepJsAwakeTask(context).apply { start() }
307
+ presentPromise = promise
308
+ if (paymentSheet != null) {
255
309
  if (!paymentIntentClientSecret.isNullOrEmpty()) {
256
- paymentSheet?.presentWithPaymentIntent(paymentIntentClientSecret!!, paymentSheetConfiguration)
310
+ paymentSheet?.presentWithPaymentIntent(
311
+ paymentIntentClientSecret!!,
312
+ paymentSheetConfiguration,
313
+ )
257
314
  } else if (!setupIntentClientSecret.isNullOrEmpty()) {
258
315
  paymentSheet?.presentWithSetupIntent(setupIntentClientSecret!!, paymentSheetConfiguration)
259
316
  } else if (intentConfiguration != null) {
260
317
  paymentSheet?.presentWithIntentConfiguration(
261
318
  intentConfiguration = intentConfiguration!!,
262
- configuration = paymentSheetConfiguration
319
+ configuration = paymentSheetConfiguration,
263
320
  )
264
321
  }
265
- } else if(flowController != null) {
322
+ } else if (flowController != null) {
266
323
  flowController?.presentPaymentOptions()
267
324
  } else {
268
325
  promise.resolve(createMissingInitError())
269
326
  }
270
327
  }
271
328
 
272
- fun presentWithTimeout(timeout: Long, promise: Promise) {
329
+ fun presentWithTimeout(
330
+ timeout: Long,
331
+ promise: Promise,
332
+ ) {
273
333
  var paymentSheetActivity: Activity? = null
274
334
 
275
- val activityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
276
- override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
277
- paymentSheetActivity = activity
278
- }
335
+ val activityLifecycleCallbacks =
336
+ object : Application.ActivityLifecycleCallbacks {
337
+ override fun onActivityCreated(
338
+ activity: Activity,
339
+ savedInstanceState: Bundle?,
340
+ ) {
341
+ paymentSheetActivity = activity
342
+ }
279
343
 
280
- override fun onActivityStarted(activity: Activity) {}
344
+ override fun onActivityStarted(activity: Activity) {}
281
345
 
282
- override fun onActivityResumed(activity: Activity) {}
346
+ override fun onActivityResumed(activity: Activity) {}
283
347
 
284
- override fun onActivityPaused(activity: Activity) {}
348
+ override fun onActivityPaused(activity: Activity) {}
285
349
 
286
- override fun onActivityStopped(activity: Activity) {}
350
+ override fun onActivityStopped(activity: Activity) {}
287
351
 
288
- override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
352
+ override fun onActivitySaveInstanceState(
353
+ activity: Activity,
354
+ outState: Bundle,
355
+ ) {}
289
356
 
290
- override fun onActivityDestroyed(activity: Activity) {
291
- paymentSheetActivity = null
292
- context.currentActivity?.application?.unregisterActivityLifecycleCallbacks(this)
357
+ override fun onActivityDestroyed(activity: Activity) {
358
+ paymentSheetActivity = null
359
+ context.currentActivity?.application?.unregisterActivityLifecycleCallbacks(this)
360
+ }
293
361
  }
294
- }
295
362
 
296
- Handler(Looper.getMainLooper()).postDelayed({
297
- paymentSheetActivity?.let {
298
- it.finish()
299
- paymentSheetTimedOut = true
300
- }
301
- }, timeout)
363
+ Handler(Looper.getMainLooper())
364
+ .postDelayed(
365
+ {
366
+ paymentSheetActivity?.let {
367
+ it.finish()
368
+ paymentSheetTimedOut = true
369
+ }
370
+ },
371
+ timeout,
372
+ )
302
373
 
303
- context.currentActivity?.application?.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
374
+ context.currentActivity
375
+ ?.application
376
+ ?.registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
304
377
 
305
378
  this.present(promise)
306
379
  }
@@ -311,70 +384,81 @@ class PaymentSheetFragment(
311
384
  }
312
385
 
313
386
  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()
387
+ val onFlowControllerConfigure =
388
+ PaymentSheet.FlowController.ConfigCallback { _, _ ->
389
+ val result =
390
+ flowController?.getPaymentOption()?.let {
391
+ val bitmap = getBitmapFromVectorDrawable(context, it.drawableResourceId)
392
+ val imageString = getBase64FromBitmap(bitmap)
393
+ val option: WritableMap = WritableNativeMap()
394
+ option.putString("label", it.label)
395
+ option.putString("image", imageString)
396
+ createResult("paymentOption", option)
397
+ } ?: run { WritableNativeMap() }
398
+ initPromise.resolve(result)
324
399
  }
325
- initPromise.resolve(result)
326
- }
327
400
 
328
401
  if (!paymentIntentClientSecret.isNullOrEmpty()) {
329
402
  flowController?.configureWithPaymentIntent(
330
403
  paymentIntentClientSecret = paymentIntentClientSecret!!,
331
404
  configuration = paymentSheetConfiguration,
332
- callback = onFlowControllerConfigure
405
+ callback = onFlowControllerConfigure,
333
406
  )
334
407
  } else if (!setupIntentClientSecret.isNullOrEmpty()) {
335
408
  flowController?.configureWithSetupIntent(
336
409
  setupIntentClientSecret = setupIntentClientSecret!!,
337
410
  configuration = paymentSheetConfiguration,
338
- callback = onFlowControllerConfigure
411
+ callback = onFlowControllerConfigure,
339
412
  )
340
413
  } else if (intentConfiguration != null) {
341
414
  flowController?.configureWithIntentConfiguration(
342
415
  intentConfiguration = intentConfiguration!!,
343
416
  configuration = paymentSheetConfiguration,
344
- callback = onFlowControllerConfigure
417
+ callback = onFlowControllerConfigure,
345
418
  )
346
419
  } else {
347
- initPromise.resolve(createError(ErrorType.Failed.toString(), "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration` is required"))
420
+ initPromise.resolve(
421
+ createError(
422
+ ErrorType.Failed.toString(),
423
+ "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration` is required",
424
+ ),
425
+ )
348
426
  return
349
427
  }
350
428
  }
351
429
 
430
+ private fun resolvePresentPromise(value: Any?) {
431
+ keepJsAwake?.stop()
432
+ presentPromise?.resolve(value)
433
+ }
434
+
352
435
  private fun resolvePaymentResult(map: WritableMap) {
353
436
  confirmPromise?.let {
354
437
  it.resolve(map)
355
438
  confirmPromise = null
356
- } ?: run {
357
- presentPromise?.resolve(map)
358
- }
439
+ } ?: run { resolvePresentPromise(map) }
359
440
  }
360
441
 
361
442
  companion object {
362
443
  internal const val TAG = "payment_sheet_launch_fragment"
363
444
 
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
- )
445
+ private val mapIntToButtonType =
446
+ mapOf(
447
+ 1 to PaymentSheet.GooglePayConfiguration.ButtonType.Buy,
448
+ 6 to PaymentSheet.GooglePayConfiguration.ButtonType.Book,
449
+ 5 to PaymentSheet.GooglePayConfiguration.ButtonType.Checkout,
450
+ 4 to PaymentSheet.GooglePayConfiguration.ButtonType.Donate,
451
+ 11 to PaymentSheet.GooglePayConfiguration.ButtonType.Order,
452
+ 1000 to PaymentSheet.GooglePayConfiguration.ButtonType.Pay,
453
+ 7 to PaymentSheet.GooglePayConfiguration.ButtonType.Subscribe,
454
+ 1001 to PaymentSheet.GooglePayConfiguration.ButtonType.Plain,
455
+ )
374
456
 
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
- }
457
+ internal fun createMissingInitError(): WritableMap =
458
+ createError(
459
+ PaymentSheetErrorType.Failed.toString(),
460
+ "No payment sheet has been initialized yet. You must call `initPaymentSheet` before `presentPaymentSheet`.",
461
+ )
378
462
 
379
463
  internal fun buildGooglePayConfig(params: Bundle?): PaymentSheet.GooglePayConfiguration? {
380
464
  if (params == null) {
@@ -386,36 +470,69 @@ class PaymentSheetFragment(
386
470
  val testEnv = params.getBoolean("testEnv")
387
471
  val amount = params.getString("amount")?.toLongOrNull()
388
472
  val label = params.getString("label")
389
- val buttonType = mapIntToButtonType.get(params.getInt("buttonType")) ?: PaymentSheet.GooglePayConfiguration.ButtonType.Pay
390
-
473
+ val buttonType =
474
+ mapIntToButtonType.get(params.getInt("buttonType"))
475
+ ?: PaymentSheet.GooglePayConfiguration.ButtonType.Pay
391
476
 
392
477
  return PaymentSheet.GooglePayConfiguration(
393
- environment = if (testEnv) PaymentSheet.GooglePayConfiguration.Environment.Test else PaymentSheet.GooglePayConfiguration.Environment.Production,
478
+ environment =
479
+ if (testEnv) {
480
+ PaymentSheet.GooglePayConfiguration.Environment.Test
481
+ } else {
482
+ PaymentSheet.GooglePayConfiguration.Environment.Production
483
+ },
394
484
  countryCode = countryCode,
395
485
  currencyCode = currencyCode,
396
486
  amount = amount,
397
487
  label = label,
398
- buttonType = buttonType
488
+ buttonType = buttonType,
489
+ )
490
+ }
491
+
492
+ internal fun buildLinkConfig(params: Bundle?): PaymentSheet.LinkConfiguration {
493
+ if (params == null) {
494
+ return PaymentSheet.LinkConfiguration()
495
+ }
496
+
497
+ val display = mapStringToLinkDisplay(params.getString("display"))
498
+
499
+ return PaymentSheet.LinkConfiguration(
500
+ display = display,
399
501
  )
400
502
  }
401
503
 
504
+ private fun mapStringToLinkDisplay(value: String?): PaymentSheet.LinkConfiguration.Display =
505
+ when (value) {
506
+ "automatic" -> PaymentSheet.LinkConfiguration.Display.Automatic
507
+ "never" -> PaymentSheet.LinkConfiguration.Display.Never
508
+ else -> PaymentSheet.LinkConfiguration.Display.Automatic
509
+ }
510
+
402
511
  @Throws(PaymentSheetException::class)
403
512
  private fun buildIntentConfiguration(intentConfigurationParams: Bundle?): PaymentSheet.IntentConfiguration? {
404
513
  if (intentConfigurationParams == null) {
405
514
  return null
406
515
  }
407
- val modeParams = intentConfigurationParams.getBundle("mode")
408
- ?: throw PaymentSheetException("If `intentConfiguration` is provided, `intentConfiguration.mode` is required")
516
+ val modeParams =
517
+ intentConfigurationParams.getBundle("mode")
518
+ ?: throw PaymentSheetException(
519
+ "If `intentConfiguration` is provided, `intentConfiguration.mode` is required",
520
+ )
409
521
 
410
522
  return PaymentSheet.IntentConfiguration(
411
523
  mode = buildIntentConfigurationMode(modeParams),
412
- paymentMethodTypes = intentConfigurationParams.getStringArrayList("paymentMethodTypes")?.toList() ?: emptyList(),
524
+ paymentMethodTypes =
525
+ intentConfigurationParams.getStringArrayList("paymentMethodTypes")?.toList()
526
+ ?: emptyList(),
413
527
  )
414
528
  }
415
529
 
416
530
  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")
531
+ val currencyCode =
532
+ modeParams.getString("currencyCode")
533
+ ?: throw PaymentSheetException(
534
+ "You must provide a value to intentConfiguration.mode.currencyCode",
535
+ )
419
536
 
420
537
  return if (modeParams.containsKey("amount")) {
421
538
  PaymentSheet.IntentConfiguration.Mode.Payment(
@@ -425,11 +542,14 @@ class PaymentSheetFragment(
425
542
  captureMethod = mapToCaptureMethod(modeParams.getString("captureMethod")),
426
543
  )
427
544
  } else {
428
- val setupFutureUsage = mapToSetupFutureUse(modeParams.getString("setupFutureUsage"))
429
- ?: throw PaymentSheetException("You must provide a value to intentConfiguration.mode.setupFutureUsage")
545
+ val setupFutureUsage =
546
+ mapToSetupFutureUse(modeParams.getString("setupFutureUsage"))
547
+ ?: throw PaymentSheetException(
548
+ "You must provide a value to intentConfiguration.mode.setupFutureUsage",
549
+ )
430
550
  PaymentSheet.IntentConfiguration.Mode.Setup(
431
551
  currency = currencyCode,
432
- setupFutureUse = setupFutureUsage
552
+ setupFutureUse = setupFutureUsage,
433
553
  )
434
554
  }
435
555
  }
@@ -440,25 +560,33 @@ class PaymentSheetFragment(
440
560
  val customerId = bundle?.getString("customerId").orEmpty()
441
561
  val customerEphemeralKeySecret = bundle?.getString("customerEphemeralKeySecret").orEmpty()
442
562
  val customerSessionClientSecret = bundle?.getString("customerSessionClientSecret").orEmpty()
443
- return if (customerSessionClientSecret.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
444
- throw PaymentSheetException("`customerEphemeralKeySecret` and `customerSessionClientSecret` cannot both be set")
563
+ return if (customerSessionClientSecret.isNotEmpty() &&
564
+ customerEphemeralKeySecret.isNotEmpty()
565
+ ) {
566
+ throw PaymentSheetException(
567
+ "`customerEphemeralKeySecret` and `customerSessionClientSecret` cannot both be set",
568
+ )
445
569
  } else if (customerId.isNotEmpty() && customerSessionClientSecret.isNotEmpty()) {
446
570
  PaymentSheet.CustomerConfiguration.createWithCustomerSession(
447
571
  id = customerId,
448
- clientSecret = customerSessionClientSecret
572
+ clientSecret = customerSessionClientSecret,
449
573
  )
450
- }
451
- else if (customerId.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
574
+ } else if (customerId.isNotEmpty() && customerEphemeralKeySecret.isNotEmpty()) {
452
575
  PaymentSheet.CustomerConfiguration(
453
576
  id = customerId,
454
- ephemeralKeySecret = customerEphemeralKeySecret
577
+ ephemeralKeySecret = customerEphemeralKeySecret,
455
578
  )
456
- } else null
579
+ } else {
580
+ null
581
+ }
457
582
  }
458
583
  }
459
584
  }
460
585
 
461
- fun getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap? {
586
+ fun getBitmapFromVectorDrawable(
587
+ context: Context?,
588
+ drawableId: Int,
589
+ ): Bitmap? {
462
590
  val drawable = AppCompatResources.getDrawable(context!!, drawableId) ?: return null
463
591
  return getBitmapFromDrawable(drawable)
464
592
  }
@@ -468,7 +596,12 @@ fun getBitmapFromDrawable(drawable: Drawable): Bitmap? {
468
596
  if (drawableCompat.intrinsicWidth <= 0 || drawableCompat.intrinsicHeight <= 0) {
469
597
  return null
470
598
  }
471
- val bitmap = Bitmap.createBitmap(drawableCompat.intrinsicWidth, drawableCompat.intrinsicHeight, Bitmap.Config.ARGB_8888)
599
+ val bitmap =
600
+ Bitmap.createBitmap(
601
+ drawableCompat.intrinsicWidth,
602
+ drawableCompat.intrinsicHeight,
603
+ Bitmap.Config.ARGB_8888,
604
+ )
472
605
  bitmap.eraseColor(Color.WHITE)
473
606
  val canvas = Canvas(bitmap)
474
607
  drawable.setBounds(0, 0, canvas.width, canvas.height)
@@ -486,45 +619,76 @@ fun getBase64FromBitmap(bitmap: Bitmap?): String? {
486
619
  return Base64.encodeToString(imageBytes, Base64.DEFAULT)
487
620
  }
488
621
 
489
- fun mapToCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode {
490
- return when (str) {
622
+ fun mapToCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode =
623
+ when (str) {
491
624
  "automatic" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Automatic
492
625
  "never" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Never
493
626
  "always" -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
494
627
  else -> PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Automatic
495
628
  }
496
- }
497
629
 
498
- fun mapToPaymentMethodLayout(str: String?): PaymentSheet.PaymentMethodLayout {
499
- return when (str) {
630
+ fun mapToPaymentMethodLayout(str: String?): PaymentSheet.PaymentMethodLayout =
631
+ when (str) {
500
632
  "Horizontal" -> PaymentSheet.PaymentMethodLayout.Horizontal
501
633
  "Vertical" -> PaymentSheet.PaymentMethodLayout.Vertical
502
634
  else -> PaymentSheet.PaymentMethodLayout.Automatic
503
635
  }
504
- }
505
636
 
506
- fun mapToAddressCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode {
507
- return when (str) {
508
- "automatic" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
637
+ fun mapToAddressCollectionMode(str: String?): PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode =
638
+ when (str) {
639
+ "automatic" ->
640
+ PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
509
641
  "never" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Never
510
642
  "full" -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Full
511
643
  else -> PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Automatic
512
644
  }
513
- }
514
645
 
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
646
+ fun mapToSetupFutureUse(type: String?): PaymentSheet.IntentConfiguration.SetupFutureUse? =
647
+ when (type) {
648
+ "OffSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OffSession
649
+ "OnSession" -> PaymentSheet.IntentConfiguration.SetupFutureUse.OnSession
650
+ else -> null
651
+ }
652
+
653
+ fun mapToCaptureMethod(type: String?): PaymentSheet.IntentConfiguration.CaptureMethod =
654
+ when (type) {
655
+ "Automatic" -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
656
+ "Manual" -> PaymentSheet.IntentConfiguration.CaptureMethod.Manual
657
+ "AutomaticAsync" -> PaymentSheet.IntentConfiguration.CaptureMethod.AutomaticAsync
658
+ else -> PaymentSheet.IntentConfiguration.CaptureMethod.Automatic
520
659
  }
521
- }
522
660
 
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
661
+ fun mapToCardBrandAcceptance(params: Bundle?): PaymentSheet.CardBrandAcceptance {
662
+ val cardBrandAcceptanceParams = params?.getBundle("cardBrandAcceptance") ?: return PaymentSheet.CardBrandAcceptance.all()
663
+ val filter = cardBrandAcceptanceParams.getString("filter") ?: return PaymentSheet.CardBrandAcceptance.all()
664
+
665
+ return when (filter) {
666
+ "all" -> PaymentSheet.CardBrandAcceptance.all()
667
+ "allowed" -> {
668
+ val brands = cardBrandAcceptanceParams.getStringArrayList("brands") ?: return PaymentSheet.CardBrandAcceptance.all()
669
+ val brandCategories = brands.mapNotNull { mapToCardBrandCategory(it) }
670
+ if (brandCategories.isEmpty()) {
671
+ return PaymentSheet.CardBrandAcceptance.all()
672
+ }
673
+ PaymentSheet.CardBrandAcceptance.allowed(brandCategories)
674
+ }
675
+ "disallowed" -> {
676
+ val brands = cardBrandAcceptanceParams.getStringArrayList("brands") ?: return PaymentSheet.CardBrandAcceptance.all()
677
+ val brandCategories = brands.mapNotNull { mapToCardBrandCategory(it) }
678
+ if (brandCategories.isEmpty()) {
679
+ return PaymentSheet.CardBrandAcceptance.all()
680
+ }
681
+ PaymentSheet.CardBrandAcceptance.disallowed(brandCategories)
682
+ }
683
+ else -> PaymentSheet.CardBrandAcceptance.all()
529
684
  }
530
685
  }
686
+
687
+ fun mapToCardBrandCategory(brand: String): PaymentSheet.CardBrandAcceptance.BrandCategory? =
688
+ when (brand) {
689
+ "visa" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Visa
690
+ "mastercard" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Mastercard
691
+ "amex" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Amex
692
+ "discover" -> PaymentSheet.CardBrandAcceptance.BrandCategory.Discover
693
+ else -> null
694
+ }