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 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);
}

View File

@ -3,9 +3,11 @@
<!-- Include the PayPal JavaScript SDK -->
@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
<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
@ -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');
</script>