Home > php教程 > php手册 > WeChat web client generates payment QR code

WeChat web client generates payment QR code

WBOY
Release: 2016-10-15 10:31:46
Original
2276 people have browsed it

Authorized to obtain QR code category:

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Trade类
 * @author xyyphp
 * @date 2016/10/10
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span> TradeController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseController
{
        </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$mchid</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$appid</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$key</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> init()
    {
            parent</span>::<span style="color: #000000;">init();
            </span><span style="color: #800080;">$this</span>->mchid = WX_MCHID; <span style="color: #008000;">//</span><span style="color: #008000;"> 微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送</span>
            <span style="color: #800080;">$this</span>->appid = WX_APPID; <span style="color: #008000;">//</span><span style="color: #008000;">公众号APPID 通过微信支付商户资料审核后邮件发送</span>
            <span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span> = WX_APIKEY;   <span style="color: #008000;">//</span><span style="color: #008000;">https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥</span>
<span style="color: #000000;">    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
    * @param float $totalFee 收款总费用 单位元
    * @param string $outTradeNo 唯一的订单号
    * @param string $orderName 订单名称
    * @param string $notifyUrl 支付结果通知url 不要有问号
    * https://mp.weixin.qq.com/ 微信支付-开发配置-测试目录
    * 测试目录 http://mp.izhanlue.com/paytest/  最后需要斜线,(需要精确到二级或三级目录)
    * @return string
    </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> createJsBizPackage(<span style="color: #800080;">$totalFee</span>, <span style="color: #800080;">$outTradeNo</span>, <span style="color: #800080;">$orderName</span>, <span style="color: #800080;">$notifyUrl</span>, <span style="color: #800080;">$timestamp</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
                    </span>'mch_id' => <span style="color: #800080;">$this</span>->mchid,
                    'appid' => <span style="color: #800080;">$this</span>->appid,
                    'key' => <span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span>,<span style="color: #000000;">
        );
        </span><span style="color: #800080;">$unified</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
            </span>'appid' => <span style="color: #800080;">$config</span>['appid'],
            'attach' => '支付',             <span style="color: #008000;">//</span><span style="color: #008000;">商家数据包,原样返回</span>
            'body' => <span style="color: #800080;">$orderName</span>,
            'mch_id' => <span style="color: #800080;">$config</span>['mch_id'],
            'nonce_str' => <span style="color: #800080;">$this</span>->createNonceStr(),
            'notify_url' => <span style="color: #800080;">$notifyUrl</span>,
            <span style="color: #008000;">//</span><span style="color: #008000;">'openid' => $openid,            //rade_type=JSAPI,此参数必传</span>
            'out_trade_no' => <span style="color: #800080;">$outTradeNo</span>,
            'spbill_create_ip' => '127.0.0.1',
            'total_fee' => <span style="color: #008080;">intval</span>(<span style="color: #800080;">$totalFee</span> * 100),       <span style="color: #008000;">//</span><span style="color: #008000;">单位 转为分</span>
            'trade_type' => 'NATIVE',<span style="color: #000000;">
        );
        </span><span style="color: #800080;">$unified</span>['sign'] = <span style="color: #800080;">$this</span>->getSign(<span style="color: #800080;">$unified</span>, <span style="color: #800080;">$config</span>['key'<span style="color: #000000;">]);
        </span><span style="color: #800080;">$responseXml</span> = <span style="color: #800080;">$this</span>->curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', <span style="color: #800080;">$this</span>->arrayToXml(<span style="color: #800080;">$unified</span><span style="color: #000000;">));
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">
        <xml>
        <return_code><![CDATA[SUCCESS]]></return_code>
        <return_msg><![CDATA[OK]]></return_msg>
        <appid><![CDATA[wx00e5904efec77699]]></appid>
        <mch_id><![CDATA[1220647301]]></mch_id>
        <nonce_str><![CDATA[1LHBROsdmqfXoWQR]]></nonce_str>
        <sign><![CDATA[ACA7BC8A9164D1FBED06C7DFC13EC839]]></sign>
        <result_code><![CDATA[SUCCESS]]></result_code>
        <prepay_id><![CDATA[wx2015032016590503f1bcd9c30421762652]]></prepay_id>
        <trade_type><![CDATA[JSAPI]]></trade_type>
        </xml>
        </span><span style="color: #008000;">*/</span>
        <span style="color: #800080;">$unifiedOrder</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$responseXml</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$unifiedOrder</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>('parse xml error'<span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$unifiedOrder</span>->return_code != 'SUCCESS'<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>(<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">return_msg);
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$unifiedOrder</span>->result_code != 'SUCCESS'<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>(<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">err_code);
            </span><span style="color: #008000;">/*</span><span style="color: #008000;">
            NOAUTH 商户无此接口权限
            NOTENOUGH 余额不足
            ORDERPAID 商户订单已支付
            ORDERCLOSED 订单已关闭
            SYSTEMERROR 系统错误
            APPID_NOT_EXIST   APPID不存在
            MCHID_NOT_EXIST MCHID不存在
            APPID_MCHID_NOT_MATCH appid和mch_id不匹配
            LACK_PARAMS 缺少参数
            OUT_TRADE_NO_USED 商户订单号重复
            SIGNERROR 签名错误
            XML_FORMAT_ERROR XML格式错误
            REQUIRE_POST_METHOD 请使用post方法
            POST_DATA_EMPTY post数据为空
            NOT_UTF8 编码格式错误
            </span><span style="color: #008000;">*/</span><span style="color: #000000;">
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">$unifiedOrder->trade_type 交易类型 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP
        //$unifiedOrder->prepay_id 预支付交易会话标识 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时
        //$unifiedOrder->code_url 二维码链接 trade_type为NATIVE是有返回,可将该参数值生成二维码展示出来进行扫码支付</span>
                <span style="color: #800080;">$code_list</span> = (<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">code_url;
        </span><span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
            </span>"appId" => <span style="color: #800080;">$config</span>['appid'],
            "timeStamp" => <span style="color: #800080;">$timestamp</span>,
            "nonceStr" => <span style="color: #800080;">$this</span>->createNonceStr(),
            "package" => "prepay_id=" . <span style="color: #800080;">$unifiedOrder</span>->prepay_id,
            "signType" => 'MD5',
                        "code_url" => <span style="color: #800080;">$code_list</span>[0<span style="color: #000000;">]
        );
        </span><span style="color: #800080;">$arr</span>['paySign'] = <span style="color: #800080;">$this</span>->getSign(<span style="color: #800080;">$arr</span>, <span style="color: #800080;">$config</span>['key'<span style="color: #000000;">]);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
    }
        
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
         * 查询订单
    </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> orderQuery(<span style="color: #800080;">$outTradeNo</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
                    </span>'mch_id' => <span style="color: #800080;">$this</span>->mchid,
                    'appid' => <span style="color: #800080;">$this</span>->appid,
                    'key' => <span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span>,<span style="color: #000000;">
        );
        </span><span style="color: #800080;">$unified</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
            </span>'appid' => <span style="color: #800080;">$config</span>['appid'],
            'mch_id' => <span style="color: #800080;">$config</span>['mch_id'],
            'out_trade_no' => <span style="color: #800080;">$outTradeNo</span>,
                        'nonce_str' => <span style="color: #800080;">$this</span>-><span style="color: #000000;">createNonceStr()
        );
        </span><span style="color: #800080;">$unified</span>['sign'] = <span style="color: #800080;">$this</span>->getSign(<span style="color: #800080;">$unified</span>, <span style="color: #800080;">$config</span>['key'<span style="color: #000000;">]);
        </span><span style="color: #800080;">$responseXml</span> = <span style="color: #800080;">$this</span>->curlPost('https://api.mch.weixin.qq.com/pay/orderquery', <span style="color: #800080;">$this</span>->arrayToXml(<span style="color: #800080;">$unified</span><span style="color: #000000;">));
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">
        <xml><return_code><![CDATA[SUCCESS]]></return_code>
                <return_msg><![CDATA[OK]]></return_msg>
                <appid><![CDATA[wx406c5455be1a2590]]></appid>
                <mch_id><![CDATA[1398638602]]></mch_id>
                <nonce_str><![CDATA[ousUeAEvH6KlDiGe]]></nonce_str>
                <sign><![CDATA[D8BFB54A52C324238D5A4BA8C8BC6D01]]></sign>
                <result_code><![CDATA[SUCCESS]]></result_code>
                <out_trade_no><![CDATA[1476157452]]></out_trade_no>
                <trade_state><![CDATA[NOTPAY]]></trade_state>
                <trade_state_desc><![CDATA[订单未支付]]></trade_state_desc>
                </xml>
        </span><span style="color: #008000;">*/</span>
        <span style="color: #800080;">$unifiedOrder</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$responseXml</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$unifiedOrder</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>('parse xml error'<span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$unifiedOrder</span>->return_code != 'SUCCESS'<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>(<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">return_msg);
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$unifiedOrder</span>->result_code != 'SUCCESS'<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>(<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">err_code);
            </span><span style="color: #008000;">/*</span><span style="color: #008000;">
            NOAUTH 商户无此接口权限
            NOTENOUGH 余额不足
            ORDERPAID 商户订单已支付
            ORDERCLOSED 订单已关闭
            SYSTEMERROR 系统错误
            APPID_NOT_EXIST   APPID不存在
            MCHID_NOT_EXIST MCHID不存在
            APPID_MCHID_NOT_MATCH appid和mch_id不匹配
            LACK_PARAMS 缺少参数
            OUT_TRADE_NO_USED 商户订单号重复
            SIGNERROR 签名错误
            XML_FORMAT_ERROR XML格式错误
            REQUIRE_POST_METHOD 请使用post方法
            POST_DATA_EMPTY post数据为空
            NOT_UTF8 编码格式错误
            </span><span style="color: #008000;">*/</span><span style="color: #000000;">
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">$unifiedOrder->trade_type 交易类型 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP
        //$unifiedOrder->prepay_id 预支付交易会话标识 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时
        //$unifiedOrder->code_url 二维码链接 trade_type为NATIVE是有返回,可将该参数值生成二维码展示出来进行扫码支付</span>
                <span style="color: #008000;">/*</span><span style="color: #008000;">
                public 'return_code' => string 'SUCCESS' (length=7)
                public 'return_msg' => string 'OK' (length=2)
                public 'appid' => string 'wx406c5455be1a2590' (length=18)
                public 'mch_id' => string '1398638602' (length=10)
                public 'nonce_str' => string 'lyt2ZGFeE9G0DI4W' (length=16)
                public 'sign' => string 'F942BB644E72FE29936C80E6780D8D8A' (length=32)
                public 'result_code' => string 'SUCCESS' (length=7)
                public 'out_trade_no' => string '1476157452' (length=10)
                public 'trade_state' => string 'NOTPAY' (length=6)
                public 'trade_state_desc' => string '订单未支付' (length=15)
                </span><span style="color: #008000;">*/</span>
                <span style="color: #008000;">//</span><span style="color: #008000;">已结支付</span>
                <span style="color: #008000;">/*</span><span style="color: #008000;">
                object(SimpleXMLElement)[26]
                    public 'return_code' => string 'SUCCESS' (length=7)
                    public 'return_msg' => string 'OK' (length=2)
                    public 'appid' => string 'wx406c5455be1a2590' (length=18)
                    public 'mch_id' => string '1398638602' (length=10)
                    public 'nonce_str' => string '6cFpzNc5BTyZ02x0' (length=16)
                    public 'sign' => string 'F01C79BD0FC4BC4FE38231BA92A91201' (length=32)
                    public 'result_code' => string 'SUCCESS' (length=7)
                    public 'openid' => string 'ojIdvuCVrweMrgaU-dFobIItTtpk' (length=28)
                    public 'is_subscribe' => string 'N' (length=1)
                    public 'trade_type' => string 'NATIVE' (length=6)
                    public 'bank_type' => string 'CFT' (length=3)
                    public 'total_fee' => string '1' (length=1)
                    public 'fee_type' => string 'CNY' (length=3)
                    public 'transaction_id' => string '4001652001201610116386495939' (length=28)
                    public 'out_trade_no' => string '1476161468' (length=10)
                    public 'attach' => string '支付' (length=6)
                    public 'time_end' => string '20161011125205' (length=14)
                    public 'trade_state' => string 'SUCCESS' (length=7)
                    public 'cash_fee' => string '1' (length=1)
                 </span><span style="color: #008000;">*/</span>
                <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">trade_state_desc)){
                    </span><span style="color: #800080;">$msg</span> = "订单未支付"<span style="color: #000000;">;
                }
                </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$unifiedOrder</span>-><span style="color: #000000;">transaction_id)){
                    </span><span style="color: #800080;">$msg</span> = "已支付"<span style="color: #000000;">;
                }
                </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$msg</span><span style="color: #000000;">;
    }
    
    
    </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> notify()
    {
        </span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
                    </span>'mch_id' => <span style="color: #800080;">$this</span>->mchid,
                    'appid' => <span style="color: #800080;">$this</span>->appid,
                    'key' => <span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span>,<span style="color: #000000;">
        );
        </span><span style="color: #800080;">$postStr</span> = <span style="color: #800080;">$GLOBALS</span>["HTTP_RAW_POST_DATA"<span style="color: #000000;">];
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">
        $postStr = '<xml>
        <appid><![CDATA[wx00e5904efec77699]]></appid>
        <attach><![CDATA[支付测试]]></attach>
        <bank_type><![CDATA[CMB_CREDIT]]></bank_type>
        <cash_fee><![CDATA[1]]></cash_fee>
        <fee_type><![CDATA[CNY]]></fee_type>
        <is_subscribe><![CDATA[Y]]></is_subscribe>
        <mch_id><![CDATA[1220647301]]></mch_id>
        <nonce_str><![CDATA[a0tZ41phiHm8zfmO]]></nonce_str>
        <openid><![CDATA[oU3OCt5O46PumN7IE87WcoYZY9r0]]></openid>
        <out_trade_no><![CDATA[550bf2990c51f]]></out_trade_no>
        <result_code><![CDATA[SUCCESS]]></result_code>
        <return_code><![CDATA[SUCCESS]]></return_code>
        <sign><![CDATA[F6F519B4DD8DB978040F8C866C1E6250]]></sign>
        <time_end><![CDATA[20150320181606]]></time_end>
        <total_fee>1</total_fee>
        <trade_type><![CDATA[JSAPI]]></trade_type>
        <transaction_id><![CDATA[1008840847201503200034663980]]></transaction_id>
        </xml>';
        </span><span style="color: #008000;">*/</span>
        <span style="color: #800080;">$postObj</span> = <span style="color: #008080;">simplexml_load_string</span>(<span style="color: #800080;">$postStr</span>, 'SimpleXMLElement',<span style="color: #000000;"> LIBXML_NOCDATA);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$postObj</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>('parse xml error'<span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$postObj</span>->return_code != 'SUCCESS'<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>(<span style="color: #800080;">$postObj</span>-><span style="color: #000000;">return_msg);
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$postObj</span>->result_code != 'SUCCESS'<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">die</span>(<span style="color: #800080;">$postObj</span>-><span style="color: #000000;">err_code);
        }
        </span><span style="color: #800080;">$arr</span> = (<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$postObj</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$arr</span>['sign'<span style="color: #000000;">]);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$this</span>->getSign(<span style="color: #800080;">$arr</span>, <span style="color: #800080;">$config</span>['key']) == <span style="color: #800080;">$postObj</span>-><span style="color: #000000;">sign) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> $mch_id = $postObj->mch_id; //微信支付分配的商户号
            // $appid = $postObj->appid; //微信分配的公众账号ID
            // $openid = $postObj->openid; //用户在商户appid下的唯一标识
            // $transaction_id = $postObj->transaction_id;//微信支付订单号
            // $out_trade_no = $postObj->out_trade_no;//商户订单号
            // $total_fee = $postObj->total_fee; //订单总金额,单位为分
            // $is_subscribe = $postObj->is_subscribe; //用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效
            // $attach = $postObj->attach;//商家数据包,原样返回
            // $time_end = $postObj->time_end;//支付完成时间</span>
            <span style="color: #0000ff;">echo</span> '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'<span style="color: #000000;">;
            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$postObj</span><span style="color: #000000;">;
        }
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * curl get
    * @param string $url
    * @param array $options
    * @return mixed
    </span><span style="color: #008000;">*/</span>
       <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> curlGet(<span style="color: #800080;">$url</span> = '', <span style="color: #800080;">$options</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">())
    {
        </span><span style="color: #800080;">$ch</span> = curl_init(<span style="color: #800080;">$url</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_TIMEOUT, 30<span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$options</span><span style="color: #000000;">)) {
            curl_setopt_array(</span><span style="color: #800080;">$ch</span>, <span style="color: #800080;">$options</span><span style="color: #000000;">);
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">https请求 不验证证书和host</span>
        curl_setopt(<span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$data</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
        curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * curl post
    * @param string $url
    * @param array $options
    * @return mixed
    </span><span style="color: #008000;">*/</span>
       <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> curlPost(<span style="color: #800080;">$url</span> = '', <span style="color: #800080;">$postData</span> = '', <span style="color: #800080;">$options</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">())
    {
        </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$postData</span><span style="color: #000000;">)) {
            </span><span style="color: #800080;">$postData</span> = <span style="color: #008080;">http_build_query</span>(<span style="color: #800080;">$postData</span><span style="color: #000000;">);
        }
        </span><span style="color: #800080;">$ch</span> =<span style="color: #000000;"> curl_init();
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_URL, <span style="color: #800080;">$url</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_POST, 1<span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: #800080;">$postData</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_TIMEOUT, 30); <span style="color: #008000;">//</span><span style="color: #008000;">设置cURL允许执行的最长秒数</span>
        <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$options</span><span style="color: #000000;">)) {
            curl_setopt_array(</span><span style="color: #800080;">$ch</span>, <span style="color: #800080;">$options</span><span style="color: #000000;">);
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">https请求 不验证证书和host</span>
        curl_setopt(<span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
        curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$data</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
        curl_close(</span><span style="color: #800080;">$ch</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * 随机生成一定长度字符串
    * @param string $url
    * @param array $options
    * @return mixed
    </span><span style="color: #008000;">*/</span>
       <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> createNonceStr(<span style="color: #800080;">$length</span> = 16<span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$chars</span> = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'<span style="color: #000000;">;
        </span><span style="color: #800080;">$str</span> = ''<span style="color: #000000;">;
        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> < <span style="color: #800080;">$length</span>; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) {
            </span><span style="color: #800080;">$str</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$chars</span>, <span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$chars</span>) - 1), 1<span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$str</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * 数组转成XML
    * @param string $url
    * @param array $options
    * @return mixed
    </span><span style="color: #008000;">*/</span>
       <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> arrayToXml(<span style="color: #800080;">$arr</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$xml</span> = "<xml>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$arr</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$val</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_numeric</span>(<span style="color: #800080;">$val</span><span style="color: #000000;">)) {
            </span><span style="color: #800080;">$xml</span> .= "<" . <span style="color: #800080;">$key</span> . ">" . <span style="color: #800080;">$val</span> . "</" . <span style="color: #800080;">$key</span> . ">"<span style="color: #000000;">;
        } </span><span style="color: #0000ff;">else</span>
            <span style="color: #800080;">$xml</span> .= "<" . <span style="color: #800080;">$key</span> . "><![CDATA[" . <span style="color: #800080;">$val</span> . "]]></" . <span style="color: #800080;">$key</span> . ">"<span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$xml</span> .= "</xml>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$xml</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * 例如:
    * appid:  wxd930ea5d5a258f4f
    * mch_id:  10000100
    * device_info: 1000
    * Body:  test
    * nonce_str: ibuaiVcKdpRxkhJA
    * 第一步:对参数按照 key=value 的格式,并按照参数名 ASCII 字典序排序如下:
    * stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
    * d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
    * 第二步:拼接支付密钥:
    * stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
    * sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7"
    </span><span style="color: #008000;">*/</span>
       <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> getSign(<span style="color: #800080;">$params</span>, <span style="color: #800080;">$key</span><span style="color: #000000;">)
    {
        </span><span style="color: #008080;">ksort</span>(<span style="color: #800080;">$params</span>,<span style="color: #000000;"> SORT_STRING);
        </span><span style="color: #800080;">$unSignParaString</span> = <span style="color: #800080;">$this</span>->formatQueryParaMap(<span style="color: #800080;">$params</span>, <span style="color: #0000ff;">false</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$signStr</span> = <span style="color: #008080;">strtoupper</span>(<span style="color: #008080;">md5</span>(<span style="color: #800080;">$unSignParaString</span> . "&key=" . <span style="color: #800080;">$key</span><span style="color: #000000;">));
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$signStr</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
    * 签名时字符组合
    * @param string $url
    * @param array $options
    * @return mixed
    </span><span style="color: #008000;">*/</span>
       <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> formatQueryParaMap(<span style="color: #800080;">$paraMap</span>, <span style="color: #800080;">$urlEncode</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$buff</span> = ""<span style="color: #000000;">;
        </span><span style="color: #008080;">ksort</span>(<span style="color: #800080;">$paraMap</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$paraMap</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span> => <span style="color: #800080;">$v</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> != <span style="color: #800080;">$v</span> && "null" != <span style="color: #800080;">$v</span><span style="color: #000000;">) {
                </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$urlEncode</span><span style="color: #000000;">) {
                    </span><span style="color: #800080;">$v</span> = <span style="color: #008080;">urlencode</span>(<span style="color: #800080;">$v</span><span style="color: #000000;">);
                }
                </span><span style="color: #800080;">$buff</span> .= <span style="color: #800080;">$k</span> . "=" . <span style="color: #800080;">$v</span> . "&"<span style="color: #000000;">;
            }
        }
        </span><span style="color: #800080;">$reqPar</span> = ''<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$buff</span>) > 0<span style="color: #000000;">) {
            </span><span style="color: #800080;">$reqPar</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$buff</span>, 0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$buff</span>) - 1<span style="color: #000000;">);
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$reqPar</span><span style="color: #000000;">;
    }
}</span>
Copy after login

Retrieve instance:

<span style="color: #800080;">$money</span> = 0.1;<span style="color: #008000;">//</span><span style="color: #008000;">订单金额</span>
<span style="color: #800080;">$out_trade_no</span> = 'test_8888888';<span style="color: #008000;">//</span><span style="color: #008000;">唯一的订单号</span>
<span style="color: #800080;">$order_name</span> = '测试订单';<span style="color: #008000;">//</span><span style="color: #008000;">订单名称</span>
<span style="color: #800080;">$notifyUrl</span> = 'http://www.test.com/';<span style="color: #008000;">//</span><span style="color: #008000;">支付结果通知url 不要有问号</span>
<span style="color: #800080;">$timeStamp</span> = <span style="color: #008080;">time</span>();<span style="color: #008000;">//</span><span style="color: #008000;">时间戳

//获取二维码信息</span>
<span style="color: #800080;">$wx_result</span> = <span style="color: #800080;">$this</span>->createJsBizPackage(<span style="color: #800080;">$money</span>, <span style="color: #800080;">$out_trade_no</span>, <span style="color: #800080;">$order_name</span>, <span style="color: #800080;">$notifyUrl</span>, <span style="color: #800080;">$timeStamp</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">检测支付状态</span>
<span style="color: #800080;">$str_status</span> = <span style="color: #800080;">$this</span>->orderQuery(<span style="color: #800080;">$out_trade_no</span>);
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template