Skip to content

Commit f6d573d

Browse files
dackers86Salakar
andauthored
feat(firestore-stripe-payments): added stripe extension events
Co-authored-by: Mike Diarmid <[email protected]>
1 parent b71e828 commit f6d573d

File tree

7 files changed

+96
-19
lines changed

7 files changed

+96
-19
lines changed

firestore-stripe-payments/PREINSTALL.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ applications would want to implement using the extension.
3535
Use a package manager like NPM to install the above package, and use it in conjunction with
3636
the Firebase Web SDK.
3737

38+
### Events
39+
40+
This extension emits events, which allows you to listen to and run custom logic at different trigger points during the functioning of the extension. For example you can listen to events when a product has been added via the `product.created` event, or whenever a payment has succeeded through the `invoice.payment_succeeded` event.
41+
3842
#### Additional setup
3943

4044
Before installing this extension, set up the following Firebase services in your Firebase project:
@@ -56,10 +60,11 @@ This extension uses the following Firebase services which may have associated ch
5660
- Cloud Functions
5761
- Cloud Secret Manager
5862
- Firebase Authentication
63+
- If you enable events [Eventarc fees apply](https://cloud.google.com/eventarc/pricing).
5964

6065
This extension also uses the following third-party services:
6166

62-
- Stripe Payments ([pricing information](https://stripe.com/pricing))
67+
- Stripe Payments ([pricing information](https://stripe.com/pricing))
6368
- Stripe Billing (when using subscriptions. [pricing information](https://stripe.com/pricing#billing-pricing))
6469

6570
You are responsible for any costs associated with your use of these services.

firestore-stripe-payments/extension.yaml

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ resources:
5555
Creates a Stripe customer object when a new user signs up.
5656
properties:
5757
location: ${LOCATION}
58-
runtime: nodejs10
58+
runtime: nodejs14
5959
eventTrigger:
6060
eventType: providers/firebase.auth/eventTypes/user.create
6161
resource: projects/${PROJECT_ID}
@@ -66,7 +66,7 @@ resources:
6666
Creates a Checkout session to collect the customer's payment details.
6767
properties:
6868
location: ${LOCATION}
69-
runtime: nodejs10
69+
runtime: nodejs14
7070
eventTrigger:
7171
eventType: providers/cloud.firestore/eventTypes/document.create
7272
resource: projects/${PROJECT_ID}/databases/(default)/documents/${CUSTOMERS_COLLECTION}/{uid}/checkout_sessions/{id}
@@ -77,7 +77,7 @@ resources:
7777
Creates links to the customer portal for the user to manage their payment & subscription details.
7878
properties:
7979
location: ${LOCATION}
80-
runtime: nodejs10
80+
runtime: nodejs14
8181
httpsTrigger: {}
8282

8383
- name: handleWebhookEvents
@@ -86,7 +86,7 @@ resources:
8686
Handles Stripe webhook events to keep subscription statuses in sync and update custom claims.
8787
properties:
8888
location: ${LOCATION}
89-
runtime: nodejs10
89+
runtime: nodejs14
9090
httpsTrigger: {}
9191

9292
- name: onUserDeleted
@@ -95,7 +95,7 @@ resources:
9595
Deletes the Stripe customer object and cancels all their subscriptions when the user is deleted in Firebase Authentication.
9696
properties:
9797
location: ${LOCATION}
98-
runtime: nodejs10
98+
runtime: nodejs14
9999
eventTrigger:
100100
eventType: providers/firebase.auth/eventTypes/user.delete
101101
resource: projects/${PROJECT_ID}
@@ -106,7 +106,7 @@ resources:
106106
Deletes the Stripe customer object and cancels all their subscriptions when the customer doc in Cloud Firestore is deleted.
107107
properties:
108108
location: ${LOCATION}
109-
runtime: nodejs10
109+
runtime: nodejs14
110110
eventTrigger:
111111
eventType: providers/cloud.firestore/eventTypes/document.delete
112112
resource: projects/${PROJECT_ID}/databases/(default)/documents/${CUSTOMERS_COLLECTION}/{uid}
@@ -246,3 +246,52 @@ params:
246246
and configuring this value.
247247
example: whsec_1234567890
248248
required: false
249+
events:
250+
- type: com.stripe.v1.product.created
251+
description: Occurs whenever a product is created.
252+
- type: com.stripe.v1.product.updated
253+
description: Occurs whenever a product is updated.
254+
- type: com.stripe.v1.product.deleted
255+
description: Occurs whenever a product is deleted.
256+
- type: com.stripe.v1.price.created
257+
description: Occurs whenever a price is created.
258+
- type: com.stripe.v1.price.updated
259+
description: Occurs whenever a price is updated.
260+
- type: com.stripe.v1.price.deleted
261+
description: Occurs whenever a price is deleted.
262+
- type: com.stripe.v1.checkout.session.completed
263+
description: Occurs when a Checkout Session has been successfully completed.
264+
- type: com.stripe.v1.checkout.session.async_payment_succeeded
265+
description: Occurs when a payment intent using a delayed payment method finally succeeds.
266+
- type: com.stripe.v1.checkout.session.async_payment_failed
267+
description: Occurs when a payment intent using a delayed payment method fails.
268+
- type: com.stripe.v1.customer.subscription.created
269+
description: Occurs whenever a customer is signed up for a new plan.
270+
- type: com.stripe.v1.customer.subscription.updated
271+
description: Occurs whenever a subscription changes (e.g., switching from one plan to another, or changing the status from trial to active).
272+
- type: com.stripe.v1.customer.subscription.deleted
273+
description: Occurs whenever a customer's subscription ends.
274+
- type: com.stripe.v1.tax_rate.created
275+
description: Occurs whenever a new tax rate is created.
276+
- type: com.stripe.v1.tax_rate.updated
277+
description: Occurs whenever a tax rate is updated.
278+
- type: com.stripe.v1.invoice.paid
279+
description: Occurs whenever an invoice payment attempt succeeds or an invoice is marked as paid out-of-band.
280+
- type: com.stripe.v1.invoice.payment_succeeded
281+
description: Occurs whenever an invoice payment attempt succeeds.
282+
- type: com.stripe.v1.invoice.payment_failed
283+
description: Occurs whenever an invoice payment attempt fails, due either to a declined payment or to the lack of a stored payment method.
284+
- type: com.stripe.v1.invoice.upcoming
285+
description: Occurs X number of days before a subscription is scheduled to create an invoice that is automatically charged—where X is determined by your subscriptions settings.
286+
- type: com.stripe.v1.invoice.marked_uncollectible
287+
description: Occurs whenever an invoice is marked uncollectible.
288+
- type: com.stripe.v1.invoice.payment_action_required
289+
description: Occurs whenever an invoice payment attempt requires further user action to complete.
290+
- type: com.stripe.v1.payment_intent.processing
291+
description: Occurs when a PaymentIntent has started processing.
292+
- type: com.stripe.v1.payment_intent.succeeded
293+
description: Occurs when a PaymentIntent has successfully completed payment.
294+
- type: com.stripe.v1.payment_intent.canceled
295+
description: Occurs when a PaymentIntent is canceled.
296+
- type: com.stripe.v1.payment_intent.payment_failed
297+
description: Occurs when a PaymentIntent has failed the attempt to create a payment method or a payment.

firestore-stripe-payments/functions/jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ module.exports = {
2020
'!**/test-data/**',
2121
],
2222
setupFiles: ['<rootDir>/__tests__/jest.setup.ts'],
23+
moduleNameMapper: {
24+
'firebase-admin/eventarc':
25+
'<rootDir>/node_modules/firebase-admin/lib/eventarc/index.js',
26+
},
2327
};

firestore-stripe-payments/functions/package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"name": "firestore-stripe-payments",
3+
"engines": {
4+
"node": "14"
5+
},
36
"main": "lib/index.js",
47
"scripts": {
58
"prepare": "npm run build",
@@ -18,29 +21,30 @@
1821
"author": "Stripe (https://stripe.com/)",
1922
"license": "Apache-2.0",
2023
"dependencies": {
21-
"firebase-admin": "^9.9.0",
22-
"firebase-functions": "^3.19.0",
23-
"stripe": "8.191.0"
24+
"firebase-admin": "^10.2.0",
25+
"firebase-functions": "^3.20.1",
26+
"rimraf": "^3.0.2",
27+
"stripe": "8.191.0",
28+
"typescript": "^3.9.9",
29+
"@types/jest": "^24.9.1"
2430
},
2531
"devDependencies": {
2632
"@faker-js/faker": "^6.0.0",
2733
"@types/chai": "^4.1.6",
28-
"@types/express": "^4.17.11",
29-
"@types/jest": "^24.9.1",
34+
"@types/express": "^4.17.13",
3035
"concurrently": "^7.0.0",
3136
"dotenv": "^16.0.0",
3237
"envfile": "^6.17.0",
3338
"firebase-functions-test": "^0.3.3",
34-
"jest": "^24.9.0",
3539
"mocked-env": "^1.3.5",
3640
"ngrok": "^4.3.1",
3741
"ts-jest": "^24.1.0",
3842
"ts-node": "^10.7.0",
39-
"typescript": "^3.9.9"
43+
"jest": "^24.9.0"
4044
},
4145
"prettier": {
4246
"singleQuote": true,
4347
"printWidth": 80
4448
},
4549
"private": true
46-
}
50+
}

firestore-stripe-payments/functions/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as admin from 'firebase-admin';
18+
import { getEventarc } from 'firebase-admin/eventarc';
1819
import * as functions from 'firebase-functions';
1920
import Stripe from 'stripe';
2021
import {
@@ -40,6 +41,12 @@ const stripe = new Stripe(config.stripeSecretKey, {
4041

4142
admin.initializeApp();
4243

44+
const eventChannel =
45+
process.env.EVENTARC_CHANNEL &&
46+
getEventarc().channel(process.env.EVENTARC_CHANNEL, {
47+
allowedEventTypes: process.env.EXT_SELECTED_EVENTS,
48+
});
49+
4350
/**
4451
* Create a customer object in Stripe when a user is created.
4552
*/
@@ -777,6 +784,12 @@ export const handleWebhookEvents = functions.handler.https.onRequest(
777784
event.type
778785
);
779786
}
787+
788+
await eventChannel?.publish({
789+
type: `com.stripe.v1.${event.type}`,
790+
data: event.data.object,
791+
});
792+
780793
logs.webhookHandlerSucceeded(event.id, event.type);
781794
} catch (error) {
782795
logs.webhookHandlerError(error, event.id, event.type);

firestore-stripe-payments/functions/tsconfig.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
"outDir": "lib",
88
"sourceMap": true,
99
"target": "es2017",
10-
"esModuleInterop": true,
11-
"moduleResolution": "Node"
10+
"esModuleInterop": true
1211
},
1312
"compileOnSave": true,
14-
"include": ["src", "__tests__"],
15-
"exclude": ["__tests__"]
13+
"include": ["src"],
14+
"exclude": ["node_modules", "__tests__"]
1615
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"devDependencies": {
3131
"lerna": "^3.4.3",
3232
"prettier": "^2.4.1"
33+
},
34+
"dependencies": {
35+
"rimraf": "^3.0.2"
3336
}
3437
}

0 commit comments

Comments
 (0)