From cacd3e0790df099d68fd65b62b159c0588997b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 9 Aug 2024 01:11:31 +0200 Subject: [PATCH] New payment flow: Refactor base driver (#70) * pass livewirePaymentView & processPaymentView thru base driver * add paymentData to the interface --- .../CheckoutComPaymentDriver.php | 18 +------- app/PaymentDrivers/Forte/ACH.php | 28 +++++++++++-- app/PaymentDrivers/Forte/CreditCard.php | 30 +++++++++++--- .../js/clients/payments/forte-ach-payment.js | 16 +++++--- .../payments/forte-credit-card-payment.js | 41 +++++++++++++------ .../gateways/forte/ach/pay.blade.php | 1 + .../gateways/forte/credit_card/pay.blade.php | 2 + .../forte/includes/credit_card.blade.php | 12 +++--- 8 files changed, 100 insertions(+), 48 deletions(-) diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index ea286ced663b..72d31a7c410a 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -46,7 +46,7 @@ use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use Checkout\Payments\Previous\PaymentRequest as PreviousPaymentRequest; use Checkout\Payments\Previous\Source\RequestIdSource as SourceRequestIdSource; -class CheckoutComPaymentDriver extends BaseDriver implements LivewireMethodInterface +class CheckoutComPaymentDriver extends BaseDriver { use SystemLogTrait; use Utilities; @@ -190,17 +190,6 @@ class CheckoutComPaymentDriver extends BaseDriver implements LivewireMethodInter return $this->payment_method->paymentView($data); } - /** - * Process payment view for the Livewire payments. - * - * @param array $data - * @return array - */ - public function processPaymentViewData(array $data): array - { - return $this->payment_method->paymentData($data); - } - /** * Process the payment response * @@ -631,9 +620,4 @@ class CheckoutComPaymentDriver extends BaseDriver implements LivewireMethodInter }); } - - public function livewirePaymentView(array $data): string - { - return $this->payment_method->livewirePaymentView($data); - } } diff --git a/app/PaymentDrivers/Forte/ACH.php b/app/PaymentDrivers/Forte/ACH.php index 8ea313e77200..750ffdd0f3b9 100644 --- a/app/PaymentDrivers/Forte/ACH.php +++ b/app/PaymentDrivers/Forte/ACH.php @@ -19,11 +19,12 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; +use App\PaymentDrivers\Common\LivewireMethodInterface; use App\PaymentDrivers\FortePaymentDriver; use App\Utils\Traits\MakesHash; use Illuminate\Support\Facades\Validator; -class ACH +class ACH implements LivewireMethodInterface { use MakesHash; @@ -79,10 +80,8 @@ class ACH public function paymentView(array $data) { - $this->forte->payment_hash->data = array_merge((array) $this->forte->payment_hash->data, $data); - $this->forte->payment_hash->save(); + $data = $this->paymentData($data); - $data['gateway'] = $this->forte; return render('gateways.forte.ach.pay', $data); } @@ -175,4 +174,25 @@ class ACH return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]); } + + /** + * @inheritDoc + */ + public function livewirePaymentView(array $data): string + { + return 'gateways.forte.ach.pay_livewire'; + } + + /** + * @inheritDoc + */ + public function paymentData(array $data): array + { + $this->forte->payment_hash->data = array_merge((array) $this->forte->payment_hash->data, $data); + $this->forte->payment_hash->save(); + + $data['gateway'] = $this->forte; + + return $data; + } } diff --git a/app/PaymentDrivers/Forte/CreditCard.php b/app/PaymentDrivers/Forte/CreditCard.php index 04942e046b74..9e3356ce8a6a 100644 --- a/app/PaymentDrivers/Forte/CreditCard.php +++ b/app/PaymentDrivers/Forte/CreditCard.php @@ -20,11 +20,12 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; +use App\PaymentDrivers\Common\LivewireMethodInterface; use App\PaymentDrivers\FortePaymentDriver; use App\Utils\Traits\MakesHash; use Illuminate\Support\Facades\Validator; -class CreditCard +class CreditCard implements LivewireMethodInterface { use MakesHash; @@ -157,10 +158,8 @@ class CreditCard public function paymentView(array $data) { - $this->forte->payment_hash->data = array_merge((array) $this->forte->payment_hash->data, $data); - $this->forte->payment_hash->save(); - - $data['gateway'] = $this->forte; + $data = $this->paymentData($data); + return render('gateways.forte.credit_card.pay', $data); } @@ -287,4 +286,25 @@ class CreditCard return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]); } + + /** + * @inheritDoc + */ + public function livewirePaymentView(array $data): string + { + return 'gateways.forte.credit_card.pay_livewire'; + } + + /** + * @inheritDoc + */ + public function paymentData(array $data): array + { + $this->forte->payment_hash->data = array_merge((array) $this->forte->payment_hash->data, $data); + $this->forte->payment_hash->save(); + + $data['gateway'] = $this->forte; + + return $data; + } } diff --git a/resources/js/clients/payments/forte-ach-payment.js b/resources/js/clients/payments/forte-ach-payment.js index 90bf9f675377..8e50c5e2a2ba 100644 --- a/resources/js/clients/payments/forte-ach-payment.js +++ b/resources/js/clients/payments/forte-ach-payment.js @@ -8,6 +8,8 @@ * @license https://opensource.org/licenses/AAL */ +import { wait, instant } from '../wait'; + class ForteAuthorizeACH { constructor(apiLoginId) { this.apiLoginId = apiLoginId; @@ -73,9 +75,13 @@ class ForteAuthorizeACH { }; } -const apiLoginId = document.querySelector( - 'meta[name="forte-api-login-id"]' -).content; +function boot() { + const apiLoginId = document.querySelector( + 'meta[name="forte-api-login-id"]' + ).content; + + /** @handle */ + new ForteAuthorizeACH(apiLoginId).handle(); +} -/** @handle */ -new ForteAuthorizeACH(apiLoginId).handle(); +instant() ? boot() : wait('#force-ach-payment').then(() => boot()); diff --git a/resources/js/clients/payments/forte-credit-card-payment.js b/resources/js/clients/payments/forte-credit-card-payment.js index e406d2153911..243d13935a38 100644 --- a/resources/js/clients/payments/forte-credit-card-payment.js +++ b/resources/js/clients/payments/forte-credit-card-payment.js @@ -8,21 +8,34 @@ * @license https://opensource.org/licenses/AAL */ +import { wait, instant } from '../wait'; +import { SimpleCard } from '@invoiceninja/simple-card'; + class ForteAuthorizeCard { constructor(apiLoginId) { this.apiLoginId = apiLoginId; this.cardHolderName = document.getElementById('cardholder_name'); + + this.sc = new SimpleCard({ + fields: { + card: { + number: '#number', + date: '#date', + cvv: '#cvv', + }, + }, + }); + + this.sc.mount(); } handleAuthorization = () => { - var myCard = $('#my-card'); - - var data = { + const data = { api_login_id: this.apiLoginId, - card_number: myCard.CardJs('cardNumber').replace(/[^\d]/g, ''), - expire_year: myCard.CardJs('expiryYear').replace(/[^\d]/g, ''), - expire_month: myCard.CardJs('expiryMonth').replace(/[^\d]/g, ''), - cvv: document.getElementById('cvv').value.replace(/[^\d]/g, ''), + card_number: this.sc.value('number')?.replace(/[^\d]/g, ''), + expire_year: `20${this.sc.value('year')?.replace(/[^\d]/g, '')}`, + expire_month: this.sc.value('month')?.replace(/[^\d]/g, ''), + cvv: this.sc.value('cvv')?.replace(/[^\d]/g, ''), }; let payNowButton = document.getElementById('pay-now'); @@ -162,9 +175,13 @@ class ForteAuthorizeCard { } -const apiLoginId = document.querySelector( - 'meta[name="forte-api-login-id"]' -).content; +function boot() { + const apiLoginId = document.querySelector( + 'meta[name="forte-api-login-id"]' + ).content; -/** @handle */ -new ForteAuthorizeCard(apiLoginId).handle(); + /** @handle */ + new ForteAuthorizeCard(apiLoginId).handle(); +} + +instant() ? boot() : wait('#forte-credit-card-payment').then(() => boot()); diff --git a/resources/views/portal/ninja2020/gateways/forte/ach/pay.blade.php b/resources/views/portal/ninja2020/gateways/forte/ach/pay.blade.php index 8dfab33c4269..89d1a308f20a 100644 --- a/resources/views/portal/ninja2020/gateways/forte/ach/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/forte/ach/pay.blade.php @@ -2,6 +2,7 @@ @section('gateway_head') + @endsection @section('gateway_content') diff --git a/resources/views/portal/ninja2020/gateways/forte/credit_card/pay.blade.php b/resources/views/portal/ninja2020/gateways/forte/credit_card/pay.blade.php index bb5901a58230..9c2c4c6a78f8 100644 --- a/resources/views/portal/ninja2020/gateways/forte/credit_card/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/forte/credit_card/pay.blade.php @@ -2,6 +2,8 @@ @section('gateway_head') + + diff --git a/resources/views/portal/ninja2020/gateways/forte/includes/credit_card.blade.php b/resources/views/portal/ninja2020/gateways/forte/includes/credit_card.blade.php index b15c3332764a..fa46d3ca7faf 100644 --- a/resources/views/portal/ninja2020/gateways/forte/includes/credit_card.blade.php +++ b/resources/views/portal/ninja2020/gateways/forte/includes/credit_card.blade.php @@ -1,11 +1,13 @@
- - - - - + + +
+ + +