WechatMiniPayConfig = config('wechat'); } /** * 微信小程序支付 * @static * @access public * @param $order_number 订单号 * @param $open_id 用户openid * @param $total_fee 支付金额 * @param $body 支付抬头 * @param $attach 附加数据 */ public function dopay(string $order_number = "", string $open_id = "", float $total_fee = 0, string $body = "充值", string $attach = "充值" , string $notify_url = "" ) { if (!$order_number || !$open_id || !$total_fee || !$body || !$attach) { return ["state" => "fail"]; } // 开始组建支付数据数组 $data['param'] = []; $data['param']['appid'] = $this->WechatMiniPayConfig["appId"]; $data['param']['mch_id'] = $this->WechatMiniPayConfig["mchId"]; $data['param']['nonce_str'] = randomStr(0, 16); $data['param']['body'] = $body; $data['param']['out_trade_no'] = $order_number;//商户下的唯一订单号 // $data['param']['total_fee'] = $total_fee * 100; // 单位:分 $data['param']['total_fee'] = 1; // 单位:分 $data['param']['spbill_create_ip'] = request()->ip(); $data['param']['notify_url'] = $notify_url; $data['param']['trade_type'] = 'JSAPI';//支付方式 NATIVE表示二维码支付 // $data['param']['product_id'] = $order["commodity_id"];//商品id 非必填 $data['param']['attach'] = $attach;//附加数据 附加数据,在查询API和支付通知中原样返回,可作为自定义参数使用。 $data['param']['openid'] = $open_id;//用户openid $data['param']['sign'] = wechatSign($data['param'], $this->WechatMiniPayConfig["mchKey"]); //key为商户平台设置的密钥key $pay_data = array_to_xml($data['param']); //请求支付 JSAPI 不会直接吊起支付请求 返回prepay_id是微信生成的预支付会话标识,用于后续接口调用中使用 $result_data2 = Tool::httpRequest("https://api.mch.weixin.qq.com/pay/unifiedorder", "post", $pay_data); $result_data = xmlToArray($result_data2); //如果返回数据验证成功 if (isset($result_data["prepay_id"]) && isset($result_data["result_code"]) && $result_data["return_code"] == "SUCCESS" && $result_data["result_code"] == "SUCCESS") { $js_data = []; $js_data["appId"] = $this->WechatMiniPayConfig["appId"]; $js_data["timeStamp"] = (string)time(); $js_data["nonceStr"] = $data['param']['nonce_str']; $js_data["package"] = "prepay_id=" . $result_data["prepay_id"]; $js_data["signType"] = "MD5"; $js_data["paySign"] = wechatSign($js_data, $this->WechatMiniPayConfig["mchKey"]);; return ["state" => "ok", "data" => $js_data]; } else { return (["state" => "fail", "message" => "fail:" . (isset($result_data["err_code_des"]) ? $result_data["err_code_des"] : '错误' )]); } } /** * 微信支付查询 * @param $order_number 订单号 */ public function wechatQueryOrder($order_number = "") { if (empty($order_number)) { return ["state" => "fail", "message" => "fail"]; } $data['param'] = []; $data['param']['appid'] = $this->WechatMiniPayConfig["appId"]; $data['param']['mch_id'] = $this->WechatMiniPayConfig["mchId"]; $data['param']['nonce_str'] = randomStr(0, 16); $data['param']['out_trade_no'] = $order_number;//商户下的唯一订单号 $data['param']['sign'] = wechatSign($data['param'], $this->WechatMiniPayConfig["mchKey"]); //key为商户平台设置的密钥key $xml = array_to_xml($data['param']); $wechat_data = Tool::httpRequest("https://api.mch.weixin.qq.com/pay/orderquery", "post", $xml); $wechat_data = xmlToArray($wechat_data); if ($wechat_data["return_code"] == "SUCCESS") {// 通信标识 if ($wechat_data["result_code"] == "SUCCESS") {//业务标识 订单不存在或者微信服务器错误 if ($wechat_data["trade_state"] == "SUCCESS") {//如果支付成功 return array("state" => "ok", "queryData" => $wechat_data); } } } return array("state" => "fail"); } }