-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsubscription_gateway.js
68 lines (54 loc) · 2.31 KB
/
subscription_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
'use strict';
let Gateway = require('./gateway').Gateway;
let Subscription = require('./subscription').Subscription;
let SubscriptionSearch = require('./subscription_search').SubscriptionSearch;
let TransactionGateway = require('./transaction_gateway').TransactionGateway;
let exceptions = require('./exceptions');
let wrapPrototype = require('@braintree/wrap-promise').wrapPrototype;
class SubscriptionGateway extends Gateway {
constructor(gateway) {
super();
this.gateway = gateway;
this.config = this.gateway.config;
}
create(attributes) {
return this.gateway.http.post(`${this.config.baseMerchantPath()}/subscriptions`, {subscription: attributes}).then(this.responseHandler());
}
cancel(subscriptionId) {
return this.gateway.http.put(`${this.config.baseMerchantPath()}/subscriptions/${subscriptionId}/cancel`, null).then(this.responseHandler());
}
find(subscriptionId) {
if (subscriptionId.trim() === '') {
return Promise.reject(exceptions.NotFoundError('Not Found'), null); // eslint-disable-line new-cap
}
return this.gateway.http.get(`${this.config.baseMerchantPath()}/subscriptions/${subscriptionId}`).then((response) => {
return new Subscription(response.subscription);
});
}
responseHandler() {
return this.createResponseHandler('subscription', Subscription);
}
retryCharge(subscriptionId, amount) {
if (typeof amount === 'function') {
amount = undefined; // eslint-disable-line no-undefined
}
return new TransactionGateway(this.gateway).sale({
amount: amount,
subscriptionId
});
}
search(fn, callback) {
let search = new SubscriptionSearch();
fn(search);
return this.createSearchResponse(`${this.config.baseMerchantPath()}/subscriptions/advanced_search_ids`, search, this.pagingFunctionGenerator(search), callback);
}
update(subscriptionId, attributes) {
return this.gateway.http.put(`${this.config.baseMerchantPath()}/subscriptions/${subscriptionId}`, {subscription: attributes}).then(this.responseHandler());
}
pagingFunctionGenerator(search) {
return super.pagingFunctionGenerator(search, 'subscriptions', Subscription, 'subscriptions', response => response.subscriptions.subscription);
}
}
module.exports = {SubscriptionGateway: wrapPrototype(SubscriptionGateway, {
ignoreMethods: ['search']
})};