携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第29天,点击查看活动详情
微信支付,发现金红包及企业支付到零钱这两个操作请求是需要携带证书的。
证书如何下载,如何配置,我这里就不赘述了。官方文档给的说明还算是清楚。
我这里直接给出我现在正在使用的微信生成支付签名的代码:
class WxPayController extends CommonController
{
/**
* @name: 公众号appid
* @author: camellia
* @date: 2021-02-19
*/
private $appid = 'xxx';
/**
* @name: 微信支付商户秘钥
* @author: camellia
* @date: 2021-02-19
*/
private $mch_id = 'xxx';
/**
* @name: 自定义秘钥key
* @author: camellia
* @date: 2021-02-19
*/
private $wxpay_key = 'xxx';
/**
* @name curl_post公共请求方法
* @author camellia
* @date 20200630
* @param $url 请求连接
* @param $data 请求参数
*/
public function curlPost($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// 请求的时候需要带上证书信息(证书路径需要是绝对路径)
curl_setopt($ch, CURLOPT_SSLCERT, '/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxxx/xxx/xxx/xxx/apiclient_cert.pem');
curl_setopt($ch, CURLOPT_SSLKEY, '/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/apiclient_key.pem');
// curl_setopt($ch, CURLOPT_CAINFO, '/xxx/xxx/xxx/xxx/rootca.pem'); // CA根证书(用来验证的网站证书是否是CA颁布)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/**
* @name: 微信签名加密算法
* @author: camellia
* @date: 2021-02-19
* @param: $data array 微信支付请求数据数组
* @return: $sign string 微信支付签名字符串
*/
public function Cryptage($data)
{
ksort($data);
$stringA = '';
foreach ($data as $k => $v)
{
$stringA .= $k . '=' . $v . '&';
}
$stringA .= 'key=' . $this->wxpay_key;
$sign = strtoupper(md5($stringA));
return $sign;
}
/**
* @name: 给用户发钱(红包)
* @author: camellia
* @date: 2021-02-19
* @param: wx_exchange 微信支付订单表
* @return: data type description
* 官方文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
*/
public function sendMoneyToUser()
{
// 用户openid
$openid = 'xxxx';
// 发放现金额度
$total_amount = 101;
$mch_billno = 'hb'.time(). rand(10000, 99999);
// 商户订单号
$data['mch_billno'] = $mch_billno;
// 微信商户号
$data['mch_id'] = $this->mch_id;
// 公众号appid
$data['wxappid'] = $this->appid;
// 商户名称
$data['send_name'] = 'xxx';
// 用户openid
$data['re_openid'] = $openid;
// 付款金额
$data['total_amount'] = $total_amount;
// 红包总数
$data['total_num'] = 1;
// 红包祝福语
$data['wishing'] = 'xxx';
// client_ip
$data['client_ip'] = 'xxx';
// 活动名称
$data['act_name'] = 'xxx';
// 备注
$data['remark'] = 'xxx';
// 场景id
// $data['scene_id'] = 'PRODUCT_2';
// 随机字符串
$data['nonce_str'] = 'xxxxx';
$sign = $this->Cryptage($data);
$str = "<xml>
<sign>$sign</sign>
<mch_billno>".$data['mch_billno']."</mch_billno>
<mch_id>".$data['mch_id']."</mch_id>
<wxappid>".$data['wxappid']."</wxappid>
<send_name>".$data['send_name']."</send_name>
<re_openid>".$data['re_openid']."</re_openid>
<total_amount>".$data['total_amount']."</total_amount>
<total_num>".$data['total_num']."</total_num>
<wishing>".$data['wishing']."</wishing>
<client_ip>".$data['client_ip']."</client_ip>
<act_name>".$data['act_name']."</act_name>
<remark>".$data['remark']."</remark>
<nonce_str>".$data['nonce_str']."</nonce_str>
</xml>";
$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
$result = $this->curlPost($url, $str);
var_dump($result);die;
}
}
这里最主要的就是curlpost请求的时候需要带上证书。否则请求会失败。
我这里指的是需要证书的微信支付的功能,正常用户扫码支付功能,则不需要携带这些证书等。
有好的建议,请在下方输入你的评论。
欢迎访问个人博客 guanchao.site
欢迎访问我的小程序:打开微信->发现->小程序->搜索“时间里的”