/
cancel-subscription.js
68 lines (45 loc) · 1.85 KB
/
cancel-subscription.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module.exports = {
friendlyName: 'Cancel Subscription',
description: 'Cancel an existing subscription attached to a customer.',
sideEffects: 'idempotent',
inputs: {
apiKey: require('../constants/apiKey.input'),
subscription: {
friendlyName: 'Subscription ID',
description: 'The subscription ID of the specific customers plan you wish to cancel.',
example: 'sub_someSubIdjsd2isnsd',
required: true
},
instant: {
friendlyName: 'Cancel immediately?',
description: 'Whether the subscription be cancelled instantly, or at the end of the subscription period. Defaults to `true`.',
example: true,
defaultsTo: true
}
},
exits: {
success: {
outputFriendlyName: 'Cancelled Stripe subscription',
outputDescription: 'Details of the Stripe subscription that was cancelled.',
outputExample: require('../constants/subscription.schema')
}
},
fn: function (inputs, exits) {
// Import `stripe`, and initialize it with the given API key.
// (Or fall back to the cached API key, if available)
var stripe = require('stripe')(inputs.apiKey||require('./private/cache').apiKey);
// Declare a var to hold the `instant` option.
var options = {
at_period_end: !inputs.instant// eslint-disable-line camelcase
};
// Make the API call via the Stripe module.
stripe.subscriptions.del(inputs.subscription, options, function(err, cancelledSubscription) {
// Send any errors through the `error` exit.
// TODO: handle more specific exits (i.e. rate limit, customer does not
// exist, etc.), possibly via a separate `negotiateError` machine.
if (err) {return exits.error(err);}
// Send information about the cancelled subscription through the `success` exit.
return exits.success(cancelledSubscription);
});
}
};