From bad7b242e1bf14ec6985dbab3448d2f82597570f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Dec 2022 23:01:16 +1100 Subject: [PATCH] Fixes for Klarna --- app/Models/Gateway.php | 1 + app/PaymentDrivers/Stripe/Klarna.php | 10 +-- public/mix-manifest.json | 1 + .../js/clients/payments/stripe-klarna.js | 28 +++++++- .../gateways/stripe/klarna/klarna.blade.php | 69 +++++++++++++++++++ .../gateways/stripe/klarna/pay.blade.php | 2 + 6 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 47f35f9c9863..24d53a510c2e 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -115,6 +115,7 @@ class Gateway extends StaticModel GatewayType::BECS => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], GatewayType::IDEAL => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], GatewayType::ACSS => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], + GatewayType::KLARNA => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded', 'payment_intent.succeeded']], GatewayType::FPX => ['refund' => true, 'token_billing' => true, 'webhooks' => ['source.chargeable', 'charge.succeeded']], ]; case 39: return [GatewayType::CREDIT_CARD => ['refund' => true, 'token_billing' => true, 'webhooks' => [' ']]]; //Checkout diff --git a/app/PaymentDrivers/Stripe/Klarna.php b/app/PaymentDrivers/Stripe/Klarna.php index d3e2678d215c..718d3f8cfa1a 100644 --- a/app/PaymentDrivers/Stripe/Klarna.php +++ b/app/PaymentDrivers/Stripe/Klarna.php @@ -19,6 +19,8 @@ use App\Models\Payment; use App\Models\PaymentType; use App\Models\SystemLog; use App\PaymentDrivers\StripePaymentDriver; +use App\Utils\Number; +use Illuminate\Support\Facades\Cache; class Klarna { @@ -50,10 +52,10 @@ class Klarna $invoice_numbers = collect($data['invoices'])->pluck('invoice_number'); - if ($invoice_numbers > 0) { - $description = ctrans('texts.payment_provider_paymenttext', ['invoicenumber' => $invoice_numbers->implode(', '), 'amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]); + if ($invoice_numbers->count() > 0) { + $description = ctrans('texts.stripe_paymenttext', ['invoicenumber' => $invoice_numbers->implode(', '), 'amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]); } else { - $description = ctrans('texts.payment_prvoder_paymenttext_without_invoice', ['amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]); + $description = ctrans('texts.stripe_paymenttext_without_invoice', ['amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]); } $intent = \Stripe\PaymentIntent::create([ @@ -149,6 +151,6 @@ class Klarna $this->stripe->client->company, ); - throw new PaymentFailed(ctrans('texts.payment_provider_failed_process_payment'), 500); + throw new PaymentFailed(ctrans('texts.gateway_error'), 500); } } diff --git a/public/mix-manifest.json b/public/mix-manifest.json index 0c428ad069cb..659ea6979811 100644 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -5,6 +5,7 @@ "/js/clients/payments/forte-credit-card-payment.js": "/js/clients/payments/forte-credit-card-payment.js?id=f42dd0caddb3603e71db061924c4b172", "/js/clients/payments/forte-ach-payment.js": "/js/clients/payments/forte-ach-payment.js?id=b8173c7c0dee76bf9ae6312a963ae0e4", "/js/clients/payments/stripe-ach.js": "/js/clients/payments/stripe-ach.js?id=207f218c44553470287f35f33a7eb154", + "/js/clients/payments/stripe-klarna.js": "/js/clients/payments/stripe-klarna.js?id=1c248bc1f4f45310cd585a95a5055375", "/js/clients/invoices/action-selectors.js": "/js/clients/invoices/action-selectors.js?id=404b7ee18e420de0e73f5402b7e39122", "/js/clients/purchase_orders/action-selectors.js": "/js/clients/purchase_orders/action-selectors.js?id=2f0c4e3bab30a98e33ac768255113174", "/js/clients/purchase_orders/accept.js": "/js/clients/purchase_orders/accept.js?id=9bb483a89a887f753e49c0b635d6276a", diff --git a/resources/js/clients/payments/stripe-klarna.js b/resources/js/clients/payments/stripe-klarna.js index dfaa63092919..67c379abd99d 100644 --- a/resources/js/clients/payments/stripe-klarna.js +++ b/resources/js/clients/payments/stripe-klarna.js @@ -33,6 +33,16 @@ class ProcessKlarna { return this; }; + handleError = (message) => { + document.getElementById('pay-now').disabled = false; + document.querySelector('#pay-now > svg').classList.add('hidden'); + document.querySelector('#pay-now > span').classList.remove('hidden'); + + this.errors.textContent = ''; + this.errors.textContent = message; + this.errors.hidden = false; + }; + handle = () => { document.getElementById('pay-now').addEventListener('click', (e) => { let errors = document.getElementById('errors'); @@ -46,14 +56,28 @@ class ProcessKlarna { { payment_method: { billing_details: { - name: document.getElementById("giropay-name").value, + name: document.getElementById("klarna-name").value, + email: document.querySelector('meta[name=email').content, + address: { + line1: document.querySelector('input[name=address1]').value, + line2: document.querySelector('input[name=address2]').value, + city: document.querySelector('input[name=city]').value, + postal_code: document.querySelector('input[name=postal_code]').value, + state: document.querySelector('input[name=state]').value, + country: document.querySelector('meta[name=country').content, + } }, }, return_url: document.querySelector( 'meta[name="return-url"]' ).content, } - ); + ).then((result) => { + if (result.hasOwnProperty('error')) { + return this.handleError(result.error.message); + } + + });; }); }; } diff --git a/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php b/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php new file mode 100644 index 000000000000..69bda5a6c29c --- /dev/null +++ b/resources/views/portal/ninja2020/gateways/stripe/klarna/klarna.blade.php @@ -0,0 +1,69 @@ +
+ @component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.name')]) +
+ +
+
+ +
+
+ +
+ +
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+ + @endcomponent +
diff --git a/resources/views/portal/ninja2020/gateways/stripe/klarna/pay.blade.php b/resources/views/portal/ninja2020/gateways/stripe/klarna/pay.blade.php index 54989216165f..1995f020acc4 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/klarna/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/klarna/pay.blade.php @@ -11,6 +11,7 @@ + @endsection @@ -22,6 +23,7 @@ @component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.payment_type')]) {{ ctrans('texts.klarna') }} ({{ ctrans('texts.bank_transfer') }}) @endcomponent + @include('portal.ninja2020.gateways.stripe.klarna.klarna') @include('portal.ninja2020.gateways.includes.pay_now') @endsection