打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
微信小程序支付中的prepay


原创 2017年08月07日 21:51:25
//获取支付必备参数prepay_id 以及进行支付返回paysignpublic function doPagegetprepayid(){   //进行查询数据库获得支付参数,    $resinfo = "SELECT * FROM " . tablename('abcshopping_pay') . " WHERE  `id` =  1 ";    $app_info = pdo_fetch($resinfo);    foreach ($app_info as $k => &$v) {        $v = trim($v);    };    $app_info['shopidshop']=$app_info['shopstart'];    header("Content-type:text/html;charset=utf-8"); //此处进行字符集初始化,    global $_GPC, $_W;    $order_id = $_GPC['orderid'];    $sql = "SELECT * FROM " . tablename('abcshopping_order') . " WHERE  `id` = '" . $order_id . "'";    $res = pdo_fetch($sql);    function http_request_curl($url, $rawData)    {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_HEADER, 0);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);        curl_setopt(            $ch, CURLOPT_HTTPHEADER,            array(                'Content-Type: text'            )        );        $data = curl_exec($ch);        curl_close($ch);        return ($data);    }    $data = array(        //'appid' => 'wxf0d27e41678c566c',  //小程序appid        'appid' => $app_info['appid'],        'body' => '腾讯-游戏',        //'mch_id' => '1484855592',         //微信支付商户号        'mch_id' =>  $app_info['shopid'],        'nonce_str' => md5(date('YmdHis') . time() . rand(1000, 9999)),           //随机字符串        'notify_url' => 'http://www.weixin.qq.com/wxpay/pay.php',    //异步回调地址        'openid' => $_GPC['openid'],        //用户登录时获取的code中含有的值        'out_trade_no' => $res['ordersn'],               //商家订单号        'spbill_create_ip' => $_GPC['ip'],           //APP和网页支付提交用户端ip        'total_fee' => $res['price'] * 100,                  //订单总额        'trade_type' => 'JSAPI'           //交易类型    );    //$key = 'z4hgl4cnf5ac2wl3msiek5p0x3aiy2yc'; //商户秘钥    $key =  $app_info['shopidshop'];    //制作签名    //签名步骤一:按字典序排序参数    ksort($data);    $buff = "";    foreach ($data as $k => $v) {        if ($k != "sign" && $v != "" && !is_array($v)) {            $buff .= $k . "=" . $v . "&";        }    }    $buff = trim($buff, "&");    //签名步骤二:在string后加入KEY    $string = $buff . "&key=" . $key;    //签名步骤三:MD5加密    $string = md5($string);    //签名步骤四:所有字符转为大写    $sign = strtoupper($string);    $data['sign'] = $sign;    ksort($data);    //进行拼接数据    $abc_xml = "<xml>";    foreach ($data as $key => $val) {        if (is_numeric($val)) {            $abc_xml .= "<" . $key . ">" . $val . "</" . $key . ">";        } else {            $abc_xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";        }    }    $abc_xml .= "</xml>";    //统一下单接口prepay_id    $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';    $xml = http_request_curl($url, $abc_xml);     //POST方式请求http    //XMl转化为数组    function xmlToArray($xml)    {        //禁止引用外部xml实体        libxml_disable_entity_loader(true);        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);        $val = json_decode(json_encode($xmlstring), true);        return $val;    }    $info = xml2array($xml);    //进行拼接sign    function MakeSign($params, $KEY)    {        //签名步骤一:按字典序排序数组参数        ksort($params);        $buff1 = '';        foreach ($params as $k => $v) {            if ($k != "sign" && $v != "" && !is_array($v)) {                $buff1 .= $k . "=" . $v . "&";            }        }        $buff1 = trim($buff1, "&");        //签名步骤二:在string后加入KEY        $string = $buff1 . "&key=" . $KEY;        //签名步骤三:MD5加密        $string = md5($string);        //签名步骤四:所有字符转为大写        $result = strtoupper($string);        return $result;    }    $params = array(        'appId' => $data['appid'],        'nonceStr' => $data['nonce_str'],        'package' => 'Sign=WXPay&prepay_id=' . $info['prepay_id'],        'signType' => 'MD5',        'timeStamp' => time()    );    //$key = 'z4hgl4cnf5ac2wl3msiek5p0x3aiy2yc'; //商户秘钥    $key =  $app_info['shopidshop'];    $info['paySign'] = MakeSign($params, $key);    $info['timeStamp'] = "" . $params['timeStamp'] . "";    $info['nonceStr'] = $params['nonceStr'];    $info['package'] = $params['package'];    $info = array_merge($info, $app_info);    //请求成功后进行返回数据信息    if ($info['return_code'] == 'SUCCESS' || $info['result_code'] == 'SUCCESS') {        $errno = 0;        $message = '请求成功';        return $this->result($errno, $message, $info);    } else {        $errno = -1;        $message = '请求失败';        return $this->result($errno, $message, $info);    }}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
php获取通过http协议post提交过来xml数据及解析xml
点播 获取视频信息
php 如何发送http请求和实现https接口
curl 发送采集请求的方法
PHP curl提交参数到某个网址,然后获取数据
php 自动提交表单
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服