import { https , config } from 'firebase-functions' ; import Stripe from 'stripe' ; export const createPaymentIntent = https . onCall ( async ( data , context ) => { const { amount , currency , testMode } = data ; const stripeConfig = config ( ) . stripe || { } ; if ( stripeConfig . unauthenticated_payments !== "enable" && ! context . auth ) { throw new https . HttpsError ( 'unauthenticated' , 'Authentication is required to process payments.' ) ; } if ( ! amount || ! currency ) { throw new https . HttpsError ( 'failed-precondition' , 'Missing amount or currency.' ) ; } if ( amount <= 0 ) { throw new https . HttpsError ( 'out-of-range' , 'Amount must be a positive integer.' ) ; } try { const stripe = new Stripe ( testMode ? stripeConfig . test_key : stripeConfig . key , null ) ; const paymentIntent = await stripe . paymentIntents . create ( { amount , currency } ) ; console . log ( 'Payment intent created! ' , paymentIntent . id ) ; return paymentIntent ; } catch ( err ) { throw new https . HttpsError ( 'unknown' , err . message ) ; } } ) ;