Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 8fbb40a

Browse files
committed
chore(docs): update readme
1 parent f0e02bd commit 8fbb40a

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

README.md

+71-12
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
[paddle.com](https://www.paddle.com/) payments integration for [Google Cloud Firestore](https://cloud.google.com/firestore).
2525

2626
This module provides
27-
- a middleware function
2827
- a body parser function
29-
- a component that stores payment-related information.
28+
- a middleware function to receive and store [Paddle Webhooks](https://developer.paddle.com/getting-started/ef9af9f700849-working-with-paddle-webhooks)
3029

31-
It does **not** provide a component or methods to query payment-related information.
30+
It does **not**
31+
- validate webhook content. Use and register [paddle-webhook-validator](https://github.com/discue/paddle-webhook-validator) in your application to validate webhooks before storing them.
32+
- provide a component or methods to query payment-related information.
3233

3334
The module stores payment-related information in aollection of the target application like e.g. `api-clients`, or `api-users` and expects the application to read this information anyhow for every request. Therefore, no extra costly read is required.
3435

@@ -39,7 +40,7 @@ npm install @discue/paddle-integration-firestore
3940

4041
## Usage
4142
### Webhooks Middleware
42-
Necessary to receive and store payment related hooks from [paddle.com](https://www.paddle.com/). Currently supported hooks are
43+
The middleware component is necessary to receive and store payment related hooks from [paddle.com](https://www.paddle.com/). Currently supported hooks are:
4344
- subscription_created
4445
- subscription_updated
4546
- subscription_cancelled
@@ -59,7 +60,7 @@ const port = process.env.PORT || 3456
5960

6061
const { bodyparser, middleware,Subscriptions } = require('@discue/paddle-firebase-integration')
6162
// pass the path to the collection here
62-
const subscriptions = new Subscriptions('api_clients/subscriptions')
63+
const subscriptions = new Subscriptions('api_clients')
6364

6465
// register body parser first and middleware second
6566
app.use('/_/payments', bodyparser())
@@ -69,9 +70,9 @@ module.exports = app.listen(port)
6970
```
7071

7172
### Preparing a New Subscription
72-
For the webhooks integration to work and to be able to correlate incoming hooks with the correct subscription, a placeholder needs to be created **first**. Additionally, a specific value must be passed via the `passthrough` parameter to the [Checkout API](https://developer.paddle.com/guides/ZG9jOjI1MzU0MDQz-pass-parameters-to-the-checkout). This value will be returned by the `addSubscriptionPlaceholder` method.
73+
For the webhooks integration to work and to be able to correlate incoming hooks with the correct subscription, a placeholder needs to be created **before the checkout** and - afterward - a specific value must be passed to the [Checkout API](https://developer.paddle.com/guides/ZG9jOjI1MzU0MDQz-pass-parameters-to-the-checkout) via the `passthrough` parameter. This value will be returned by the `addSubscriptionPlaceholder` method.
7374

74-
To create a subscription placeholder, you need to pass the id of the target parent document. The placeholder will be created and the method will return the required `passthrough` value.
75+
You can see in the example below, the Subscriptions constructor is called with the name of the target `collection` and the id of the target document. The id could be your `user` or `api_client` id. Remember: the target document must exist before creating the placeholder.
7576

7677
```js
7778
'use strict'
@@ -83,7 +84,7 @@ const { Subscriptions } = require('@discue/paddle-firebase-integration')
8384
const subscriptions = new Subscriptions('api_clients')
8485

8586
module.exports = async (req, res, next) => {
86-
// require application to read api_client information
87+
// requires application to read api_client information
8788
// based on incoming information like a JWT or a cookie
8889
const apiClient = readApiClient(req)
8990
// get id of target client
@@ -97,7 +98,7 @@ module.exports = async (req, res, next) => {
9798
```
9899

99100
### Checking Subscription Status
100-
Expects the parent application to read the actual subscription from the database. The subscription object can then be passed without modification to the `isSubscriptionActive` method.
101+
Will return the status for all subscriptions associated with the given user/api_client.
101102

102103
```js
103104
'use strict'
@@ -106,14 +107,16 @@ const { Subscriptions } = require('@discue/paddle-firebase-integration')
106107
// pass the path to the collection here
107108
const subscriptions = new Subscriptions('api_clients')
108109

110+
const PREMIUM_SUBSCRIPTION_PLAN_ID = '123'
111+
109112
module.exports = (req,res,next) => {
110-
// require application to read api_client information
113+
// requires application to read api_client information
111114
// based on incoming information like a JWT or a cookie
112115
const apiClient = readApiClient(req)
113116
const { subscription } = apiClient
114117

115-
const isActive = await subscriptions.isSubscriptionActive(subscription)
116-
if (!isActive) {
118+
const status = await subscriptions.getAllSubscriptionsStatus(subscription)
119+
if (!status[PREMIUM_SUBSCRIPTION_PLAN_ID]) {
117120
// subscription is not active anymore or never was
118121
res.status(422).send('Subscription needed!')
119122
} else {
@@ -123,6 +126,62 @@ module.exports = (req,res,next) => {
123126
}
124127
```
125128

129+
### Get list of payment events
130+
Returns list of payments for for all subscriptions associated with the given user/api_client.
131+
132+
```js
133+
'use strict'
134+
135+
const { Subscriptions } = require('@discue/paddle-firebase-integration')
136+
// pass the path to the collection here
137+
const subscriptions = new Subscriptions('api_clients')
138+
139+
const PREMIUM_SUBSCRIPTION_PLAN_ID = '123'
140+
141+
module.exports = (req,res,next) => {
142+
// requires application to read api_client information
143+
// based on incoming information like a JWT or a cookie
144+
const apiClient = readApiClient(req)
145+
const { subscription } = apiClient
146+
147+
const payments = await subscriptions.getPaymentsTrail(subscription)
148+
// payments = {
149+
// "123": [
150+
// { event_time: "2021-08-08 11:49:59", type: subscription_payment_failed", ...},
151+
// { event_time: "2021-08-09 11:49:59", type: subscription_payment_succeeded", ...},
152+
// ]
153+
// }
154+
}
155+
```
156+
157+
### Get list of subscription update events
158+
Returns list of payments for for all subscriptions associated with the given user/api_client.
159+
160+
```js
161+
'use strict'
162+
163+
const { Subscriptions } = require('@discue/paddle-firebase-integration')
164+
// pass the path to the collection here
165+
const subscriptions = new Subscriptions('api_clients')
166+
167+
const PREMIUM_SUBSCRIPTION_PLAN_ID = '123'
168+
169+
module.exports = (req,res,next) => {
170+
// requires application to read api_client information
171+
// based on incoming information like a JWT or a cookie
172+
const apiClient = readApiClient(req)
173+
const { subscription } = apiClient
174+
175+
const status = await subscriptions.getStatusTrail(subscription)
176+
// status = {
177+
// "123": [
178+
// { start_at: "2021-08-08 11:49:59", type: "subscription_created", ... },
179+
// { start_at: "2021-08-09 11:49:59", type: "subscription_cancelled", ... },
180+
// ]
181+
// }
182+
}
183+
```
184+
126185
## Run Tests
127186

128187
To run tests, run the following command

0 commit comments

Comments
 (0)