Skip to content

Commit

Permalink
feat: add method to compile payments list
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Sep 9, 2022
1 parent 0b86cae commit 8b90029
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
73 changes: 73 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,79 @@ class Subscriptions {
return { start, end }
}

/**
*
* @param {Object} subscription
* @returns {StartAndEndDate} containing the start and end date
*/
async getPaymentsTrail(subscription) {
const payments = subscription.payments
.sort((a, b) => new Date(a.event_time).getTime() - new Date(b.event_time).getTime())

return payments.map(payment => {
if (payment.alert_name === 'subscription_payment_failed') {
return {
event_time: payment.event_time,
description: payment.alert_name,
amount: {
currency: payment.currency,
total: payment.amount,
quantity: payment.quantity,
unit_price: payment.unit_price,
},
next_try: {
date: payment.next_retry_date
},
instalments: payment.instalments,
subscription_plan_id: payment.subscription_plan_id,
}
} else if (payment.alert_name === 'subscription_payment_refunded') {
return {
event_time: payment.event_time,
description: payment.alert_name,
amount: {
currency: payment.currency,
total: payment.gross_refund,
quantity: payment.quantity,
tax: payment.tax_refund,
unit_price: payment.unit_price
},
refund: {
reason: payment.refund_reason,
type: payment.refund_type
},
instalments: payment.instalments,
subscription_plan_id: payment.subscription_plan_id,
}
} else if (payment.alert_name === 'subscription_payment_succeeded') {
return {
event_time: payment.event_time,
description: payment.alert_name,
amount: {
currency: payment.currency,
total: payment.sale_gross,
quantity: payment.quantity,
tax: payment.payment_tax,
unit_price: payment.unit_price,
method: payment.payment_method,
},
next_payment: {
date: payment.next_bill_date,
amount: {
currency: payment.currency,
total: payment.next_payment_amount
}
},
receipt_url: payment.receipt_url,
instalments: payment.instalments,
subscription_plan_id: payment.subscription_plan_id,
}
} else {
console.error(`Ignoring payment of unknown type ${payment.alert_name}`)
}
})
}

/**
*
* @param {Array} subscriptions
Expand Down
74 changes: 74 additions & 0 deletions test/spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,78 @@ describe('PaddleIntegration', () => {
expect(end).to.equal(endTimeString)
})
})

describe('.getPaymentsTrail', () => {
beforeEach(async () => {
const subscriptionId = uuid()
const createPayload = Object.assign({}, subscriptionCreated, {
event_time: '2028-08-08 10:47:47',
subscription_id: subscriptionId,
passthrough: JSON.stringify({ ids }),
})
await paddleIntegration.addSubscriptionCreatedStatus(createPayload)

const paymentSuccesfulPayload = Object.assign({}, paymentSucceded, {
event_time: '2027-08-08 10:47:47',
subscription_id: subscriptionId,
passthrough: JSON.stringify({ ids }),
})
await paddleIntegration.addSuccessfulPayment(paymentSuccesfulPayload);

const paymentRefundedPayload = Object.assign({}, paymentRefunded, {
event_time: '2024-08-08 10:47:47',
subscription_id: subscriptionId,
passthrough: JSON.stringify({ ids }),
})
await paddleIntegration.addRefundedPayment(paymentRefundedPayload)

const paymentFailedPayload = Object.assign({}, paymentFailed, {
event_time: '2022-08-08 10:47:47',
subscription_id: subscriptionId,
passthrough: JSON.stringify({ ids }),
})
await paddleIntegration.addFailedPayment(paymentFailedPayload)
})
it('returns a sorted listed of payments', async () => {
const { subscription: sub } = await storage.get(ids)
const trail = await paddleIntegration.getPaymentsTrail(sub)

expect(trail).to.have.length(3)
expect(trail[0].description).to.equal('subscription_payment_failed')
expect(trail[0].amount.currency).to.equal(paymentFailed.currency)
expect(trail[0].amount.total).to.equal(paymentFailed.amount)
expect(trail[0].amount.quantity).to.equal(paymentFailed.quantity)
expect(trail[0].amount.unit_price).to.equal(paymentFailed.unit_price)
expect(trail[0].next_try.date).to.equal(paymentFailed.next_retry_date)
expect(trail[0].instalments).to.equal(paymentFailed.instalments)
expect(trail[0].subscription_plan_id).to.equal(paymentFailed.subscription_plan_id)


expect(trail[1].description).to.equal('subscription_payment_refunded')
expect(trail[1].amount.currency).to.equal(paymentRefunded.currency)
expect(trail[1].amount.total).to.equal(paymentRefunded.gross_refund)
expect(trail[1].amount.quantity).to.equal(paymentRefunded.quantity)
expect(trail[1].amount.tax).to.equal(paymentRefunded.tax_refund)
expect(trail[1].amount.unit_price).to.equal(paymentRefunded.unit_price)
expect(trail[1].refund.reason).to.equal(paymentRefunded.refund_reason)
expect(trail[1].refund.type).to.equal(paymentRefunded.refund_type)
expect(trail[1].instalments).to.equal(paymentRefunded.instalments)
expect(trail[1].subscription_plan_id).to.equal(paymentRefunded.subscription_plan_id)


expect(trail[2].description).to.equal('subscription_payment_succeeded')
expect(trail[2].amount.currency).to.equal(paymentSucceded.currency)
expect(trail[2].amount.total).to.equal(paymentSucceded.sale_gross)
expect(trail[2].amount.quantity).to.equal(paymentSucceded.quantity)
expect(trail[2].amount.tax).to.equal(paymentSucceded.payment_tax)
expect(trail[2].amount.unit_price).to.equal(paymentSucceded.unit_price)
expect(trail[2].amount.method).to.equal(paymentSucceded.payment_method)
expect(trail[2].next_payment.date).to.equal(paymentSucceded.next_bill_date)
expect(trail[2].next_payment.amount.currency).to.equal(paymentSucceded.currency)
expect(trail[2].next_payment.amount.total).to.equal(paymentSucceded.next_payment_amount)
expect(trail[2].receipt_url).to.equal(paymentSucceded.receipt_url)
expect(trail[2].instalments).to.equal(paymentSucceded.instalments)
expect(trail[2].subscription_plan_id).to.equal(paymentSucceded.subscription_plan_id)
})
})
})

0 comments on commit 8b90029

Please sign in to comment.