UNPKG

@commercelayer/react-components

Version:
2 lines 1.06 kB
"use client"; import{loadStripe}from"@stripe/stripe-js";async function retrievePaymentIntent({publicApiKey,paymentIntentClientSecret}){const stripe=await loadStripe(publicApiKey);try{return await stripe?.retrievePaymentIntent(paymentIntentClientSecret)}catch(error){return console.error("Error retrieving payment intent:",error),null}}function paymentIntentValidation({paymentIntent}){switch(paymentIntent?.status){case"succeeded":case"requires_capture":return"valid";case"processing":return"processing";case"requires_payment_method":return"invalid";default:return"invalid"}}export async function checkPaymentIntent({publicApiKey,paymentIntentClientSecret}){const paymentIntentResult=await retrievePaymentIntent({publicApiKey,paymentIntentClientSecret});if(!paymentIntentResult)return{status:"invalid",message:"Payment intent not found"};const paymentIntent=paymentIntentResult.paymentIntent,error=paymentIntent?.last_payment_error,status=paymentIntentValidation({paymentIntent});return{status,message:status==="invalid"&&error?.message!=null?error.message:""}}