微信小程序 wx.requestPayment({}) 唤起微信支付

2023-01-16 09:09:16 浏览数 (1)

1. 登录商户平台,将商户和小程序进行关联

产品中心 中的 APPID账号管理 中,添加关联的小程序 appid

2. 小程序调起支付API

小程序调起支付API:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5

小程序API开发文档:https://developers.weixin.qq.com/miniprogram/dev/api/api-pay.html#wxrequestpaymentobject

3. 封装类

代码语言:javascript复制
<?php
class Wechat
{
// 公众号的或者小程序支付参数
private $appId = "wx5117bexxxxxx";
private $appSecret = "136ae3xxxxxxx";
// 微信商户号
private $mch_id = "15xxxx";
private $mch_key = "xxxasdfghjkxxxxxx";
// 回调地址
public $notify_url = '';
// 退款回掉地址
public $refund_notify_url = '';
private $request;
private $nonce_str;
public function __construct()
{
$this->request = request();
$this->notify_url = 'notice.php';
$this->refund_notify_url = 'refund.php';
$this->nonce_str = md5(date('YmdHis') . time() . rand(1000, 9999));
}
//小程序登录
/**
* @param $code 获取微信支付的登录code
* @return mixed
*/
public function wxLogin($code)
{
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $this->appId . "&secret=" . $this->appSecret . "&js_code=" . $code . "&grant_type=authorization_code";
return json_decode($this->execute($url), true);
}
/**
* @param $out_trade_no 微信支付唯一订单
* @param $openid 微信支付用户的openid
* @param $price 订单支付的价格,(单位,元)
* @param string $desc 订单描述
* @return array|mixed 组装支付参数
*/
public function getPayParameter($out_trade_no, $openid, $price, $desc = '')
{
header("Content-type:text/html;charset=utf-8"); //此处进行字符集初始化,
$data = [
'appid' => $this->appId,
'body' => $desc,
'mch_id' => $this->mch_id,
'nonce_str' => $this->nonce_str,// 随机字符串
'notify_url' => $this->notify_url,//异步回调地址
'openid' => $openid,//用户登录时获取的code中含有的值
'out_trade_no' => $out_trade_no,//商家订单号
'spbill_create_ip' => $this->get_real_ip(),//APP和网页支付提交用户端ip
'total_fee' => $price * 100,//订单总额,单位:分
'attach' => 'order',//确定是哪个商家进行的支付
'trade_type' => 'JSAPI'//交易类型
];
//将数组转化为Xml
$data['sign'] = $this->makeSign($data);
$abc_xml = $this->arrayToXml($data);
//统一下单接口prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$xml = $this->execute($url, $abc_xml, 1);
//将XMl转化为数组
$info = $this->xml2array($xml);
if (!isset($info['prepay_id'])) {
return $info;
}
$params = array(
'appId' => $this->appId,
'nonceStr' => $data['nonce_str'],
'package' => 'prepay_id=' . $info['prepay_id'],
'signType' => 'MD5',
'timeStamp' => '' . time(),
);
$_info['paySign'] = $this->makeSign($params);
$_info['timeStamp'] = "" . $params['timeStamp'];
$_info['nonceStr'] = $params['nonceStr'];
$_info['package'] = $params['package'];
$_info['signType'] = $params['signType'];
$_info['notify_url'] = $this->notify_url;
// 请求成功后进行返回数据信息
if ($info['return_code'] == 'SUCCESS' || $info['result_code'] == 'SUCCESS') {
return $_info;
} else {
return [];
}
}
/**
* @param $openid 支付的用户的openid
* @return mixed
*/
public function userInfo($openid)
{
$token = $this->getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $token . "&openid=$openid&lang=zh_CN";
return json_decode($this->execute($url), true);
}
//获取微信的token
public function getWxAccessToken()
{
$key = $this->appId . 'miniProgram_access_token';
$accessToken = <收藏 | 0点赞 | 0打赏

0 人点赞