-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
165 lines (160 loc) · 4.79 KB
/
app.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
"use strict";
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
var _customer = require("./endpoints/customer");
var _customer2 = _interopRequireDefault(_customer);
var _payment = require("./endpoints/payment");
var _payment2 = _interopRequireDefault(_payment);
var _axios3 = require("./helpers/axios");
var _axios4 = _interopRequireDefault(_axios3);
var _session = require("./endpoints/session");
var _session2 = _interopRequireDefault(_session);
/**
* Thawani client class
* @author Muhannad Al-Risi
* @since 1.0.0
*/
class ThawaniClient {
/**
* @param {string} secret api secret_key
* @param {string} publishable publishable_key
* @param {string} env API environment
*/
constructor(secret, publishable, env) {
this.secret_key = secret;
this.publishable_key = publishable;
this.env = env;
this.api = new (0, _axios4.default)(this.env, this.secret_key);
this.axios = this.api.getInstance();
this.customer = new (0, _customer2.default)(this.axios);
this.payment = new (0, _payment2.default)(this.axios);
this.session = new (0, _session2.default)(this.axios);
}
/**
* This function is used to get the information about a single customer
* that has been previously registered.
* @Endpoint api/v1/customers/
* @http_method GET
* @param {string} customer_key or customer token
* @return {Promise} response
*/
find_customer(customer_token) {
return this.customer.find(customer_token);
}
/**
* This function is used to get the information about all customers
* that have been previously registered.
* @Endpoint api/v1/customers/
* @http_method GET
* @param {Object} payload http query string
* @return {Promise} response
*/
findAll_customers(payload) {
if (payload) return this.customer.findAll(payload);
return this.customer.findAll();
}
/**
* Create customer token in order to use it for
* session creation
* @Endpoint api/v1/customers
* @http_method POST
* @param {Object} payload
* @return {Promise} response
*/
create_customer(payload) {
return this.customer.create(payload);
}
/**
* This function is used to remove a single customer
* that has been previously registered from the merchant customers list.
* @Endpoint api/v1/customers/
* @http_method DELETE
* @param {String} customer_key
* @return {Promise} response
*/
delete_customer(payload) {
return this.customer.create(payload);
}
/**
* This function will return the card token that has been stored against a specific customer
* to use this end point you must use the customer token/id
* @Endpoint api/v1/payment_methods
* @http_method GET
* @param {Object} payload
* @return {Promise} response
*/
get_customer_payment(payload) {
return this.payment.get(payload);
}
/**
* This function is used to remove specific payment method for the customer.
* @Endpoint api/v1/payment_methods/
* @http_method DELETE
* @param {String} card_token
* @return {Promise} response
*/
remove_customer_payment(card_token) {
return this.payment.delete(card_token);
}
/**
* This function is to create the payment link
* this will request api/v1/checkout/session to generate a session token
*
* @Endpoint api/v1/checkout/session
* @http_method POST
* @param {Object} payload the request body
*
* @return {Promise} response
*/
create_session(payload) {
return this.session.create(payload);
}
/**
* This function will return all information
* about sessions .
* @Endpoint api/v1/checkout/session/ .
* @http_method GET
* @param {String} session_id
* @return {Promise} response
*/
find_session(session_id) {
return this.session.find(session_id);
}
/**
* This function will return all information about sessions
* as per the limit. Limit is a number of records to
* be returned order in descended order.
* if the payload provided it will get the information
* according to the parameters. otherwise it will get
* the default set of latest session
* @Endpoint api/v1/checkout/session/
* @http_method GET
* @param {Object=} payload query string
*
* @return {Promise} response
*/
findAll_sessions(payload) {
if (payload) return this.session.findAll(payload);
return this.session.findAll();
}
/**
* Get the full redirect link to Thawani checkout page
* @param {String} session_id
* @return {String} full uri
*/
redirect(session_id) {
return (
this.endpoint() + this.session.redirect(session_id, this.publishable_key)
);
}
/**
* Get the endpoint base url
* @return {String} base url
*/
endpoint() {
return this.api.get_base_url();
}
}
module.exports = ThawaniClient;
module.exports.default = ThawaniClient;