2020-03-25 14:08:37 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-13 09:22:36 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-25 14:08:37 +01:00
|
|
|
*
|
2022-06-08 06:25:44 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-25 14:08:37 +01:00
|
|
|
*/
|
|
|
|
|
2020-10-20 14:58:48 +02:00
|
|
|
class StripeCreditCard {
|
2021-04-20 14:17:44 +02:00
|
|
|
constructor(key, secret, onlyAuthorization, stripeConnect) {
|
2020-03-25 14:08:37 +01:00
|
|
|
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;
|
2020-03-25 14:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-25 14:08:37 +01:00
|
|
|
this.elements = this.stripe.elements();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
createElement() {
|
2021-05-10 13:01:59 +02:00
|
|
|
this.cardElement = this.elements.create('card', {
|
2021-08-09 14:05:39 +02:00
|
|
|
hidePostalCode: document.querySelector('meta[name=stripe-require-postal-code]')?.content === "0",
|
2021-05-10 13:01:59 +02:00
|
|
|
value: {
|
|
|
|
postalCode: document.querySelector('meta[name=client-postal-code]').content,
|
|
|
|
}
|
|
|
|
});
|
2020-03-25 14:08:37 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
mountCardElement() {
|
2020-08-24 10:28:55 +02:00
|
|
|
this.cardElement.mount('#card-element');
|
2020-03-25 14:08:37 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
completePaymentUsingToken() {
|
2021-01-21 14:03:38 +01:00
|
|
|
let token = document.querySelector('input[name=token]').value;
|
2020-08-24 10:28:55 +02:00
|
|
|
|
2021-01-21 14:03:38 +01:00
|
|
|
let payNowButton = document.getElementById('pay-now');
|
2020-08-24 10:28:55 +02:00
|
|
|
this.payNowButton = payNowButton;
|
2021-01-21 14:03:38 +01:00
|
|
|
|
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');
|
2020-03-25 14:08:37 +01:00
|
|
|
|
|
|
|
this.stripe
|
2020-10-20 15:19:21 +02:00
|
|
|
.handleCardPayment(this.secret, {
|
2021-01-21 14:03:38 +01:00
|
|
|
payment_method: token,
|
2020-03-25 14:08:37 +01:00
|
|
|
})
|
2020-08-24 10:28:55 +02:00
|
|
|
.then((result) => {
|
2020-03-25 14:08:37 +01:00
|
|
|
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-03-25 14:08:37 +01:00
|
|
|
|
2020-09-29 12:46:28 +02:00
|
|
|
let cardHolderName = document.getElementById('cardholder-name');
|
2020-08-24 09:07:44 +02:00
|
|
|
|
2020-03-25 14:08:37 +01:00
|
|
|
this.stripe
|
2020-10-20 14:58:48 +02:00
|
|
|
.handleCardPayment(this.secret, this.cardElement, {
|
2020-03-25 14:08:37 +01:00
|
|
|
payment_method_data: {
|
2021-01-21 14:03:38 +01:00
|
|
|
billing_details: {name: cardHolderName.value},
|
2020-08-24 10:28:55 +02:00
|
|
|
},
|
2020-03-25 14:08:37 +01:00
|
|
|
})
|
2020-08-24 10:28:55 +02:00
|
|
|
.then((result) => {
|
2020-03-25 14:08:37 +01:00
|
|
|
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(
|
2021-01-26 17:21:12 +01:00
|
|
|
'input[name="token-billing-checkbox"]:checked'
|
2020-03-25 14:08:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (tokenBillingCheckbox) {
|
|
|
|
document.querySelector('input[name="store_card"]').value =
|
2021-02-03 16:49:29 +01:00
|
|
|
tokenBillingCheckbox.value;
|
2020-03-25 14:08:37 +01:00
|
|
|
}
|
|
|
|
|
2020-08-24 10:28:55 +02:00
|
|
|
document.getElementById('server-response').submit();
|
2020-03-25 14:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleFailure(message) {
|
2020-08-24 10:28:55 +02:00
|
|
|
let errors = document.getElementById('errors');
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2020-08-24 10:28:55 +02:00
|
|
|
errors.textContent = '';
|
2020-03-25 14:08:37 +01:00
|
|
|
errors.textContent = message;
|
|
|
|
errors.hidden = false;
|
2020-08-24 08:25:12 +02:00
|
|
|
|
|
|
|
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-03-25 14:08:37 +01:00
|
|
|
}
|
|
|
|
|
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: {
|
2021-01-21 14:03:38 +01:00
|
|
|
billing_details: {name: cardHolderName.value},
|
2020-10-20 16:08:21 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
if (result.error) {
|
2022-09-06 11:40:34 +02:00
|
|
|
return this.handleFailure(result.error.message);
|
2020-10-20 16:08:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.handleSuccessfulAuthorization(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulAuthorization(result) {
|
|
|
|
document.getElementById('gateway_response').value = JSON.stringify(
|
|
|
|
result.setupIntent
|
|
|
|
);
|
|
|
|
|
|
|
|
document.getElementById('server_response').submit();
|
|
|
|
}
|
|
|
|
|
2020-03-25 14:08:37 +01:00
|
|
|
handle() {
|
|
|
|
this.setupStripe();
|
|
|
|
|
2020-10-20 16:08:21 +02:00
|
|
|
if (this.onlyAuthorization) {
|
|
|
|
this.createElement().mountCardElement();
|
|
|
|
|
2020-03-25 14:08:37 +01:00
|
|
|
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-03-25 14:08:37 +01:00
|
|
|
});
|
2020-10-20 16:08:21 +02:00
|
|
|
} else {
|
2021-01-21 14:03:38 +01:00
|
|
|
Array
|
|
|
|
.from(document.getElementsByClassName('toggle-payment-with-token'))
|
|
|
|
.forEach((element) => element.addEventListener('click', (element) => {
|
|
|
|
document.getElementById('stripe--payment-container').classList.add('hidden');
|
2021-01-26 13:55:32 +01:00
|
|
|
document.getElementById('save-card--container').style.display = 'none';
|
2021-01-21 14:03:38 +01:00
|
|
|
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');
|
2021-01-26 13:55:32 +01:00
|
|
|
document.getElementById('save-card--container').style.display = 'grid';
|
2021-01-21 14:03:38 +01:00
|
|
|
document.querySelector('input[name=token]').value = "";
|
|
|
|
});
|
|
|
|
|
|
|
|
this.createElement().mountCardElement();
|
|
|
|
|
|
|
|
document
|
|
|
|
.getElementById('pay-now')
|
|
|
|
.addEventListener('click', () => {
|
2023-03-12 05:51:26 +01:00
|
|
|
|
|
|
|
try {
|
2021-01-21 14:03:38 +01:00
|
|
|
let tokenInput = document.querySelector('input[name=token]');
|
|
|
|
|
|
|
|
if (tokenInput.value) {
|
2020-10-20 16:08:21 +02:00
|
|
|
return this.completePaymentUsingToken();
|
2021-01-21 14:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.completePaymentWithoutToken();
|
2023-03-12 05:51:26 +01:00
|
|
|
}catch(error){
|
|
|
|
console.log(error.message);
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:03:38 +01:00
|
|
|
});
|
2020-03-25 14:08:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ?? '';
|
2020-03-25 14:08:37 +01:00
|
|
|
|
2021-05-10 13:01:59 +02:00
|
|
|
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
|
|
|
|
2021-05-10 13:01:59 +02:00
|
|
|
let s = new StripeCreditCard(publishableKey, secret, onlyAuthorization, stripeConnect);
|
|
|
|
|
|
|
|
s.handle();
|
|
|
|
|
|
|
|
Livewire.on('passed-required-fields-check', () => s.handle());
|