集成paypal支付
This commit is contained in:
parent
ede0cd9fe2
commit
607e48b8dc
|
|
@ -56,7 +56,11 @@ class PaymentService
|
|||
if (!view()->exists($viewPath)) {
|
||||
throw new \Exception("找不到支付方式 {$orderPaymentCode} 模板 {$viewPath}");
|
||||
}
|
||||
$paymentView = view($viewPath, ['order' => $this->order])->render();
|
||||
$paymentData = [
|
||||
'order' => $this->order,
|
||||
'payment_setting' => plugin_setting($orderPaymentCode),
|
||||
];
|
||||
$paymentView = view($viewPath, $paymentData)->render();
|
||||
return view('checkout.payment', ['order' => $this->order, 'payment' => $paymentView]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,50 +16,115 @@
|
|||
|
||||
namespace Plugin\Paypal\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Srmklive\PayPal\Services\PayPal;
|
||||
|
||||
class PaypalController
|
||||
{
|
||||
public function handlePayment()
|
||||
private PayPal $paypalClient;
|
||||
|
||||
/**
|
||||
* PaypalController constructor.
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$product = [];
|
||||
$product['items'] = [
|
||||
[
|
||||
'name' => 'Nike Joyride 2',
|
||||
'price' => 112,
|
||||
'desc' => 'Running shoes for Men',
|
||||
'qty' => 2
|
||||
]
|
||||
$paypalSetting = plugin_setting('paypal');
|
||||
$config = [
|
||||
'mode' => $paypalSetting['sandbox_mode'] ? 'sandbox' : 'live',
|
||||
'sandbox' => [
|
||||
'client_id' => $paypalSetting['sandbox_client_id'],
|
||||
'client_secret' => $paypalSetting['sandbox_secret'],
|
||||
],
|
||||
'live' => [
|
||||
'client_id' => $paypalSetting['live_client_id'],
|
||||
'client_secret' => $paypalSetting['live_secret'],
|
||||
],
|
||||
'payment_action' => 'Sale', // Can only be 'Sale', 'Authorization' or 'Order'
|
||||
'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.
|
||||
];
|
||||
|
||||
$product['invoice_id'] = 1;
|
||||
$product['invoice_description'] = "Order #{$product['invoice_id']} Bill";
|
||||
$product['return_url'] = route('success.payment');
|
||||
$product['cancel_url'] = route('cancel.payment');
|
||||
$product['total'] = 224;
|
||||
|
||||
$paypalModule = new PayPal();
|
||||
|
||||
$res = $paypalModule->setExpressCheckout($product);
|
||||
$res = $paypalModule->setExpressCheckout($product, true);
|
||||
|
||||
return redirect($res['paypal_link']);
|
||||
config(['paypal' => null]);
|
||||
$this->paypalClient = new PayPal($config);
|
||||
$token = $this->paypalClient->getAccessToken();
|
||||
$this->paypalClient->setAccessToken($token);
|
||||
}
|
||||
|
||||
public function paymentCancel()
|
||||
public function test()
|
||||
{
|
||||
dd('Your payment has been declend. The payment cancelation page goes here!');
|
||||
|
||||
}
|
||||
|
||||
public function paymentSuccess(Request $request)
|
||||
{
|
||||
$paypalModule = new ExpressCheckout;
|
||||
$response = $paypalModule->getExpressCheckoutDetails($request->token);
|
||||
|
||||
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
|
||||
dd('Payment was successfull. The payment success page goes here!');
|
||||
/**
|
||||
* 创建 paypal 订单
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function create(Request $request): JsonResponse
|
||||
{
|
||||
$orderId = $request->get('order_number');
|
||||
dd($orderId);
|
||||
$data = json_decode($request->getContent(), true);
|
||||
|
||||
|
||||
$order = $this->paypalClient->createOrder([
|
||||
"intent" => "CAPTURE",
|
||||
"purchase_units" => [
|
||||
[
|
||||
"amount" => [
|
||||
"currency_code" => "USD",
|
||||
"value" => $data['amount']
|
||||
],
|
||||
'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');
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// $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->save();
|
||||
DB::commit();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
dd($e);
|
||||
}
|
||||
|
||||
dd('Error occured!');
|
||||
return response()->json($result);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
/**
|
||||
* admin.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-08-12 10:33:01
|
||||
* @modified 2022-08-12 10:33:01
|
||||
*/
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* shop.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-08-12 10:33:13
|
||||
* @modified 2022-08-12 10:33:13
|
||||
*/
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Plugin\Paypal\Controllers\PaypalController;
|
||||
|
||||
Route::group(['prefix' => 'paypal'], function () {
|
||||
Route::get('/test', [PaypalController::class, 'test']);
|
||||
Route::post('/create', [PaypalController::class, 'create']);
|
||||
Route::post('/capture', [PaypalController::class, 'capture']);
|
||||
});
|
||||
|
|
@ -1 +1,57 @@
|
|||
PAYPAL
|
||||
<!-- Set up a container element for the button -->
|
||||
<div id="paypal-button-container"></div>
|
||||
|
||||
<!-- 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') }}¤cy=USD"></script>
|
||||
@else
|
||||
<script src="https://www.paypal.com/sdk/js?client-id={{ plugin_setting('paypal.live_client_id') }}¤cy=USD"></script>
|
||||
@endif
|
||||
|
||||
|
||||
<script>
|
||||
// Render the PayPal button into #paypal-button-container
|
||||
paypal.Buttons({
|
||||
// Call your server to set up the transaction
|
||||
createOrder: function (data, actions) {
|
||||
return fetch('/plugin/paypal/create', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
'order_number': "{{$order->number}}",
|
||||
})
|
||||
}).then(function (res) {
|
||||
//res.json();
|
||||
return res.json();
|
||||
}).then(function (orderData) {
|
||||
//console.log(orderData);
|
||||
return orderData.id;
|
||||
});
|
||||
},
|
||||
|
||||
// Call your server to finalize the transaction
|
||||
onApprove: function (data, actions) {
|
||||
return fetch('/plugin/paypal/capture', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
orderId: 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'
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}).render('#paypal-button-container');
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@
|
|||
<div class="card-header"><h5 class="card-title">订单详情</h5></div>
|
||||
<div class="card-body">
|
||||
<div class="bg-light p-2">
|
||||
<a href="{{ shop_route('orders.pay', $order->number) }}">去支付</a>
|
||||
<table class="table table-borderless mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
|
|||
Loading…
Reference in New Issue