袁来如此的工作笔记
袁来如此的工作笔记
竹杖芒鞋轻胜马,谁怕? 一蓑烟雨任平生。

微信支付沙箱测试使用方法

浏览量:583

what:

步骤是:

1、获得sandbox_signkey,替换WxPayConfig里面GetKey()的返回值

2、修改WxPay.Api.php里面的链接,基本是https://api.mch.weixin.qq.com/后面加上sandboxnew/


why:

注意点:

1、沙箱要使用MD5加密做key

2、测试金额使用实例里面的,比如:301,单位是分

3、扫码支付不用扫码,显示二维码后5秒左右自动支付成功,可以用来测试异步通知


how:

获得sandbox_signkey的步骤:

    $config = new WXPayConfig();
    $sandbox_test = new Sandbox();
    $sandbox_test->s_setValues(\mch_id\, $config->GetMerchantId());
    $sandbox_test->s_setValues(\nonce_str\, $sandbox_test->getNonceStr());
    $sandbox_test->s_setValues(\sign\, $sandbox_test->setSign($config));

    $rs = Sandbox::getSignKey($sandbox_test);
    
    $sandbox_signkey=$rs[\sandbox_signkey\];	
    return $sandbox_signkey;

Sandbox()的内容:


class Sandbox extends WxPayDatabase{

    //设置对象的属性
    public function s_setValues($k, $v){
        $this->values[$k] = $v;
    }

    //读取对象的属性
    public function s_getValues($k){
        return $this->values[$k];
    }
    
    //获取密钥API
    public static function getSignKey($input){
        
        //提交业务
        $url = \https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey\;
        
        //生成签名
        
        $xml = $input->array2xml();

        //向微信请求接口
        $result = self::postXmlCurl($xml, $url);
        $result = $input->xml2array($result);

        return $result;
    }

    

    
    public static function getNonceStr($length = 32) 
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {  
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
        } 
        return $str;
    }


    
    private static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
    {    
        $ch = curl_init();
        
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);

        //设置访问的网址
        curl_setopt($ch, CURLOPT_URL, $url);
        
        //skysowe_modifid
        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);

        //console_log( \cert:\.$useCert );
        if($useCert == true){
            //设置证书
            //使用证书:cert 与 key 分别属于两个.pem文件
            curl_setopt($ch,CURLOPT_SSLCERTTYPE,\PEM\);
            curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH);
            curl_setopt($ch,CURLOPT_SSLKEYTYPE,\PEM\);
            curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH);
        }
        
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        //console_log( \xml:\.json_encode($xml) );

        //运行curl
        $data = curl_($ch);
        //console_log( \curl_result:\.json_encode($data) );
        
        //返回结果
        if($data){
            curl_close($ch);
            return $data;
        } else { 
            $error = curl_errno($ch);
            curl_close($ch);
            throw new WxPayException("curl出错,错误码:$error");
            //console_log( \curl_error:\.$error );
        }
    }


    
    public function array2xml()
    {
        if(!is_array($this->values) 
            || count($this->values) <= 0)
        {
            throw new WxPayException("数组数据异常!");
        }
        
        $xml = "<xml>";
        foreach ($this->values as $key=>$val)
        {
            if (is_numeric($val) || $key=="nonce_str" || $key=="sign" ){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml; 
    }

    
    public function xml2array($xml)
    {    
        if(!$xml){
            throw new WxPayException("xml数据异常!");
        }
        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $this->values = json_decode(json_encode(simplexml_load_string($xml, \SimpleXMLElement\, LIBXML_NOCDATA)), true);        
        return $this->values;
    }

}





打赏