2021-10-31 16:50:16 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
2022-06-08 06:25:44 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-10-31 16:50:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
class StripeBrowserPay {
|
|
|
|
constructor() {
|
|
|
|
this.clientSecret = document.querySelector(
|
|
|
|
'meta[name=stripe-pi-client-secret]'
|
|
|
|
)?.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2021-11-01 17:17:15 +01:00
|
|
|
let config = {};
|
|
|
|
|
|
|
|
if (document.querySelector('meta[name=stripe-account-id]')) {
|
|
|
|
config.apiVersion = '2020-08-27';
|
|
|
|
|
|
|
|
config.stripeAccount = document.querySelector(
|
|
|
|
'meta[name=stripe-account-id]'
|
|
|
|
)?.content;
|
|
|
|
}
|
|
|
|
|
2021-10-31 16:50:16 +01:00
|
|
|
this.stripe = Stripe(
|
2021-11-02 16:30:37 +01:00
|
|
|
document.querySelector('meta[name=stripe-publishable-key]')
|
|
|
|
?.content,
|
2021-11-01 17:17:15 +01:00
|
|
|
config
|
2021-10-31 16:50:16 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.elements = this.stripe.elements();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
createPaymentRequest() {
|
|
|
|
this.paymentRequest = this.stripe.paymentRequest(
|
|
|
|
JSON.parse(
|
|
|
|
document.querySelector('meta[name=payment-request-data').content
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
createPaymentRequestButton() {
|
|
|
|
this.paymentRequestButton = this.elements.create(
|
|
|
|
'paymentRequestButton',
|
|
|
|
{
|
|
|
|
paymentRequest: this.paymentRequest,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePaymentRequestEvents(stripe, clientSecret) {
|
2021-11-01 14:44:32 +01:00
|
|
|
document.querySelector('#errors').hidden = true;
|
|
|
|
|
2021-10-31 16:50:16 +01:00
|
|
|
this.paymentRequest.on('paymentmethod', function (ev) {
|
|
|
|
stripe
|
|
|
|
.confirmCardPayment(
|
|
|
|
clientSecret,
|
|
|
|
{ payment_method: ev.paymentMethod.id },
|
|
|
|
{ handleActions: false }
|
|
|
|
)
|
|
|
|
.then(function (confirmResult) {
|
|
|
|
if (confirmResult.error) {
|
2022-03-17 01:39:00 +01:00
|
|
|
|
2021-11-01 14:44:32 +01:00
|
|
|
document.querySelector('#errors').innerText =
|
|
|
|
confirmResult.error.message;
|
|
|
|
document.querySelector('#errors').hidden = false;
|
2022-03-17 01:39:00 +01:00
|
|
|
|
|
|
|
ev.complete('fail');
|
2021-10-31 16:50:16 +01:00
|
|
|
} else {
|
|
|
|
ev.complete('success');
|
2021-11-01 14:39:18 +01:00
|
|
|
|
2021-10-31 16:50:16 +01:00
|
|
|
if (
|
|
|
|
confirmResult.paymentIntent.status ===
|
|
|
|
'requires_action'
|
|
|
|
) {
|
|
|
|
stripe
|
|
|
|
.confirmCardPayment(clientSecret)
|
|
|
|
.then(function (result) {
|
|
|
|
if (result.error) {
|
|
|
|
ev.complete('fail');
|
2021-11-01 14:44:32 +01:00
|
|
|
|
|
|
|
document.querySelector(
|
|
|
|
'#errors'
|
|
|
|
).innerText = result.error.message;
|
|
|
|
|
|
|
|
document.querySelector(
|
|
|
|
'#errors'
|
|
|
|
).hidden = false;
|
2021-10-31 16:50:16 +01:00
|
|
|
} else {
|
2021-11-01 14:39:18 +01:00
|
|
|
document.querySelector(
|
|
|
|
'input[name="gateway_response"]'
|
|
|
|
).value = JSON.stringify(
|
|
|
|
result.paymentIntent
|
|
|
|
);
|
|
|
|
|
|
|
|
document
|
|
|
|
.getElementById('server-response')
|
|
|
|
.submit();
|
2021-10-31 16:50:16 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2021-11-01 14:39:18 +01:00
|
|
|
document.querySelector(
|
|
|
|
'input[name="gateway_response"]'
|
|
|
|
).value = JSON.stringify(
|
|
|
|
confirmResult.paymentIntent
|
|
|
|
);
|
|
|
|
|
|
|
|
document.getElementById('server-response').submit();
|
2021-10-31 16:50:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handle() {
|
|
|
|
this.init().createPaymentRequest().createPaymentRequestButton();
|
|
|
|
|
|
|
|
this.paymentRequest.canMakePayment().then((result) => {
|
|
|
|
if (result) {
|
|
|
|
return this.paymentRequestButton.mount(
|
|
|
|
'#payment-request-button'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelector('#errors').innerHTML = JSON.parse(
|
|
|
|
document.querySelector('meta[name=no-available-methods]')
|
|
|
|
?.content
|
|
|
|
);
|
|
|
|
|
|
|
|
document.querySelector('#errors').hidden = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.handlePaymentRequestEvents(this.stripe, this.clientSecret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new StripeBrowserPay().handle();
|