invertase / stripe-firebase-extensions

Repository of Firebase Extensions built by Stripe.
https://firebase.google.com/products/extensions
Apache License 2.0
435 stars 164 forks source link

[FAQ] Write your own Cloud Functions for Firebase #54

Closed leetworx closed 2 years ago

leetworx commented 4 years ago

For anyone trying to use the source and customize these functions, FYI I found that there appears to be an issue which causes deployment to fail using the latest firebase CLI/tools.

Issue manifests as error during firebase deploy --only functions

HTTP Error: 400, Invalid JSON payload received. Unknown name "children" at 'function': Cannot find field.
Invalid JSON payload received. Unknown name "exit" at 'function': Cannot find field.
Invalid JSON payload received. Unknown name "message" at 'function': Cannot find field.

The root cause appears to be that the trigger for the function is missing (details available by running above with --debug)

The resolution for me was the following change:

functions.handler.https.onRequest should be replaced with

functions.https.onRequest

thorsten-stripe commented 4 years ago

Hey @leetworx, yes, the function syntax is different for normal Firebase Cloud Functions: https://firebase.google.com/docs/functions/http-events

leetworx commented 4 years ago

Ah, so this is a distinction between cloud functions and firebase extensions? Thanks very much for the clarification - got it working!

mshenawy22 commented 3 years ago

@thorsten-stripe @leetworx I want to customize the create user functions so it also stores the phone number and email of the user inside the Customer document. Shall I configure the index.js in the src folder or the index.js in the lib folder. and what is the difference between the two.

Thanks very much

thorsten-stripe commented 3 years ago

@mshenawy22 you cannot modify the extension, but you can deploy your own additional Cloud Functions to run alongside the extension. With version 0.1.7 (to be released this week), you can disable the automatic syncing (see https://github.com/stripe/stripe-firebase-extensions/blob/next/firestore-stripe-subscriptions/CHANGELOG.md#version-017---2020-10-15) and instead you write your own function to manage the customer details in Cloud Firestore. The extension will not overwrite your customer data in Cloud Firestore but rather merge in the stripe data when needed.

mshenawy22 commented 3 years ago

@thorsten-stripe : Why I can't modify the extension , if I downloaded your extension source code then added a function in the index.js that makes use of local functions and variables in the file ,then deployed that new function only , why won't this work?

Disabling the automatic syncing won't help in my case and I'm sure in many other cases . for ex : There are two type of users, paying customer and service receiving customer. I want to create a Customer document only for the paying customer who register through a certain page on the web , hence an HTTP triggered google function is needed.if non paying customer has signed up , I don't want a stripe customer document to be created for them.

It would be great if you made the syncing function configurable , specially what triggers it .

thorsten-stripe commented 3 years ago

@mshenawy22 sorry, this slipped through the cracks.

Why I can't modify the extension , if I downloaded your extension source code then added a function in the index.js that makes use of local functions and variables in the file ,then deployed that new function only , why won't this work?

Developing extensions is currently still in closed alpha. While the source code for the extensions is public as a reference, it is not possible for you to deploy an extension from a local code base to your Firebase project.

But you don't need to. You can simply write your own Cloud Functions to run next to the extension.

It would be great if you made the syncing function configurable , specially what triggers it .

By turning off the automatic sync of the extension, you are completely free to write your own function with your own desired trigger to create your customer documents. The extension will then create a Stripe customer object on the fly when you create the first checkout session for a given user and simply merge the Stripe specific data in your user doc. So you have complete control over how you manage your users.

Apsistec commented 3 years ago

@mshenawy22 sorry, this slipped through the cracks.

Why I can't modify the extension , if I downloaded your extension source code then added a function in the index.js that makes use of local functions and variables in the file ,then deployed that new function only , why won't this work?

Developing extensions is currently still in closed alpha. While the source code for the extensions is public as a reference, it is not possible for you to deploy an extension from a local code base to your Firebase project.

But you don't need to. You can simply write your own Cloud Functions to run next to the extension.

It would be great if you made the syncing function configurable , specially what triggers it .

By turning off the automatic sync of the extension, you are completely free to write your own function with your own desired trigger to create your customer documents. The extension will then create a Stripe customer object on the fly when you create the first checkout session for a given user and simply merge the Stripe specific data in your user doc. So you have complete control over how you manage your users.

If you look in Google Cloud Functions Reports you can see the raw files and make changes there or copy them into your IDE and work on them from there. What is great about it is they have already done the heavy lifting, so be sure to appreciate them for providing this to us. It is quite amazing.

mshenawy22 commented 3 years ago

Got the idea , thanks very much for answering !

Get Outlook for Androidhttps://aka.ms/ghei36


From: Doug White notifications@github.com Sent: Wednesday, December 9, 2020 10:15:27 PM To: stripe/stripe-firebase-extensions stripe-firebase-extensions@noreply.github.com Cc: mshenawy mohamed.shenawy@outlook.com; Mention mention@noreply.github.com Subject: Re: [stripe/stripe-firebase-extensions] [FAQ] Write your own Cloud Functions for Firebase (#54)

@mshenawy22https://github.com/mshenawy22 sorry, this slipped through the cracks.

Why I can't modify the extension , if I downloaded your extension source code then added a function in the index.js that makes use of local functions and variables in the file ,then deployed that new function only , why won't this work?

Developing extensions is currently still in closed alpha. While the source code for the extensions is public as a reference, it is not possible for you to deploy an extension from a local code base to your Firebase project.

But you don't need to. You can simply write your own Cloud Functions to run next to the extension.

It would be great if you made the syncing function configurable , specially what triggers it .

By turning off the automatic sync of the extension, you are completely free to write your own function with your own desired trigger to create your customer documents. The extension will then create a Stripe customer object on the fly when you create the first checkout session for a given user and simply merge the Stripe specific data in your user doc. So you have complete control over how you manage your users.

If you look in Google Cloud Functions Reports you can see the raw files and make changes there or copy them into your IDE and work on them from there. What is great about it is they have already done the heavy lifting, so be sure to appreciate them for providing this to us. It is quite amazing.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/stripe/stripe-firebase-extensions/issues/54#issuecomment-742097318, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALWBQ3ZPGX6E7JNX5R5ECG3ST7ZH7ANCNFSM4QPZ5VQQ.

DCSnip3r commented 3 years ago

I use a firebase function to create a product using the stripe npm package. This triggers a webhook that creates a product document in firebase.

However, I have additional data that I want to add to the product after I create it with the stripe API. To do this, I can write to a firebase using the product ID that the stripe module returns, and using 'set' with {merge:true}.

This will work fine if the webhook fires and creates the product before this line of my code executes, but if it fires after I think it will overwrite the meta data I want to add. Is it possible to turn on {merge:true} for the product creation that fires on the stripe product.created webhook?

Otherwise, a workaround is I can set these product details with a new function that triggers on firestore product document creations. This will work only if the webhook-driven product doc creation is never faster than the response from calling the Stripe API product creation function plus an unrelated document write for querying the reference later

thorsten-stripe commented 3 years ago

@DCSnip3r I'd recommend that you add your additional data to the metadata on the product object (either when you create it or later on via an updat request), that's automatically synced to the Firestore: image

DCSnip3r commented 3 years ago

@thorsten-stripe Does this work with data types like a firestore geopoint? This should generally work, thank you. One disadvantage is the 'stripemetadata' prefix. I personally would not be confused if this were removed

dackers86 commented 2 years ago

HI @DCSnip3r.

Is this issue something that needs to be consider. Or you happy for me to close?

DCSnip3r commented 2 years ago

@dackers86 I believe they turned on merge:true so this is feasible to accomplish now. The general solution for this issue is to create function triggers that watch document writes that the extension initiates. I'm fine to close this