0

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?

  • Are there any successful logs for Ephemeral Key creation in your Stripe Dashboard logs? Have you checked the logs for your Firebase Function(s)? – Jonathan Steele Jan 10 '23 at 11:39
  • No successful logs. Each reads: `Error creating ephemeral key Error: Stripe: Unknown arguments ([object Object]). Did you mean to pass an options object? See https://github.com/stripe/stripe-node/wiki/Passing-Options. (on API request to POST /v1/ephemeral_keys)` Any ideas? Thanks. – iosBeginner Guy Jan 12 '23 at 00:01
  • The `stripe_version` parameter should be camel-cased (`stripeVersion`). Also, the error indicates you're passing an object as an argument. What is the value of your `customerId` and `stripeVersion` parameters? – Jonathan Steele Jan 12 '23 at 09:53
  • I made the following changes: ```const customerId = data.customerId``` & ```const stripeVersion = data.stripeVersion``` & ```{customer: customerId}, { stripeVersion: stripeVersion}``` (thus being camel-cased instead of snake-cased). Same errors as before. customerId returns the Stripe ID of the user, and stripeVersion shows as ```2020-08-27``` The ```key``` value prints as undefined, but that might be expected. – iosBeginner Guy Jan 13 '23 at 06:42

1 Answers1

0

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
]