|
| 1 | +# Project switching |
| 2 | +firebase use dev |
| 3 | +firebase use prod |
| 4 | +# Firebase Functions Setup (Node 22) |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +- Node 22 or higher |
| 9 | +- Firebase CLI: `npm install -g firebase-tools` |
| 10 | +- both are installed already with nix |
| 11 | +- install stripe cli |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +### 1. Install Dependencies |
| 16 | + |
| 17 | +```bash |
| 18 | +cd functions |
| 19 | +npm install |
| 20 | +``` |
| 21 | + |
| 22 | +### 2. Add Firebase Projects |
| 23 | + |
| 24 | +```bash |
| 25 | +firebase use --add |
| 26 | +# Alias: dev |
| 27 | +# Project: whatsanalyze-dev (or your dev project ID) |
| 28 | + |
| 29 | +firebase use --add |
| 30 | +# Alias: prod |
| 31 | +# Project: whatsanalyze-prod (or your prod project ID) |
| 32 | +``` |
| 33 | + |
| 34 | +### 3. Build & Test Locally |
| 35 | + |
| 36 | +```bash |
| 37 | +npm run build # Compile TypeScript |
| 38 | +npm run dev # Run emulator with functions |
| 39 | +``` |
| 40 | + |
| 41 | +Visit `http://localhost:5001` for emulator UI. |
| 42 | + |
| 43 | +### 4. Deploy to Dev |
| 44 | + |
| 45 | +```bash |
| 46 | +firebase use dev |
| 47 | +npm run deploy:dev |
| 48 | +``` |
| 49 | + |
| 50 | +### 5. Deploy to Prod |
| 51 | + |
| 52 | +```bash |
| 53 | +firebase use prod |
| 54 | +npm run deploy:prod |
| 55 | +``` |
| 56 | + |
| 57 | +### 6. Setup Stripe |
| 58 | + |
| 59 | +If You want to test the payment/subscription flow locally, stripe relies on webhook. We use the stripe cli to make sure that we can recieve those locally. Have a look at the stripe section further down. |
| 60 | + |
| 61 | +## Node Version |
| 62 | + |
| 63 | +Functions target **Node 22** (latest LTS supported by Firebase Functions runtime). |
| 64 | + |
| 65 | +## Environment Variables |
| 66 | +There are some values which are different between dev and prod. For that we use environment files (`.env.FIREBASE_PROJECT_NAME`). It is possible to overwrite those when running locally by creating a `.env.local` file and adding the values there. This file takes precedence over the `.env.FIREBASE_PROJECT_NAME` file. There are also some values that are sensitive, so we treat them as secrets. |
| 67 | + |
| 68 | +### Secrets |
| 69 | +`STRIPE_WEBHOOK_SECRET` and `STRIPE_SECRET_KEY` are secrets which we can not simply put in the `.env.FIREBASE_PROJECT_NAME` files. For that reason we create and manage secrets via the firebase cli: |
| 70 | + |
| 71 | +```bash |
| 72 | +firebase --project $PROJECT functions:secrets:set STRIPE_SECRET_KEY |
| 73 | +firebase --project $PROJECT functions:secrets:set STRIPE_WEBHOOK_SECRET |
| 74 | +``` |
| 75 | +You can use the `scripts/setup-stripe.sh` file for that. Make sure that there are no whitespaces in the secrets stored. You can access the secret with `firebase --project $PROJECT functions:secrets:access STRIPE_SECRET_KEY`. |
| 76 | + |
| 77 | +## Init mail templates |
| 78 | +Install the extension in the firebase console first: |
| 79 | + |
| 80 | +```bash |
| 81 | +firebase login |
| 82 | +# authenticate gcloud |
| 83 | +gcloud auth application-default login |
| 84 | +npm run init:templates:dev |
| 85 | +``` |
| 86 | + |
| 87 | +# Troubleshooting |
| 88 | + |
| 89 | +If you get cors issues when trying to invoke the firebase Callable Cloud Function the most likely issue is that |
| 90 | +anonymous access is not allow and needs ot be enabled in gcp. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +# Stripe Firebase Functions |
| 95 | + |
| 96 | +Firebase Cloud Functions for handling Stripe checkout and subscriptions, translated from the [Stripe checkout-single-subscription sample](https://github.com/stripe-samples/checkout-single-subscription). |
| 97 | + |
| 98 | +## Quick Start |
| 99 | + |
| 100 | +### 1. Stripe Local testing |
| 101 | +- stripe login |
| 102 | +- stripe listen --forward-to http://127.0.0.1:5001/whatsanalyze-wrapped/us-central1/stripeWebhook |
| 103 | + - add "Your webhook signing secret is `whsec_2f7....`" output to `.secret.local` file |
| 104 | + - `STRIPE_WEBHOOK_SECRET=`whsec_2f7....`` |
| 105 | +- stripe trigger checkout.session.completed |
| 106 | + |
| 107 | +### 2. Stripe dev testing |
| 108 | +- ./scripts/setup-stripe.sh to set the stripe keys (secret + api) |
| 109 | +- stripe trigger subscription.payment_succeeded --add "customer:email=stripe@whatsanalyze.com" |
| 110 | + |
| 111 | +### 3. Stripe Production Webhook Testing |
| 112 | + |
| 113 | +To verify webhooks work correctly in production, create a test checkout session: |
| 114 | + |
| 115 | +```bash |
| 116 | +npm run stripe:test-checkout:prod |
| 117 | +``` |
| 118 | + |
| 119 | +This creates a checkout session with a €0.50 test product (`prod_TeTooduQWBMod7`). Open the returned `url` in your browser, complete the purchase with a real card, verify the webhook fires correctly, then refund immediately from Stripe Dashboard. |
| 120 | + |
| 121 | +**Note**: This uses the `STRIPE_SECRET_KEY` from Firebase secrets, so you need Firebase project access. |
| 122 | + |
| 123 | +### 4. Stripe Subscription Renewal testing |
| 124 | +In theory it should be enough to set the renewal date to now with this command: |
| 125 | +```bash |
| 126 | +stripe subscriptions update "$SUB_ID" --billing-cycle-anchor=now --proration-behavior=none |
| 127 | +``` |
| 128 | +And stripe would then handle the rest in the background (automatic invoice creation and payment of that). |
| 129 | +But I was not able to make it work. The anchor is successfully set, but the subscription cycle is not happening. |
| 130 | + |
| 131 | +What was working is following this tutorial: |
| 132 | +https://docs.stripe.com/billing/testing/test-clocks/simulate-subscriptions |
| 133 | +It simulates what would happen by forwarding the time at stripe BE. |
| 134 | + |
| 135 | +## Stripe Web setup |
| 136 | +Create api keys and make sure to store them accordingly to the firebase function secret setup (look above). |
| 137 | +Create a subscription and add the price_id as env variable accordingly to the firebase environment setup (look above). |
| 138 | +Make sure that the webook is correctly configured. Webhook URL looks something like: `https://us-central1-whatsanalyze-wrapped.cloudfunctions.net/stripeWebhook` (This is just an example, make sure to find the correct url. It should be displayed when deploying functions). |
0 commit comments