Skip to content

Commit 4c5909e

Browse files
committed
feat(api): add get subscription payments method
1 parent 7d4bdb2 commit 4c5909e

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README_SUBSCRIPTION_API.md

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Low level wrapper for Paddle API.
1111
- [List all subscriptions](#list-all-subscriptions)
1212
- [Get subscription plan](#get-subscription-plan)
1313
- [List all subscription plans](#list-all-subscription-plans)
14+
- [List all subscription payments](#list-all-subscription-payments)
1415
- [Update subscription plan](#update-subscription-plan)
1516
- [Update subscription post code](#update-subscription-post-code)
1617
- [Cancel subscription](#cancel-subscription)
@@ -211,6 +212,25 @@ const plans = await paddleApi.listPlans()
211212

212213
[API reference](https://developer.paddle.com/api-reference/a835554495295-list-plans)
213214

215+
### List all subscription payments
216+
```js
217+
const plans = await paddleApi.getSubscriptionPayments({ subscription_id: 12345 }, { plan: 4815, from: '2022-04-01', to: '2023-01-01' })
218+
// [
219+
// {
220+
// "id": 8936,
221+
// "subscription_id": 2746,
222+
// "amount": 1,
223+
// "currency": "USD",
224+
// "payout_date": "2015-10-15",
225+
// "is_paid": 0,
226+
// "is_one_off_charge": false,
227+
// "receipt_url": "https://my.paddle.com/receipt/469214-8936/1940881-chrea0eb34164b5-f0d6553bdf"
228+
// }
229+
// ]
230+
```
231+
232+
[API reference](https://developer.paddle.com/api-reference/80462f27b2011-list-payments)
233+
214234

215235
### Update subscription plan
216236
```js

lib/paddle/api.js

+45
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,51 @@ module.exports = class {
137137
})
138138
}
139139

140+
/**
141+
* @typedef GetSubscriptionPaymentsOptions
142+
* @property {string} [plan=null] subscription plan id
143+
* @property {string} [from=null] filter payments starting after the date specified (date in format YYYY-MM-DD)
144+
* @property {string} [to=null] filter payments ending the day before the date specified (date in format YYYY-MM-DD)
145+
*/
146+
147+
/**
148+
* @typedef SubscriptionPayment
149+
* @property {string} id
150+
* @property {string} subscription_id
151+
* @property {string} amount
152+
* @property {string} currency
153+
* @property {string} payout_date format YYYY-MM-DD
154+
* @property {number} is_paid 1 = true, 0 = false
155+
* @property {boolean} is_one_off_charge
156+
* @property {string} receipt_url
157+
*/
158+
159+
/**
160+
* @param {Object} subscription target subscription object
161+
* @param {GetSubscriptionPaymentsOptions} [subscription=undefined] the target subscription object
162+
* @returns {Array.<SubscriptionPayment>}
163+
*/
164+
async getSubscriptionPayments({ subscription_id }, { plan, from, to } = {}) {
165+
const form = {
166+
vendor_id: this._vendorId,
167+
vendor_auth_code: this._authCode,
168+
subscription_id
169+
}
170+
171+
if (plan) {
172+
form.plan = plan
173+
}
174+
if (from) {
175+
form.from = from
176+
}
177+
if (to) {
178+
form.to = to
179+
}
180+
return this._returnResponseIf200(this._request(this._vendorsBaseUrl + PATH_LIST_PAYMENTS, {
181+
form
182+
}))
183+
}
184+
140185
/**
141186
*
142187
* @param {Object} checkout the target checkout object

test-e2e/spec/api.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
const { test, expect } = require('@playwright/test')
44

5+
/**
6+
* @type {import('../../lib/paddle/api.js')}
7+
*/
58
let api
69

710
test.beforeAll(async () => {
@@ -29,6 +32,31 @@ test('list all plans', async () => {
2932
expect(subs.length).toBeGreaterThanOrEqual(2)
3033
})
3134

35+
test('list all subscription payments', async () => {
36+
const payments = await api.getSubscriptionPayments({ subscription_id: '399236' })
37+
expect(payments).toHaveLength(1)
38+
})
39+
40+
test('filters subscription payments by plan (correct plan)', async () => {
41+
const payments = await api.getSubscriptionPayments({ subscription_id: '399236' }, { plan: '33590' })
42+
expect(payments).toHaveLength(1)
43+
})
44+
45+
test('filters subscription payments starting after', async () => {
46+
const payments = await api.getSubscriptionPayments({ subscription_id: '399236' }, { from: '2030-01-01' })
47+
expect(payments).toHaveLength(0)
48+
})
49+
50+
test('filters subscription payments starting before 2030', async () => {
51+
const payments = await api.getSubscriptionPayments({ subscription_id: '399236' }, { to: '2030-01-01' })
52+
expect(payments).toHaveLength(1)
53+
})
54+
55+
test('filters subscription payments starting before 2020', async () => {
56+
const payments = await api.getSubscriptionPayments({ subscription_id: '399236' }, { to: '2020-01-01' })
57+
expect(payments).toHaveLength(0)
58+
})
59+
3260
test('get plan by id', async () => {
3361
const subs = await api.getPlan({ plan_id: 33590 })
3462
expect(subs.length).toEqual(1)

0 commit comments

Comments
 (0)