braintree
Version:
A library for integrating with Braintree.
87 lines (67 loc) • 3.31 kB
JavaScript
//@ sourceMappingURL=subscription_gateway.map
// Generated by CoffeeScript 1.6.1
var Gateway, Subscription, SubscriptionGateway, SubscriptionSearch, TransactionGateway, exceptions,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice;
Gateway = require('./gateway').Gateway;
Subscription = require('./subscription').Subscription;
SubscriptionSearch = require('./subscription_search').SubscriptionSearch;
TransactionGateway = require('./transaction_gateway').TransactionGateway;
exceptions = require('./exceptions');
SubscriptionGateway = (function(_super) {
__extends(SubscriptionGateway, _super);
function SubscriptionGateway(gateway) {
this.gateway = gateway;
}
SubscriptionGateway.prototype.create = function(attributes, callback) {
return this.gateway.http.post('/subscriptions', {
subscription: attributes
}, this.responseHandler(callback));
};
SubscriptionGateway.prototype.cancel = function(subscriptionId, callback) {
return this.gateway.http.put("/subscriptions/" + subscriptionId + "/cancel", null, this.responseHandler(callback));
};
SubscriptionGateway.prototype.find = function(subscriptionId, callback) {
if (subscriptionId.trim() === '') {
return callback(exceptions.NotFoundError("Not Found"), null);
} else {
return this.gateway.http.get("/subscriptions/" + subscriptionId, function(err, response) {
if (err) {
return callback(err, null);
} else {
return callback(null, new Subscription(response.subscription));
}
});
}
};
SubscriptionGateway.prototype.responseHandler = function(callback) {
return this.createResponseHandler("subscription", Subscription, callback);
};
SubscriptionGateway.prototype.retryCharge = function() {
var amount, callback, subscriptionId, _i;
subscriptionId = arguments[0], amount = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), callback = arguments[_i++];
return new TransactionGateway(this.gateway).sale({
amount: amount[0],
subscriptionId: subscriptionId
}, callback);
};
SubscriptionGateway.prototype.search = function(fn, callback) {
var search;
search = new SubscriptionSearch();
fn(search);
return this.createSearchResponse("/subscriptions/advanced_search_ids", search, this.pagingFunctionGenerator(search), callback);
};
SubscriptionGateway.prototype.update = function(subscriptionId, attributes, callback) {
return this.gateway.http.put("/subscriptions/" + subscriptionId, {
subscription: attributes
}, this.responseHandler(callback));
};
SubscriptionGateway.prototype.pagingFunctionGenerator = function(search) {
return SubscriptionGateway.__super__.pagingFunctionGenerator.call(this, search, 'subscriptions', Subscription, function(response) {
return response.subscriptions.subscription;
});
};
return SubscriptionGateway;
})(Gateway);
exports.SubscriptionGateway = SubscriptionGateway;