简体   繁体   中英

Stripe - Update default card

I am trying to allow the user to update their default payment method after they add it. I am getting this in Firebase Functions: Error: No such source: card_1EhmibFZW9pBNLO2aveVfEm6 .

This leads me to believe that I need to pass default_source a src_XXX... id rather than a card_XXX... id. Anyone have an idea on this?

Firebase Function:

// Update Stripe default card based on user choice
exports.updateDefaultSource = functions.firestore
  .document("users/{userId}")
  .onUpdate(async (change, context) => {
    const newValue = change.after.data();
    const previousValue = change.before.data();
    console.log("previousValue.default_source: "+previousValue.default_source)
    console.log("newValue.default_source: "+newValue.default_source)
    if (
      previousValue.default_source &&
      newValue.default_source !== previousValue.default_source
    ) {
      // this triggers on every update to profile (more overhead), can we reduce this?
      try {
        console.log("newValue.default_source: "+newValue.default_source)
        const response = await stripe.customers.update(
          previousValue.customer_id,
          { default_source: newValue.default_source },
          (err, customer) => {
            console.log(err);
          }
        );
        return console.log("Response from Stripe update: " + response);
      } catch (error) {
        console.log(error);
        await change.ref.set(
          { error: userFacingMessage(error) },
          { merge: true }
        );
        return reportError(error, { user: context.params.userId });
      }
    }
  });

Firebase Function logs after I add the second Card to account: 在此处输入图片说明

Looks like this error solved itself, not 100% sure on how, but my guess is it had to do with Redux and/or Redux Persist not having everything loaded into the store.

My main question was answered by @hmunoz on whether or not the default_source accepted the card_123 type, which it does.

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