简体   繁体   中英

Firebase Functions Error for Stripe Create Ephemeral Key

I am attempting to use firebase-functions to create a Stripe ephemeral key via a tutorial. Here is the node.js code to do so:

  exports.createEphemeralKey = functions.https.onCall(async (data, context) => {
 
    const customerId = data.customer_id;
    const stripeVersion = data.stripe_version;
    const uid = context.auth.uid;
   
    if (uid === null) {
        console.log('Illegal access attempt due to unauthenticated attempt.')
        throw new functions.https.HttpsError('internal', 'Illegal access attempt');
    }
   
    return stripe.ephemeralKeys.create(
      { customer: customerId },
      { stripe_version: stripeVersion }
    ).then((key) => {
      return key
    }).catch( (err) => {
      functions.logger.log('Error creating ephemeral key', err)
      throw new functions.https.HttpsError('internal', 'Unable to create ephemeral key: ' + err)
    });
  });

Immediately upon running, Xcode shows the following error code:

Error Domain=com.firebase.functions Code=13 "INTERNAL" UserInfo={NSLocalizedDescription=INTERNAL}

When I click to Manage my credit cards (which triggers the Stripe Payment Sheet), the Stripe payment sheet never loads and just shows "Loading..."

My hunch is that my Swift code is OK, and that this is a problem solely with the node.js createEphemeralKey function. I think the customerID is fine, as I can generate it with a print function in Xcode. Might this be an issue with the stripeVersion? Or something else?

here you will find how to check in the Firebase logs if the ephemeral key is created

Figured it out. There were two issues:

  1. You need to use camelcase (camelCase) as opposed to snakecase (snake_case).
  2. You need to use apiVersion as opposed to stripeVersion.

Here is the code that worked:

exports.createEphemeralKey = functions.https.onCall(async (data, context) => {
 
    const customerId = data.customerId;
    const apiVersion = data.stripeVersion;
    const uid = context.auth.uid;
   
    if (uid === null) {
        console.log('Illegal access attempt due to unauthenticated attempt.')
        throw new functions.https.HttpsError('internal', 'Illegal access attempt');
    }
   
    return stripe.ephemeralKeys.create(
      { customer: customerId},
      { apiVersion: apiVersion} 
    ).then((key) => {
      return key
    }).catch( (err) => {
      functions.logger.log('Error creating ephemeral key', err)
      throw new functions.https.HttpsError('internal', 'Unable to create ephemeral key: ' + err)
    })
  })

Then in Xcode remember to change your data:

let data = [
"apiVersion": apiVersion,
"customerId": UserManager.instance.user?.stripeId
] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM