-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathaddress_gateway.js
63 lines (48 loc) · 1.89 KB
/
address_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
'use strict';
let Gateway = require('./gateway').Gateway;
let Address = require('./address').Address;
let exceptions = require('./exceptions');
let wrapPrototype = require('@braintree/wrap-promise').wrapPrototype;
class AddressGateway extends Gateway {
constructor(gateway) {
super();
this.gateway = gateway;
this.config = this.gateway.config;
}
create(attributes) {
let customerId = attributes.customerId;
delete attributes.customerId;
return this.gateway.http.post(`${this.config.baseMerchantPath()}/customers/${customerId}/addresses`, {address: attributes}).then(this.responseHandler());
}
delete(customerId, id) {
let path = `${this.config.baseMerchantPath()}/customers/${customerId}/addresses/${id}`;
return this.gateway.http.delete(path);
}
find(customerId, id) {
if (customerId.trim() === '' || id.trim() === '') {
return Promise.reject(exceptions.NotFoundError('Not Found')); // eslint-disable-line new-cap
}
return this.gateway.http.get(`${this.config.baseMerchantPath()}/customers/${customerId}/addresses/${id}`).then((response) => {
return response.address;
});
}
update(customerId, id, attributes) {
return this.gateway.http.put(`${this.config.baseMerchantPath()}/customers/${customerId}/addresses/${id}`, {address: attributes}).then(this.responseHandler());
}
responseHandler() {
return this.createResponseHandler('address', Address);
}
sharedSignature(prefix) {
let signatureKeys = [
'company', 'countryCodeAlpha2', 'countryCodeAlpha3', 'countryCodeNumeric',
'countryName', 'extendedAddress', 'firstName',
'lastName', 'locality', 'postalCode', 'region', 'streetAddress'
];
let signature = [];
for (let val of signatureKeys) {
signature.push(prefix + '[' + val + ']');
}
return signature;
}
}
module.exports = {AddressGateway: wrapPrototype(AddressGateway)};