提示错误:
[400] ClientException in RequestException.php line 113
Client error: `POST https:
一下是请求参数:
$resp = $client->request(
'POST',
'https://api.mch.weixin.qq.com/v3/pay/transactions/native',
[
'json' => [
"time_expire" => "2018-06-08T10:34:56+08:00",
"amount" => [
"total" => 100,
"currency" => "CNY",
],
"mchid" => "1230000109",
"description" => "Image形象店-深圳腾大-QQ公仔",
"notify_url" => "https://www.weixin.qq.com/wxpay/pay.php",
"out_trade_no" => "1217752501201407033233368018",
"goods_tag" => "WXG",
"appid" => "wxd678efh567hg6787",
"attach" => "自定义数据说明",
"detail" => [
"invoice_id" => "wx123",
"goods_detail" => [
[
"goods_name" => "iPhoneX 256G",
"wechatpay_goods_id" => "1001",
"quantity" => 1,
"merchant_goods_id" => "商品编码",
"unit_price" => 828800,
],
[
"goods_name" => "iPhoneX 256G",
"wechatpay_goods_id" => "1001",
"quantity" => 1,
"merchant_goods_id" => "商品编码",
"unit_price" => 828800,
],
],
"cost_price" => 608800,
],
"scene_info" => [
"store_info" => [
"address" => "广东省深圳市南山区科技中一道10000号",
"area_code" => "440305",
"name" => "腾讯大厦分店",
"id" => "0001",
],
"device_id" => "013467007045764",
"payer_client_ip" => "14.23.150.211",
]
],
'headers' => [ 'Accept' => 'application/json' ]
]
你的接口,是否真的正确?正常情况下,你把接口直接写在网页中,是有返回值的。我用的接口是:https://api.mch.weixin.qq.com/pay/unifiedorder
这个是我调用支付时用的接口,直接在网页中调用显示如下 :
使用你的接口(https://api.mch.weixin.qq.com/v3/pay/transactions/native)
显示如下:
其中,http_request函数代码如下:
//curl请求
public function http_request($url,$data = null,$headers=array())
{
$curl = curl_init();
if( count($headers) >= 1 ){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
wechatpay/wechatpay-guzzle-middleware 处于维护状态,言外之意就是,即将被放弃,建议用 wechatpay/wechatpay 新包。
另外
PemUtil::loadPrivateKey('./path/to/mch/private/key.pem');
这是要加载私钥文件,新包内用
Rsa::from(‘file:///…');
新包另配有IDE提示包 iwechatpay/openapi , 接口的数据描述例如:https://github.com/TheNorthMemory/wechatpay-openapi/blob/a21da30bc08dcb220151c3fc221cbc21f7ae6a29/src/V3/Pay/Transactions/Native.php#L18-L54 几乎就是cv操作了。
/** * Native下单(同步模式) * @param array $options * @link https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/transactions/chapter3_3.shtml */ public function post(array $options = [ 'json' => [ 'appid' => 'wxd678efh567hg6787', 'mchid' => '1230000109', 'description' => 'Image形象店-深圳腾大-QQ公仔', 'attach' => '自定义数据', 'out_trade_no' => '1217752501201407033233368018', 'amount' => [ 'total' => 100, 'currency' => 'CNY', ], 'time_expire' => '2018-06-08T10:34:56+08:00', 'notify_url' => 'https://www.weixin.qq.com/wxpay/pay.php', 'goods_tag' => 'WXG', 'detail' => [ 'cost_price' => 608800, 'invoice_id' => '微信123', 'goods_detail' => [[ 'merchant_goods_id' => '商品编码', 'wechatpay_goods_id' => '1001', 'goods_name' => 'iPhoneX 256G', 'quantity' => 1, 'unit_price' => 828800, ],], ], 'scene_info' => [ 'payer_client_ip' => '14.23.150.211', 'device_id' => '013467007045764', 'store_info' => [ 'id' => '0001', 'name' => '腾讯大厦分店', 'area_code' => '440305', 'address' => '广东省深圳市南山区科技中一道10000号', ], ], ], ]): ResponseInterface;
我的是Java,我用Java 的时候直接传JSON对象也会报参数异常,需要传json字符串;试试把传递参数转为字符串,不要传类似map的对象。
400是协议或者参数非法,自己根据文档查参数,可以用这个脚本辅助排查https://github.com/wechatpay-apiv3/wechatpay-postman-script
use GuzzleHttp\Exception\RequestException;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use WechatPay\GuzzleMiddleware\Util\PemUtil;
use GuzzleHttp\HandlerStack;
// 商户相关配置,
$merchantId = '1000100'; // 商户号
$merchantSerialNumber = 'XXXXXXXXXX'; // 商户API证书序列号
$merchantPrivateKey = PemUtil::loadPrivateKey('./path/to/mch/private/key.pem'); // 商户私钥文件路径
// 微信支付平台配置
$wechatpayCertificate = PemUtil::loadCertificate('./path/to/wechatpay/cert.pem'); // 微信支付平台证书文件路径
// 构造一个WechatPayMiddleware
$wechatpayMiddleware = WechatPayMiddleware::builder()
->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置
->withWechatPay([ $wechatpayCertificate ]) // 可传入多个微信支付平台证书,参数类型为array
->build();
// 将WechatPayMiddleware添加到Guzzle的HandlerStack中
$stack = GuzzleHttp\HandlerStack::create();
$stack->push($wechatpayMiddleware, 'wechatpay');
// 创建Guzzle HTTP Client时,将HandlerStack传入,接下来,正常使用Guzzle发起API请求,WechatPayMiddleware会自动地处理签名和验签
$client = new GuzzleHttp\Client(['handler' => $stack]);
try {
$resp = $client->request(
'POST',
'https://api.mch.weixin.qq.com/v3/pay/transactions/native', //请求URL
[
// JSON请求体
'json' => [
"time_expire" => "2018-06-08T10:34:56+08:00",
"amount" => [
"total" => 100,