From e63a7f9875e1b99b40db40144fe3f8b3bc3e675d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 19 Jun 2021 17:31:05 +1000 Subject: [PATCH] WePay checkout --- app/PaymentDrivers/WePay/CreditCard.php | 57 ++++++++++++++++++- app/PaymentDrivers/WePayPaymentDriver.php | 8 ++- .../gateways/wepay/credit_card/pay.blade.php | 3 + 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/app/PaymentDrivers/WePay/CreditCard.php b/app/PaymentDrivers/WePay/CreditCard.php index b3249cf5e6..a927692648 100644 --- a/app/PaymentDrivers/WePay/CreditCard.php +++ b/app/PaymentDrivers/WePay/CreditCard.php @@ -99,9 +99,40 @@ class CreditCard public function paymentResponse(PaymentResponseRequest $request) { + nlog("payment response"); + $this->wepay_payment_driver->init(); + + //it could be an existing token or a new credit_card_id that needs to be converted into a wepay token + if($request->has('credit_card_id') && $request->input('credit_card_id')) + { + nlog("authorize the card first!"); + + $response = $this->wepay_payment_driver->wepay->request('credit_card/authorize', array( + 'client_id' => config('ninja.wepay.client_id'), + 'client_secret' => config('ninja.wepay.client_secret'), + 'credit_card_id' => (int)$request->input('credit_card_id'), + )); + + $credit_card_id = (int)$response->credit_card_id; + + if(in_array($response->state, ['new', 'authorized']) && boolval($request->input('store_card'))){ + + $this->storePaymentMethod($response, GatewayType::CREDIT_CARD); + + } + + } + else { + + $credit_card_id = (int)$request->input('token'); + + } + // USD, CAD, and GBP. nlog($request->all()); // charge the credit card + nlog($this->wepay_payment_driver->wepay); + $response = $this->wepay_payment_driver->wepay->request('checkout/create', array( 'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'), 'amount' => $this->wepay_payment_driver->payment_hash->data->amount_with_fee, @@ -111,13 +142,37 @@ class CreditCard 'payment_method' => array( 'type' => 'credit_card', 'credit_card' => array( - 'id' => $data['token'] + 'id' => $credit_card_id ) ) )); + print_r($response); + + if(in_array($response->state, ['authorized', 'captured'])){ + //success + nlog("success"); + } + + if(in_array($response->state, ['released', 'cancelled', 'failed', 'expired'])){ + //some type of failure + nlog("failure"); + } + } +/* +new The checkout was created by the application. This state typically indicates that checkouts created in WePay's hosted checkout flow are waiting for the payer to submit their information. +authorized The payer entered their payment info and confirmed the payment on WePay. WePay has successfully charged the card. +captured The payment has been reserved from the payer. +released The payment has been credited to the payee account. Note that the released state may be active although there are active partial refunds or partial chargebacks. +cancelled The payment has been cancelled by the payer, payee, or application. +refunded The payment was captured and then refunded by the payer, payee, or application. The payment has been debited from the payee account. +charged back The payment has been charged back by the payer and the payment has been debited from the payee account. +failed The payment has failed. +expired Checkouts expire if they remain in the new state for more than 30 minutes (e.g., they have been abandoned). + */ + /* https://developer.wepay.com/api/api-calls/checkout { diff --git a/app/PaymentDrivers/WePayPaymentDriver.php b/app/PaymentDrivers/WePayPaymentDriver.php index 0ef6c063bd..d5f976bef2 100644 --- a/app/PaymentDrivers/WePayPaymentDriver.php +++ b/app/PaymentDrivers/WePayPaymentDriver.php @@ -65,8 +65,8 @@ class WePayPaymentDriver extends BaseDriver if ($this->company_gateway) $this->wepay = new WePay($this->company_gateway->getConfigField('accessToken')); - - $this->wepay = new WePay(null); + else + $this->wepay = new WePay(null); return $this; @@ -148,11 +148,15 @@ class WePayPaymentDriver extends BaseDriver public function processPaymentView(array $data) { + $this->init(); + return $this->payment_method->paymentView($data); //this is your custom implementation from here } public function processPaymentResponse($request) { + $this->init(); + return $this->payment_method->paymentResponse($request); //this is your custom implementation from here } diff --git a/resources/views/portal/ninja2020/gateways/wepay/credit_card/pay.blade.php b/resources/views/portal/ninja2020/gateways/wepay/credit_card/pay.blade.php index 99ada51176..572fd27af2 100644 --- a/resources/views/portal/ninja2020/gateways/wepay/credit_card/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/wepay/credit_card/pay.blade.php @@ -17,6 +17,7 @@ + @@ -147,6 +148,7 @@ function handleTokenPayment(token) { + document.querySelector('input[name="credit_card_id"]').value = null; document.querySelector('input[name="token"]').value = token; document.getElementById('server-response').submit(); @@ -230,6 +232,7 @@ var token = data.credit_card_id; + document.querySelector('input[name="credit_card_id"]').value = null; document.querySelector('input[name="token"]').value = token; document.getElementById('server-response').submit();