评论

微信支付商户免充值代金券接口升级验收脚本(PHP版) 用例组合1001+1002+1003+1004+1005

微信支付商户免充值代金券接口升级验收脚本(PHP版) 用例组合1001+1002+1003+1004+1005

前言:

本文是在社区网友Memory的原贴基础上,转译成的PHP版本用例。

附原文地址:微信支付商户免充值代金券接口升级验收脚本 用例组合1001+1002+1003+1004+1005(强迫症专用)? - 微信开放社区 https://developers.weixin.qq.com/community/develop/article/doc/0002e82b060c3028230c915f150813

1,为什么要升级?

开通免充值代金券后,几个支付相关接口参数会发生变动,需要改相应代码适配变动。

详见:开通免充值业务API接口升级详情 https://pay.weixin.qq.com/wiki/doc/api/download/ptsh_mczjksj.pdf

2,官方的测试验收示例

详见:https://pay.weixin.qq.com/wiki/doc/api/download/mczyscsyl.pdf

3,接口升级验收进度查询

详见:https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=15_6&index=4

4,使用自动升级验收脚本前的重要提醒!

请务必去改自己的应用层代码,这里只是方便统一验收用。

5,自动升级验收脚本代码(PHP版)

上代码:

 "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'body'=> "check",
		'out_trade_no'=> $out_trade_no,
		'total_fee'=> "501",
		'spbill_create_ip'=> "8.8.8.8",
		'auth_code'=> "120061098828009406"];
	$MicroPay_xml = build_xml($MicroPay_param, $key);
	request_handler(MICROPAY_URL, $MicroPay_xml, "亲,用例编号1001刷卡正常支付有结果了,快来看呀  
");
	sleep(1);

	$OrderQuery_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_trade_no'=> $out_trade_no];
	$OrderQuery_xml = build_xml($OrderQuery_param, $key);
	request_handler(ORDERQUERY_URL, $OrderQuery_xml, "亲,用例编号1001刷卡正常支付查询出结果了,快来看呀 
");
	sleep(1);

	$out_trade_no_2nd = time();
	print_r("我是1002下单的订单号:".$out_trade_no_2nd."
");
	$MicroPay_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'body'=> "check",
		'out_trade_no'=> $out_trade_no_2nd,
		'total_fee'=> "502",
		'spbill_create_ip'=> "8.8.8.8",
		'auth_code'=> "120061098828009406"];
	$MicroPay_xml = build_xml($MicroPay_param, $key);
    request_handler(MICROPAY_URL, $MicroPay_xml, "亲,用例编号1002刷卡正常支付结果来了,你还抓紧不来看  
");
    sleep(1);

	$OrderQuery_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_trade_no'=> $out_trade_no_2nd];
	$OrderQuery_xml = build_xml($OrderQuery_param, $key);
    request_handler(ORDERQUERY_URL, $OrderQuery_xml, "亲,用例编号1002刷卡正常支付查询结果,结果好像有点不太对呢  
");
    sleep(1);

	$ReFund_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_refund_no'=> $out_trade_no,
		'total_fee'=> "502",
		'refund_fee'=> "501",
		'out_trade_no'=> $out_trade_no_2nd];
	$ReFund_xml = build_xml($ReFund_param, $key);
    request_handler(REFUND_URL, $ReFund_xml, "亲,下面展示的是用例编号1002刷卡支付退款的结果,你猜对不对  
");
    sleep(1);

	$RefundQuery_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_trade_no'=> $out_trade_no_2nd];
	$RefundQuery_xml = build_xml($RefundQuery_param, $key);
    request_handler(REFUNDQUERY_URL, $RefundQuery_xml, "亲,用例编号1002刷卡支付退款查询结果返回中,加载不出来长按电源键或Alt+F4重试哦  
");
    sleep(1);

	$nonce_str = getRandChar(32);
	$out_trade_no = time();
	$UnifiedOrder_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'body'=> "check",
		'out_trade_no'=> $out_trade_no,
		'total_fee'=> "551",
		'notify_url'=> "https://www.weixin.qq.com/wxpay/pay.php",
		'spbill_create_ip'=> "8.8.8.8",
		'trade_type'=> "JSAPI"];
	$UnifiedOrder_xml = build_xml($UnifiedOrder_param, $key);
    request_handler(UNIFIEDORDER_URL, $UnifiedOrder_xml, "亲,用例编号1003-公众号/APP/扫码正常支付有结果了,快来看呀  
");
    sleep(1);

	$OrderQuery_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_trade_no'=> $out_trade_no];
    $OrderQuery_xml = build_xml($OrderQuery_param, $key);
    request_handler(ORDERQUERY_URL, $OrderQuery_xml, "亲,用例编号1003-公众号/APP/扫码正常支付查询出结果了,快来看呀  
");
	sleep(1);

    $out_trade_no_2nd = time();
	print_r("我是1002下单的订单号:".$out_trade_no_2nd."
");
    $UnifiedOrder_param =  ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'body'=> "check",
		'out_trade_no'=> $out_trade_no_2nd,
		'total_fee'=> "552",
		'notify_url'=> "https://www.weixin.qq.com/wxpay/pay.php",
		'spbill_create_ip'=> "8.8.8.8",
		'trade_type'=> "JSAPI"];
    $UnifiedOrder_xml = build_xml($UnifiedOrder_param, $key);
    request_handler(UNIFIEDORDER_URL, $UnifiedOrder_xml, "亲,用例编号1004-公众号/APP/扫码支付退款结果来了,你还抓紧不来看  
");
    sleep(1);

    $OrderQuery_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_trade_no'=> $out_trade_no_2nd];
    $OrderQuery_xml = build_xml($OrderQuery_param, $key);
    request_handler(ORDERQUERY_URL, $OrderQuery_xml, "亲,用例编号1004-公众号/APP/扫码支付退款查询结果,结果好像有点不太对呢  
");
    sleep(1);

    $ReFund_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_refund_no'=> $out_trade_no,
		'total_fee'=> "552",
		'refund_fee'=> "551",
		'out_trade_no'=> $out_trade_no_2nd];
    $ReFund_xml = build_xml($ReFund_param, $key);
    request_handler(REFUND_URL, $ReFund_xml, "亲,下面展示的是用例编号1004-公众号/APP/扫码支付退款的结果,你猜对不对  
");
    sleep(1);

    $RefundQuery_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'out_trade_no'=> $out_trade_no_2nd];
    $RefundQuery_xml = build_xml($RefundQuery_param, $key);
    request_handler(REFUNDQUERY_URL, $RefundQuery_xml, "亲,用例编号1004-公众号/APP/扫码支付退款查询结果返回中,加载不出来长按电源键或Alt+F4重试哦  
");
    sleep(1);

    $DownloadBill_param = ['appid'=> "wxd678efh567hg6787",
		'mch_id'=> $mch_id,
		'nonce_str'=> $nonce_str,
		'bill_date'=> "2021-04-01",
		'bill_type'=> "ALL"];


    $DownloadBill_xml = build_xml($DownloadBill_param, $key);
    request_handler(DOWNLOADBILL_URL, $DownloadBill_xml, "亲,你要下载交易的对账单来了,加载中······,加载不出来长按电源键或Alt+F4重试哦  
");
	print_r("
亲,升级验证结束了哦:  
");
}

function get_sign_key($mch_id, $key){
	$template = "";
	$nonce_str = getRandChar(32);
	$encrypted_str = "mch_id=".$mch_id."&nonce_str=".$nonce_str."&key=".$key;
	$encrypted_value = md5($encrypted_str);
	$xml = sprintf($template, $mch_id, $nonce_str, $encrypted_value);
	$response = postXmlCurl($xml,SANDBOX_URL);
	$responseArr = xmlToArray($response);
	$sandbox_signkey = '';
	if($responseArr["return_code"] == "FAIL"){
		throw new Exception("请求出了点小错误:".$responseArr["return_msg"]);
	}else{
		$sandbox_signkey = $responseArr["sandbox_signkey"];
	}
	print_r("亲,这就是你的沙箱密钥了哦:  
".$sandbox_signkey."

");
	return $sandbox_signkey;
}

function build_xml($param, $wxpay_key){
	if(!is_array($param) || count($param) <= 0){
		throw new Exception("数组数据异常!");
	}

	$xml = "";
	foreach ($param as $key=>$val){
		if (is_numeric($val)){
			$xml.="<".$key.">".$val."";
		}else{
			$xml.="<".$key.">";
		}
	}
	$encrypted_str = formatParaMap($param,false);
	$encrypted_str.= "&key=".$wxpay_key;
	$encrypted_value = strtoupper(md5($encrypted_str));
	$xml.="";
	$xml.="";
	return $xml;
}

function postXmlCurl($xml, $url, $second=30, $useCert=false, $sslcert_path='', $sslkey_path=''){
	$ch = curl_init();
	//设置超时
	curl_setopt($ch, CURLOPT_TIMEOUT, $second);
	curl_setopt($ch,CURLOPT_URL, $url);
	//设置header
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	//要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
	curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
	curl_setopt($ch,CURLOPT_HTTPHEADER,[
		"Content-Type: text/xml",
		"cache-control: no-cache"
	]);

	//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);
		return false;
	}
}

function getRandChar($length){
	$str = null;
	$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
	$max = strlen($strPol)-1;
	for($i=0;$i<$length;$i++){
		$str.=$strPol[rand(0,$max)];
	}
	return $str;
}

function xmlToArray($xml){
	//将XML转为array
	$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
	return $array_data;
}

function request_handler($url, $xml, $desc){
	$result = postXmlCurl($xml,$url);
	$result = htmlentities($result ,ENT_QUOTES,"UTF-8");
	print_r($desc."我才不是请求结果呢:".$result."");
}

function formatParaMap($paraMap, $urlencode)
{
	$buff = "";
	ksort($paraMap);
	foreach ($paraMap as $k => $v)
	{
		if($urlencode)
		{
			$v = urlencode($v);
		}
		$buff .= $k . "=" . $v . "&";
	}
	$reqPar = "";
	if (strlen($buff) > 0)
	{
		$reqPar = substr($buff, 0, strlen($buff)-1);
	}
	return $reqPar;
}

最后一次编辑于  2021-05-10  
点赞 7
收藏
评论
登录 后发表内容