diff --git a/app/api/controller/PayController.php b/app/api/controller/PayController.php index 5827cf5da..5cf4e8e84 100755 --- a/app/api/controller/PayController.php +++ b/app/api/controller/PayController.php @@ -16,11 +16,8 @@ namespace app\api\controller; use app\api\validate\PayValidate; -use app\common\enum\PayEnum; use app\common\enum\user\UserTerminalEnum; use app\common\logic\PaymentLogic; -use app\common\logic\PayRequestLogic; -use app\common\model\Company; use app\common\service\pay\WeChatPayService; use think\facade\Log; @@ -65,23 +62,13 @@ class PayController extends BaseApiController if (false === $order) { return $this->fail(PaymentLogic::getError(), $params); } - // 请求支付系统 - $companyInfo = Company::where(['id' => $this->userInfo['company_id']])->find(); - $requestData = [ - 'street' => $companyInfo['street'], - 'order_from' => 12, - 'order_type' => 101, - 'pay_user_role' => $this->userInfo['group_id'], - 'pay_user_info' => $this->userInfo, - 'business_order_no' => $order['sn'], - 'total_fee' => intval(bcmul($order['order_amount'], 100)), - 'business_callback_url' => (string)url('pay/notifyApp', [], false, true) - ]; - $result = PayRequestLogic::getPrePayId($requestData); + //支付流程 + $redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; + $result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl); if (false === $result) { return $this->fail(PaymentLogic::getError(), $params); } - return $this->success('', $result['data']); + return $this->success('', $result); } @@ -135,18 +122,18 @@ class PayController extends BaseApiController /** - * @notes 支付系统回调 + * @notes app支付回调 + * @return \Psr\Http\Message\ResponseInterface + * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException + * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException + * @throws \ReflectionException + * @throws \Throwable * @date 2023/2/28 14:21 */ public function notifyApp() { - try { - $param = $this->request->param(); - Log::info(['支付系统回调', $param]); - (new WeChatPayService(UserTerminalEnum::ANDROID))->notify($param); - return $this->success('ok'); - } catch (\Exception $e) { - return $this->fail($e->getMessage()); - } + return (new WeChatPayService(UserTerminalEnum::ANDROID))->notify(); } + + } diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 36718fbcb..272986af4 100755 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -113,7 +113,7 @@ class PayNotifyLogic extends BaseLogic // 更新充值订单状态 $order->transaction_id = $extra['transaction_id']; - $order->pay_status = $extra['pay_status'];; + $order->pay_status = PayEnum::ISPAID; $order->pay_time = time(); $order->save(); } diff --git a/app/common/service/pay/WeChatPayService.php b/app/common/service/pay/WeChatPayService.php index 1a628c559..e695208cc 100755 --- a/app/common/service/pay/WeChatPayService.php +++ b/app/common/service/pay/WeChatPayService.php @@ -354,14 +354,43 @@ class WeChatPayService extends BasePayService /** * @notes 支付回调 + * @return \Psr\Http\Message\ResponseInterface + * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException + * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException + * @throws \ReflectionException + * @throws \Throwable + * @author 段誉 + * @date 2023/2/28 14:20 */ - public function notify($param=[]) + public function notify() { - Log::info(['支付回调', $param]); - $extra['transaction_id'] = $param['transaction_id']; - $extra['pay_status'] = $param['pay_status']; - PayNotifyLogic::handle('recharge', $param['out_trade_no'], $extra); - return true; + $server = $this->app->getServer(); + // 支付通知 + $server->handlePaid(function (Message $message) { + $data = ['trade_state' => $message['trade_state'] ?? '', 'out_trade_no' => $message['out_trade_no'] ?? '', 'transaction_id' => $message['transaction_id'] ?? '', 'attach' => $message['attach'] ?? '']; + Log::info('wechat pay notify: ' . var_export($data, true)); + if ($message['trade_state'] === 'SUCCESS') { + $extra['transaction_id'] = $message['transaction_id']; + $attach = $message['attach']; + $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18); + switch ($attach) { + case 'recharge': + $order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty(); + if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) { + return true; + } + PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra); + break; + } + } + return true; + }); + + // 退款通知 + $server->handleRefunded(function (Message $message) { + return true; + }); + return $server->serve(); } public function configForPayment($prepayId, $appId)