Standalone credit card adding

This commit is contained in:
Benjamin Beganović 2021-08-17 13:16:58 +02:00
parent 5f7f859768
commit e0b0879ed5

View File

@ -2,6 +2,8 @@
=> ctrans('texts.payment_type_credit_card')]) => ctrans('texts.payment_type_credit_card')])
@section('gateway_head') @section('gateway_head')
<meta name="square-appId" content="{{ $gateway->company_gateway->getConfigField('applicationId') }}">
<meta name="square-locationId" content="{{ $gateway->company_gateway->getConfigField('locationId') }}">
@endsection @endsection
@section('gateway_content') @section('gateway_content')
@ -9,24 +11,21 @@
method="post" id="server_response"> method="post" id="server_response">
@csrf @csrf
<input type="text" name="sourceId" id="sourceId" hidden> <input type="text" name="sourceId" id="sourceId" hidden>
</form>
<div class="alert alert-failure mb-4" hidden id="errors"></div> <div class="alert alert-failure mb-4" hidden id="errors"></div>
@component('portal.ninja2020.components.general.card-element-single') @component('portal.ninja2020.components.general.card-element-single')
<div id="card-container"></div> <div id="card-container"></div>
<div id="payment-status-container"></div> <div id="payment-status-container"></div>
</form>
@endcomponent @endcomponent
@component('portal.ninja2020.gateways.includes.pay_now') @component('portal.ninja2020.gateways.includes.pay_now', ['id' => 'authorize-card'])
{{ ctrans('texts.add_payment_method') }} {{ ctrans('texts.add_payment_method') }}
@endcomponent @endcomponent
@endsection @endsection
@section('gateway_footer') @section('gateway_footer')
@if ($gateway->company_gateway->getConfigField('testMode')) @if ($gateway->company_gateway->getConfigField('testMode'))
<script type="text/javascript" src="https://sandbox.web.squarecdn.com/v1/square.js"></script> <script type="text/javascript" src="https://sandbox.web.squarecdn.com/v1/square.js"></script>
@else @else
@ -34,136 +33,47 @@
@endif @endif
<script> <script>
const appId = "{{ $gateway->company_gateway->getConfigField('applicationId') }}"; class SquareCreditCard {
const locationId = "{{ $gateway->company_gateway->getConfigField('locationId') }}"; constructor() {
this.appId = document.querySelector('meta[name=square-appId]').content;
const darkModeCardStyle = { this.locationId = document.querySelector('meta[name=square-locationId]').content;
'.input-container': {
borderColor: '#2D2D2D',
borderRadius: '6px',
},
'.input-container.is-focus': {
borderColor: '#006AFF',
},
'.input-container.is-error': {
borderColor: '#ff1600',
},
'.message-text': {
color: '#999999',
},
'.message-icon': {
color: '#999999',
},
'.message-text.is-error': {
color: '#ff1600',
},
'.message-icon.is-error': {
color: '#ff1600',
},
input: {
backgroundColor: '#2D2D2D',
color: '#FFFFFF',
fontFamily: 'helvetica neue, sans-serif',
},
'input::placeholder': {
color: '#999999',
},
'input.is-error': {
color: '#ff1600',
},
};
async function initializeCard(payments) {
const card = await payments.card({
style: darkModeCardStyle,
});
await card.attach('#card-container');
return card;
} }
async function tokenize(paymentMethod) { async init() {
const tokenResult = await paymentMethod.tokenize(); this.payments = Square.payments(this.appId, this.locationId);
if (tokenResult.status === 'OK') {
return tokenResult.token; this.card = await this.payments.card();
} else {
let errorMessage = `Tokenization failed with status: ${tokenResult.status}`; await this.card.attach('#card-container');
if (tokenResult.errors) {
errorMessage += ` and errors: ${JSON.stringify(
tokenResult.errors
)}`;
} }
throw new Error(errorMessage); async completePaymentWithoutToken(e) {
document.getElementById('errors').hidden = true;
e.target.parentElement.disabled = true;
let result = await this.card.tokenize();
if (result.status === 'OK') {
document.getElementById('sourceId').value = result.token;
return document.getElementById('server_response').submit();
}
document.getElementById('errors').textContent = result.errors[0].message;
document.getElementById('errors').hidden = false;
e.target.parentElement.disabled = false;
}
async handle() {
await this.init();
document
.getElementById('authorize-card')
?.addEventListener('click', (e) => this.completePaymentWithoutToken(e));
} }
} }
// status is either SUCCESS or FAILURE; new SquareCreditCard().handle();
function displayPaymentResults(status) {
const statusContainer = document.getElementById(
'payment-status-container'
);
if (status === 'SUCCESS') {
statusContainer.classList.remove('is-failure');
statusContainer.classList.add('is-success');
} else {
statusContainer.classList.remove('is-success');
statusContainer.classList.add('is-failure');
}
statusContainer.style.visibility = 'visible';
}
document.addEventListener('DOMContentLoaded', async function () {
if (!window.Square) {
throw new Error('Square.js failed to load properly');
}
let payments;
try {
payments = window.Square.payments(appId, locationId);
} catch {
const statusContainer = document.getElementById(
'payment-status-container'
);
statusContainer.className = 'missing-credentials';
statusContainer.style.visibility = 'visible';
return;
}
let card;
try {
card = await initializeCard(payments);
} catch (e) {
console.error('Initializing Card failed', e);
return;
}
async function handlePaymentMethodSubmission(event, paymentMethod) {
event.preventDefault();
try {
// disable the submit button as we await tokenization and make a payment request.
cardButton.disabled = true;
const token = await tokenize(paymentMethod);
document.getElementById('sourceId').value = token;
document.getElementById('server_response').submit();
displayPaymentResults('SUCCESS');
} catch (e) {
cardButton.disabled = false;
displayPaymentResults('FAILURE');
console.error(e.message);
}
}
const cardButton = document.getElementById('pay-now');
cardButton.addEventListener('click', async function (event) {
await handlePaymentMethodSubmission(event, card);
});
});
</script> </script>
@endsection @endsection