stripe / stripe-react-native

React Native library for Stripe.
https://stripe.dev/stripe-react-native
MIT License
1.25k stars 256 forks source link

How to get the card info on presentPaymentSheet #1633

Closed faizanbutt closed 4 months ago

faizanbutt commented 4 months ago

I'm currently working on integrating the Stripe SDK for React Native into my project. I've successfully added the card using SetupIntent.create at the backend and initiated the payment sheet using initPaymentSheet(client_secret) returned from the backend.

Now, my challenge is retrieving the card details such as the last 4 digits, expiration month, expiration year, and brand of the card after successfully adding it. I need this information to perform certain actions within my application.

I've attempted to explore the library files, particularly looking into presentPaymentSheet, but haven't found any relevant information.

Could someone guide me on how to retrieve the card details after adding it using the Stripe SDK for React Native? Is there an existing method or approach I can try?

Any help or pointers would be greatly appreciated.

charliecruzan-stripe commented 4 months ago

Hi! These are the docs you're looking for: https://docs.stripe.com/payments/save-and-reuse?platform=react-native&mobile-ui=payment-element#charge-saved-payment-method

faizanbutt commented 4 months ago

@charliecruzan-stripe Hi,

Thank you for providing the documentation link.

I've followed the documentation and managed to retrieve the required data, such as the last 4 digits and expiration date. However, I encountered an error when intentCreationCallback is called with the client secret, which is returned in confirmPaymentSheetPayment.

The error message I received is: { "code": "Failed", "declineCode": null, "localizedMessage": "There was an unexpected error -- try again in a few seconds", "message": "There was an unexpected error -- try again in a few seconds", "stripeErrorCode": null, "type": null } I've also attached the relevant code for reference. Could you please review it and let me know if I'm missing something?

const initializePaymentSheet = async () => { const { error } = await initPaymentSheet({ merchantDisplayName: "Example, Inc.", customFlow: true, style: 'automatic', intentConfiguration: { mode: { currencyCode: 'USD', }, confirmHandler: confirmHandler } }); if (!error) { choosePaymentOption() } else { Alert.alert(Error code: ${error.code}, error.message); } };

const confirmHandler = async (paymentMethod, shouldSavePaymentMethod, intentCreationCallback) => { callStripeMutation(paymentMethod.id, intentCreationCallback) }

const choosePaymentOption = async () => { const { error, paymentOption } = await presentPaymentSheet(); if (error && error.code != "Canceled") { console.log('error', error); } else if (paymentOption) { savePaymentOption() } };

const savePaymentOption = async () => { const { error } = await confirmPaymentSheetPayment(); if (error) { console.log('error'); // Returns error, There was an unexpected error -- try again in a few seconds } else { console.log('success'); } };

const callStripeMutation = (paymentMethodId, intentCreationCallback) => { const secretIntent = callMutation(paymentMethod: paymentMethodId) //Example intentCreationCallback(secretIntent) } Looking forward to your guidance. Thanks!