-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestSubscriptions.js
315 lines (290 loc) · 9.39 KB
/
testSubscriptions.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
const Subscription = require("../lib/subscription");
const ApiCore = require("../lib/api-core");
const ApiAuth = require("../lib/api-auth");
const Twocheckout = require("../twocheckout");
const config = {
sellerId: "",
secretKey: "",
secretWord: "",
jwtExpireTime: 20, //minutes
};
let lastRefNo = null;
const paymentOrderWithSubscriptionSuccessPayload = {
Country: "us",
Currency: "USD",
CustomerIP: "91.220.121.21",
ExternalReference: "REST_API_AVANGTE",
Language: "en",
Source: "testAPI.com",
BillingDetails: {
Address1: "Test Address",
City: "LA",
State: "California",
CountryCode: "US",
Email: "[email protected]",
FirstName: "Customer",
LastName: "2Checkout",
Zip: "12345",
},
Items: [
{
Name: "Dynamic product",
Description: "Test description",
Quantity: 1,
IsDynamic: true,
Tangible: false,
PurchaseType: "PRODUCT",
CrossSell: {
CampaignCode: "CAMPAIGN_CODE",
ParentCode: "MASTER_PRODUCT_CODE",
},
Price: {
Amount: 2,
Type: "CUSTOM",
},
// PriceOptions: [
// {
// Name: "OPT1",
// Options: [
// {
// Name: "Name LR",
// Value: "Value LR",
// Surcharge: 7,
// },
// ],
// },
// ],
RecurringOptions: {
CycleLength: 1,
CycleUnit: "MONTH",
CycleAmount: 3,
ContractLength: 3,
ContractUnit: "Year",
},
},
],
PaymentDetails: {
Type: "TEST",
Currency: "USD",
CustomerIP: "91.220.121.21",
PaymentMethod: {
CardNumber: "4111111111111111",
CardType: "VISA",
Vendor3DSReturnURL: "www.success.com",
Vendor3DSCancelURL: "www.fail.com",
ExpirationYear: "2024",
ExpirationMonth: "12",
CCID: "123",
HolderName: "John Doe",
RecurringEnabled: true,
HolderNameTime: 1,
CardNumberTime: 1,
},
},
};
let orderCatalogProductParams = {
Country: "us",
Currency: "USD",
CustomerIP: "91.220.121.21",
ExternalReference: "REST_API_AVANGTE",
Language: "en",
Source: "testAPI.com",
BillingDetails: {
Address1: "Test Address",
City: "LA",
State: "California",
CountryCode: "US",
Email: "[email protected]",
FirstName: "Customer",
LastName: "2Checkout",
Zip: "12345",
},
Items: [
{
Code: "E377076E6A_COPY1",
Quantity: "1",
RecurringOptions: {
CycleLength: 2,
CycleUnit: "DAY",
CycleAmount: 12.2,
ContractLength: 3,
ContractUnit: "DAY",
}
}
],
PaymentDetails: {
Type: "TEST",
Currency: "USD",
CustomerIP: "91.220.121.21",
PaymentMethod: {
CardNumber: "4111111111111111",
CardType: "VISA",
Vendor3DSReturnURL: "www.success.com",
Vendor3DSCancelURL: "www.fail.com",
ExpirationYear: "2024",
ExpirationMonth: "12",
CCID: "123",
HolderName: "John Doe",
RecurringEnabled: true,
HolderNameTime: 1,
CardNumberTime: 1,
},
},
}
const tco = new Twocheckout(config);
const apiAuth = new ApiAuth(config);
const apiCore = new ApiCore(apiAuth);
const subscription = new Subscription(apiCore);
////TEST 1 - Get subscriptions by payment order RefNo;
//With callback
tco.order().create(paymentOrderWithSubscriptionSuccessPayload, (err, res) => {
if (err) {
console.error("Error getting subscriptions: Error Message: %s", JSON.stringify(err, null, 2));
} else if (res) {
console.log("Created order with RefNo: " + res.RefNo);
lastRefNo = res.RefNo;
setTimeout(() => {
console.log("Get subscription");
subscription.getSubscriptionsByOrderRefNo(lastRefNo, (err, res) => {
console.log("Subscription response:");
if (err !== null) {
console.log(err);
}
if (res !== null) {
console.log(JSON.stringify(res, null, 2));
}
});
}, 2000);
}
});
//Place payment order with Catalog Product. You need a 2Checkout by Verifone CPanel vendor account
//then just change the product code in orderCatalogProductParams
(async () => {
try {
let result = await tco.order().create(orderCatalogProductParams);
console.error(result);
console.log("Created order with async/await RefNo: %s", result.RefNo);
setTimeout(async() => {
let subscriptions = await subscription.getSubscriptionsByOrderRefNo(result.RefNo);
console.log("Retrieved subscriptions: %s", JSON.stringify(subscriptions));
}, 2000);
} catch (error) {
console.error(error);
}
})();
///With Dynamic Products
// await/async
(async () => {
try {
let result = await tco.order().create(paymentOrderWithSubscriptionSuccessPayload);
console.log("Created order with async/await RefNo: %s", result.RefNo);
setTimeout(async() => {
let subscriptions = await subscription.getSubscriptionsByOrderRefNo(result.RefNo);
console.log("Retrieved subscriptions: %s", JSON.stringify(subscriptions));
}, 2000);
let subscriptions = await subscription.getSubscriptionsByOrderRefNo(result.RefNo);
console.log("Retrieved subscriptions: %s", JSON.stringify(subscriptions));
} catch (error) {
console.error(error);
}
})();
//With promise
tco.order()
.create(paymentOrderWithSubscriptionSuccessPayload)
.then((result) => {
lastRefNo = result.RefNo;
console.log("Created Payment with promise with RefNo %s.", lastRefNo);
setTimeout(() => {
subscription
.getSubscriptionsByOrderRefNo(lastRefNo)
.then((subscriptions) => {
console.log(subscriptions);
})
.catch((e) => {
console.error("Error getting subscriptions: Error Message: %s", JSON.stringify(e, null, 2));
});
}, 2000);
});
//TEST 2 - GET subscription by SubscriptionReference ID
let refno = "151933178"; //lastRefNo;
let subscriptionReferenceId = "YA5745Z23V";
//the RefNo now is the subscription ID
const searchArgs = {
path: "/subscriptions",
subRefId: subscriptionReferenceId,
};
////With callback
tco.subscription().retrieve(searchArgs, (err, res) => {
if (err !== null) {
console.log(err);
}
if (res !== null) {
console.log(res);
}
});
////Async /Await
(async () => {
try {
const response = await tco.subscription().retrieve(searchArgs);
if ("SubscriptionReference" in response) {
console.log("Subscription params: %s", JSON.stringify(response, null, 2));
}
} catch (e) {
console.error("Exception retrieving subscription. Error Message: %s", JSON.stringify(e, null, 2));
}
})();
////TEST 3 - Update subscription
const updateArgs = {
subRefId: subscriptionReferenceId,
subParams: { RecurringEnabled: true, SubscriptionEnabled: true },
};
(async () => {
try {
const subscription = await tco.subscription().retrieve(searchArgs);
updateArgs.subRefId = subscription.SubscriptionReference;
updateArgs.subParams.ExpirationDate = "2025-12-30";
const updateResponse = await tco.subscription().update(updateArgs);
console.log(updateResponse);
if (updateResponse === true) {
console.log("Subscription updated with success!");
} else {
console.error("Subscription update failed!");
}
} catch (e) {
console.error("Exception updating subscription. Error Message: %s", JSON.stringify(e, null, 2));
}
})();
////Test 4 - Disable subscription
const disableArgs = {
subRefId: subscriptionReferenceId,
};
(async () => {
try {
const disableSubscription = await tco.subscription().disable(searchArgs);
console.log(disableSubscription);
if (disableSubscription === true) {
console.log("Subscription disabled with success!");
} else {
console.error("Subscription disable failed!");
}
} catch (e) {
console.error("Exception disabling subscription. Error Message: %s", JSON.stringify(e, null, 2));
}
})();
//Test 5 - Enable subscription
const disableArgs = {
subRefId: subscriptionReferenceId,
};
(async () => {
try {
const enabledSubscription = await tco.subscription().enable(searchArgs);
console.log(enabledSubscription);
if (enabledSubscription === true) {
console.log("Subscription enabled with success!");
} else {
console.error("Subscription enable failed!");
}
} catch (e) {
console.error("Exception enabling subscription. Error Message: %s", JSON.stringify(e, null, 2));
}
})();