From 69e12dbdb936439af6e9c4fd80e63e3eaaf06c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 1 Nov 2021 14:58:10 +0100 Subject: [PATCH] Storing payment method --- app/PaymentDrivers/Stripe/BrowserPay.php | 33 ++++++++++++++++++- .../js/clients/payments/stripe-browserpay.js | 18 ++++++++++ .../gateways/stripe/browser_pay/pay.blade.php | 28 ++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/app/PaymentDrivers/Stripe/BrowserPay.php b/app/PaymentDrivers/Stripe/BrowserPay.php index ade74e2708..ab02049776 100644 --- a/app/PaymentDrivers/Stripe/BrowserPay.php +++ b/app/PaymentDrivers/Stripe/BrowserPay.php @@ -112,7 +112,7 @@ class BrowserPay implements MethodInterface if ($gateway_response->status === 'succeeded') { if ($request->shouldStoreToken()) { - // + $this->storePaymentMethod(); } return $this->processSuccessfulPayment(); @@ -120,6 +120,37 @@ class BrowserPay implements MethodInterface return $this->processUnsuccessfulPayment(); } + + protected function storePaymentMethod() + { + $customer = new \stdClass; + $customer->id = $this->stripe->findOrCreateCustomer()->id; + + $payment_method = $this->stripe->payment_hash->data->gateway_response->payment_method; + + $this->stripe->attach($payment_method, $customer); + + $method = $this->stripe->getStripePaymentMethod($payment_method); + + try { + $payment_meta = new \stdClass; + $payment_meta->exp_month = (string) $method->card->exp_month; + $payment_meta->exp_year = (string) $method->card->exp_year; + $payment_meta->brand = (string) $method->card->brand; + $payment_meta->last4 = (string) $method->card->last4; + $payment_meta->type = GatewayType::APPLE_PAY; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $method->id, + 'payment_method_id' => GatewayType::APPLE_PAY, + ]; + + $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]); + } catch (\Exception $e) { + return $this->stripe->processInternallyFailedPayment($this->stripe, $e); + } + } /** * Handle successful payment for browser pay. diff --git a/resources/js/clients/payments/stripe-browserpay.js b/resources/js/clients/payments/stripe-browserpay.js index de371e968a..b4b8d8187b 100644 --- a/resources/js/clients/payments/stripe-browserpay.js +++ b/resources/js/clients/payments/stripe-browserpay.js @@ -91,6 +91,15 @@ class StripeBrowserPay { result.paymentIntent ); + 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(); @@ -103,6 +112,15 @@ class StripeBrowserPay { confirmResult.paymentIntent ); + 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(); } } diff --git a/resources/views/portal/ninja2020/gateways/stripe/browser_pay/pay.blade.php b/resources/views/portal/ninja2020/gateways/stripe/browser_pay/pay.blade.php index baf36493f0..968515f103 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/browser_pay/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/browser_pay/pay.blade.php @@ -20,12 +20,40 @@ + @include('portal.ninja2020.gateways.includes.payment_details') + @component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.pay_with')]) + @if(count($tokens) > 0) + @foreach($tokens as $token) + + @endforeach + @endisset + + + @endcomponent + + @include('portal.ninja2020.gateways.includes.save_card') + @component('portal.ninja2020.components.general.card-element-single')
@endcomponent