微信小程序支付,回调,以及如何获取openId都在这里
全是干活 直接上代码
<?php
/**
* 小程序支付
* @author Administrator
*
*/
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
require_once(APP_PATH.\'api/SmsApi.php\');
class Smallpay extends Controller {
public $appid = \'wx0ab7e7b9319e07de\';
public $secret = \'7cb12701b03c25d215851bdf7b69ad02\';
public function _initialize(){
parent::_initialize();
}
/**
* 获取用户openID
* @return string
*/
public function phpGet(){
$code = input(\'code\');
$url = \'https://api.weixin.qq.com/sns/jscode2session?appid=\'.$this->appid.\'&secret=\'.$this->secret.\'&js_code=\'.$code.\'&grant_type=authorization_code\';
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
$post_data = array();
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//执行命令
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
$data = substr($data,strripos($data,"{"));
//显示获得的数据
return $data; //返回用户openID
}
/*
* 小程序根据openID登录和注册
*/
public function userLoginAndRegister(){
$openid = input(\'openid\'); //接收用户openid
$user = Db::name(\'users\')->where(\'openid\',$openid)->find();
if($user){ //如果存在该openID用户 显示登陆成功
$re[\'info\'] = \'登陆成功\';
$re[\'status\'] = 9;
return json($re);
}else{ //如果没有该openID信息 则为注册用户
//新增用户信息操作
//----------------
}
}
// //小程序微信支付 生成预支付交易单
public function wxPayGetOrder(){
$order_sn=input(\'order_sn\');
$token = input(\'token\');
$total=input(\'total\');
$openid=DB::name(\'Users\')->where(\'token\',$token)->value(\'openid\');
$url = \'https://api.mch.weixin.qq.com/pay/unifiedorder\';
$xml = $this->getXML($openid,$order_sn,$total);
$curl = curl_init();
//设置抓取的url
$headerArray =array("Content-type:application/json;charset=\'utf-8\'","Accept:application/json");
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml[\'xml\']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post数据
// $post_data = array(
// "body:$xml"
// );
//执行命令
$data = curl_exec($curl);
//关闭URL请求
$data = json_decode(json_encode(simplexml_load_string($data, \'SimpleXMLElement\', LIBXML_NOCDATA)), true);
curl_close($curl);
//显示获得的数据
$data[\'str\'] = md5(date(\'ymd\').rand(\'1000\',\'9999\')); //随机字符串
$data[\'out_trade_no\'] = $xml[\'out_trade_no\'];
$dangqian=time();
$data[\'time\']=strval($dangqian);
return json($data);
}
//获取统一下单的xml字符串
public function getXml($openid,$order_sn,$total){
$request = Request::instance();
$ip = $request->ip();
$out_trade_no = $order_sn;
$notify_url = \'https://xymxjk.com/home/Smallpay/wxPayNotify\';
$nonce_str = md5(rand(\'1000000\',\'9999999\'));
$mch_id = \'****\';
$body = \'test\';
$app_id = \'****\';
$total_fee = 1;
$trade_type = \'JSAPI\';
$bodyData = \'<xml>\';
$bodyData .= \'<appid>\'.$app_id.\'</appid>\'; // 小程序ID
$bodyData .= \'<body>\'.$body.\'</body>\'; // 商品描述
$bodyData .= \'<mch_id>\'.$mch_id.\'</mch_id>\'; // 商户号
$bodyData .= \'<nonce_str>\'.$nonce_str.\'</nonce_str>\'; // 随机字符串
$bodyData .= \'<notify_url>\'.$notify_url.\'</notify_url>\'; // 支付成功的回调地址
$bodyData .= \'<openid>\' .$openid. \'</openid>\'; // 用户标识
$bodyData .= \'<out_trade_no>\'.$out_trade_no.\'</out_trade_no>\'; // 商户订单号
$bodyData .= \'<spbill_create_ip>\'.$ip.\'</spbill_create_ip>\'; // 终端IP
$bodyData .= \'<total_fee>\'.$total_fee.\'</total_fee>\'; // 总金额 单位为分
$bodyData .= \'<trade_type>\'.$trade_type.\'</trade_type>\'; // 交易类型 小程序取值如下:JSAPI
$sign = $this->getSign($app_id,$body,$mch_id,$nonce_str,$notify_url,$openid,$out_trade_no,$ip,$total_fee,$trade_type);
$bodyData .= \'<sign>\'.$sign.\'</sign>\'; // 签名
$bodyData .= \'</xml>\';
$re[\'xml\'] = $bodyData;
$re[\'out_trade_no\'] = $out_trade_no;
return $re;
}
public function getSign($app_id,$body,$mch_id,$nonce_str,$notify_url,$openid,$out_trade_no,$ip,$total_fee,$trade_type){
$strA = \'appid=\'.$app_id.\'&body=\'.$body.\'&mch_id=\'.$mch_id.\'&nonce_str=\'.$nonce_str.\'¬ify_url=\'.$notify_url.\'&openid=\'.$openid.\'&out_trade_no=\'.$out_trade_no.\'&spbill_create_ip=\'.$ip.\'&total_fee=\'.$total_fee.\'&trade_type=\'.$trade_type;
$sign = $strA.\'&key=xiangyumeixuexiaochengxu20190628\'; //拼接商户平台的api密钥
$sign = strtoupper(md5($sign)); //字符串转换成大写
return $sign;
}
//返回前台需要的签名字符串
public function getMd5Str(){
$appId = \'****\';
$time = time();
$nonceStr = md5(date(\'ymd\').rand(\'1000\',\'9999\'));
$package = input(\'pack_age\');
$paySign = MD5(\'appId=\'.$appId.\'&nonceStr=\'.$nonceStr.\'&package=\'.$package.\'&signType=MD5&timeStamp=\'.$time.\'&key=你的apiKey\');
$sign[\'sign\'] = strtoupper($paySign);
$sign[\'nonceStr\'] = $nonceStr;
$sign[\'time\'] = $time;
return json($sign);
}
//支付成功回调地址
public function wxPayNotify(){
$testxml = file_get_contents("php://input");
$jsonxml = json_encode(simplexml_load_string($testxml, \'SimpleXMLElement\', LIBXML_NOCDATA));
$result = json_decode($jsonxml, true);//转成数组,
//缺少验签操作 -- 自行加上
/*****自己的逻辑代码*****/
/**********************/
$str = \'<xml>\';
$str .= \'<return_code>\'.\'SUCCESS\'.\'</return_code>\';
$str .= \'<return_msg>\'.\'OK\'.\'</return_msg>\';
$str .= \'</xml>\';
$data[\'return_code\'] = \'SUCCESS\';
$data[\'return_msg\'] = $str;
return json($data);
}
}
相关文章: