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-03-10 13:34:24 +11:00
|
|
|
donateAmounts: [
|
2017-11-26 12:24:27 +00:00
|
|
|
{ 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-03-10 13:34:24 +11:00
|
|
|
],
|
2017-05-03 15:13:41 +10:00
|
|
|
result: [],
|
2018-02-02 16:19:21 +08:00
|
|
|
amount: 1,
|
2017-02-28 12:39:07 +11:00
|
|
|
stripe: null,
|
2017-03-06 11:12:22 +11:00
|
|
|
transactionInProgress: null,
|
2017-03-07 15:21:51 +11:00
|
|
|
settings: null,
|
2017-02-27 14:22:52 +11:00
|
|
|
|
2017-02-28 12:39:07 +11:00
|
|
|
init() {
|
|
|
|
this._super();
|
2018-01-18 00:21:41 +00:00
|
|
|
this.set('anon', (!Discourse.User.current()));
|
2017-03-07 15:21:51 +11:00
|
|
|
this.set('settings', getRegister(this).lookup('site-settings:main'));
|
2017-03-22 15:05:06 +11:00
|
|
|
this.set('create_accounts', this.get('anon') && this.get('settings').discourse_donations_enable_create_accounts);
|
2017-03-07 15:21:51 +11:00
|
|
|
this.set('stripe', Stripe(this.get('settings').discourse_donations_public_key));
|
2017-02-28 12:39:07 +11:00
|
|
|
},
|
2017-02-22 10:21:42 +11:00
|
|
|
|
2017-02-21 10:25:31 +11:00
|
|
|
card: function() {
|
2017-03-13 11:19:10 +11:00
|
|
|
let elements = this.get('stripe').elements();
|
2017-03-07 15:21:51 +11:00
|
|
|
return elements.create('card', {
|
2017-11-26 12:24:27 +00:00
|
|
|
hidePostalCode: !this.get('settings').discourse_donations_zip_code
|
2017-03-07 15:21:51 +11:00
|
|
|
});
|
2017-02-21 10:25:31 +11:00
|
|
|
}.property('stripe'),
|
|
|
|
|
|
|
|
didInsertElement() {
|
2017-02-23 09:30:40 +11:00
|
|
|
this._super();
|
2017-02-22 10:21:42 +11:00
|
|
|
this.get('card').mount('#card-element');
|
2017-02-21 10:25:31 +11:00
|
|
|
},
|
|
|
|
|
2017-04-03 11:05:22 +10:00
|
|
|
setSuccess() {
|
2017-04-03 12:49:23 +10:00
|
|
|
this.set('paymentSuccess', true);
|
2017-04-03 11:05:22 +10:00
|
|
|
},
|
|
|
|
|
|
|
|
endTranscation() {
|
|
|
|
this.set('transactionInProgress', false);
|
|
|
|
},
|
|
|
|
|
2017-05-03 15:13:41 +10:00
|
|
|
concatMessages(messages) {
|
|
|
|
this.set('result', this.get('result').concat(messages));
|
|
|
|
},
|
|
|
|
|
2017-02-21 10:25:31 +11:00
|
|
|
actions: {
|
|
|
|
submitStripeCard() {
|
2017-03-13 11:19:10 +11:00
|
|
|
let self = this;
|
2017-02-23 09:30:40 +11:00
|
|
|
|
2017-05-29 10:48:32 +10:00
|
|
|
self.set('transactionInProgress', true);
|
|
|
|
|
2017-03-06 12:34:03 +11:00
|
|
|
this.get('stripe').createToken(this.get('card')).then(data => {
|
2017-02-23 09:30:40 +11:00
|
|
|
|
2017-05-03 15:13:41 +10:00
|
|
|
self.set('result', []);
|
2017-02-23 09:30:40 +11:00
|
|
|
|
2017-03-06 12:34:03 +11:00
|
|
|
if (data.error) {
|
|
|
|
self.set('result', data.error.message);
|
2017-05-29 10:48:32 +10:00
|
|
|
self.endTranscation();
|
2017-02-21 10:25:31 +11:00
|
|
|
}
|
|
|
|
else {
|
2017-03-13 11:19:10 +11:00
|
|
|
let params = {
|
2017-03-06 12:34:03 +11:00
|
|
|
stripeToken: data.token.id,
|
2017-03-20 12:08:30 +11:00
|
|
|
amount: self.get('amount') * 100,
|
2018-02-02 16:06:43 +08:00
|
|
|
user_id: self.get('currentUser.id'),
|
2017-03-21 11:52:48 +11:00
|
|
|
email: self.get('email'),
|
2017-05-18 12:09:37 +10:00
|
|
|
username: self.get('username'),
|
|
|
|
create_account: self.get('create_accounts')
|
2017-02-22 10:21:42 +11:00
|
|
|
};
|
|
|
|
|
2017-04-03 12:49:23 +10:00
|
|
|
if(!self.get('paymentSuccess')) {
|
2018-02-02 16:06:43 +08:00
|
|
|
ajax('/charges', { data: params, method: 'post' }).then(d => {
|
|
|
|
self.concatMessages(d.messages);
|
2017-05-10 14:17:28 +10:00
|
|
|
self.endTranscation();
|
2017-04-03 12:49:23 +10:00
|
|
|
});
|
|
|
|
}
|
2017-02-21 10:25:31 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|