fixed paypal capture

This commit is contained in:
Edward Yang 2022-08-12 14:15:10 +08:00
parent 8731be2ab1
commit 98f4a25520
2 changed files with 29 additions and 38 deletions

View File

@ -19,6 +19,7 @@ namespace Plugin\Paypal\Controllers;
use Beike\Repositories\OrderRepo; use Beike\Repositories\OrderRepo;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Srmklive\PayPal\Services\PayPal; use Srmklive\PayPal\Services\PayPal;
class PaypalController class PaypalController
@ -42,11 +43,11 @@ class PaypalController
'client_id' => $paypalSetting['live_client_id'], 'client_id' => $paypalSetting['live_client_id'],
'client_secret' => $paypalSetting['live_secret'], 'client_secret' => $paypalSetting['live_secret'],
], ],
'payment_action' => 'Sale', // Can only be 'Sale', 'Authorization' or 'Order' 'payment_action' => 'Sale',
'currency' => 'USD', 'currency' => 'USD',
'notify_url' => '', // Change this accordingly for your application. 'notify_url' => '',
'locale' => 'en_US', // force gateway language i.e. it_IT, es_ES, en_US ... (for express checkout only) 'locale' => 'en_US',
'validate_ssl' => true, // Validate SSL when creating api client. 'validate_ssl' => true,
]; ];
config(['paypal' => null]); config(['paypal' => null]);
$this->paypalClient = new PayPal($config); $this->paypalClient = new PayPal($config);
@ -69,7 +70,7 @@ class PaypalController
public function create(Request $request): JsonResponse public function create(Request $request): JsonResponse
{ {
$data = \json_decode($request->getContent(), true); $data = \json_decode($request->getContent(), true);
$orderNumber = $data['order_number']; $orderNumber = $data['orderNumber'];
$customer = current_customer(); $customer = current_customer();
$order = OrderRepo::getOrderByNumber($orderNumber, $customer); $order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$orderTotalUsd = currency_format($order->total, 'USD', '', false); $orderTotalUsd = currency_format($order->total, 'USD', '', false);
@ -100,30 +101,22 @@ class PaypalController
*/ */
public function capture(Request $request) public function capture(Request $request)
{ {
$data = json_decode($request->getContent(), true); $data = \json_decode($request->getContent(), true);
$orderId = $data['orderId']; $orderNumber = $data['orderNumber'];
$this->paypalClient->setApiCredentials(config('paypal')); $customer = current_customer();
$token = $this->paypalClient->getAccessToken(); $order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$this->paypalClient->setAccessToken($token);
$result = $this->paypalClient->capturePaymentOrder($orderId); $paypalOrderId = $data['paypalOrderId'];
$result = $this->paypalClient->capturePaymentOrder($paypalOrderId);
// $result = $result->purchase_units[0]->payments->captures[0];
try { try {
DB::beginTransaction(); DB::beginTransaction();
if ($result['status'] === "COMPLETED") { if ($result['status'] === "COMPLETED") {
$transaction = new Transaction; $order->status = 'paid';
$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->save(); $order->save();
DB::commit(); DB::commit();
} }
} catch (Exception $e) { } catch (\Exception $e) {
DB::rollBack(); DB::rollBack();
dd($e); dd($e);
} }

View File

@ -3,9 +3,11 @@
<!-- Include the PayPal JavaScript SDK --> <!-- Include the PayPal JavaScript SDK -->
@if($payment_setting['sandbox_mode']) @if($payment_setting['sandbox_mode'])
<script src="https://www.paypal.com/sdk/js?client-id={{ plugin_setting('paypal.sandbox_client_id') }}&currency=USD"></script> <script
src="https://www.paypal.com/sdk/js?client-id={{ plugin_setting('paypal.sandbox_client_id') }}&currency=USD"></script>
@else @else
<script src="https://www.paypal.com/sdk/js?client-id={{ plugin_setting('paypal.live_client_id') }}&currency=USD"></script> <script
src="https://www.paypal.com/sdk/js?client-id={{ plugin_setting('paypal.live_client_id') }}&currency=USD"></script>
@endif @endif
@ -18,10 +20,10 @@
return fetch('/plugin/paypal/create', { return fetch('/plugin/paypal/create', {
method: 'POST', method: 'POST',
headers: { headers: {
'X-CSRF-Token':token 'X-CSRF-Token': token
}, },
body: JSON.stringify({ body: JSON.stringify({
'order_number': "{{$order->number}}", orderNumber: "{{$order->number}}",
}) })
}).then(function (res) { }).then(function (res) {
//res.json(); //res.json();
@ -38,28 +40,24 @@
return fetch('/plugin/paypal/capture', { return fetch('/plugin/paypal/capture', {
method: 'POST', method: 'POST',
headers: { headers: {
'X-CSRF-Token':token 'X-CSRF-Token': token
}, },
body: JSON.stringify({ body: JSON.stringify({
orderId: data.orderID, orderNumber: "{{$order->number}}",
paypalOrderId: data.orderID,
payment_gateway_id: $("#payapalId").val(), payment_gateway_id: $("#payapalId").val(),
user_id: "{{ auth()->user()->id }}",
}) })
}).then(function (res) { }).then(function (res) {
// console.log(res.json()); // console.log(res.json());
return res.json(); return res.json();
}).then(function (orderData) { }).then(function (orderData) {
// Successful capture! For demo purposes: // Successful capture! For demo purposes:
// console.log('Capture result', orderData, JSON.stringify(orderData, null, 2)); console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0]; let captureStatus = orderData.status
iziToast.success({ if (captureStatus === 'COMPLETED') {
title: 'Success', window.location.href = "{{ shop_route('account.order.show', $order->number) }}"
message: 'Payment completed', }
position: 'topRight'
});
}); });
} }
}).render('#paypal-button-container'); }).render('#paypal-button-container');
</script> </script>