集成paypal支付

This commit is contained in:
Edward Yang 2022-08-12 12:25:21 +08:00
parent 262031236f
commit 8731be2ab1
2 changed files with 26 additions and 16 deletions

View File

@ -16,6 +16,7 @@
namespace Plugin\Paypal\Controllers;
use Beike\Repositories\OrderRepo;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Srmklive\PayPal\Services\PayPal;
@ -67,35 +68,36 @@ class PaypalController
*/
public function create(Request $request): JsonResponse
{
$orderId = $request->get('order_number');
dd($orderId);
$data = json_decode($request->getContent(), true);
$data = \json_decode($request->getContent(), true);
$orderNumber = $data['order_number'];
$customer = current_customer();
$order = OrderRepo::getOrderByNumber($orderNumber, $customer);
$orderTotalUsd = currency_format($order->total, 'USD', '', false);
$order = $this->paypalClient->createOrder([
$paypalOrder = $this->paypalClient->createOrder([
"intent" => "CAPTURE",
"purchase_units" => [
[
"amount" => [
"currency_code" => "USD",
"value" => $data['amount']
"currency_code" => 'USD',
"value" => round($orderTotalUsd, 2),
],
'description' => 'test'
]
],
]);
$mergeData = array_merge($data, ['status' => TransactionStatus::PENDING, 'vendor_order_id' => $order['id']]);
DB::beginTransaction();
Order::create($mergeData);
DB::commit();
return response()->json($order);
//return redirect($order['links'][1]['href'])->send();
// echo('Create working');
return response()->json($paypalOrder);
//return redirect($paypalOrder['links'][1]['href'])->send();
}
/**
* 客户同意后扣款回调
* @param Request $request
* @return JsonResponse
* @throws \Throwable
*/
public function capture(Request $request)
{
$data = json_decode($request->getContent(), true);

View File

@ -14,8 +14,12 @@
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function (data, actions) {
const token = $('meta[name="csrf-token"]').attr('content')
return fetch('/plugin/paypal/create', {
method: 'POST',
headers: {
'X-CSRF-Token':token
},
body: JSON.stringify({
'order_number': "{{$order->number}}",
})
@ -30,8 +34,12 @@
// Call your server to finalize the transaction
onApprove: function (data, actions) {
const token = $('meta[name="csrf-token"]').attr('content')
return fetch('/plugin/paypal/capture', {
method: 'POST',
headers: {
'X-CSRF-Token':token
},
body: JSON.stringify({
orderId: data.orderID,
payment_gateway_id: $("#payapalId").val(),