1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/resources/js/clients/payments/process.js

141 lines
4.0 KiB
JavaScript
Raw Normal View History

/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
class ProcessStripePayment {
constructor(key, usingToken) {
this.key = key;
this.usingToken = usingToken;
}
setupStripe() {
this.stripe = Stripe(this.key);
this.elements = this.stripe.elements();
return this;
}
createElement() {
2020-08-24 10:28:55 +02:00
this.cardElement = this.elements.create('card');
return this;
}
mountCardElement() {
2020-08-24 10:28:55 +02:00
this.cardElement.mount('#card-element');
return this;
}
completePaymentUsingToken() {
let payNowButton = document.getElementById('pay-now-with-token');
2020-08-24 10:28:55 +02:00
this.payNowButton = payNowButton;
this.payNowButton.disabled = true;
2020-09-29 12:46:28 +02:00
this.payNowButton.querySelector('svg').classList.remove('hidden');
this.payNowButton.querySelector('span').classList.add('hidden');
this.stripe
.handleCardPayment(payNowButton.dataset.secret, {
2020-08-24 10:28:55 +02:00
payment_method: payNowButton.dataset.token,
})
2020-08-24 10:28:55 +02:00
.then((result) => {
if (result.error) {
return this.handleFailure(result.error.message);
}
return this.handleSuccess(result);
});
}
completePaymentWithoutToken() {
2020-08-24 10:28:55 +02:00
let payNowButton = document.getElementById('pay-now');
this.payNowButton = payNowButton;
2020-10-09 12:03:12 +02:00
2020-09-29 12:46:28 +02:00
this.payNowButton.disabled = true;
2020-08-24 10:28:55 +02:00
2020-09-29 12:46:28 +02:00
this.payNowButton.querySelector('svg').classList.remove('hidden');
this.payNowButton.querySelector('span').classList.add('hidden');
2020-09-29 12:46:28 +02:00
let cardHolderName = document.getElementById('cardholder-name');
2020-08-24 09:07:44 +02:00
this.stripe
.handleCardPayment(payNowButton.dataset.secret, this.cardElement, {
payment_method_data: {
2020-08-24 10:28:55 +02:00
billing_details: { name: cardHolderName.value },
},
})
2020-08-24 10:28:55 +02:00
.then((result) => {
if (result.error) {
return this.handleFailure(result.error.message);
}
return this.handleSuccess(result);
});
}
handleSuccess(result) {
document.querySelector(
'input[name="gateway_response"]'
).value = JSON.stringify(result.paymentIntent);
let tokenBillingCheckbox = document.querySelector(
'input[name="token-billing-checkbox"]'
);
if (tokenBillingCheckbox) {
document.querySelector('input[name="store_card"]').value =
tokenBillingCheckbox.checked;
}
2020-08-24 10:28:55 +02:00
document.getElementById('server-response').submit();
}
handleFailure(message) {
2020-08-24 10:28:55 +02:00
let errors = document.getElementById('errors');
2020-08-24 10:28:55 +02:00
errors.textContent = '';
errors.textContent = message;
errors.hidden = false;
this.payNowButton.disabled = false;
2020-09-29 12:46:28 +02:00
this.payNowButton.querySelector('svg').classList.add('hidden');
this.payNowButton.querySelector('span').classList.remove('hidden');
}
handle() {
this.setupStripe();
if (this.usingToken) {
document
2020-08-24 10:28:55 +02:00
.getElementById('pay-now-with-token')
.addEventListener('click', () => {
return this.completePaymentUsingToken();
});
}
if (!this.usingToken) {
this.createElement().mountCardElement();
2020-08-24 10:28:55 +02:00
document.getElementById('pay-now').addEventListener('click', () => {
return this.completePaymentWithoutToken();
});
}
}
}
const publishableKey = document.querySelector(
'meta[name="stripe-publishable-key"]'
).content;
const usingToken = document.querySelector('meta[name="using-token"]').content;
new ProcessStripePayment(publishableKey, usingToken).handle();