Skip to content

Instantly share code, notes, and snippets.

@tatey
Created January 8, 2014 11:32
Show Gist options
  • Select an option

  • Save tatey/8315517 to your computer and use it in GitHub Desktop.

Select an option

Save tatey/8315517 to your computer and use it in GitHub Desktop.
if (typeof window.EDH === 'undefined') {
window.EDH = {};
}
if (typeof window.EDH.Donation === 'undefined') {
window.EDH.Donation = {};
}
var Donation = window.EDH.Donation;
jQuery(function($) {
braintree.onSubmitEncryptForm($form, function(event) {
disableSubmitButton(true);
event.preventDefault();
Donation.MakePayment.make($form.serialize())
.done(function(status) {
window.location = status.thankYouURL;
})
.fail(function(status) {
// update form
});
});
});
(function() {
var Payment = window.EDH.Donation.Payment,
Poll = window.EDH.Donation.Poll;
var MakePayment = {
make: function(props) {
var payment = new Payment(props),
poll = new Poll();
payment.save()
.done(function() {
return poll.ready(payment.getPollURL());
});
}
};
})();
MakePayment.make();
(function() {
var Payment = function(props) {
this.props = {};
this.props.url = '<%= asset_url %>';
var prop;
for (prop in props) {
this.props = [prop] = props[prop];
}
return this;
};
Payment.prototype.save = function() {
var deferred = new $.Deferred();
var _this = this;
$.post(this.getURL(), this.props, {dataType: 'json'})
.done(function(data) {
_this.props = data;
deferred.resolve({thankYouURL: _this.props.thank_you_url});
});
return deferred;
};
Payment.prototype.getURL = function() {
var retry_order_url = this.props.retry_order_url;
if (retry_order_url) {
return retry_order_url;
} else {
return this.url;
}
};
Payment.prototype.getPollURL = function() {
return this.props.poll_url;
};
window.EDH.Payment = Payment;
})();
(function() {
var Poll = function(props) {
};
Poll.prototype.ready = function(url) {
var deferred = new $.Deferred();
$.get(url, 'json')
.done(function(data) {
if (data.state === 'payment_successful') {
deferred.resolve();
} else {
deferred.reject();
}
})
.fail(function() {
// TODO
});
return deferred;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment