From 98f4a25520ad6caaae94f610a7f8f7b7d5c1f6ed Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Fri, 12 Aug 2022 14:15:10 +0800 Subject: [PATCH] fixed paypal capture --- .../Paypal/Controllers/PaypalController.php | 37 ++++++++----------- .../Paypal/Views/checkout/payment.blade.php | 30 +++++++-------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/plugins/Paypal/Controllers/PaypalController.php b/plugins/Paypal/Controllers/PaypalController.php index 191603df..ebc0dada 100644 --- a/plugins/Paypal/Controllers/PaypalController.php +++ b/plugins/Paypal/Controllers/PaypalController.php @@ -19,6 +19,7 @@ namespace Plugin\Paypal\Controllers; use Beike\Repositories\OrderRepo; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; +use Illuminate\Support\Facades\DB; use Srmklive\PayPal\Services\PayPal; class PaypalController @@ -42,11 +43,11 @@ class PaypalController 'client_id' => $paypalSetting['live_client_id'], 'client_secret' => $paypalSetting['live_secret'], ], - 'payment_action' => 'Sale', // Can only be 'Sale', 'Authorization' or 'Order' + 'payment_action' => 'Sale', 'currency' => 'USD', - 'notify_url' => '', // Change this accordingly for your application. - 'locale' => 'en_US', // force gateway language i.e. it_IT, es_ES, en_US ... (for express checkout only) - 'validate_ssl' => true, // Validate SSL when creating api client. + 'notify_url' => '', + 'locale' => 'en_US', + 'validate_ssl' => true, ]; config(['paypal' => null]); $this->paypalClient = new PayPal($config); @@ -69,7 +70,7 @@ class PaypalController public function create(Request $request): JsonResponse { $data = \json_decode($request->getContent(), true); - $orderNumber = $data['order_number']; + $orderNumber = $data['orderNumber']; $customer = current_customer(); $order = OrderRepo::getOrderByNumber($orderNumber, $customer); $orderTotalUsd = currency_format($order->total, 'USD', '', false); @@ -100,30 +101,22 @@ class PaypalController */ public function capture(Request $request) { - $data = json_decode($request->getContent(), true); - $orderId = $data['orderId']; - $this->paypalClient->setApiCredentials(config('paypal')); - $token = $this->paypalClient->getAccessToken(); - $this->paypalClient->setAccessToken($token); - $result = $this->paypalClient->capturePaymentOrder($orderId); + $data = \json_decode($request->getContent(), true); + $orderNumber = $data['orderNumber']; + $customer = current_customer(); + $order = OrderRepo::getOrderByNumber($orderNumber, $customer); + + $paypalOrderId = $data['paypalOrderId']; + $result = $this->paypalClient->capturePaymentOrder($paypalOrderId); -// $result = $result->purchase_units[0]->payments->captures[0]; try { DB::beginTransaction(); if ($result['status'] === "COMPLETED") { - $transaction = new Transaction; - $transaction->vendor_payment_id = $orderId; - $transaction->payment_gateway_id = $data['payment_gateway_id']; - $transaction->user_id = $data['user_id']; - $transaction->status = TransactionStatus::COMPLETED; - $transaction->save(); - $order = Order::where('vendor_order_id', $orderId)->first(); - $order->transaction_id = $transaction->id; - $order->status = TransactionStatus::COMPLETED; + $order->status = 'paid'; $order->save(); DB::commit(); } - } catch (Exception $e) { + } catch (\Exception $e) { DB::rollBack(); dd($e); } diff --git a/plugins/Paypal/Views/checkout/payment.blade.php b/plugins/Paypal/Views/checkout/payment.blade.php index 595e43a8..e8b0d519 100644 --- a/plugins/Paypal/Views/checkout/payment.blade.php +++ b/plugins/Paypal/Views/checkout/payment.blade.php @@ -3,9 +3,11 @@ @if($payment_setting['sandbox_mode']) - + @else - + @endif @@ -18,10 +20,10 @@ return fetch('/plugin/paypal/create', { method: 'POST', headers: { - 'X-CSRF-Token':token + 'X-CSRF-Token': token }, body: JSON.stringify({ - 'order_number': "{{$order->number}}", + orderNumber: "{{$order->number}}", }) }).then(function (res) { //res.json(); @@ -38,28 +40,24 @@ return fetch('/plugin/paypal/capture', { method: 'POST', headers: { - 'X-CSRF-Token':token + 'X-CSRF-Token': token }, body: JSON.stringify({ - orderId: data.orderID, + orderNumber: "{{$order->number}}", + paypalOrderId: data.orderID, payment_gateway_id: $("#payapalId").val(), - user_id: "{{ auth()->user()->id }}", }) }).then(function (res) { // console.log(res.json()); return res.json(); }).then(function (orderData) { - // Successful capture! For demo purposes: - // console.log('Capture result', orderData, JSON.stringify(orderData, null, 2)); - var transaction = orderData.purchase_units[0].payments.captures[0]; - iziToast.success({ - title: 'Success', - message: 'Payment completed', - position: 'topRight' - }); + console.log('Capture result', orderData, JSON.stringify(orderData, null, 2)); + let captureStatus = orderData.status + if (captureStatus === 'COMPLETED') { + window.location.href = "{{ shop_route('account.order.show', $order->number) }}" + } }); } - }).render('#paypal-button-container');