-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathmerchant_account_gateway.js
93 lines (76 loc) · 2.95 KB
/
merchant_account_gateway.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
'use strict';
let _ = require('underscore');
let Gateway = require('./gateway').Gateway;
let MerchantAccount = require('./merchant_account').MerchantAccount;
let PaginatedResponse = require('./paginated_response').PaginatedResponse;
let exceptions = require('./exceptions');
let wrapPrototype = require('@braintree/wrap-promise').wrapPrototype;
class MerchantAccountGateway extends Gateway {
constructor(gateway) {
super();
this.gateway = gateway;
this.config = this.gateway.config;
}
create(attributes) {
return this.gateway.http.post(`${this.config.baseMerchantPath()}/merchant_accounts/create_via_api`, {merchantAccount: attributes}).then(this.responseHandler());
}
update(id, attributes) {
return this.gateway.http.put(`${this.config.baseMerchantPath()}/merchant_accounts/${id}/update_via_api`, {merchantAccount: attributes}).then(this.responseHandler());
}
find(id) {
if (id.trim() === '') {
return Promise.reject(exceptions.NotFoundError('Not Found'), null); // eslint-disable-line new-cap
}
return this.gateway.http.get(`${this.config.baseMerchantPath()}/merchant_accounts/${id}`).then(function (response) {
return new MerchantAccount(response.merchantAccount);
});
}
responseHandler() {
return this.createResponseHandler('merchantAccount', MerchantAccount);
}
all(callback) {
let response = new PaginatedResponse(this.fetchMerchantAccounts.bind(this));
if (callback != null) {
return response.all(callback);
}
response.ready();
return response.stream;
}
fetchMerchantAccounts(pageNumber, callback) {
return this.gateway.http.get(this.config.baseMerchantPath() + '/merchant_accounts?page=' + pageNumber, (err, response) => {
let body, merchantAccounts, pageSize, ref, totalItems;
if (err) {
return callback(err);
}
body = response.merchantAccounts;
ref = response.merchantAccounts;
totalItems = ref.totalItems;
pageSize = ref.pageSize;
merchantAccounts = body.merchantAccount;
if (!_.isArray(merchantAccounts)) {
merchantAccounts = [merchantAccounts];
}
return callback(null, totalItems, pageSize, merchantAccounts);
});
}
createForCurrency(attributes) {
return this.gateway.http.post(this.config.baseMerchantPath() + '/merchant_accounts/create_for_currency', {
merchantAccount: attributes
}).then(this.createForCurrencyResponseHandler());
}
createForCurrencyResponseHandler() {
let handler = this.createResponseHandler(null, null);
return function (payload) {
return handler(payload).then((response) => {
if (response.success) {
response.merchantAccount = new MerchantAccount(response.response.merchantAccount);
delete response.response;
}
return response;
});
};
}
}
module.exports = {MerchantAccountGateway: wrapPrototype(MerchantAccountGateway, {
ignoreMethods: ['all', 'fetchMerchantAccounts']
})};