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

88 lines
2.5 KiB
Plaintext
Raw Normal View History

2017-02-21 18:21:42 -05:00
import { ajax } from 'discourse/lib/ajax';
2017-02-27 20:39:07 -05:00
import { getRegister } from 'discourse-common/lib/get-owner';
2017-02-21 18:21:42 -05:00
export default Ember.Component.extend({
2017-03-09 21:34:24 -05:00
donateAmounts: [
{ value: 1, name: '$1.00'},
{ value: 2, name: '$2.00'},
{ value: 5, name: '$5.00'},
{ value: 10, name: '$10.00'},
{ value: 20, name: '$20.00'},
{ value: 50, name: '$50.00'}
],
result: null,
2017-02-22 20:48:06 -05:00
amount: null,
2017-02-27 20:39:07 -05:00
stripe: null,
2017-03-05 19:12:22 -05:00
transactionInProgress: null,
settings: null,
2017-02-26 22:22:52 -05:00
2017-02-27 20:39:07 -05:00
init() {
this._super();
2017-03-19 21:08:30 -04:00
this.set('anon', (Discourse.User.current() == null));
this.set('settings', getRegister(this).lookup('site-settings:main'));
2017-03-22 00:05:06 -04:00
this.set('create_accounts', this.get('anon') && this.get('settings').discourse_donations_enable_create_accounts);
this.set('stripe', Stripe(this.get('settings').discourse_donations_public_key));
2017-02-27 20:39:07 -05:00
},
2017-02-21 18:21:42 -05:00
card: function() {
let elements = this.get('stripe').elements();
return elements.create('card', {
hidePostalCode: this.get('settings').discourse_donations_hide_zip_code
});
}.property('stripe'),
didInsertElement() {
this._super();
2017-02-21 18:21:42 -05:00
this.get('card').mount('#card-element');
},
actions: {
submitStripeCard() {
let self = this;
this.get('stripe').createToken(this.get('card')).then(data => {
self.set('result', null);
2017-03-13 00:26:48 -04:00
self.set('success', false);
if (data.error) {
self.set('result', data.error.message);
}
else {
2017-03-05 19:12:22 -05:00
self.set('transactionInProgress', true);
let params = {
stripeToken: data.token.id,
2017-03-19 21:08:30 -04:00
amount: self.get('amount') * 100,
2017-03-20 20:52:48 -04:00
email: self.get('email'),
2017-02-21 18:21:42 -05:00
};
ajax('/charges', { data: params, method: 'post' }).then(data => {
self.set('result', data.outcome.seller_message);
if(!this.get('create_accounts')) {
if(data.status == 'succeeded') { self.set('success', true) };
self.set('transactionInProgress', false);
}
else {
let params = {
email: self.get('email'),
username: self.get('username'),
name: self.get('name'),
password: self.get('password')
};
ajax('/users', { data: params, method: 'post' }).then(data => {
self.set('success', data.success);
self.set('transactionInProgress', false);
self.set('result', self.get('result') + data.message);
});
}
2017-02-21 18:21:42 -05:00
});
}
});
}
}
});