From 00597d2207f25986147be0c3c51c05fe66efc04a Mon Sep 17 00:00:00 2001 From: xucong <850806214@qq.com> Date: Thu, 7 Nov 2024 15:12:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E9=94=80=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Pay.php | 16 ++++++++ app/api/logic/PayLogic.php | 30 ++++++++++++++ app/common/server/WeChatPayServer.php | 58 +++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/app/api/controller/Pay.php b/app/api/controller/Pay.php index 37208933..b56621aa 100755 --- a/app/api/controller/Pay.php +++ b/app/api/controller/Pay.php @@ -130,6 +130,22 @@ class Pay extends Api $result = PayLogic::localWxPay($user_id,$post, $client); return $result; } + // 分销支付 + public function distributionPay() + { + $post = $this->request->post(); + $user_id = $this->user_id; + if(!isset($user_id)){ + $user_id = $post['user_id']; + } + if(isset($this->client)){ + $client = $this->client; + }else{ + $client = 1; + } + $result = PayLogic::distributionPay($user_id,$post, $client); + return $result; + } // 本地生活余额支付 public function localBalance() diff --git a/app/api/logic/PayLogic.php b/app/api/logic/PayLogic.php index 7163dfdb..b36e8a2c 100755 --- a/app/api/logic/PayLogic.php +++ b/app/api/logic/PayLogic.php @@ -658,6 +658,36 @@ class PayLogic extends Logic } return JsonServer::success('微信支付发起成功', $res, 1); } + // + public static function distributionPay($user_id,$post,$client){ + $out_trade_no = createSn('user', 'order_no'); + $res = WeChatPayServer::distributionserverWxPay($out_trade_no,$user_id,$post, $client); + if (false === $res) { + return JsonServer::error(WeChatPayServer::getError()); + } + if ((is_object($res) || is_string($res)) && $client != Client_::pc) { + $res = (array)($res); + } + $data['order_sn'] = $out_trade_no; + $data['user_id'] = $user_id; + $data['order_amount'] = $post['money']; + $data['partner_id'] = $post['partner_id']; + $data['type_id'] = $post['type_id']; + $data['create_time'] = date("Y-m-d H:i:s"); + $r = Db::name("local_pay")->save($data); + if (is_string($res)) { + $res['order_sn'] = $out_trade_no; + $data = [ + 'code' => 1, + 'msg' => '微信支付发起成功', + 'show' => 0, + 'data' => $res + ]; + return json($data); + } + + return JsonServer::success('微信支付发起成功', $res, 1); + } //本地商家支付 public static function localWxPay($user_id,$post,$client){ // $post['lid'] = 10; diff --git a/app/common/server/WeChatPayServer.php b/app/common/server/WeChatPayServer.php index 36d79ab4..8ba9b280 100755 --- a/app/common/server/WeChatPayServer.php +++ b/app/common/server/WeChatPayServer.php @@ -205,6 +205,64 @@ class WeChatPayServer return false; } } + public static function distributionserverWxPay($out_trade_no,$user_id,$post, $client){ + try { + $order['user_id'] = $user_id; + $wechat_config = self::getWeChatConfig($order, $client); + $auth = $wechat_config['auth']; + $config = $wechat_config['config']; + $notify_url = $wechat_config['notify_url']; + $app = Factory::payment($config); + $attributes = [ + 'trade_type' => 'JSAPI', + 'body' => '分销支付', + 'total_fee' => $post['money'] * 100, // 单位:分 + 'notify_url' => $notify_url, + 'openid' => $auth['openid'] ?? '', + 'attach' => 'trade' + ]; + $attributes['out_trade_no'] = $out_trade_no. $attributes['trade_type'] . $client; + $result = $app->order->unify($attributes); + // var_dump($result); + if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { + + //小程序,公众号 + if (in_array($client, [Client_::mnp, Client_::oa])) { + $data = $app->jssdk->bridgeConfig($result['prepay_id'], false); + } + + //app客户端 + if (in_array($client, [Client_::ios, Client_::android])) { + $data = $app->jssdk->appConfig($result['prepay_id'], false); + } + + //pc端 + if ($client == Client_::pc) { + $data = self::getNativeCode($result, $order); + } + + //h5(非微信环境) + if ($client == Client_::h5) { + $redirect_url = request()->domain() . '/mobile/pages/user_order/user_order'; + $redirect_url = urlencode($redirect_url); + $data = $result['mweb_url'] . '&redirect_url=' . $redirect_url; + } + return $data; + } else { + if (isset($result['return_code']) && $result['return_code'] == 'FAIL') { + throw new Exception($result['return_msg']); + } + if (isset($result['err_code_des'])) { + throw new Exception($result['err_code_des']); + } + throw new Exception('未知原因'); + } + + } catch (Exception $e) { + self::$error = '支付失败:' . $e->getMessage(); + return false; + } + } /** * @notes NATIVE 支付二维码 * @param $result