forked from xendit/xendit-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurring.js
More file actions
155 lines (129 loc) · 3.86 KB
/
Copy pathrecurring.js
File metadata and controls
155 lines (129 loc) · 3.86 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* eslint-disable no-console */
const x = require('../xendit');
const Recurring = x.Recurring;
const r = new Recurring({});
/**
* Before running this example consider the following
* 1. make sure to use a valid exampleCustomerId and examplePaymentMethodId
* 2. After plan has been deactivated - it's scheduled cycles become cancelled
* which makes impossible to be edit or cancel them
*/
const exampleBusinessId = '6066ebf68204c740b61aa3c1';
const exampleReferenceId = 'cf53cdbb-f92d-4220-835d-b907915d9551';
const exampleCustomerId = 'ba0e4584-4fc4-4cb2-a835-6191500540ef';
const examplePaymentMethodId = 'pm-fb182048-7448-4ffe-877a-a29efa3d7195';
const scheduledTimestamp = new Date(Date.now() + 3600 * 1000).toISOString();
let scheduleId;
let planId;
async function runSchedules() {
const createdSchedule = await r.createSchedule({
referenceId: exampleReferenceId,
businessId: exampleBusinessId,
interval: 'DAY',
intervalCount: 1,
totalRecurrence: 3,
anchorDate: '2022-01-01T00:00:00.001Z',
retryInterval: 'DAY',
retryIntervalCount: 1,
totalRetry: 1,
failedAttemptNotifications: [1],
});
scheduleId = createdSchedule.id;
console.log('created recurring schedule:', createdSchedule);
const schedule = await r.getSchedule({
id: createdSchedule.id,
businessId: exampleBusinessId,
});
console.log('recurring schedule:', schedule);
const editedSchedule = await r.editSchedule({
id: schedule.id,
businessId: exampleBusinessId,
interval: 'MONTH',
intervalCount: 1,
});
console.log('edited recurring schedule:', editedSchedule);
}
async function runPlans() {
const createdPlan = await r.createPlan({
businessId: exampleBusinessId,
referenceId: exampleReferenceId,
customerId: exampleCustomerId,
recurringAction: 'PAYMENT',
currency: 'IDR',
amount: 1000,
paymentMethods: [
{
paymentMethodId: examplePaymentMethodId,
rank: 1,
},
],
scheduleId: scheduleId,
immediateActionType: null,
notification_config: {
recurring_created: ['SMS'],
recurring_succeeded: ['SMS'],
recurring_failed: ['SMS'],
},
failedCycleAction: 'STOP',
metadata: null,
});
planId = createdPlan.id;
console.log('created recurring plan:', createdPlan);
const plan = await r.getPlan({
id: createdPlan.id,
businessId: exampleBusinessId,
});
console.log('recurring plan:', plan);
const editedPlan = await r.editPlan({
id: plan.id,
businessId: exampleBusinessId,
amount: 1000,
});
console.log('edited recurring plan:', editedPlan);
/** This is commented out to allow cycle "edit" and "cancel" actions */
// const deactivatedPlan = await r.deactivatePlan({
// id: plan.id,
// businessId: exampleBusinessId,
// });
// console.log('deactivated recurring plan:', deactivatedPlan);
}
async function runCycles() {
const response = await r.getAllCycles({
planId: planId,
businessId: exampleBusinessId,
limit: 2,
});
console.log('recurring cycles:', response.data);
console.log('has more recurring cycles:', response.has_more);
const cycle = await r.getCycle({
id: response.data[0].id,
planId: planId,
businessId: exampleBusinessId,
});
console.log('recurring cycle:', cycle);
const editedCycle = await r.editCycle({
id: cycle.id,
planId: planId,
businessId: exampleBusinessId,
amount: 1000,
currency: 'IDR',
scheduledTimestamp: scheduledTimestamp,
});
console.log('edited recurring cycle:', editedCycle);
const canceledCycle = await r.cancelCycle({
id: cycle.id,
planId: cycle.plan_id,
businessId: exampleBusinessId,
});
console.log('canceled recurring cycle:', canceledCycle);
}
(async function() {
try {
await runSchedules();
await runPlans();
await runCycles();
} catch (e) {
console.error(e);
process.exit(1);
}
})();