0
votes

I am trying to write a firebase cloud function that attaches a payment method to a stripe customer, subscribes them to a plan, and writes the subscription object to firestore.

I actually just wrote a function that worked but am not sure what I changed.

exports.attachAndSubscribe = functions.firestore
    .document('stripe_customers/{userId}')
    .onUpdate(async (change, context) => {

        const source = change.after.data();
        const paymentMethod = source.paymentMethod;

        await stripe.paymentMethods.attach(paymentMethod.id, 
            {customer: source.customer_id},
            {invoice_settings: {default_payment_method: paymentMethod.id}
        });

        const subscription = await stripe.subscriptions.create(
            {customer: source.customer_id,items: [{plan: 'plan_FnA3IsFL5Xc6Ct'}]
        });

        return admin.firestore()
        .collection('stripe_customers')
        .doc(userId)
        .set(
            {subscription: subscription});

    });

When the function gets triggered I get the following error:

Stripe: Unknown arguments ([object Object]). Did you mean to pass an options object?

1
I believe your issue is with stripe.paymentMethods.attach the only param available is customer, stripe.com/docs/api/payment_methods/attach?lang=node - duck

1 Answers

0
votes

duck was right. The default_payment_method property only exists in the customer object, not in the paymentMethod object. I solved the problem by separately updating the stripe customer after attaching the payment method.