6
0
Fork 0

订单微信支付成功回调通知

release
李静 2021-12-18 16:46:49 +08:00
parent 1042bfcb23
commit df775de964
3 changed files with 83 additions and 2 deletions

View File

@ -2,7 +2,14 @@
namespace App\Endpoint\Callback\Http\Controllers;
use App\Exceptions\BizException;
use App\Models\Order;
use App\Services\OrderService;
use App\Services\WeChatPayService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Throwable;
class WeChatPayController extends Controller
{
@ -10,12 +17,50 @@ class WeChatPayController extends Controller
* 直接结果通知
*
* @param \App\Services\WeChatPayService $request
* @param \App\Services\WeChatPayService $request
* @return \Illuminate\Http\Response
*/
public function paidNotify(WeChatPayService $weChatPayService)
{
$weChatPayService->handlePaidNotify(function ($message, $fail) {
logger()->debug('wechat notify', $message);
return $weChatPayService->handlePaidNotify(function ($message, $fail) {
// 通信失败
if (data_get($message, 'return_code') !== 'SUCCESS') {
return $fail('通信失败');
}
// 支付失败
if (data_get($message, 'result_code') !== 'SUCCESS') {
return true;
}
$attach = json_decode(data_get($message, 'attach'), true);
if (! isset($attach['pay_target'])) {
return true;
}
switch ($attach['pay_target']) {
// 支付订单
case 'order':
try {
DB::beginTransaction();
(new OrderService())->paySuccess($message['out_trade_no'], [
'pay_sn' => $message['transaction_id'],
'pay_way' => Order::PAY_WAY_WXPAY,
'pay_at' => Carbon::parse($message['time_end']),
]);
DB::commit();
} catch (BizException|ModelNotFoundException $e) {
DB::rollBack();
} catch (Throwable $e) {
DB::rollBack();
throw $e;
}
break;
}
return true;
});

View File

@ -13,15 +13,26 @@ class Order extends Model
use Filterable;
use HasDateTimeFormatter;
/**
* 订单状态
*/
public const STATUS_PENDING = 0; // 待付款
public const STATUS_PAID = 1; // 已付款
public const STATUS_COMPLETED = 9; // 已完成
public const STATUS_CANCELLED = 10; // 已取消
/**
* 发货状态
*/
public const SHIPPING_STATE_PENDING = 0; // 待发货
public const SHIPPING_STATE_PROCESSING = 1; // 发货中
public const SHIPPING_STATE_PROCESSED = 2; // 已完成
/**
* 支付方式
*/
public const PAY_WAY_WXPAY = 'wxpay'; // 微信支付
/**
* @var array
*/

View File

@ -696,4 +696,29 @@ class OrderService
}
}
}
/*
* 支付成功
*
* @param string $sn
* @param array $params
* @return void
*/
public function paySuccess(string $sn, array $params = []): void
{
$order = Order::where('sn', $sn)->findOrFail();
if (! $order->isPending()) {
throw new BizException('订单状态不是待支付');
}
$order->update([
'pay_sn' => $params['pay_sn'],
'pay_way' => $params['pay_way'],
'pay_at' => $params['pay_at'],
'status' => Order::STATUS_PAID,
]);
// todo 处理预收益
}
}