invertase / stripe-firebase-extensions

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

Error installing firestore-stripe-payments in emulator #394

Open penx opened 2 years ago

penx commented 2 years ago

Bug report

Describe the bug

When trying to install firestore-stripe-payments in the emulator, I get:

Ref does not have a version

To Reproduce

firebase ext:install --local stripe/firestore-stripe-payments

Expected behavior

Should either be able to install in the emulator or receive a meaningful error message.

System information

marcojacobsNL commented 2 years ago

I'm also experiencing this issue, any update?

Edit: Do you have a open billing account linked to your Google account? I'm starting to think it's related to that. I tried to instal with --debug and it seems everything is fetched until GET https://firebaseextensions.googleapis.com/v1beta/publishers/stripe/extensions/firestore-stripe-payments/versions which also seems to succeed but than the error comes up.

The ref that isn't available is probably stripe/firestore-stripe-payments@0.1.0 as it seems from the response of mentioned request:

[debug] [2022-06-07T13:56:42.319Z] <<< [apiv2][body] GET https://firebaseextensions.googleapis.com/v1beta/publishers/stripe/extensions/firestore-stripe-payments/versions {"extensionVersions":[{"name":"publishers/stripe/extensions/firestore-stripe-payments/versions/0.1.0","ref":"stripe/firestore-stripe-payments@0.1.0","spec":{"specVersion":"v1beta","name":"firestore-stripe-subscriptions","version":"0.1.0"

CondorW commented 2 years ago

Got the same error - a working workaround is specifying the version you want to install - the local flag does not need to be set anymore. firebase ext:install stripe/firestore-stripe-payments@0.2.7

dackers86 commented 2 years ago

HI @penx

Did the above comment fix your scenario? If this is still an issue could you report it under https://github.com/firebase/firebase-tools repository as it is this package that would update this particular issue.

Please see https://github.com/firebase/firebase-tools/blob/b2327da886ded6c71b991aee461c9fbd06003538/src/extensions/refs.ts for more information.

b0ot commented 2 years ago

I had the same issue, but this seemed to work: firebase ext:export --project=<project_name>

The only potential issue I see now is: ⚠ Function 'createPortalLink is missing a trigger in extension.yaml. Please add one, as triggers defined in code are ignored. ⚠ Function 'handleWebhookEvents is missing a trigger in extension.yaml. Please add one, as triggers defined in code are ignored.

I don't know what that will effect.

thetwentyseven commented 1 year ago

I am using Stripe Test Mode. When I create a product on Stripe, or create a user in Firebase, the product gets added to the Firestore database in production, and when adding a new user, a new customer is added in Stripe.

I would like to have the same communication but in my local using Emulators.

After I installed this extension locally, it created a folder call "extension" with two files, firestore-stripe-payments.env and firestore-stripe-payments.secret.local. In the last file, I do have STRIPE_API_KEY, and STRIPE_WEBHOOK_SECRET fill in.

I run the emulators:

i  emulators: Starting emulators: auth, functions, firestore, database, extensions
⚠  Function 'createPortalLink is missing a trigger in extension.yaml. Please add one, as triggers defined in code are ignored.
⚠  Function 'handleWebhookEvents is missing a trigger in extension.yaml. Please add one, as triggers defined in code are ignored.
functions: Watching "/home/user/.cache/firebase/extensions/stripe/firestore-stripe-payments@0.3.2/functions" for Cloud Functions...
✔  functions: Loaded functions definitions from source: createCustomer, createCheckoutSession, createPortalLink, handleWebhookEvents, onUserDeleted, onCustomerDataDeleted.
✔  functions[us-central1-ext-firestore-stripe-payments-createCustomer]: auth function initialized.
✔  functions[us-central1-ext-firestore-stripe-payments-createCheckoutSession]: firestore function initialized.
✔  functions[us-central1-ext-firestore-stripe-payments-createPortalLink]: http function initialized (http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-createPortalLink).
✔  functions[us-central1-ext-firestore-stripe-payments-handleWebhookEvents]: http function initialized (http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents).
✔  functions[us-central1-ext-firestore-stripe-payments-onUserDeleted]: auth function initialized.
✔  functions[us-central1-ext-firestore-stripe-payments-onCustomerDataDeleted]: firestore function initialized.

Then using Stripe CLI I started to listen to events: stripe listen -f http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents

However, when I edit a product on Stripe Test Mode, I get the following errors:

2023-02-23 11:00:57   --> product.updated [evt_1MecXlIPR8sY7dy0Jrs9oLuq]
2023-02-23 11:00:57  <--  [401] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1MecXlIPR8sY7dy0Jrs9oLuq]
2023-02-23 11:00:58   --> product.updated [evt_1MecXlIPR8sY7dy0PtrVDQ2i]
2023-02-23 11:00:58  <--  [401] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1MecXlIPR8sY7dy0PtrVDQ2i]
2023-02-23 11:25:55   --> product.updated [evt_1MecvvIPR8sY7dy0OZtlcOGD]
2023-02-23 11:25:56   --> product.updated [evt_1MecvwIPR8sY7dy0IvLWBERg]
2023-02-23 11:25:56  <--  [500] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1MecvwIPR8sY7dy0IvLWBERg]

I do get mostly 500s errors, and sometimes without changing anything also 401s.

I have tried to change the STRIPE_WEBHOOK_SECRET with the one that stripe-cli supply when you start listing:

Ready! You are using Stripe API Version [2022-11-15]. Your webhook signing secret is whsec_5879fb5aaf08f84084f17ce176bd12e4719a....ce16b38f99ba9b6 (^C to quit)

but still not working either.

If I go to the Stripe Dashboard event. And I look at the localhost response I can see:

My guess is that I need to add the webhook secret somewhere apart from the firestore-stripe-payments.secret.local but I do not know where or how.

**Also another thing I was not expecting is that when I go to extension emulator http://127.0.0.1:4000/extensions There is no extensions there.

thetwentyseven commented 1 year ago

This is so weird. If I try to trigger for the first time "stripe trigger product.updated" it gives me 500 error, but if I create a user using Firebase Emulator Authentication, it sends the user to Stripe and responds with 200. And after if I request the "stripe trigger product.updated" it works!!

2023-02-24 12:58:26   --> product.updated [evt_1Mf0r0IPR8sY7dy0yZ4BHKbD]
2023-02-24 12:58:26  <--  [500] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1Mf0r0IPR8sY7dy0yZ4BHKbD]
    2023-02-24 12:58:43            [ERROR] Failed to POST: Post "http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

2023-02-24 12:58:56            [ERROR] Failed to POST: Post "http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

2023-02-24 12:59:13   --> customer.created [evt_1Mf0rlIPR8sY7dy0WJ8sq3SM]
2023-02-24 12:59:14  <--  [200] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1Mf0rlIPR8sY7dy0WJ8sq3SM]
    2023-02-24 12:59:34   --> product.updated [evt_1Mf0s6IPR8sY7dy00RqE1Il6]
2023-02-24 12:59:35  <--  [200] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1Mf0s6IPR8sY7dy00RqE1Il6]
    2023-02-24 12:59:35   --> product.updated [evt_1Mf0s7IPR8sY7dy0GUKP0RWr]
2023-02-24 12:59:35  <--  [200] POST http://127.0.0.1:5001/my-project/us-central1/ext-firestore-stripe-payments-handleWebhookEvents [evt_1Mf0s7IPR8sY7dy0GUKP0RWr]
merri-ment commented 1 year ago

@thetwentyseven did you find where to add the webhook secret key locally? i'm having the exact same issue

thetwentyseven commented 1 year ago

@merri-ment the webhook secret is created after you install the extension. It is in a folder call extensions and in a file called firestore-stripe-payments.secret.local. Dont you have that one?

thetwentyseven commented 1 year ago

In order to connect to the emulator database, and auth you need to do the following:

import {getApp} from "firebase/app";
import {Firestore, connectFirestoreEmulator, getFirestore} from "firebase/firestore";
import {Auth, connectAuthEmulator, getAuth} from "firebase/auth";
import {getStripePayments} from "@stripe/firestore-stripe-payments";

// *** firestore-stripe-payments API ***
        const firebaseApp = getApp();
        const payments = getStripePayments(firebaseApp, {
            productsCollection: "products",
            customersCollection: "customers",
        });
if (location.hostname === "localhost") {
            const db: Firestore = getFirestore(firebaseApp);
            const auth: Auth = getAuth(firebaseApp);

            connectFirestoreEmulator(db, "localhost", 8080);
            connectAuthEmulator(auth, "http://localhost:9099", {
                disableWarnings: true,
            });

        }

Even if you are using the Firebase v8 api syntax, as I am, you need to use the code above.

You can check the example in https://github.com/stripe/stripe-firebase-extensions/blob/055d467b3aa9441dae372904818847fb222ba57d/firestore-stripe-web-sdk/test/emulator.spec.ts

merri-ment commented 1 year ago

Hey @thetwentyseven, thanks for your reply.

Yes firestore-stripe-payments.secret.local has installed correctly, with the two env vars pointing to my Sprite Test Mode account.

All Auth, Firestore and Functions emulators are working locally too.

When i update a product in Stripe and look at the Webhook log in Test Mode, i see 401 ERR - Webhook Error: Invalid Secret. As mentioned, STRIPE_API_KEY and STRIPE_WEBHOOK_SECRET in firestore-stripe-payments.secret.local both correspond with my Test mode account 🤷

This is ultimately the issue, nothing returned from Stripe SDK, which i guess is an issue with the extension webhooks.

        const payments = await getStripePayments(firebaseApp, {
            productsCollection: "products",
            customersCollection: "customers",
        });
        const products = await getProducts(payments, {
            includePrices: true,
            activeOnly: true,
        })
        console.log(products) //  two active products are in stripe test-mode, returns empty array [] 
        // same code works in prod
merri-ment commented 1 year ago

okay, i missed the crucial step of spinning up the Stripe CLI locally and porting the webhook. Now it works.

stripe listen --api-key rk_test_*********** --forward-to http://127.0.0.1:5001/{project-name}/{firebase-server}/ext-firestore-stripe-payments-handleWebhookEvents 
jelling commented 1 year ago

okay, i missed the crucial step of spinning up the Stripe CLI locally and porting the webhook. Now it works.

Had the same experience and lost a couple of hours because this isn't mentioned in the docs, as least as far as I can see.

It looks like the docs are expecting the user to being running on the cloud based on "Here's your function's URL: ${function:handleWebhookEvents.url}"

image

Also, if one creates a restricted key with only the permissions mentioned in the docs (Customers: Write, Checkout Sessions: Write, Customer portal: Write, Subscriptions: Read), those are not enough perms to run the webhook with the emulator. My quick work around was to use the secret key for my test environment instead (sk_) but probably worth updating the docs.

matheusbaumgart commented 7 months ago

Yep, I'm also loosing a lot of time figuring this out... definitely worth updating the docs.

kelsheikh commented 5 months ago

Yea this was a headache to figure out. There should be more about emulator / stripe integration.