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
|
|
|
|
2017-02-20 18:25:31 -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'}
|
|
|
|
],
|
2017-05-03 01:13:41 -04:00
|
|
|
result: [],
|
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,
|
2017-03-06 23:21:51 -05:00
|
|
|
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));
|
2017-03-06 23:21:51 -05:00
|
|
|
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);
|
2017-03-06 23:21:51 -05:00
|
|
|
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
|
|
|
|
2017-02-20 18:25:31 -05:00
|
|
|
card: function() {
|
2017-03-12 20:19:10 -04:00
|
|
|
let elements = this.get('stripe').elements();
|
2017-03-06 23:21:51 -05:00
|
|
|
return elements.create('card', {
|
|
|
|
hidePostalCode: this.get('settings').discourse_donations_hide_zip_code
|
|
|
|
});
|
2017-02-20 18:25:31 -05:00
|
|
|
}.property('stripe'),
|
|
|
|
|
|
|
|
didInsertElement() {
|
2017-02-22 17:30:40 -05:00
|
|
|
this._super();
|
2017-02-21 18:21:42 -05:00
|
|
|
this.get('card').mount('#card-element');
|
2017-02-20 18:25:31 -05:00
|
|
|
},
|
|
|
|
|
2017-04-02 21:05:22 -04:00
|
|
|
setSuccess() {
|
2017-04-02 22:49:23 -04:00
|
|
|
this.set('paymentSuccess', true);
|
2017-04-02 21:05:22 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
endTranscation() {
|
|
|
|
this.set('transactionInProgress', false);
|
|
|
|
},
|
|
|
|
|
2017-05-03 01:13:41 -04:00
|
|
|
concatMessages(messages) {
|
|
|
|
this.set('result', this.get('result').concat(messages));
|
|
|
|
},
|
|
|
|
|
2017-04-02 21:05:22 -04:00
|
|
|
createUser() {
|
|
|
|
let self = this;
|
|
|
|
ajax('/users/hp', { method: 'get' }).then(data => {
|
|
|
|
let params = {
|
|
|
|
email: self.get('email'),
|
|
|
|
username: self.get('username'),
|
|
|
|
name: self.get('name'),
|
|
|
|
password: self.get('password'),
|
|
|
|
password_confirmation: data.value,
|
|
|
|
challenge: data.challenge.split('').reverse().join(''),
|
|
|
|
};
|
|
|
|
|
|
|
|
ajax('/users', { data: params, method: 'post' }).then(data => {
|
|
|
|
self.setSuccess();
|
2017-05-03 01:13:41 -04:00
|
|
|
self.concatMessages(data.messages);
|
2017-04-03 23:25:51 -04:00
|
|
|
self.endTranscation();
|
2017-04-02 21:05:22 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-02-20 18:25:31 -05:00
|
|
|
actions: {
|
|
|
|
submitStripeCard() {
|
2017-03-12 20:19:10 -04:00
|
|
|
let self = this;
|
2017-02-22 17:30:40 -05:00
|
|
|
|
2017-03-05 20:34:03 -05:00
|
|
|
this.get('stripe').createToken(this.get('card')).then(data => {
|
2017-02-22 17:30:40 -05:00
|
|
|
|
2017-05-03 01:13:41 -04:00
|
|
|
self.set('result', []);
|
2017-02-22 17:30:40 -05:00
|
|
|
|
2017-03-05 20:34:03 -05:00
|
|
|
if (data.error) {
|
|
|
|
self.set('result', data.error.message);
|
2017-02-20 18:25:31 -05:00
|
|
|
}
|
|
|
|
else {
|
2017-03-05 19:12:22 -05:00
|
|
|
self.set('transactionInProgress', true);
|
|
|
|
|
2017-03-12 20:19:10 -04:00
|
|
|
let params = {
|
2017-03-05 20:34:03 -05:00
|
|
|
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-04-03 23:44:13 -04:00
|
|
|
username: self.get('username'),
|
|
|
|
create_account: this.get('create_accounts')
|
2017-02-21 18:21:42 -05:00
|
|
|
};
|
|
|
|
|
2017-04-02 22:49:23 -04:00
|
|
|
if(!self.get('paymentSuccess')) {
|
|
|
|
ajax('/charges', { data: params, method: 'post' }).then(data => {
|
2017-05-03 01:13:41 -04:00
|
|
|
self.concatMessages(data.messages);
|
2017-03-21 22:56:45 -04:00
|
|
|
|
2017-04-02 22:49:23 -04:00
|
|
|
if(!this.get('create_accounts')) {
|
|
|
|
if(data.status == 'succeeded') { this.setSuccess() };
|
|
|
|
self.endTranscation();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(data.status == 'succeeded') {
|
|
|
|
this.createUser();
|
|
|
|
}
|
2017-04-03 23:25:51 -04:00
|
|
|
else {
|
|
|
|
self.endTranscation();
|
|
|
|
}
|
2017-03-28 23:29:07 -04:00
|
|
|
}
|
2017-04-02 22:49:23 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (this.get('create_accounts')) {
|
2017-05-03 01:13:41 -04:00
|
|
|
self.set('result', []);
|
2017-04-02 22:49:23 -04:00
|
|
|
self.createUser();
|
|
|
|
}
|
2017-02-20 18:25:31 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|