import { createError , isiOS } from './helpers' ; import NativeStripeSdk from './NativeStripeSdk' ; import { ApplePay , ApplePayError , ApplePayResult , ConfirmPaymentResult , ConfirmPaymentSheetPaymentResult , ConfirmSetupIntent , ConfirmSetupIntentResult , CreatePaymentMethodResult , CreateTokenForCVCUpdateResult , CreateTokenResult , GooglePayInitResult , HandleCardActionResult , InitPaymentSheetResult , PaymentMethodCreateParams , PaymentSheet , PayWithGooglePayResult , PresentPaymentSheetResult , RetrievePaymentIntentResult , RetrieveSetupIntentResult , StripeError , GooglePay , CreateGooglePayPaymentMethodResult , OpenApplePaySetupResult , } from './types' ; import type { Card } from './types/Card' ; const APPLE_PAY_NOT_SUPPORTED_MESSAGE = 'Apple pay is not supported on this device' ; export const createPaymentMethod = async ( data , options = { } ) : Promise < CreatePaymentMethodResult > => { try { const { paymentMethod , error } = await NativeStripeSdk . createPaymentMethod ( data , options ) ; if ( error ) { return { error , } ; } return { paymentMethod : paymentMethod ! , } ; } catch ( error ) { return { error , } ; } } ; export const createToken = async ( params ) : Promise < CreateTokenResult > => { try { const { token , error } = await NativeStripeSdk . createToken ( params ) ; if ( error ) { return { error , } ; } return { token : token ! , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const retrievePaymentIntent = async ( clientSecret ) : Promise < RetrievePaymentIntentResult > => { try { const { paymentIntent , error } = await NativeStripeSdk . retrievePaymentIntent ( clientSecret ) ; if ( error ) { return { error , } ; } return { paymentIntent : paymentIntent ! , } ; } catch ( error ) { return { error , } ; } } ; export const retrieveSetupIntent = async ( clientSecret ) : Promise < RetrieveSetupIntentResult > => { try { const { setupIntent , error } = await NativeStripeSdk . retrieveSetupIntent ( clientSecret ) ; if ( error ) { return { error , } ; } return { setupIntent : setupIntent ! , } ; } catch ( error ) { return { error , } ; } } ; export const confirmPayment = async ( paymentIntentClientSecret , data , options = { } ) : Promise < ConfirmPaymentResult > => { try { const { paymentIntent , error } = await NativeStripeSdk . confirmPayment ( paymentIntentClientSecret , data , options ) ; if ( error ) { return { error , } ; } return { paymentIntent : paymentIntent ! , } ; } catch ( error ) { return { error , } ; } } ; export const isApplePaySupported = async ( ) : Promise < boolean > => { return isiOS && ( await NativeStripeSdk . isApplePaySupported ( ) ) ; } ; export const presentApplePay = async ( params ) : Promise < ApplePayResult > => { if ( ! ( await NativeStripeSdk . isApplePaySupported ( ) ) ) { return { error : createError ( { code : ApplePayError . Canceled , message : APPLE_PAY_NOT_SUPPORTED_MESSAGE , } ) , } ; } try { const { paymentMethod , error } = await NativeStripeSdk . presentApplePay ( params ) ; if ( error ) { return { error , } ; } return { paymentMethod : paymentMethod ! } ; } catch ( error ) { return { error , } ; } } ; export const updateApplePaySummaryItems = async ( summaryItems , errorAddressFields = [ ] ) : Promise < { error ? : StripeError < ApplePayError > ; } > => { if ( ! ( await NativeStripeSdk . isApplePaySupported ( ) ) ) { return { error : createError ( { code : ApplePayError . Canceled , message : APPLE_PAY_NOT_SUPPORTED_MESSAGE , } ) , } ; } try { await NativeStripeSdk . updateApplePaySummaryItems ( summaryItems , errorAddressFields ) ; return { } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const confirmApplePayPayment = async ( clientSecret ) : Promise < { error ? : StripeError < ApplePayError > ; } > => { if ( ! ( await NativeStripeSdk . isApplePaySupported ( ) ) ) { return { error : createError ( { code : ApplePayError . Canceled , message : APPLE_PAY_NOT_SUPPORTED_MESSAGE , } ) , } ; } try { await NativeStripeSdk . confirmApplePayPayment ( clientSecret ) ; return { } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const handleCardAction = async ( paymentIntentClientSecret ) : Promise < HandleCardActionResult > => { try { const { paymentIntent , error } = await NativeStripeSdk . handleCardAction ( paymentIntentClientSecret ) ; if ( error ) { return { error , } ; } return { paymentIntent : paymentIntent ! , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const confirmSetupIntent = async ( paymentIntentClientSecret , data , options = { } ) : Promise < ConfirmSetupIntentResult > => { try { const { setupIntent , error } = await NativeStripeSdk . confirmSetupIntent ( paymentIntentClientSecret , data , options ) ; if ( error ) { return { error , } ; } return { setupIntent : setupIntent ! , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const createTokenForCVCUpdate = async ( cvc ) : Promise < CreateTokenForCVCUpdateResult > => { try { const { tokenId , error } = await NativeStripeSdk . createTokenForCVCUpdate ( cvc ) ; if ( error ) { return { error , } ; } return { tokenId : tokenId ! , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const handleURLCallback = async ( url ) : Promise < boolean > => { const stripeHandled = await NativeStripeSdk . handleURLCallback ( url ) ; return stripeHandled ; } ; export const initPaymentSheet = async ( params ) : Promise < InitPaymentSheetResult > => { try { const { paymentOption , error } = await NativeStripeSdk . initPaymentSheet ( params ) ; if ( error ) { return { error , } ; } return { paymentOption , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const presentPaymentSheet = async ( ) : Promise < PresentPaymentSheetResult > => { try { const { paymentOption , error } = await NativeStripeSdk . presentPaymentSheet ( ) ; if ( error ) { return { error , } ; } return { paymentOption : paymentOption , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const confirmPaymentSheetPayment = async ( ) : Promise < ConfirmPaymentSheetPaymentResult > => { try { const { error } = await NativeStripeSdk . confirmPaymentSheetPayment ( ) ; if ( error ) { return { error , } ; } return { } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const initGooglePay = async ( params ) : Promise < GooglePayInitResult > => { try { const { error } = await NativeStripeSdk . initGooglePay ( params ) ; if ( error ) { return { error , } ; } return { } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const presentGooglePay = async ( params ) : Promise < PayWithGooglePayResult > => { try { const { error } = await NativeStripeSdk . presentGooglePay ( params ) ; if ( error ) { return { error , } ; } return { } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const createGooglePayPaymentMethod = async ( params ) : Promise < CreateGooglePayPaymentMethodResult > => { try { const { error , paymentMethod } = await NativeStripeSdk . createGooglePayPaymentMethod ( params ) ; if ( error ) { return { error , } ; } return { paymentMethod : paymentMethod ! , } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ; export const openApplePaySetup = async ( ) : Promise < OpenApplePaySetupResult > => { try { const { error } = await NativeStripeSdk . openApplePaySetup ( ) ; if ( error ) { return { error , } ; } return { } ; } catch ( error ) { return { error : createError ( error ) , } ; } } ;