-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubscription.js
92 lines (83 loc) · 3.59 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use strict";
const TwocheckoutError = require("./error");
const Order = require("./order");
const Helper = require("./helper");
class Subscription {
constructor(apiCore) {
this.apiCore = apiCore;
this.helper = new Helper();
}
getSubscriptionFromItems(items) {
let subscriptionsRefNoArray = {};
if (items.length > 0) {
Object.keys(items).forEach((key) => {
let product = items[key];
if (
"ProductDetails" in product &&
"Subscriptions" in product["ProductDetails"] &&
product["ProductDetails"]["Subscriptions"].length > 0
) {
const productSubcriptions = product["ProductDetails"]["Subscriptions"];
const lineItemReference = items[key]["LineItemReference"];
subscriptionsRefNoArray[lineItemReference] = productSubcriptions;
}
});
}
return subscriptionsRefNoArray;
}
async getSubscriptionsByOrderRefNo(orderRefNo, callback) {
let order = new Order(this.apiCore);
let orderData;
if (typeof callback !== undefined && typeof callback === "function") {
if (typeof orderRefNo === "string" && orderRefNo.length <= 10) {
try {
orderData = await order.retrieve({ RefNo: orderRefNo });
const items = orderData["Items"];
const subscriptions = this.getSubscriptionFromItems(items);
callback(null, subscriptions);
} catch (error) {
callback(error, null);
}
} else {
callback(new TwocheckoutError("Order reference No is not a string or the size is to big"), null);
}
} else {
return new Promise((resolve, reject) => {
if (typeof orderRefNo === "string" && orderRefNo.length <= 10) {
order
.retrieve({ RefNo: orderRefNo })
.then((orderData) => {
const subscriptions = this.getSubscriptionFromItems(orderData["Items"]);
resolve(subscriptions);
})
.catch((error) => {
const parsedResponse = typeof error.response !== "undefined" ? error.response.body : null;
if (parsedResponse) {
let err = new TwocheckoutError(parsedResponse.error_code, parsedResponse.message);
reject(err);
}
});
} else {
reject(new TwocheckoutError("Order reference No is not a string or the size is too big"));
}
});
}
}
async retrieve(searchParams, callback) {
const args = this.helper.prepareSubForGet(searchParams);
return await this.apiCore.execute(args, callback);
}
async update(updateParams, callback) {
const args = this.helper.prepareSubForUpdate(updateParams);
return await this.apiCore.execute(args, callback);
}
async enable(updateParams, callback) {
const args = this.helper.prepareSubForPost(updateParams);
return await this.apiCore.execute(args, callback);
}
async disable(updateParams, callback) {
const args = this.helper.prepareSubForDelete(updateParams);
return await this.apiCore.execute(args, callback);
}
}
module.exports = Subscription;