Files
discourse-subscriptions/assets/javascripts/discourse/components/stripe-card.js.es6
T

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-02-22 10:21:42 +11:00
import { ajax } from 'discourse/lib/ajax';
2017-02-28 12:39:07 +11:00
import { getRegister } from 'discourse-common/lib/get-owner';
2017-02-22 10:21:42 +11:00
2017-02-21 10:25:31 +11:00
export default Ember.Component.extend({
2017-02-28 12:39:07 +11:00
donateAmounts: [1, 5, 10, 25],
result: null,
2017-02-23 12:48:06 +11:00
amount: null,
2017-02-28 12:39:07 +11:00
stripe: null,
2017-02-27 14:22:52 +11:00
2017-02-28 12:39:07 +11:00
init() {
this._super();
var public_key = getRegister(this).lookup('site-settings:main').discourse_donations_public_key
this.set('stripe', Stripe(public_key));
},
2017-02-22 10:21:42 +11:00
2017-02-21 10:25:31 +11:00
card: function() {
var elements = this.get('stripe').elements();
return elements.create('card', { hidePostalCode: true });
}.property('stripe'),
didInsertElement() {
this._super();
2017-02-22 10:21:42 +11:00
this.get('card').mount('#card-element');
2017-02-21 10:25:31 +11:00
},
actions: {
submitStripeCard() {
var self = this;
2017-02-21 10:25:31 +11:00
this.get('stripe').createToken(this.get('card')).then(function(result) {
self.set('result', null);
2017-02-21 10:25:31 +11:00
if (result.error) {
console.log('error yo');
}
else {
var params = {
2017-02-22 10:21:42 +11:00
stripeToken: result.token.id,
2017-02-23 12:48:06 +11:00
amount: self.get('amount') * 100
2017-02-22 10:21:42 +11:00
};
2017-02-23 12:48:06 +11:00
console.log(params);
ajax('/charges', { data: params, method: 'post' }).then(data => {
self.set('result', (data.status == 'succeeded' ? true : null));
}).catch((data) => {
console.log('catch', data);
2017-02-22 10:21:42 +11:00
});
2017-02-21 10:25:31 +11:00
}
});
}
}
});