统一下单
public function unifiedOrder($product_type = 1, $openid = '', $phone = '') { //订单总价 $total_fee = 79900; if ($product_type == 1) { $total_fee = 79900; } if ($product_type == 2) { $total_fee = 99900; } //$total_fee = 1; $data = array( 'appid' => config('mp.appid'),//参数 'body' => '商城', 'mch_id' => config('mp.mch_id'),//商户id 'nonce_str' => self::getRandChar(32), //随机字符串 'notify_url' => '', //回调地址 'out_trade_no' => $phone . '_' . time(), //订单号 'spbill_create_ip' => self::get_client_ip(), 'total_fee' => $total_fee, 'trade_type' => "JSAPI", 'openid' => $openid, ); $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $signkey = config('mp.key'); $data["sign"] = self::getSign($data, $signkey); $xml = self::arrayToXml($data); $response = self::postXmlCurl($xml, $url); $ress = self::xmlstr_to_array($response); if ($ress['return_code'] == 'FAIL') { return array('code' => 0, 'message' => $ress['return_msg']); } if ($ress['result_code'] == 'FAIL') { return array('code' => $ress['err_code'], 'message' => $ress['err_code_des']); } $datas = [ 'appId' => config('mp.appid'),//参数 'timeStamp' => time() . '', 'nonceStr' => self::getRandChar(32), 'package' => "prepay_id=" . $ress['prepay_id'], 'signType' => "MD5", ]; //生成签名 $datas['paySign'] = self::getSign($datas, $signkey); $this->success('success', $datas); } private static function getRandChar($length) { $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; $max = strlen($strPol) - 1; for ($i = 0; $i < $length; $i++) { $str .= $strPol[rand(0, $max)]; // rand($min,$max)生成介于min和max两个数之间的一个随机整数 } return $str; } private static function get_client_ip() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } private static function getSign($Obj, $api_key) { foreach ($Obj as $k => $v) { //$Parameters[strtolower($k)] = $v; $Parameters[$k] = $v; } ksort($Parameters); //dump($Parameters); $String = self::formatBizQueryParaMap($Parameters, false); //dump($String); $String = $String . "&key=" . $api_key; //dump($String); $result = strtoupper(md5($String)); return $result; } private static function formatBizQueryParaMap($paraMap, $urlencode) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if ($urlencode) { $v = urlencode($v); } //$buff .= strtolower($k) . "=" . $v . "&"; $buff .= $k . "=" . $v . "&"; } $reqPar; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff) - 1); } return $reqPar; } private static function arrayToXml($arr) { $xml = "" ; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "" . $key . ">"; } else $xml .= "<" . $key . "> . $val . "]]>" . $key . ">"; } $xml .= ""; return $xml; } private static function postXmlCurl($xml, $url, $second = 30) { $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //设置 header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //post 提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行 curl $data = curl_exec($ch); //返回结果 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); echo "curl 出错,错误码:$error" . "
"; } } private static function xmlstr_to_array($xmlstr) { //禁止引用外部xml实体 libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring), true); return $val; }
支付成功回调
public function notify() { //获取返回的xml $testxml = file_get_contents("php://input"); //将xml转化为json格式 $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA)); //转成数组 $result = json_decode($jsonxml, true); file_log("mp_pay_notify_content.log", json_encode($result)); echo ' ';//业务逻辑 }
微信小程序端
uni.request({url: '后端下单地址',method: 'GET',data: {product_type: this.product_type,openid: this.openid,phone: this.phone},success: (cts) => {var result = cts.data.datalet that = thisuni.requestPayment({appId: result.appId, // 公众号名称,由商户传入timeStamp: result.timeStamp, // 时间戳,自1970年以来的秒数nonceStr: result.nonceStr, // 随机串package: result.package,signType: 'MD5', // 微信签名方式:paySign: result.paySign, // 微信签名success(res) {if (res.errMsg === 'requestPayment:ok') {//支付成功跳转uni.navigateTo({url: "/pages/index/order?phone=" + that.phone})}},fail(err) {console.log("支付失败", err)}})}});
来源地址:https://blog.csdn.net/qq_41526316/article/details/127652916