Skip to content

Commit ca75e16

Browse files
committed
feat: add client function to create customData objects
1 parent b465a0d commit ca75e16

File tree

7 files changed

+101
-54
lines changed

7 files changed

+101
-54
lines changed

lib/client/custom-data.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
/**
4+
* Creates a passthrough/customData object that can be passed to Paddle Checkout.
5+
* Guarantees the passthrough/customData object will be understood by
6+
* the paddle integration middleware.
7+
*
8+
* @param {Array} ids
9+
*/
10+
module.exports = (ids) => {
11+
return {
12+
_pi: { ids }
13+
}
14+
}

lib/client/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
'customData': require('./custom-data.js')
3+
}

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"files": [
99
"lib"
1010
],
11+
"exports": {
12+
"default": "./lib/index.js",
13+
"client": "./lib/client/index.js",
14+
"server": "./lib/index.js"
15+
},
1116
"scripts": {
1217
"emulators": "firebase emulators:start",
1318
"test": "mocha test/spec/**/*.spec.js --check-leaks --timeout 50000 --file test/global-mocha-setup.js",
@@ -48,4 +53,4 @@
4853
"got": "^12.5.3",
4954
"html-entities": "^2.3.3"
5055
}
51-
}
56+
}

test/spec/client/customData.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const { expect } = require('chai')
4+
const customData = require('../../../lib/client/custom-data')
5+
6+
describe('CustomData', () => {
7+
it('creates a valid customData object', () => {
8+
const result = customData([123])
9+
expect(result).to.have.keys('_pi')
10+
expect(result._pi).to.have.keys('ids')
11+
expect(result._pi.ids).to.deep.equal([123])
12+
})
13+
})

test/spec/client/index.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const { expect } = require('chai')
4+
const index = require('../../../lib/client/index')
5+
6+
describe('Client', () => {
7+
it('exports customData function', () => {
8+
expect(typeof index.customData).to.equal('function')
9+
})
10+
})

test/spec/index.spec.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const subscriptionInfo = new SubscriptionInfo('api_client', { hookStorage: paddl
1515
const storage = require('../../lib/firestore/nested-firestore-resource')({ documentPath: 'api_client', resourceName: 'api_clients' })
1616

1717
const { expect } = require('chai')
18+
const { customData } = require('../../lib/client')
1819

1920
describe('PaddleIntegration', () => {
2021

@@ -28,7 +29,7 @@ describe('PaddleIntegration', () => {
2829

2930
describe('.addSubscription', () => {
3031
it('creates an aactive subscription', async () => {
31-
const createPayload = Object.assign({}, subscriptionCreated, { passthrough: JSON.stringify({ ids }) })
32+
const createPayload = Object.assign({}, subscriptionCreated, { passthrough: JSON.stringify(customData(ids)) })
3233

3334
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
3435
const { subscription: sub } = await storage.get(ids)
@@ -38,7 +39,7 @@ describe('PaddleIntegration', () => {
3839
it('stores subscription related info', async () => {
3940
const createPayload = Object.assign({}, subscriptionCreated,
4041
{
41-
subscription_id: uuid(), passthrough: JSON.stringify({ ids })
42+
subscription_id: uuid(), passthrough: JSON.stringify(customData(ids))
4243
}
4344
)
4445
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
@@ -80,14 +81,14 @@ describe('PaddleIntegration', () => {
8081

8182
const createPayload = Object.assign({}, subscriptionCreated,
8283
{
83-
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
84+
subscription_id: subscriptionId, passthrough: JSON.stringify(customData(ids))
8485
}
8586
)
8687
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
8788

8889
const updatePayload = Object.assign({}, subscriptionUpdated,
8990
{
90-
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
91+
subscription_id: subscriptionId, passthrough: JSON.stringify(customData(ids))
9192
}
9293
)
9394
await paddleIntegration.addSubscriptionUpdatedStatus(updatePayload)
@@ -168,15 +169,15 @@ describe('PaddleIntegration', () => {
168169

169170
const createPayload = Object.assign({}, subscriptionCreated,
170171
{
171-
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
172+
subscription_id: subscriptionId, passthrough: JSON.stringify(customData(ids))
172173
}
173174
)
174175
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
175176

176177
const payload = Object.assign({}, subscriptionCancelled,
177178
{
178179
subscription_id: subscriptionId,
179-
passthrough: JSON.stringify({ ids }),
180+
passthrough: JSON.stringify(customData(ids)),
180181
cancellation_effective_date: new Date(new Date().getTime() - 1000).toISOString()
181182
}
182183
)
@@ -191,15 +192,15 @@ describe('PaddleIntegration', () => {
191192

192193
const createPayload = Object.assign({}, subscriptionCreated,
193194
{
194-
subscription_id: subscriptionId, passthrough: JSON.stringify({ ids })
195+
subscription_id: subscriptionId, passthrough: JSON.stringify(customData(ids))
195196
}
196197
)
197198
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
198199

199200
const cancelPayload = Object.assign({}, subscriptionCancelled,
200201
{
201202
subscription_id: subscriptionId,
202-
passthrough: JSON.stringify({ ids }),
203+
passthrough: JSON.stringify(customData(ids)),
203204
cancellation_effective_date: new Date(new Date().getTime() - 1000).toISOString()
204205
}
205206
)
@@ -233,7 +234,7 @@ describe('PaddleIntegration', () => {
233234
const subscriptionId = uuid()
234235
const createPayload = Object.assign({}, subscriptionCreated, {
235236
subscription_id: subscriptionId,
236-
passthrough: JSON.stringify({ ids }),
237+
passthrough: JSON.stringify(customData(ids)),
237238
})
238239
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
239240

@@ -242,7 +243,7 @@ describe('PaddleIntegration', () => {
242243

243244
const paymentPayload = Object.assign({}, paymentSucceded, {
244245
subscription_id: subscriptionId,
245-
passthrough: JSON.stringify({ ids }),
246+
passthrough: JSON.stringify(customData(ids)),
246247
})
247248
await paddleIntegration.addSuccessfulPayment(paymentPayload);
248249

@@ -259,13 +260,13 @@ describe('PaddleIntegration', () => {
259260
const subscriptionId = uuid()
260261
const createPayload = Object.assign({}, subscriptionCreated, {
261262
subscription_id: subscriptionId,
262-
passthrough: JSON.stringify({ ids }),
263+
passthrough: JSON.stringify(customData(ids)),
263264
})
264265
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
265266

266267
const paymentPayload = Object.assign({}, paymentRefunded, {
267268
subscription_id: subscriptionId,
268-
passthrough: JSON.stringify({ ids }),
269+
passthrough: JSON.stringify(customData(ids)),
269270
})
270271
await paddleIntegration.addRefundedPayment(paymentPayload)
271272

@@ -283,13 +284,13 @@ describe('PaddleIntegration', () => {
283284
const subscriptionId = uuid()
284285
const createPayload = Object.assign({}, subscriptionCreated, {
285286
subscription_id: subscriptionId,
286-
passthrough: JSON.stringify({ ids }),
287+
passthrough: JSON.stringify(customData(ids)),
287288
})
288289
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)
289290

290291
const paymentPayload = Object.assign({}, paymentFailed, {
291292
subscription_id: subscriptionId,
292-
passthrough: JSON.stringify({ ids }),
293+
passthrough: JSON.stringify(customData(ids)),
293294
})
294295
await paddleIntegration.addFailedPayment(paymentPayload)
295296

0 commit comments

Comments
 (0)