ThinkPHP5.1框架实现微信JsApi支付
- ThinkPHP
- 时间:2020-12-16
- 305人已阅读
简介ThinkPHP5.1框架实现微信JsApi支付
控制器代码
<?php
namespace app\index\controller;
class Index
{
const APPID = 'wx763ee42xxxxxxxx'; //微信商户绑定的公众号appid
const SECRET = 'a161c979394fb807xxxxxxxxxx';//公众号密钥
const MCHID = '16005xxxxx'; //商户号
const KEY = 'qwertyuixxxxxxxxxxxxxx'; //商户支付秘钥
const NOTIFY_URL = 'http://pay.beambitious.cn/index/index/JsApiNotify';//接收微信支付结果通知的回调地址
//微信JsApi支付(公众号支付)
public function JsApiPay(){
if(session('openid')){
$openid = session('openid');
}else{
return redirect('/index/index/getCode');
}
$params['appid'] = self::APPID;
$params['mch_id'] = self::MCHID;
$params['nonce_str'] = md5(time());
$params['openid'] = $openid;
$params['body'] = "测试商品";
$params['out_trade_no'] = 1234567889;
$params['total_fee'] = 1;
$params['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];;
$params['trade_type'] = 'JSAPI';
$params['notify_url'] = self::NOTIFY_URL;
$data = $params;
$data['sign'] = $this->getSign($params);
//数组转 xml
$xml = $this->ArrToXml($data);
//发送数据到统一下单 API 地址
$res = $this->httpRequest('https://api.mch.weixin.qq.com/pay/unifiedorder',$xml);
$arr = $this->XmlToArr($res);
if($arr['return_code'] == "SUCCESS" && $arr['return_msg'] == "OK"){
$rows["appId"]= $arr["appid"]; //公众号名称,由商户传入
$rows["timeStamp"]= time(); //时间戳,自1970 年以来的秒数
$rows["nonceStr"]= $arr["nonce_str"]; //随机串
$rows["package"]= "prepay_id=".$arr["prepay_id"];//预支付id
$rows["signType"]= "MD5"; //微信签名方式:
$rows["paySign"] = $this->getSign($rows);
return view('pay/index',['rows'=>$rows]);
}
}
public function getCode(){
//1.获取code
$APPID = self::APPID;
$REDIRECT_URI = "http://pay.beambitious.cn/index/index/getOpenId";
$SCOPE = "snsapi_base";
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$APPID&redirect_uri=$REDIRECT_URI&response_type=code&scope=$SCOPE";
Header("Location:$url");
}
public function getOpenId(){
//2.通过code获取用户openid和access_token
$APPID = self::APPID;
$SECRET = self::SECRET;
$code = input('code');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$APPID&secret=$SECRET&code=$code&grant_type=authorization_code";
$res = $this->httpGet($url);
session('openid',$res['openid']);//openid存入session
echo "获取openid成功";
}
public function httpGet($url){
$headerArray =array("Content-type:application/json;","Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}
//获取签名
public function getSign($params)
{
//0.移除 sign
if (isset($params['sign'])) unset($params['sign']);
//1.参数名 ASCII 码从小到大排序(字典序)
ksort($params);
//2.组装字符串,键值对形式 & md5 加密 & 转化为大写
$str = http_build_query($params) . '&key=' . self::KEY;
$str = strtoupper(md5(urldecode($str)));
//3.返回签名
return $str;
}
//发起请求
public function httpRequest($api, $postData)
{
//1.初始化
$ch = curl_init();
//2.配置
//2.1 设置请求地址
curl_setopt($ch, CURLOPT_URL, $api);
//2.2 数据流不直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//2.3POST 请求
if ($postData) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
//curl 注意事项,如果发送的请求是 https,必须要禁止服务器端校检 SSL 证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//3.发送请求
$data = curl_exec($ch);
//4.释放资源
curl_close($ch);
return $data;
}
/**
* 将 XML 文档转化为数组
*/
public function XmlToArr($xml)
{
if ($xml == '') return '';
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arr;
}
/**
* 将数组转化为 XML 文档
*/
public function ArrToXml($arr)
{
if(!is_array($arr) || count($arr) == 0) return '';
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
}
2.视图代码
3.实现效果如下图
上一篇:个人基金操作心得分享