master
wangxinglong 2021-12-09 14:14:22 +08:00
parent 2345115913
commit ca2f391843
2 changed files with 57 additions and 56 deletions

View File

@ -369,6 +369,7 @@ class Business extends Base
$payData['sign'] = generate_sign($payData, config("wechat.key")); $payData['sign'] = generate_sign($payData, config("wechat.key"));
$payData['order_num'] = $order->order_num; $payData['order_num'] = $order->order_num;
Db::commit(); Db::commit();
return $this->json(0, "success", [ return $this->json(0, "success", [
"payData" => $payData "payData" => $payData

View File

@ -82,71 +82,71 @@ class Recharge extends Base
* *
* @throws \EasyWeChat\Kernel\Exceptions\Exception * @throws \EasyWeChat\Kernel\Exceptions\Exception
*/ */
public function notify() public function notify(){
{ if ($this->request->isPost()) {
$this->log("回调触发了");
$app = WechatPay::getInstance();
$response = $app->handlePaidNotify(function ($message, $fail) {
// $aa = '{"appid":"wxa02e44170bc722cd","bank_type":"OTHERS","cash_fee":"1","fee_type":"CNY","is_subscribe":"N","mch_id":"1605090111","nonce_str":"60f7d8a1e4ac8","openid":"oKrEm0ehgsy2ZTWzEva4tbLuUgFw","out_trade_no":"16268555858753004863","result_code":"SUCCESS","return_code":"SUCCESS","sign":"DB3F6CDCB7FBB3B9DDF7C0CC8BBD5AAD","time_end":"20210721162000","total_fee":"1","trade_type":"JSAPI","transaction_id":"4200001200202107217942681078"}';
// $message = json_decode($aa, true);
$m = json_encode($message, JSON_UNESCAPED_UNICODE);
$app = WechatPay::getInstance(); $recharge = RechargeRepository::getInstance()->getModel()->where(["order_num" => $message['out_trade_no']])->lock(true)->find();
$response = $app->handlePaidNotify(function ($message, $fail) { if (empty($recharge)) {
// $aa = '{"appid":"wxa02e44170bc722cd","bank_type":"OTHERS","cash_fee":"1","fee_type":"CNY","is_subscribe":"N","mch_id":"1605090111","nonce_str":"60f7d8a1e4ac8","openid":"oKrEm0ehgsy2ZTWzEva4tbLuUgFw","out_trade_no":"16268555858753004863","result_code":"SUCCESS","return_code":"SUCCESS","sign":"DB3F6CDCB7FBB3B9DDF7C0CC8BBD5AAD","time_end":"20210721162000","total_fee":"1","trade_type":"JSAPI","transaction_id":"4200001200202107217942681078"}'; $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功,但系统查无此订单 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'error');
// $message = json_decode($aa, true); return true;//订单不存在
$m = json_encode($message, JSON_UNESCAPED_UNICODE); }
if ($recharge['state'] == RechargeModel::state_on) {
$recharge = RechargeRepository::getInstance()->getModel()->where(["order_num" => $message['out_trade_no']])->lock(true)->find(); return true;//订单已经支付
if (empty($recharge)) { }
$this->log(sprintf("[微信支付回调][%s][%s]订单支付成功,但系统查无此订单 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'error'); $business = BusinessRepository::getInstance()->getModel()->where(["code" => $recharge['business_code']])->lock(true)->find();
return true;//订单不存在 if (empty($business)) {
} $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功,但商家不存在 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'error');
if ($recharge['state'] == RechargeModel::state_on) { return true;
return true;//订单已经支付 }
}
$business = BusinessRepository::getInstance()->getModel()->where(["code" => $recharge['business_code']])->lock(true)->find();
if (empty($business)) {
$this->log(sprintf("[微信支付回调][%s][%s]订单支付成功,但商家不存在 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'error');
return true;
}
if ($message['return_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态 if ($message['return_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
if (isset($message['result_code']) && $message['result_code'] == 'SUCCESS') { if (isset($message['result_code']) && $message['result_code'] == 'SUCCESS') {
if (isset($message['trade_state']) && $message['trade_state'] == 'SUCCESS') { if (isset($message['trade_state']) && $message['trade_state'] == 'SUCCESS') {
Db::startTrans(); Db::startTrans();
try { try {
//这里确定支付成功 //这里确定支付成功
$total_fee = $message['total_fee'] / 100; $total_fee = $message['total_fee'] / 100;
//加余额 //加余额
$business->inc()->save([ $business->inc()->save([
"balance" => ($business["balance"] + $total_fee), "balance" => ($business["balance"] + $total_fee),
"total_recharge" => ($business["total_recharge"] + $total_fee)] "total_recharge" => ($business["total_recharge"] + $total_fee)]
); );
//修改支付状态 //修改支付状态
$recharge->save([ $recharge->save([
"money" => $total_fee, "money" => $total_fee,
"state" => RechargeModel::state_on, "state" => RechargeModel::state_on,
"open_id" => $message['openid'], "open_id" => $message['openid'],
"update_time" => date("Y-m-d H:i:s"), "update_time" => date("Y-m-d H:i:s"),
"balance" => $business->balance "balance" => $business->balance
]); ]);
Db::commit(); Db::commit();
//记录日志 //记录日志
$this->log(sprintf("[微信支付回调][%s][%s]订单支付成功 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info'); $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info');
return true; return true;
} catch (RepositoryException $e) { } catch (RepositoryException $e) {
Db::rollback(); Db::rollback();
$this->log(sprintf("[微信支付回调][%s][%s]订单支付成功-修改订单状态失败-RepositoryException info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info'); $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功-修改订单状态失败-RepositoryException info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info');
return $fail('Order status edit failed.'); return $fail('Order status edit failed.');
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
$this->log(sprintf("[微信支付回调][%s][%s]订单支付成功-修改订单状态失败-Exception info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info'); $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功-修改订单状态失败-Exception info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info');
return $fail('Order status edit failed.'); return $fail('Order status edit failed.');
}
} }
} }
} }
} return $fail('通信失败,请稍后再通知我');
return $fail('通信失败,请稍后再通知我'); });
});
$response->send();
$response->send();
}
} }
/** /**
* 记录订单日志 * 记录订单日志