import {
useStripe,
useElements,
CardNumberElement,
} from '@stripe/react-stripe-js';
const stripe = useStripe();
const elements = useElements();
const setupIntentResponse ...
if (setupIntentResponse) {
const billing_details = {
...billingAddress,
email: customerEmail,
};
const paymentMethodResponse = await stripe.createPaymentMethod({
type: 'card',
card: elements.getElement(CardNumberElement),
billing_details,
});
if (!isEmpty(paymentMethodResponse.error)) {
setSetupIntentError(true);
return;
}
stripe
.confirmCardSetup(setupIntentResponse.clientSecret, {
payment_method: paymentMethodResponse.paymentMethod.id,
})
.then(function (result) {
if (result.error || result.setupIntent.status !== 'succeeded') {
setSetupIntentError(true);
} else {
const savedPaymentMethodRequest = {
name: 'Saved Payment Method Name',
billingAddress: billingAddress,
owningUserType: 'BLC_CUSTOMER',
owningUserId: 'customer_id',
gatewayType: 'STRIPE',
displayAttributes: {
creditCardHolder: 'Credit Card Holder Name',
creditCardNumber: `****${paymentMethodResponse.paymentMethod.card.last4}`,
creditCardExpDateMonth: `${paymentMethodResponse.paymentMethod.card.exp_month}`,
creditCardExpDateYear: `${paymentMethodResponse.paymentMethod.card.exp_year}`,
},
paymentMethodProperties: {
PAYMENT_METHOD_ID: result.setupIntent.payment_method,
CUSTOMER_ID: setupIntentResponse.stripeCustomerId
},
});
// addSavedPaymentMethod - can be obtained from our payment SDK - useAddSavedPaymentMethod(). It creates the saved payment method in PaymentTransactionServices
await addSavedPaymentMethod(savedPaymentMethodRequest);
}
});
}