diff --git a/README.md b/README.md index bf8f1440..e14b68dd 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # UPDATE 2023-10-08: -This project has now being officailly transferred to [Invertase](https://github.com/invertase), who will maintain this extension going forward. Please see [this issue](https://github.com/stripe/stripe-firebase-extensions/issues/524) for more details. -It is now reccomended to uninstall the `stripe/firestore-stripe-payments` extension and install `invertase/firestore-stripe-payments` from the Firebase Extension Hub. +This project has now being officially transferred to [Invertase](https://github.com/invertase), who will maintain this extension going forward. Please see [this issue](https://github.com/stripe/stripe-firebase-extensions/issues/524) for more details. +It is now recommended to uninstall the `stripe/firestore-stripe-payments` extension and install `invertase/firestore-stripe-payments` from the Firebase Extension Hub. -Alternativley, you can also use the following link to convert your current installation to the Invertase version +Alternatively, you can also use the following link to convert your current installation to the Invertase version `https://console.firebase.google.com/project/_/extensions/install?instanceId=STRIPE_EXTENSION_INSTANCE_ID&ref=invertase%2Ffirestore-stripe-payments@0.3.5` diff --git a/firestore-stripe-payments/functions/src/index.ts b/firestore-stripe-payments/functions/src/index.ts index 74a72472..cbc1543b 100644 --- a/firestore-stripe-payments/functions/src/index.ts +++ b/firestore-stripe-payments/functions/src/index.ts @@ -69,7 +69,30 @@ const createCustomerRecord = async ({ }; if (email) customerData.email = email; if (phone) customerData.phone = phone; - const customer = await stripe.customers.create(customerData); + let customer; + + + if(email) { + + // Search for existing customer by email to avoid creating Stripe customers + const existingCustomers = await stripe.customers.list({ + email: email, + limit: 1 + }); + + if (existingCustomers.data.length > 0) { + + // Use existing customer and update their details and metadata + const existingCustomer = existingCustomers.data[0]; + customer = await stripe.customers.update( + existingCustomer.id, + customerData + ); + } + + if(!customer) { + customer = await stripe.customers.create(customerData); + } // Add a mapping record in Cloud Firestore. const customerRecord = {