$params = [
'business_code' => 'SUB_' . date('YmdHis') . rand(1000, 9999),
'subject_info' => [
"subject_type" => "INDIVIDUAL", // 正确字段名:type(必须是type)
'individual_license' => [
'license_copy' => base64_encode(file_get_contents(Env::get('root_path') . 'public/uploads/20250714/0a44aa7ca8139ad7f05c72e59426f526.jpg')), // 必传,营业执照照片
'license_number' => '92440101MA5XXXXXXX', // 必传,注册号
'merchant_name' => 'XX小吃店', // 必传,与执照一致
'operator' => '张三', // 必传,经营者姓名
'address' => '广东省深圳市XX区XX路XX号', // 必传,注册地址ƒƒ√
'valid_date' => '2020-01-01至长期' // 必传,有效期
],
'operator' => [
'id_card_copy' => base64_encode(file_get_contents(Env::get('root_path') . 'public/uploads/20250714/0a44aa7ca8139ad7f05c72e59426f526.jpg')), // 必传,身份证正面
'id_card_national' => base64_encode(file_get_contents(Env::get('root_path') . 'public/uploads/20250714/0a44aa7ca8139ad7f05c72e59426f526.jpg')), // 必传,身份证反面
'id_card_number' => '440301XXXXXXXXXXXX', // 必传,身份证号
'name' => '张三', // 必传,与身份证一致
'id_card_valid_time' => '2020-01-01至长期' // 必传,有效期
]
],
'contact_info' => [
'contact_name' => '张三',
'mobile_phone' => '13800138000', // 11位有效手机号
'contact_email' => 'zhangsan@example.com'
],
'business_info' => [
'merchant_shortname' => 'XX小吃店',
'service_phone' => '13800138000'
],
'settlement_info' => [
'bank_account_type' => 'ACCOUNT_TYPE_PRIVATE',
'account_name' => '张三',
'account_number' => '622848XXXXXXXXXXXX',
'bank_name' => '中国农业银行',
'bank_branch_id' => '103584000018'
]
];
$downloader = new WechatPayService();
$downloader->apply($params);
这是调用sdk
<?php
namespace app\service;
use think\Exception;
use think\facade\Config;
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
class WechatPayService
{
//创建一个实例
private $instance;
//商户私钥
private $merchantPrivateKeyInstance;
//商户证书序列号
private $merchantCertificateSerial;
//平台证书序列号
private $platformPublicKeyInstance;
//平台证书序列号
private $platformCertificateSerial;
//平台公钥ID
private $platformPublicKeyId;
//微信支付公钥
private $twoPlatformPublicKeyInstance;
//商户号
private $merchantId;
//加密器
private $encryptor;
/**
* 构造函数
* @param array $config 配置参数
* @throws Exception
*/
public function __construct()
{
//获取配置
$config = Config::get('main.WXPAYV3', []);
// 商户号
$this->merchantId = $config['mch_id'];
// 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名
$this->merchantPrivateKeyInstance = Rsa::from(file_get_contents($config['private_key_path']), Rsa::KEY_TYPE_PRIVATE);
// 「商户API证书」的「证书序列号」
$this->merchantCertificateSerial = $config['cert_serial'];
// 补充平台证书(建议配置)
$this->platformPublicKeyInstance = Rsa::from(file_get_contents($config['cert_path']), Rsa::KEY_TYPE_PUBLIC);
//平台证书序列号
$this->platformCertificateSerial = $config['cert_serial_no'];
//平台公钥ID
$this->platformPublicKeyId = $config['platform_cert_id'];
// 从本地文件中加载「微信支付公钥」,用来验证微信支付应答的签名
$this->twoPlatformPublicKeyInstance = Rsa::from(file_get_contents($config['wechatpay_pub_key']), Rsa::KEY_TYPE_PUBLIC);
// 构造一个 APIv3 客户端实例
$this->instance = Builder::factory([
'mchid' => $this->merchantId,
'serial' => $this->merchantCertificateSerial,
'privateKey' => $this->merchantPrivateKeyInstance,
'verify' => false,
'certs' => [
$this->platformCertificateSerial => $this->platformPublicKeyInstance,
$this->platformPublicKeyId => $this->twoPlatformPublicKeyInstance,
],
]);
$platformPublicKeyInstance = $this->platformPublicKeyInstance;
$this->encryptor = static function (string $msg) use ($platformPublicKeyInstance): string {
return Rsa::encrypt($msg, $platformPublicKeyInstance);
};
}
//特约商户进件
public function apply($params = [])
{
$resp = $this->instance
->chain('v3/applyment4sub/applyment/')
->post([
'json' => $params,
'headers' => [
// $platformCertificateSerialOrPublicKeyId 见初始化章节
'Wechatpay-Serial' => $this->platformPublicKeyId,
],
]);
dump($resp);
}
}
报错
liyongai@DJ-Ocean-J-MacBook-Pro api % php think task
[GuzzleHttp\Exception\ClientException]
Client error: `POST https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/` resulted in a `400 Bad Request` response:
{"code":"PARAM_ERROR","message":"请确认待处理的消息是否为加密后的密文"}
liyongai@DJ-Ocean-J-MacBook-Pro api % php think task
[GuzzleHttp\Exception\ClientException]
Client error: `POST https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/` resulted in a `400 Bad Request` response:
{"code":"PARAM_ERROR","detail":{"location":"body","value":"INDIVIDUAL"},"message":"无法将 JSON 输入源“/body/subj (truncated...)
liyongai@DJ-Ocean-J-MacBook-Pro api %

"code":"PARAM_ERROR","message":"请确认待处理的消息是否为加密后的密文"
----
请检查涉及敏感信息的字段是否有加密。用微信支付公钥加密请参考https://pay.weixin.qq.com/doc/v3/partner/4013059044
array(6) {
["business_code"] => string(22) "SUB_202508072158451514"
["contact_info"] => array(4) {
["contact_type "] => string(5) "LEGAL"
["contact_name"] => string(344) "MZDVLsVEL+gJVVF4jz8lfb+eORRwZfoh3xtcWP2HO34TXerVMHsm++gNAkq7hfxzcYpJ0nxU38Z9XUX5tREdwkZn5B2S3uXjisqXNKhtIguRE1iNQ0urVApIdtQ7rJCTFOQ6ZwJaetBuzNkonugDisK36gKD3as6w7zUWCVAHgQGWSC/lbkKT5DeGV/CgHbQe890qxUsOYcITlyDCeHh7FoJ7sWWCeEOd1PpfSeszopugrRM07A5d5VxlXdnuUKFvAAYQIRCPvsLE1alIb5X1w6qFsLw5q8NVtFL1oQiKOGG4upkMmf14FCMYKyVDCjVZQi22CQbXD+0ZYsEcBV9EQ=="
["mobile_phone"] => string(344) "ZOiRUr+6o0iQqTDsvcOQoOHEY5TeNuUGkeAZ/agl+qjVgGloYlD0FqwORynXVzGTjZN19Aitj8NTQeFAWbZEz6bisDZNp6JHFhzmWBH0T5GdKvWnXH9dOunbZVY3XQ+hmo1s7PHue0Q9KytW7L+Loip1Yq6itCVfsLHIet3T5lTrlTnDKqzFHQAuuGeAPuEgMIQdqVWxpJrgv+YitQUq4307+MVO7NWpuUIeySYttO5jNCwbF8BFUB58lM3K5NoXAEy2eB5PjLH4Ne9juZ972uNECOD9WHzmQnCZ55jDIZjMw099RrQUC02BIxHTzdqQLzcATjS2Hylu3fGL8k/EvQ=="
["contact_email"] => string(344) "vT0B3xQsLjaXH7eEZIhj8Tf6DFbk8R752Ev53QiCCN15quSM0utRswdS+DsjfCtmdK+tB8qW/VnJRR4Qn/cvJkbwWjZXni1ZYdT4p74/PcH9L8AwJ72drXtZP9IpXPxWwKcVhFbbLWnDjdextOgdGaS0dL+DXLylPp1zCzdu+5ToyFz7T9EuUAT0p+0/+FGB+HfqUdmqG5R8zehWZHUSVM6Q7zEygl6VLmv7/j/NL3oIo0IJKg4XmDYP2QVhRrFwEPMXbnnig1FGplO+3buuYvVxKysl00RyvBY0eT+dzFF9hHIe0aAyWkbcOY9fkLhIxpNXhVHhMKws28SRtv66/g=="
}
["subject_info"] => array(3) {
["subject_type"] => string(23) "SUBJECT_TYPE_ENTERPRISE"
["business_license_info"] => array(4) {
["license_copy"] => string(111) "V1_uhmFn9Yk6ZLfSz-ItHyS3R4DCI17DmHZy4Kx710b68S8_nRazx7-BiWS4FE0r-gDDB44orEJILAisW8hGc4bR9nhCQxYfQ9hsKcnyfisSnBL"
["license_number"] => string(18) "2313333"
["merchant_name"] => string(42) "23478632423"
["legal_person"] => string(9) "32i4u34"
}
["identity_info"] => array(2) {
["id_card_info"] => array(6) {
["id_card_copy"] => string(111) "V1_8cH8fKPaKLZYuUHXXqDFkR4DCI17DmHZCG3svLIgLaytmXXHDWrQlSW9m1A0r-gDDPM_LK_EfFZKYyXCf5xZLSXhCQxYfQ9hsKcnyfisSnBL"
["id_card_national"] => string(111) "V1_Hz-IYcFELdCvcDG_zHj1AB4DCI17DmHZZ-HLsYyWCIQwG9tFdJz2LCWtk1A0r-gDDANg1Yr5FlteemR-20o9HvLhCQxYfQ9hsKcnyfisSnBL"
["id_card_number"] => string(344) "wbfLm52+FbWvYzBjJVUe5yBwISc04wNHozT65SQReZidEWrT0QChSdplM137B24vp/O9txdbeIf1/uD7R2eINs7Sjy3B5pzt1AqgKL5+hJLXj55fVnEbziqjWjjcrfHKfgToFfbTD8iN+6rQDZHhJ/utMRqv1F2jodL4N3VzZZ4KnBzl4i5JMnNsgBwb8C/qoLs/CkyJKn/N0T5uAKxh7odD57rid1pT48WLSzQkms76bU8aT8L/qmvxqHkHg/Cl4n1oILPZ6GlC6NFwHmSJQdBYwUh+vgTG+3skhYIbpF44GdGFG1/xofW2Q4HufBZ04hDw/jEEZS+L0/6BzDkvXQ=="
["id_card_name"] => string(344) "rdyAyvf58a2egkkktW9ExiejvYTAhfxN9SlREJZP5ry+rcYZdLlc1kgtJR54rsX4Jsw4XDDJVgntiGJBxFMnOKigCuy3VY970AK/kmQhaIvsZ7iVTAMe6ZvpT3b/6A+mfsjpAYE7wbaLpFtzvx3UDpaoesv63X8YWI2H6s+b1Y8d2Z1DSJ/sd/wUTyw2+GulOGvMC2XlwCWcbthERgm0YynAeD3i/i/SYjz3hgMfjzA8ONju2nAcGros6qpaTaqmCnfxYgJvu8wK8VC4jLvVOdsdh2qlIqGYN8u8ojHEjlSTKAfIjt2Fu/CU7mjVikqttlC6CLbPA/sfkpHXB9A5Qg=="
["card_period_begin"] => string(10) "2015-07-24"
["card_period_end"] => string(10) "2025-07-24"
}
["owner"] => bool(true)
}
}
["business_info"] => array(3) {
["merchant_shortname"] => string(42) "成36245"
["service_phone"] => string(11) "18380503975"
["sales_info"] => array(1) {
["sales_scenes_type"] => array(1) {
[0] => string(15) "SALES_SCENES_MP"
}
}
}
["settlement_info"] => array(2) {
["settlement_id"] => int(721)
["qualification_type"] => int(18)
}
["bank_account_info"] => array(4) {
["bank_account_type"] => string(27) "BANK_ACCOUNT_TYPE_CORPORATE"
["account_name"] => string(6) "张三"
["account_bank"] => string(18) "中国工商银行"
["account_number"] => string(16) "6222020200020345"
}
}这是传输的数据
报错 [GuzzleHttp\Exception\ClientException]
Client error: `POST https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/` resulted in a `400 Bad Request` response:
{"code":"PARAM_ERROR","detail":{"location":"body","value":721},"message":"无法将 JSON 输入源“/body/settlement_in (truncated...)
sdk 调用 $resp = $this->instance
->chain('v3/applyment4sub/applyment/')
->post([
'json' => $params,
'headers' => [
// $platformCertificateSerialOrPublicKeyId 见初始化章节
'Wechatpay-Serial' => $this->platformPublicKeyId,
],
]);
dump($resp);
第一个问题是接口有要求一些敏感信息字段需要加密后再传,加密方式可以看 sdk 的 readme
第二个问题是你传的参数有问题,比对文档去核对参数
Client error: `POST https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/` resulted in a `400 Bad Request` response:
{"code":"PARAM_ERROR","detail":{"location":"body","value":721},"message":"无法将 JSON 输入源“/body/settlement_in (truncated...)