From 2345115913bcf33541f94ee22b35cb86f116bb11 Mon Sep 17 00:00:00 2001 From: wangxinglong <2371974647@qq.com> Date: Thu, 9 Dec 2021 13:58:21 +0800 Subject: [PATCH] settter --- app/controller/api/Business.php | 2 +- app/controller/api/Recharge.php | 111 ++++++++++++++++---------------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/app/controller/api/Business.php b/app/controller/api/Business.php index f8c56d2..b203b91 100644 --- a/app/controller/api/Business.php +++ b/app/controller/api/Business.php @@ -368,7 +368,7 @@ class Business extends Base ]; $payData['sign'] = generate_sign($payData, config("wechat.key")); - + $payData['order_num'] = $order->order_num; Db::commit(); return $this->json(0, "success", [ "payData" => $payData diff --git a/app/controller/api/Recharge.php b/app/controller/api/Recharge.php index 1593796..fd91ae4 100644 --- a/app/controller/api/Recharge.php +++ b/app/controller/api/Recharge.php @@ -82,70 +82,71 @@ class Recharge extends Base * * @throws \EasyWeChat\Kernel\Exceptions\Exception */ - public function notify(){ - if ($this->request->isPost()) { - $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); + public function notify() + { - $recharge = RechargeRepository::getInstance()->getModel()->where(["order_num" => $message['out_trade_no']])->lock(true)->find(); - if (empty($recharge)) { - $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功,但系统查无此订单 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'error'); - return true;//订单不存在 - } - if ($recharge['state'] == RechargeModel::state_on) { - 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; - } + $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); + + $recharge = RechargeRepository::getInstance()->getModel()->where(["order_num" => $message['out_trade_no']])->lock(true)->find(); + if (empty($recharge)) { + $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功,但系统查无此订单 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'error'); + return true;//订单不存在 + } + if ($recharge['state'] == RechargeModel::state_on) { + 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 (isset($message['result_code']) && $message['result_code'] == 'SUCCESS') { - if (isset($message['trade_state']) && $message['trade_state'] == 'SUCCESS') { - Db::startTrans(); - try { - //这里确定支付成功 - $total_fee = $message['total_fee'] / 100; - //加余额 - $business->inc()->save([ + if ($message['return_code'] == 'SUCCESS') { // return_code 表示通信状态,不代表支付状态 + if (isset($message['result_code']) && $message['result_code'] == 'SUCCESS') { + if (isset($message['trade_state']) && $message['trade_state'] == 'SUCCESS') { + Db::startTrans(); + try { + //这里确定支付成功 + $total_fee = $message['total_fee'] / 100; + //加余额 + $business->inc()->save([ "balance" => ($business["balance"] + $total_fee), "total_recharge" => ($business["total_recharge"] + $total_fee)] - ); - //修改支付状态 - $recharge->save([ - "money" => $total_fee, - "state" => RechargeModel::state_on, - "open_id" => $message['openid'], - "update_time" => date("Y-m-d H:i:s"), - "balance" => $business->balance - ]); - Db::commit(); - //记录日志 - $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info'); - return true; - } catch (RepositoryException $e) { - Db::rollback(); - $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.'); - } catch (\Exception $e) { - Db::rollback(); - $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.'); - } + ); + //修改支付状态 + $recharge->save([ + "money" => $total_fee, + "state" => RechargeModel::state_on, + "open_id" => $message['openid'], + "update_time" => date("Y-m-d H:i:s"), + "balance" => $business->balance + ]); + Db::commit(); + //记录日志 + $this->log(sprintf("[微信支付回调][%s][%s]订单支付成功 info:%s", date('Y-m-d H:i:s'), $message['out_trade_no'], $m), 'info'); + return true; + } catch (RepositoryException $e) { + Db::rollback(); + $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.'); + } catch (\Exception $e) { + Db::rollback(); + $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('通信失败,请稍后再通知我'); - }); + } + return $fail('通信失败,请稍后再通知我'); + }); + + $response->send(); - $response->send(); - } } /** * 记录订单日志