1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/resources/js/clients/payments/paytrace-credit-card.js
Benjamin Beganović b7c248eec5
PayTrace improvements (#43)
* Refactor credit card payment JavaScript

* CreditCard.php: Rename $paytrace_driver to $paytrace

* Credit card: Support for token billing (frontend)

* Fixes for "save card" label

* Credit card: Authorize

* Allow PayTrace to be seeded from CreateSingleAccount

* Add PayTrace decrypted config to ninja.testvars

* Extract to separate JavaScript

* Scaffold Dusk test

* CreditCard -> CreditCardTest

* CreditCard -> CreditCardTest

* Scaffold pay with new card test

* Fixes for gateway_key in CreateSingleAccount

* Production builds
2021-07-28 07:59:44 +10:00

187 lines
5.9 KiB
JavaScript
Vendored

/**
* 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://www.elastic.co/licensing/elastic-license
*/
class PayTraceCreditCard {
constructor() {
this.clientKey = document.querySelector(
'meta[name=paytrace-client-key]'
)?.content;
}
get creditCardStyles() {
return {
font_color: '#111827',
border_color: 'rgba(210,214,220,1)',
label_color: '#111827',
label_size: '12pt',
background_color: 'white',
border_style: 'solid',
font_size: '15pt',
height: '30px',
width: '100%',
};
}
get codeStyles() {
return {
font_color: '#111827',
border_color: 'rgba(210,214,220,1)',
label_color: '#111827',
label_size: '12pt',
background_color: 'white',
border_style: 'solid',
font_size: '15pt',
height: '30px',
width: '300px',
};
}
get expStyles() {
return {
font_color: '#111827',
border_color: 'rgba(210,214,220,1)',
label_color: '#111827',
label_size: '12pt',
background_color: 'white',
border_style: 'solid',
font_size: '15pt',
height: '30px',
width: '85px',
type: 'dropdown',
};
}
updatePayTraceLabels() {
window.PTPayment.getControl('securityCode').label.text(
document.querySelector('meta[name=ctrans-cvv]').content
);
window.PTPayment.getControl('creditCard').label.text(
document.querySelector('meta[name=ctrans-card_number]').content
);
window.PTPayment.getControl('expiration').label.text(
document.querySelector('meta[name=ctrans-expires]').content
);
}
setupPayTrace() {
return window.PTPayment.setup({
styles: {
code: this.codeStyles,
cc: this.creditCardStyles,
exp: this.expStyles,
},
authorization: {
clientKey: this.clientKey,
},
});
}
handlePaymentWithCreditCard(event) {
event.target.parentElement.disabled = true;
document.getElementById('errors').hidden = true;
window.PTPayment.validate((errors) => {
if (errors.length >= 1) {
let errorsContainer = document.getElementById('errors');
errorsContainer.textContent = errors[0].description;
errorsContainer.hidden = false;
return (event.target.parentElement.disabled = false);
}
this.ptInstance
.process()
.then((response) => {
document.getElementById('HPF_Token').value =
response.message.hpf_token;
document.getElementById('enc_key').value =
response.message.enc_key;
let tokenBillingCheckbox = document.querySelector(
'input[name="token-billing-checkbox"]:checked'
);
if (tokenBillingCheckbox) {
document.querySelector(
'input[name="store_card"]'
).value = tokenBillingCheckbox.value;
}
document.getElementById('server_response').submit();
})
.catch((error) => {
document.getElementById(
'errors'
).textContent = JSON.stringify(error);
document.getElementById('errors').hidden = false;
console.log(error);
});
});
}
handlePaymentWithToken(event) {
event.target.parentElement.disabled = true;
document.getElementById('server_response').submit();
}
handle() {
this.setupPayTrace().then((instance) => {
this.ptInstance = instance;
this.updatePayTraceLabels();
Array.from(
document.getElementsByClassName('toggle-payment-with-token')
).forEach((element) =>
element.addEventListener('click', (element) => {
document
.getElementById('paytrace--credit-card-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('paytrace--credit-card-container')
.classList.remove('hidden');
document.getElementById(
'save-card--container'
).style.display = 'grid';
document.querySelector('input[name=token]').value = '';
});
document
.getElementById('pay-now')
.addEventListener('click', (e) => {
if (
document.querySelector('input[name=token]').value === ''
) {
return this.handlePaymentWithCreditCard(e);
}
return this.handlePaymentWithToken(e);
});
});
}
}
new PayTraceCreditCard().handle();