I am trying to set the last card added to the stripe as the default via firebase functions though I can't seem to get it to work.
// Add a payment source (card) for a user by writing a stripe payment source token to Realtime database
exports.addPaymentSource = functions.database.ref('/users/{userId}/sources/{pushId}/token').onWrite(event => {
const source = event.data.val();
if (source === null) return null;
return admin.database().ref(`/users/${event.params.userId}/customer_id`).once('value').then(snapshot => {
return snapshot.val();
}).then(customer => {
return stripe.customers.createSource(customer, {source});
return stripe.customers.update(customer, {default_source: source});
}).then(response => {
return event.data.adminRef.parent.set(response);
}, error => {
return event.data.adminRef.parent.child('error').set(userFacingMessage(error)).then(() => {
// return reportError(error, {user: event.params.userId});
consolg.log(error, {user: event.params.userId});
});
});
});
Stay organized with collections Save and categorize content based on your preferences. Using a few different Firebase features and Stripe, you can process payments in your web app without building your own server infrastructure.
Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.
Firebase also allows free setup with no credit card. The Spark plan. You don't get a "server" but you can run cloud functions for free. The Cloud functions do have some limitations.
Firebase offers a no-cost tier pricing plan for all its products. For some products, usage continues at no cost no matter your level of use. For other products, if you need high levels of use, you'll need to switch your project to a paid-tier pricing plan. Learn more about Firebase pricing plans.
You're trying to return two things in this one function. That isn't going to work. It should create the source, but it won't update it.
return stripe.customers.createSource(customer, {source});
return stripe.customers.update(customer, {default_source: source});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With