Skip to content

Commit

Permalink
feat(jobs): add cancel-stripe-subscription job
Browse files Browse the repository at this point in the history
Realtin committed Oct 2, 2017
1 parent dd94125 commit 1674c5a
Showing 2 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions jobs/cancel-stripe-subscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const env = require('../lib/env')
const stripe = require('stripe')(env.STRIPE_SECRET_KEY)
const dbs = require('../lib/dbs')
const upsert = require('../lib/upsert')

module.exports = async function ({ accountId, stripeSubscriptionId }) {
const { payments } = await dbs()
await stripe.subscriptions.del(
stripeSubscriptionId,
async (err, confirmation) => {
if (err) {
throw err
}
await upsert(payments, accountId, {
stripeSubscriptionId: null
})
}
)
}
44 changes: 44 additions & 0 deletions test/jobs/cancel-stripe-subscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { test, tearDown } = require('tap')
const nock = require('nock')
const worker = require('../../jobs/cancel-stripe-subscription')

const dbs = require('../../lib/dbs')

nock.disableNetConnect()
nock.enableNetConnect('localhost')

test('Cancel Stripe Subscription', async t => {
const { payments } = await dbs()
t.plan(2)

nock('https://api.stripe.com/v1')
.delete('/subscriptions/345')
.reply(200, () => {
t.pass('Stripe called')
return {
stripeSubscriptionId: '345'
}
})

await payments.put({
_id: '123',
plan: 'team',
stripeCustomerId: 'cus_abc',
stripeItemId: 'si_xyz',
stripeSubscriptionId: '345'
})

await worker({
accountId: '123',
stripeSubscriptionId: '345'
})

const payment = await payments.get('123')
t.is(payment.stripeSubscriptionId, null)
t.end()
})

tearDown(async () => {
const { payments } = await dbs()
await payments.remove(await payments.get('123'))
})

0 comments on commit 1674c5a

Please sign in to comment.