-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathcustomer_gateway.js
56 lines (44 loc) · 1.82 KB
/
customer_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
'use strict';
let Gateway = require('./gateway').Gateway;
let Customer = require('./customer').Customer;
let CustomerSearch = require('./customer_search').CustomerSearch;
let exceptions = require('./exceptions');
let wrapPrototype = require('@braintree/wrap-promise').wrapPrototype;
class CustomerGateway extends Gateway {
constructor(gateway) {
super();
this.gateway = gateway;
this.config = this.gateway.config;
}
create(attributes) {
return this.gateway.http.post(`${this.config.baseMerchantPath()}/customers`, {customer: attributes}).then(this.responseHandler());
}
delete(customerId) {
return this.gateway.http.delete(`${this.config.baseMerchantPath()}/customers/${customerId}`);
}
find(customerId) {
if (customerId.trim() === '') {
return Promise.reject(exceptions.NotFoundError('Not Found'), null); // eslint-disable-line new-cap
}
return this.gateway.http.get(`${this.config.baseMerchantPath()}/customers/${customerId}`).then(function (response) {
return new Customer(response.customer);
});
}
update(customerId, attributes) {
return this.gateway.http.put(`${this.config.baseMerchantPath()}/customers/${customerId}`, {customer: attributes}).then(this.responseHandler());
}
search(fn, callback) {
let search = new CustomerSearch();
fn(search);
return this.createSearchResponse(`${this.config.baseMerchantPath()}/customers/advanced_search_ids`, search, this.pagingFunctionGenerator(search), callback);
}
responseHandler() {
return this.createResponseHandler('customer', Customer);
}
pagingFunctionGenerator(search) {
return super.pagingFunctionGenerator(search, 'customers', Customer, 'customers', response => response.customers.customer);
}
}
module.exports = {CustomerGateway: wrapPrototype(CustomerGateway, {
ignoreMethods: ['search']
})};