订单支付 - 微信支付
parent
e5fe6a135a
commit
6f598af1fc
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Constants;
|
||||||
|
|
||||||
|
use App\Services\WeChatPayService;
|
||||||
|
|
||||||
|
class PayWay
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
|--------------------------------------------
|
||||||
|
| 微信支付
|
||||||
|
|--------------------------------------------
|
||||||
|
*/
|
||||||
|
public const WXPAY_APP = 'wxpay_app'; // App 支付
|
||||||
|
public const WXPAY_JSAPI = 'wxpay_jsapi'; // JSAPI 支付
|
||||||
|
public const WXPAY_MINI = 'wxpay_mini'; // 小程序支付
|
||||||
|
public const WXPAY_H5 = 'wxpay_h5'; // H5 支付
|
||||||
|
public const WXPAY_NATIVE = 'wxpay_native'; // Native 支付
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付交易类型
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $wxpayTradeTypes = [
|
||||||
|
self::WXPAY_APP => WeChatPayService::TRADE_TYPE_APP,
|
||||||
|
self::WXPAY_JSAPI => WeChatPayService::TRADE_TYPE_JSAPI,
|
||||||
|
self::WXPAY_MINI => WeChatPayService::TRADE_TYPE_JSAPI,
|
||||||
|
self::WXPAY_H5 => WeChatPayService::TRADE_TYPE_H5,
|
||||||
|
self::WXPAY_NATIVE => WeChatPayService::TRADE_TYPE_NATIVE,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -148,4 +148,34 @@ class OrderController extends Controller
|
||||||
|
|
||||||
return response()->noContent();
|
return response()->noContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支付订单
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function pay($id, Request $request)
|
||||||
|
{
|
||||||
|
$input = $request->validate([
|
||||||
|
'pay_way' => ['bail', 'required'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return DB::transaction(function () use ($id, $user, $input) {
|
||||||
|
$order = $user->orders()->findOrFail($id);
|
||||||
|
|
||||||
|
return (new OrderService())->pay($order, $input['pay_way']);
|
||||||
|
});
|
||||||
|
} catch (BizException $e) {
|
||||||
|
throw $e;
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
report($e);
|
||||||
|
|
||||||
|
throw new BizException('订单支付失败,请重试');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,7 @@ Route::group([
|
||||||
Route::apiResource('order/orders', OrderController::class)->only([
|
Route::apiResource('order/orders', OrderController::class)->only([
|
||||||
'index', 'store', 'show',
|
'index', 'store', 'show',
|
||||||
]);
|
]);
|
||||||
|
Route::post('order/orders/{order}/pay', [OrderController::class, 'pay']);
|
||||||
Route::post('order/orders/{order}/confirm', [OrderController::class, 'confirm']);
|
Route::post('order/orders/{order}/confirm', [OrderController::class, 'confirm']);
|
||||||
Route::post('order/orders/{order}/cancel', [OrderController::class, 'cancel']);
|
Route::post('order/orders/{order}/cancel', [OrderController::class, 'cancel']);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Constants\PayWay;
|
||||||
use App\Endpoint\Api\Http\Resources\ProductSkuSimpleResource;
|
use App\Endpoint\Api\Http\Resources\ProductSkuSimpleResource;
|
||||||
use App\Endpoint\Api\Http\Resources\ShippingAddressResource;
|
use App\Endpoint\Api\Http\Resources\ShippingAddressResource;
|
||||||
use App\Endpoint\Api\Http\Resources\UserCouponResource;
|
use App\Endpoint\Api\Http\Resources\UserCouponResource;
|
||||||
|
|
@ -23,6 +24,16 @@ use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class OrderService
|
class OrderService
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $wxpayWays = [
|
||||||
|
PayWay::WXPAY_APP,
|
||||||
|
PayWay::WXPAY_JSAPI,
|
||||||
|
PayWay::WXPAY_MINI,
|
||||||
|
PayWay::WXPAY_H5,
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快速下单
|
* 快速下单
|
||||||
*
|
*
|
||||||
|
|
@ -727,4 +738,36 @@ class OrderService
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单付款
|
||||||
|
*
|
||||||
|
* @param \App\Models\Order $order
|
||||||
|
* @param string $payWay
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @throws \App\Exceptions\WeChatPayException
|
||||||
|
*/
|
||||||
|
public function pay(Order $order, string $payWay)
|
||||||
|
{
|
||||||
|
if (! $order->isPending()) {
|
||||||
|
throw new BizException('订单状态不是待付款');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($payWay, $this->wxpayWays)) {
|
||||||
|
return (new WeChatPayService())->pay([
|
||||||
|
'attach' => json_encode([
|
||||||
|
'pay_target' => 'order',
|
||||||
|
'pay_way' => $payWay,
|
||||||
|
]),
|
||||||
|
'body' => config('settings.app_name').'-商城订单',
|
||||||
|
'out_trade_no' => $order->sn,
|
||||||
|
'total_fee' => $order->total_amount,
|
||||||
|
'notify_url' => url(route('wxpay.paid_notify', [], false), [], true),
|
||||||
|
'trade_type' => PayWay::$wxpayTradeTypes[$payWay],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new BizException('支付方式不支持');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue