-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathcredit_card_verification_gateway.js
67 lines (55 loc) · 2.43 KB
/
credit_card_verification_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
'use strict';
let Gateway = require('./gateway').Gateway;
let CreditCardVerification = require('./credit_card_verification').CreditCardVerification;
let CreditCardVerificationSearch = require('./credit_card_verification_search').CreditCardVerificationSearch;
let _ = require('underscore');
let exceptions = require('./exceptions');
let wrapPrototype = require('@braintree/wrap-promise').wrapPrototype;
class CreditCardVerificationGateway extends Gateway {
constructor(gateway) {
super();
this.gateway = gateway;
this.config = this.gateway.config;
}
find(creditCardVerificationId) {
if (creditCardVerificationId.trim() === '') {
return Promise.reject(exceptions.NotFoundError('Not Found')); // eslint-disable-line new-cap
}
return this.gateway.http.get(`${this.config.baseMerchantPath()}/verifications/${creditCardVerificationId}`).then(function (response) {
return new CreditCardVerification(response.verification);
});
}
search(fn, callback) {
let search = new CreditCardVerificationSearch();
fn(search);
return this.createSearchResponse(`${this.config.baseMerchantPath()}/verifications/advanced_search_ids`, search, this.pagingFunctionGenerator(search), callback);
}
create(params) {
return this.gateway.http.post(`${this.config.baseMerchantPath()}/verifications`, {
verification: params
}).then(this.createResponseHandler('verification', CreditCardVerification));
}
responseHandler() {
return this.createResponseHandler('creditCardVerification', CreditCardVerification);
}
pagingFunctionGenerator(search) {
return (ids, callback) => {
let searchCriteria = search.toHash();
searchCriteria.ids = ids;
return this.gateway.http.post(`${this.config.baseMerchantPath()}/verifications/advanced_search`,
{search: searchCriteria},
function (err, response) {
if (err) {
return callback(err, null);
} else if (_.isArray(response.creditCardVerifications.verification)) {
return response.creditCardVerifications.verification.map((creditCardVerification) =>
callback(null, new CreditCardVerification(creditCardVerification)));
}
return callback(null, new CreditCardVerification(response.creditCardVerifications.verification));
});
};
}
}
module.exports = {CreditCardVerificationGateway: wrapPrototype(CreditCardVerificationGateway, {
ignoreMethods: ['search']
})};