Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendInvoice dont' use existing customer #223

Open
waste-app opened this issue Jul 16, 2021 · 5 comments
Open

sendInvoice dont' use existing customer #223

waste-app opened this issue Jul 16, 2021 · 5 comments
Labels
question Further information is requested

Comments

@waste-app
Copy link

Bug report

  • Extension name: Send Invoices using Stripe

Describe the bug

customer infos cannot be retrieved
// Use the existing customer is out of code coverage

/* Emails an invoice to a customer when a new document is created */
export const sendInvoice = functions.handler.firestore.document.onCreate(
  async (snap, context) => {
...
      let email: string;

      if (payload.uid) {
        // Look up the Firebase Authentication UserRecord to get the email
        const user = await admin.auth().getUser(payload.uid);
        email = user.email;
      } else {
        // Use the email provided in the payload
        email = payload.email;
      }

      if (!email) {
        logs.noEmailForUser(payload.uid);
        return;
      }

      // Check to see if there's a Stripe customer associated with the email address
      let customers: Stripe.ApiList<Stripe.Customer> = await stripe.customers.list(
        { email }
      );
      let customer: Stripe.Customer;

      if (customers.data.length) {
        // Use the existing customer
        customer = customers.data.find(
          (cus) => cus.currency === payload.items[0].currency
        );
        if (customer) logs.customerRetrieved(customer.id, customer.livemode);
      }
      if (!customer) {
        // Create new Stripe customer with this email
        customer = await stripe.customers.create(
          {
            email,
            metadata: {
              createdBy:
                'Created by the Firebase Extension: Send Invoices using Stripe', // optional metadata, adds a note
            },
          },
          { idempotencyKey: `customers-create-${eventId}` }
        );

        logs.customerCreated(customer.id, customer.livemode);
      }
...

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

create a new invoice on existing user

Expected behavior

load all billing information on existing user e.g VAT NUMBER

Additional context

stripe/[email protected]

@thorsten-stripe thorsten-stripe added the question Further information is requested label Jul 29, 2021
@dackers86
Copy link
Member

Hi @waste-app.

Can you confirm which retrieval method you are using, for example is it a uid or an email address.

Additionally, do you haver an active firebase user which represents the provided uid/email?

@antoniosap
Copy link

in any case: uid or email.
note: your published TS code don't compile.
i fork you code and make my customisation

@sarbogast
Copy link

I have the same issue: I create customers in Stripe based on my users in Firebase so that all the properties like name, address, VAT number and so on are properly set. The one thing I can't seem to be able to do is to set the currency of the customer when I create it. So by default it is left undefined. And when I create an invoice for the Firebase extension to pick up, I only set its email address because I don't want to use the email address associated to the Firebase auth account, but one that is specific to invoicing. So no uid in my invoice, only an email address that is the same as the Stripe customer I already created with the API beforehand. But since the extension searches for existing users based on email address AND currency, and the currency of my existing customer is undefined instead of 'eur' (the currency of the first item of my invoice), then it creates a new one.

Why this limitation in the extension? Why not use only the email? Or even better, why not let us specify the customer id altogether, to remove any ambiguity?

@johnpuaoi
Copy link

@sarbogast I agree, using just the customer id or the email would be great. I discovered this issue when invoicing customers through my new ticketing system that utilizes this extension.

For now, I guess as a workaround I will just set the customer's currency manually in the stripe dashboard before invoicing them.

@sarbogast
Copy link

I had so many customization issues like that that I ended up copying the code into my own Cloud Functions, uninstalling the extension, and customizing what I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants