微信支付SDK开启新版沙盒仿真系统测试的方案

阅读3评论0

在对接V3版微信支付的SDK时,测试的时候想用沙盒,查了一圈,微信支付老的沙盒下线了,新版的沙盒倒是有,用起来很难受。

新的PHP SDK虽然支持V2和V3的API,但不支持沙盒哈,想用沙盒了,还得配置一套URI,很费事。

看下了SDK的包,在原基础上加上了沙盒的功能,链式调用很方便。

需要的朋友自行下载使用。

文件:vendor\wechatpay\wechatpay\src\Builder.php

代码:

private $sandbox=false;
public function sandbox($sandbox=true): BuilderChainable
{
	$this->sandbox=$sandbox;
	return $this;
}
protected function pathname(string $seperator = '/'): string
{
	$driver=$this->getDriver();
	$path=implode($seperator, $this->simplized());
	$version='';
	if(0 === strncasecmp($driver::XML_BASED . '/', $path, 3) || 0 === strncasecmp($driver::JSON_BASED . '/', $path, 3)){
		$path=explode('/', $path);
		$version=array_shift($path);
		$path=implode('/', $path);
	}
	$path=($this->sandbox ? 'xdc/apiv2sandbox/' : '').$path;
        return $version.'/'.$path;
}

调式示例:

$instance=WeixinBuilder::factory($config);
$res=$instance->chain('v2/pay/micropay')->sandbox($config['debug'])->post(['xml' => $params]);

sandbox要在chain后使用,参数是bool类型的:

  • true:沙盒支付

  • false:真实支付

0 位网友评论:

欢迎来评论

请填写验证码