Commit a2f53081 authored by Aral Balkan's avatar Aral Balkan

Add resume patronage feature

parent d5b5198c
......@@ -81,7 +81,7 @@
const planId = '${planId}'
const itemId = '${itemId}'
const currentAmount = ${patronageAmount}
const isPaused = '${isPaused}'
const isPaused = ${isPaused}
console.log('isPaused', isPaused)
......
......@@ -23,6 +23,7 @@ window.addEventListener('load', _ => {
}
switch (response.for) {
case 'update':
$('#updateButton').value = 'Update my patronage'
$('#updateButton').disabled = false
......@@ -46,8 +47,19 @@ window.addEventListener('load', _ => {
'success'
)
break
}
case 'resume':
$('#resume-your-patronage').hidden = true
$('#resumeButton').value = 'Resume my patronage'
$('#resumeButton').disabled = false
$('#pause-your-patronage').hidden = false
Modal.fire(
'Patronage resumed',
`Your patronage has been resumed effective immediately. You can pause it again at any time. Thank you for supporting us.`,
'success'
)
break
}
}
//
......@@ -85,6 +97,16 @@ window.addEventListener('load', _ => {
$('#updateButton').disabled = true
}
if (commandName === 'pause') {
$('#pauseButton').value = 'Pausing…'
$('#pauseButton').disabled = true
}
if (commandName === 'resume') {
$('#resumeButton').value = 'Resuming…'
$('#resumeButton').disabled = true
}
let proceed = true
if (commandName === 'cancel') proceed = await confirmCancellation()
......@@ -96,6 +118,6 @@ window.addEventListener('load', _ => {
handleCommand('update')
handleCommand('pause')
handleCommand('resume')
handleCommand('cancel')
})
......@@ -63,7 +63,7 @@ module.exports = async (request, response, next) => {
const patronageAmount = subscription.quantity
const isPaused = subscription.discount !== undefined
const isPaused = subscription.discount !== null
// Newly-created?
let newPatronMessage = ''
......
......@@ -10,38 +10,64 @@ module.exports = (webSocket, request) => {
let result
switch (command.action) {
case 'update':
// Update the patronage amount, starting with the next payment (not prorated).
try {
result = await stripe.subscriptions.update(command.subscriptionId, {
prorate: false,
items: [{
id: command.itemId,
plan: command.planId,
quantity: command.amount
}]
})
result = await stripe.subscriptions.update(
command.subscriptionId,
{
prorate: false,
items: [
{
id: command.itemId,
plan: command.planId,
quantity: command.amount
}
]
}
)
} catch (error) {
webSocket.send(JSON.stringify({for: command.action, error}))
}
webSocket.send(JSON.stringify({for: command.action, amount: result.quantity}))
break;
break;
case 'pause':
// Pausing a subscription is not a native concept within the Stripe API. Instead, we implement
// one of the recommended strategies for obtaining that outcome by applying a 100% discount coupon.
try {
result = await stripe.subscriptions.update(command.subscriptionId, {
prorate: false,
coupon: 'VKQ30S0b'
})
result = await stripe.subscriptions.update(
command.subscriptionId,
{
prorate: false,
coupon: 'VKQ30S0b'
}
)
} catch (error) {
webSocket.send(JSON.stringify({for: command.action, error}))
}
webSocket.send(JSON.stringify({for: command.action, coupon: result.coupon}))
break;
break;
case 'resume':
// A paused subscription (see above) is resumed by deleting the 100% subscription discount.
try {
result = await stripe.subscriptions.deleteDiscount(command.subscriptionId)
} catch (error) {
webSocket.send(JSON.stringify({for: command.action, error}))
}
webSocket.send(JSON.stringify({for: command.action}))
break;
case 'cancel':
console.log('CANCEL')
break;
break;
default:
console.log(`Unknown command received: ${command.action}`)
const error = new Error(`Unknown command received: ${command.action}`)
console.log(error)
webSocket.send(JSON.stringify({for: command.action, error }))
}
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment