From 1c58d4d82ad5dfc7934a8a7235547b18b4c7ac3b Mon Sep 17 00:00:00 2001 From: liutong <836164388@qq.com> Date: Tue, 4 Mar 2025 20:05:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(hygl):=20=E6=94=AF=E6=8C=81=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增微信小程序 openid 获取功能 - 实现微信小程序支付接口 - 更新微信支付配置,支持小程序 app_id- 优化用户信息获取逻辑,支持小程序 openid - 新增微信小程序支付回调处理 --- .../addon/hygl/app/api/controller/Home.php | 21 +++- .../app/api/controller/WeChatController.php | 107 ++++++++++++++++-- niucloud/app/api/route/hygl.php | 8 +- niucloud/public/login/test0.txt | 1 + ...支付回调返回数据结构示例.txt | 34 ++++++ niucloud/public/rItAiGxXXx.txt | 1 + 6 files changed, 155 insertions(+), 17 deletions(-) create mode 100644 niucloud/public/login/test0.txt create mode 100644 niucloud/public/login/微信小程序支付回调返回数据结构示例.txt create mode 100644 niucloud/public/rItAiGxXXx.txt diff --git a/niucloud/addon/hygl/app/api/controller/Home.php b/niucloud/addon/hygl/app/api/controller/Home.php index 58addb2..89e8587 100644 --- a/niucloud/addon/hygl/app/api/controller/Home.php +++ b/niucloud/addon/hygl/app/api/controller/Home.php @@ -39,11 +39,12 @@ class Home extends BaseApiController $site_id = $this->request->param('site_id', ''); $site_name = Site::where('site_id', $site_id)->value('site_name');//获取站点信息 //获取微信公众号id - $we_chat_pay_app_id = Config::where('site_id', $site_id)->value('we_chat_pay_app_id'); + $config = Config::where('site_id', $site_id)->field('id,we_chat_pay_app_id,we_chat_pay_miniapp_id')->find(); $res = [ 'site_id' => $site_id, 'site_name' => $site_name, - 'we_chat_pay_app_id' => $we_chat_pay_app_id, + 'we_chat_pay_app_id' => $config['we_chat_pay_app_id'] ?? '',//微信支付-公众号APPID + 'we_chat_pay_miniapp_id' => $config['we_chat_pay_miniapp_id'] ?? '',//微信支付-小程序APPID ]; return success($res); } @@ -54,16 +55,21 @@ class Home extends BaseApiController $site_id = $this->request->param('site_id', ''); $role = $this->request->param('role', '');//身份//wx=微信 zfb=支付宝 $openid = $this->request->param('openid', ''); + $wx_mini_openid = $this->request->param('wx_mini_openid', ''); - if (!$site_id || !in_array($role, ['wx', 'zfb']) || !$openid) { + if (!$site_id || !in_array($role, ['wx', 'zfb']) || (!$openid && !$wx_mini_openid)) { return fail('确少参数'); } $user = User::where('site_id', $site_id); if ($role == 'wx') { - $user = $user->where('wx_openid', $openid); + if($openid){ + $user = $user->where('wx_openid', $openid); + }else{ + $user = $user->where('wx_mini_openid', $wx_mini_openid); + } } else { $user = $user->where('zfb_openid', $openid); } @@ -73,6 +79,7 @@ class Home extends BaseApiController if (!$user) { if ($role == 'wx') { $wx_openid = $openid; + $wx_mini_openid = $wx_mini_openid; $name = "微信用户{$openid}"; } else { $zfb_openid = $openid; @@ -82,6 +89,7 @@ class Home extends BaseApiController 'name' => $name, 'site_id' => $site_id, 'wx_openid' => $wx_openid ?? null, + 'wx_mini_openid' => $wx_mini_openid ?? null, 'zfb_openid' => $zfb_openid ?? null, 'is_show' => 1, 'pid' => 0, @@ -224,11 +232,12 @@ class Home extends BaseApiController 'pay_type' => $pay_type,// 'original_price' => $original_price,//订单原价(元)|优惠前的金额 'price' => $price,//订单应付金额(元)|(优惠后的金额)精确到`分` - 'user_coupons_id' => $user_coupons['id'],//用户优惠券中间表id + 'user_coupons_id' => $user_coupons['id'] ?? 0,//用户优惠券中间表id ]; + Db::startTrans(); try { - $res = TransactionHistory::create($create_data); + $res = TransactionHistory::create($create_data);//会员交易记录模型 Db::commit(); } catch (\Exception $exception) { Db::rollback(); diff --git a/niucloud/addon/hygl/app/api/controller/WeChatController.php b/niucloud/addon/hygl/app/api/controller/WeChatController.php index 22d6311..bb2e54f 100644 --- a/niucloud/addon/hygl/app/api/controller/WeChatController.php +++ b/niucloud/addon/hygl/app/api/controller/WeChatController.php @@ -28,7 +28,7 @@ use Yansongda\Pay\Pay; class WeChatController extends BaseApiController { /** - * 获取openid + * 获取公众号openid */ public function getOpenid(){ $code = $this->request->param('code','');// @@ -54,6 +54,36 @@ class WeChatController extends BaseApiController return success($res); } + //获取微信小程序openid + public function getMiniOpenid(){ + $code = $this->request->param('code','');// + $site_id = $this->request->param('site_id',''); + + + $wx_config = Config::where('site_id',$site_id)->field('we_chat_pay_miniapp_id,we_chat_pay_miniapp_secret')->find(); + + $appid = $wx_config['we_chat_pay_miniapp_id']; + $secret = $wx_config['we_chat_pay_miniapp_secret']; + + //获取微信小程序openid + // 调用微信接口获取 openid 和 session_key + $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code"; + $tokeninfo = $this->http_curl($url); // 发送 HTTP 请求 + $tokeninfo = json_decode($tokeninfo, true); // 解析返回的 JSON 数据 + + + if (empty($tokeninfo['openid'])){ + return fail('链接失效,请重新扫码'); + } + + $res = [ + 'openid'=>$tokeninfo['openid'],//微信openid + ]; + return success($res); + } + + + //curl请求 public function http_curl($url){ $curl = curl_init(); @@ -83,9 +113,10 @@ class WeChatController extends BaseApiController // 必填-商户公钥证书路径 'mch_public_cert_path' => 'addon/hygl/wx_mch_cert/100000/apiclient_cert.pem', // 必填 - 'notify_url' => 'http://hycrm.zeyan.wang/api/hygl/wechatPayNotify', + 'notify_url' => 'https://zhifuguanli.zeyan.wang/api/hygl/wechatPayNotify', // 选填-公众号 的 app_id 'mp_app_id' => 'wxd2d733a7163bff05', + 'mini_app_id' => 'wx42762c713f7bfa2e',//精准时光-微信小程序appid // 选填-默认为正常模式。可选为: MODE_NORMAL, MODE_SERVICE 'mode' => Pay::MODE_NORMAL, ] @@ -169,13 +200,66 @@ class WeChatController extends BaseApiController return success($pay->toArray()); } - //支付成功异步回调 + //创建微信小程序支付 + public function wxMiniPay(){ + $order_id = $this->request->param('order_id',''); + $user_id = $this->request->param('user_id',''); + $site_id = $this->request->param('site_id',''); + + if (!$order_id || !$user_id || !$site_id) { + return fail('缺少参数'); + } + + $order = TransactionHistory::where('id',$order_id) + ->where('user_id',$user_id) + ->where('pay_status',0) + ->find(); + + if (!$order){ + return fail('订单不正确,请重新扫码'); + } + + + $config = $this->initWxPayConfig($site_id); + + if(!$config['code']){ + return fail($config['msg']); + } + + $openid = User::where('id',$user_id)->value('wx_mini_openid');//小程序openid + + if (!$openid){ + return fail('请刷新页面后再试'); + } + + $site_name = Site::where('site_id',$site_id)->value('site_name'); + + + $order_pay = [ + 'out_trade_no' => $order['order_num'],//我方订单号 + 'description' => $site_name, + 'amount' => [ + 'total' => $order['price'] * 100,//金额(分) + 'currency' => 'CNY', + ], + 'payer' => [ + 'openid' => $openid,//支付用户的openid + ] + ]; + + //拉起微信小程序支付 + $pay = Pay::wechat($config['data'])->mini($order_pay); + return success($pay->toArray()); + } + + //微信公众号支付成功异步回调 public function wechatPayNotify($site_id) { $filePath = 'login/test.txt'; -// $date = date('Y-m-d H:i:s') . '_$site_id=' .$site_id; -// $data = "回调成功了-{$date}"; // 要写入的数据 -// $a = file_put_contents($filePath, $data); + $filePath_0 = 'login/test0.txt'; + $date = date('Y-m-d H:i:s') . '_$site_id=' .$site_id; + $data = "回调成功了-{$date}"; // 要写入的数据 + $site_id = $this->request->param('site_id',''); @@ -185,8 +269,9 @@ class WeChatController extends BaseApiController try { // 是的,你没有看错,就是这么简单!返回值为微信回调的订单数据 $data = $pay->callback(); -// $a = "{$data->summary} - {$data['resource']['ciphertext']['trade_state_desc']}"; -// file_put_contents($filePath,json_encode($data, JSON_UNESCAPED_UNICODE)); + + $a = "{$data->summary} - {$data['resource']['ciphertext']['trade_state_desc']}"; + file_put_contents($filePath_0,json_encode($data, JSON_UNESCAPED_UNICODE)); Log::write($data->all(),'notice');//记录日志 //判断是否支付成功 if ($data['event_type'] == "TRANSACTION.SUCCESS" && $data['resource']['ciphertext']['trade_state'] == "SUCCESS") { @@ -240,8 +325,6 @@ class WeChatController extends BaseApiController return $str; } - // - /** * 初始化微信支付配置项 * @param $site_id 站点id @@ -276,8 +359,12 @@ class WeChatController extends BaseApiController 'notify_url' => $wx_config['we_chat_pay_notify_url'], // 选填-公众号 的 app_id 'mp_app_id' => $wx_config['we_chat_pay_app_id'], + // 「选填」小程序 的 app_id + 'mini_app_id' => $wx_config['we_chat_pay_miniapp_id'], // 选填-默认为正常模式。可选为: MODE_NORMAL, MODE_SERVICE 'mode' => Pay::MODE_NORMAL, + // 「选填」app 的 app_id + 'app_id' => '' ] ], 'logger' => [ // optional diff --git a/niucloud/app/api/route/hygl.php b/niucloud/app/api/route/hygl.php index 56959c1..4ec24a0 100644 --- a/niucloud/app/api/route/hygl.php +++ b/niucloud/app/api/route/hygl.php @@ -43,10 +43,16 @@ Route::group('hygl', function() { //创建订单 Route::get('addOrder', 'addon\hygl\app\api\controller\Home@addOrder'); - //获取微信openid + //获取微信公众号openid Route::any('getOpenid', 'addon\hygl\app\api\controller\WeChatController@getOpenid'); + //获取微信小程序openid + Route::any('getMiniOpenid', 'addon\hygl\app\api\controller\WeChatController@getMiniOpenid'); //微信JSAPI支付 Route::any('wxMpPay', 'addon\hygl\app\api\controller\WeChatController@wxMpPay'); + + //微信小程序支付 + Route::any('wxMiniPay', 'addon\hygl\app\api\controller\WeChatController@wxMiniPay'); + //微信支付成功异步回调 Route::any('wechatPayNotify/:site_id', 'addon\hygl\app\api\controller\WeChatController@wechatPayNotify'); diff --git a/niucloud/public/login/test0.txt b/niucloud/public/login/test0.txt new file mode 100644 index 0000000..676020c --- /dev/null +++ b/niucloud/public/login/test0.txt @@ -0,0 +1 @@ +{"id":"2f687ead-97b7-5eb3-a1ed-557a26e3267b","create_time":"2025-03-04T19:54:39+08:00","resource_type":"encrypt-resource","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"original_type":"transaction","algorithm":"AEAD_AES_256_GCM","ciphertext":{"mchid":"1647413230","appid":"wx42762c713f7bfa2e","out_trade_no":"17410892552683","transaction_id":"4200002550202503049820164925","trade_type":"JSAPI","trade_state":"SUCCESS","trade_state_desc":"支付成功","bank_type":"OTHERS","attach":"","success_time":"2025-03-04T19:54:39+08:00","payer":{"openid":"oEoCN69YWD34_pXAJ62LGmCUGZ0s"},"amount":{"total":1,"payer_total":1,"currency":"CNY","payer_currency":"CNY"}},"associated_data":"transaction","nonce":"RUcoNL6FNrtq"}} \ No newline at end of file diff --git a/niucloud/public/login/微信小程序支付回调返回数据结构示例.txt b/niucloud/public/login/微信小程序支付回调返回数据结构示例.txt new file mode 100644 index 0000000..689b7fc --- /dev/null +++ b/niucloud/public/login/微信小程序支付回调返回数据结构示例.txt @@ -0,0 +1,34 @@ +{ + "id": "96ebf103-a48c-5bef-814b-764603f82d10", + "create_time": "2025-03-04T20:01:22+08:00", + "resource_type": "encrypt-resource", + "event_type": "TRANSACTION.SUCCESS", + "summary": "支付成功", + "resource": { + "original_type": "transaction", + "algorithm": "AEAD_AES_256_GCM", + "ciphertext": { + "mchid": "1647413230", + "appid": "wx42762c713f7bfa2e", + "out_trade_no": "17410896275527", + "transaction_id": "4200002528202503042257714180", + "trade_type": "JSAPI", + "trade_state": "SUCCESS", + "trade_state_desc": "支付成功", + "bank_type": "OTHERS", + "attach": "", + "success_time": "2025-03-04T20:01:22+08:00", + "payer": { + "openid": "oEoCN69YWD34_pXAJ62LGmCUGZ0s" + }, + "amount": { + "total": 1, + "payer_total": 1, + "currency": "CNY", + "payer_currency": "CNY" + } + }, + "associated_data": "transaction", + "nonce": "64wrO4phLhy9" + } +} \ No newline at end of file diff --git a/niucloud/public/rItAiGxXXx.txt b/niucloud/public/rItAiGxXXx.txt new file mode 100644 index 0000000..7f3821a --- /dev/null +++ b/niucloud/public/rItAiGxXXx.txt @@ -0,0 +1 @@ +2a077c4630554e1df86bd2447bb8a924 \ No newline at end of file