1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/resources/js/clients/payments/stripe-credit-card.js

229 lines
7.1 KiB
JavaScript
Raw Normal View History

/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2020-10-20 14:58:48 +02:00
class StripeCreditCard {
2021-04-20 14:17:44 +02:00
constructor(key, secret, onlyAuthorization, stripeConnect) {
this.key = key;
2020-10-20 14:58:48 +02:00
this.secret = secret;
2020-10-20 16:08:21 +02:00
this.onlyAuthorization = onlyAuthorization;
2021-04-20 14:17:44 +02:00
this.stripeConnect = stripeConnect;
}
setupStripe() {
2021-04-20 14:17:44 +02:00
2021-05-11 16:40:55 +02:00
if (this.stripeConnect){
// this.stripe.stripeAccount = this.stripeConnect;
this.stripe = Stripe(this.key, {
stripeAccount: this.stripeConnect,
});
}
else {
this.stripe = Stripe(this.key);
}
this.elements = this.stripe.elements();
return this;
}
createElement() {
this.cardElement = this.elements.create('card', {
value: {
postalCode: document.querySelector('meta[name=client-postal-code]').content,
}
});
return this;
}
mountCardElement() {
2020-08-24 10:28:55 +02:00
this.cardElement.mount('#card-element');
return this;
}
completePaymentUsingToken() {
let token = document.querySelector('input[name=token]').value;
2020-08-24 10:28:55 +02:00
let payNowButton = document.getElementById('pay-now');
2020-08-24 10:28:55 +02:00
this.payNowButton = payNowButton;
2020-08-24 10:28:55 +02:00
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(this.secret, {
payment_method: 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
2020-10-20 14:58:48 +02:00
.handleCardPayment(this.secret, this.cardElement, {
payment_method_data: {
billing_details: {name: cardHolderName.value},
2020-08-24 10:28:55 +02:00
},
})
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"]:checked'
);
if (tokenBillingCheckbox) {
document.querySelector('input[name="store_card"]').value =
2021-02-03 16:49:29 +01:00
tokenBillingCheckbox.value;
}
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');
}
2020-10-20 16:08:21 +02:00
handleAuthorization() {
let cardHolderName = document.getElementById('cardholder-name');
let payNowButton = document.getElementById('authorize-card');
this.payNowButton = payNowButton;
this.payNowButton.disabled = true;
this.payNowButton.querySelector('svg').classList.remove('hidden');
this.payNowButton.querySelector('span').classList.add('hidden');
this.stripe
.handleCardSetup(this.secret, this.cardElement, {
payment_method_data: {
billing_details: {name: cardHolderName.value},
2020-10-20 16:08:21 +02:00
},
})
.then((result) => {
if (result.error) {
return this.handleFailure(result);
}
return this.handleSuccessfulAuthorization(result);
});
}
handleSuccessfulAuthorization(result) {
document.getElementById('gateway_response').value = JSON.stringify(
result.setupIntent
);
document.getElementById('server_response').submit();
}
handle() {
this.setupStripe();
2020-10-20 16:08:21 +02:00
if (this.onlyAuthorization) {
this.createElement().mountCardElement();
document
2020-10-20 16:08:21 +02:00
.getElementById('authorize-card')
2020-08-24 10:28:55 +02:00
.addEventListener('click', () => {
2020-10-20 16:08:21 +02:00
return this.handleAuthorization();
});
2020-10-20 16:08:21 +02:00
} else {
Array
.from(document.getElementsByClassName('toggle-payment-with-token'))
.forEach((element) => element.addEventListener('click', (element) => {
document.getElementById('stripe--payment-container').classList.add('hidden');
document.getElementById('save-card--container').style.display = 'none';
document.querySelector('input[name=token]').value = element.target.dataset.token;
}));
document
.getElementById('toggle-payment-with-credit-card')
.addEventListener('click', (element) => {
document.getElementById('stripe--payment-container').classList.remove('hidden');
document.getElementById('save-card--container').style.display = 'grid';
document.querySelector('input[name=token]').value = "";
});
this.createElement().mountCardElement();
document
.getElementById('pay-now')
.addEventListener('click', () => {
let tokenInput = document.querySelector('input[name=token]');
if (tokenInput.value) {
2020-10-20 16:08:21 +02:00
return this.completePaymentUsingToken();
}
return this.completePaymentWithoutToken();
});
}
}
}
2020-10-20 16:08:21 +02:00
const publishableKey =
2021-05-11 15:44:14 +02:00
document.querySelector('meta[name="stripe-publishable-key"]')?.content ?? '';
2020-10-20 16:08:21 +02:00
const secret =
2021-05-11 15:44:14 +02:00
document.querySelector('meta[name="stripe-secret"]')?.content ?? '';
2020-10-20 16:08:21 +02:00
const onlyAuthorization =
2021-05-11 15:44:14 +02:00
document.querySelector('meta[name="only-authorization"]')?.content ?? '';
const stripeConnect =
2021-05-11 15:44:14 +02:00
document.querySelector('meta[name="stripe-account-id"]')?.content ?? '';
2021-04-20 14:17:44 +02:00
let s = new StripeCreditCard(publishableKey, secret, onlyAuthorization, stripeConnect);
s.handle();
Livewire.on('passed-required-fields-check', () => s.handle());