企业付款到零钱,已经限额500以内,完全不够用
商家转账到零钱,限额20000单笔,需要申请要按不通过原因修改多试几次
准备前提:
1,平台证书,商家证书,一共2个;
2,设置APIv3密钥;
正文:
商家证书可直接申请下载:
https://pay.weixin.qq.com/index.php/core/cert/api_cert
平台证书必须用API获取下载:
https://github.com/wechatpay-apiv3/wechatpay-php
项目根目录运行
composer require wechatpay/wechatpay
//composer 后可在 看到 \项目\vendor\wechatpay\wechatpay//README.md//php -f ./bin/CertificateDownloader.php -- -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}//运行php -f ./bin/CertificateDownloader.php -- -k 111111111111111111111111 -m 333333333 -f D:/work/web/项目/config/apiclient_key.pem -s 222222222222222222222222222222 -o D:/work/web/项目/view/wp/wechatpay-php-main/cert//运行正确返回* Trying 220.194.111.102:443...* Connected to api.mch.weixin.qq.com (220.194.111.102) port 443 (#0)* ALPN, offering http/1.1* successfully set certificate verify locations:* CAfile: D:\php\extras\ssl\cacert.pem CApath: none* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384* ALPN, server accepted to use http/1.1* Server certificate:* subject: C=CN; ST=Guangdong Province; L=Shenzhen; O=Tenpay Technology Company Limited; CN=payapp.weixin.qq.com* start date: Sep 21 00:00:00 2022 GMT* expire date: Oct 21 23:59:59 2023 GMT* subjectAltName: host "api.mch.weixin.qq.com" matched cert's "*.mch.weixin.qq.com"* issuer: C=US; O=DigiCert Inc; CN=DigiCert Secure Site CN CA G3* SSL certificate verify ok.> GET /v3/certificates HTTP/1.1Host: api.mch.weixin.qq.comAccept: application/json, text/plain, application/x-gzip, application/pdf, image/png, image/*;q=0.5Content-Type: application/json; charset=utf-8User-Agent: wechatpay-php/1.4.8 GuzzleHttp/7 curl/7.70.0 (WINNT/10.0) PHP/7.4.30Authorization: WECHATPAY2-SHA256-RSA2048 mchid=...........-----BEGIN CERTIFICATE-----AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...........AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-----END CERTIFICATE-----
复制证书内容,或者在输出目录outputFilePath可找到一个.pem文件
page(0,15)->order(['configid'=>'asc'])->select()->toArray();$cfg = ['appid' => $res[0]['value'],'app_id' => $res[0]['value'],'appsecret' => $res[1]['value'],'secret' => $res[1]['value'],'pc_appid' => $res[2]['value'],'pc_appsecret' => $res[3]['value'],'access_token' => $res[4]['value'],'pc_access_token' => $res[5]['value'],'mch_id' => $res[6]['value'],'key' => $res[7]['value'],'notify_url' => $res[8]['value'],'cert_path' => $res[9]['value'],//商家证书'key_path' => $res[10]['value'],];$this->cfg = $cfg;}private static function shopTransfer($obj,$config){$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';$http_method = 'POST';$orderno=$obj['orderno'];$data = ['appid' => $config['app_id'],'out_batch_no' => $orderno,'batch_name' => '货款到账','batch_remark' => '货款已到账至微信','total_amount' => round($obj['amount']*100),'total_num' => 1,'transfer_detail_list' => [['out_detail_no' => $orderno.'1','transfer_amount' => round($obj['amount']*100),'transfer_remark' => '货款到账','user_name' => self::getEncrypt($obj['real_name'],$config),'openid' => $obj['openid'],]]];$token = self::token($url,$http_method,$data,$config);$result = self::https_request($url,json_encode($data),$token);$result_arr = json_decode($result,true);var_dump($result);if(!isset($result_arr['create_time'])) {Db::execute("insert into err_log (title,text,createtime) values ('$orderno','$result',now())");throw new \Exception($result_arr['message']);}return true;}private static function token($url,$http_method,$data,$config){$timestamp = time();$url_parts = parse_url($url);$nonce = $timestamp.rand('10000','99999');$body = empty($data) ? '' : json_encode((object)$data);$stream_opts = ["ssl" => ["verify_peer"=>false,"verify_peer_name"=>false,]];$apiclient_cert_arr = openssl_x509_parse(file_get_contents($config['cert_path'],false, stream_context_create($stream_opts)));//商家证书路径$serial_no = $apiclient_cert_arr['serialNumberHex'];$mch_private_key = file_get_contents($config['key_path'],false, stream_context_create($stream_opts));//商家证书路径$merchant_id = $config['mch_id'];$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));$message = $http_method."\n".$canonical_url."\n".$timestamp."\n".$nonce."\n".$body."\n";openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');$sign = base64_encode($raw_sign);$schema = 'WECHATPAY2-SHA256-RSA2048';$token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',$merchant_id, $nonce, $timestamp, $serial_no, $sign);return $schema.' '.$token;}private static function https_request($url,$data,$token){$curl = curl_init();curl_setopt($curl, CURLOPT_URL, (string)$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);$headers = ['Authorization:'.$token,'Wechatpay-Serial:EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE',//平台证书序列号,文件名称里'Accept: application/json','Content-Type: application/json; charset=utf-8','User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',];var_dump($headers);echo '
';if(!empty($headers)){curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);}$output = curl_exec($curl);var_dump($output);curl_close($curl);return $output;}private static function getEncrypt($str,$config){$public_key = file_get_contents('/home/api/config/wechatpay_EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE.pem');//平台证书路径$encrypted = '';if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) {$sign = base64_encode($encrypted);} else {throw new \Exception('encrypt failed');}return $sign;}public function withdrawals(){$config = $this->cfg;$obj = Db::query("select a.amount,a.orderno,b.real_name,ifnull(openid,pcopenid)as openid from withdrawals a,shop b,users c where a.shopid=b.shopid and b.usersid=c.usersid and withdrawalsid=45")[0];if($obj['amount']<0.3){$obj['amount']=0.3;}self::shopTransfer($obj,$config);}}
注意商家证书和平台证书别搞混就没问题了
运行结果
{"code":"INVALID_REQUEST","message":"此IP地址不允许调用该接口\t"}"
https://pay.weixin.qq.com/index.php/xphp/csptrans_pocket_v2/product_manage
里设置白名单
{"batch_id":"111111111111111111111111111111111","create_time":"2023-04-06T18:28:18+08:00","out_batch_no":"W222222222222222222"}"
参考内容
GitHub - wechatpay-apiv3/wechatpay-php: 微信支付 APIv3 的官方 PHP Library,同时也支持 APIv2
其它
里维护转账用户,姓名和openid
来源地址:https://blog.csdn.net/qq_34368953/article/details/129996103