thinkphp微信扫码支付模式二

时间:2022-09-24 20:05:46

1.参考本博客的博文《thinkphp3.2实现微信JSAPI支付》,在此基础上实现微信扫码支付只需做简单修改;


2.只需改WxPayAction里面pay函数里面三个地方:

1)将支付类型JSAPI改成NATIVE:

$this->setParameter("trade_type", "NATIVE");   
2)屏蔽掉以下获取openid相关的代码:

$openid = $this->get_openid();
$this->setParameter("openid", $openid);


3)将获取prepayid的函数改成获取code_url的函数:

$prepay_id = $this->getPrepayId();

private function getPrepayId() {
$response = $this->postXml();
$result = $this->xmlToArray($response);
$prepay_id = $result["prepay_id"];
return $prepay_id;
}
改成:

$code_url = $this->getCodeUrl();
private function getCodeUrl() {
$response = $this->postXml();
$result = $this->xmlToArray($response);
$code_url = $result["code_url"];
return $code_url;
}


3.调用我自己写的生成二维码的函数生成二维码:

createQRcode($code_url,0,"","");

整个文件代码如下:

<?php
namespace Home\Action;
class WxPayAction extends BaseAction {

private $wxpayConfig;
private $wxpay;
private $parameters;
private $returnParameters;

public function _initialize(){
vendor('Wxpay.jssdk.log_');
$this->wxpayConfig = array('CURL_TIMEOUT' => 30);

$this->wxpayConfig['appid'] = C('APP_ID'); // 微信公众号身份的唯一标识
$this->wxpayConfig['appsecret'] = C('APP_SECRET'); // JSAPI接口中获取openid
$this->wxpayConfig['mchid'] = ""; // 受理商ID
$this->wxpayConfig['key'] = ""; // 商户支付密钥Key
$this->wxpayConfig['notifyurl'] = "";
$this->wxpayConfig['returnurl'] = "";
$this->wxpayConfig['url'] = "https://api.mch.weixin.qq.com/pay/unifiedorder";
}
/**
* 支付
*/
public function pay() {

$orderid = time();
$payprice = "0.01";
if(empty($orderid) || empty($payprice)) {
// $this->redirect('Mobile/Goods/home');
die('订单参数不完整!');
}

// $openid = $this->get_openid();

// 设置统一支付接口参数
// 设置必填参数
// appid已填,商户无需重复填写
// mch_id已填,商户无需重复填写
// noncestr已填,商户无需重复填写
// spbill_create_ip已填,商户无需重复填写
// sign已填,商户无需重复填写
// $this->setParameter("openid", $openid);
$this->setParameter("body", "百港汇环球购网上商城购物"); // 商品描述
// 自定义订单号,此处仅作举例
//$timeStamp = time();
//$out_trade_no = \WxPayConf_pub::$APPID . $timeStamp;
$out_trade_no = $orderid;
//$out_trade_no = time();
$this->setParameter("out_trade_no", $out_trade_no); // 商户订单号
$this->setParameter("total_fee", $payprice * 100); // 总金额
$this->setParameter("notify_url", $this->wxpayConfig['notifyurl']); // 通知地址
$this->setParameter("trade_type", "NATIVE"); // 交易类型
// 非必填参数,商户可根据实际情况选填
//$unifiedOrder->setParameter("sub_mch_id", "XXXX"); // 子商户号
//$unifiedOrder->setParameter("device_info", "XXXX"); // 设备号
//$unifiedOrder->setParameter("attach", "XXXX"); // 附加数据
//$unifiedOrder->setParameter("time_start", "XXXX"); // 交易起始时间
//$unifiedOrder->setParameter("time_expire", "XXXX"); // 交易结束时间
//$unifiedOrder->setParameter("goods_tag", "XXXX"); // 商品标记
//$unifiedOrder->setParameter("openid", "XXXX"); // 用户标识
//$unifiedOrder->setParameter("product_id", "XXXX"); // 商品ID

$code_url = $this->getCodeUrl();
if(empty($code_url)) {
die('参数出错,请重试!');
}
createQRcode($code_url,0,"","");
// $this->getParameters($prepay_id);
// $this->parameters;
// $returnurl = $this->wxpayConfig['returnurl'];

// $parameters = json_decode($this->parameters);
// $ptimeStamp = $parameters->timeStamp;
// $pnonceStr = $parameters->nonceStr;
// $ppackage = $parameters->package;
// $psignType = $parameters->signType;
// $ppaySign = $parameters->paySign;
// $signPackage = $this->getSignPackage();

}

/**
* 服务器异步通知页面路径
*/
public function Paynotify() {

$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$data = $this->xmlToArray($xml);
if($this->checkSign($data) == FALSE){
$this->setReturnParameter("return_code", "FAIL"); // 返回状态码
$this->setReturnParameter("return_msg", "签名失败"); // 返回信息
} else {
$this->setReturnParameter("return_code", "SUCCESS"); // 设置返回码
}
$returnXml = $this->returnXml();
// echo $returnXml;
if($this->checkSign($data) == TRUE) {
if ($data["return_code"] == "FAIL") {
// 此处应该更新一下订单状态,商户自行增删操作
die('【通信出错】'.$xml);
// $log_->log_result($log_name, "【通信出错】:\n" . $xml . "\n");
} elseif ($data["result_code"] == "FAIL"){
// 此处应该更新一下订单状态,商户自行增删操作
// $log_->log_result($log_name, "【业务出错】:\n" . $xml . "\n");
die('【业务出错】'.$xml);
} else {
// 此处应该更新一下订单状态,商户自行增删操作
// $log_->log_result($log_name, "【支付成功】:\n" . $orderid . "\n");
$orderNum = $data["out_trade_no"];
$m=M('orders');
$state = $m->where('order_num='.$orderNum)->field('order_state,order_id,order_sum_all')->find();
$orderSum = $state["order_sum_all"];
if($state['order_state']==0){

//保存订单状态
$newState['order_state']=4;
$newState['order_pay_way']=3;//微信支付
$newState["order_pay_time"] = date('Y-m-d H:i:s',time());
$res1 = $m->where('order_num='.$orderNum)->save($newState);

$child=$m->where('parent_id='.$state['order_id'])->select();
$userInfo = M('user')->where('user_id='.session('user_id'))->find();
$orders_goods=M('orders_goods');
$goods=M('goods');
if($child){
//保存子订单的状态
$newChildState['order_state']=4;
$newChildState['order_pay_way']=3;//微信支付
$newChildState["order_pay_time"] = date('Y-m-d H:i:s',time());
$m->where('parent_id='.$state['order_id'])->save($newChildState);
//获取父订单购买的所有商品
$idList='';
$ids=array();
foreach ($child as $key => $value) {
$ids[]=$value['order_id'];
}
$idList=implode(',',$ids);
$goodsList=$orders_goods->where('order_id in ('.$idList.')')->select();

}else{
$goodsList=$orders_goods->where('order_id='.$state['order_id'])->select();
}
$allPoints=0;
foreach ($goodsList as $key => $value) {
$goodsPoints =$goods->where('goods_id='.$value['goods_id'])->getField('goods_points');
$allPoints =$allPoints+ $goodsPoints* $value['goods_buy_num'];
D('MyLog')->MyLogWrite("order-goodslist==>".D('MyLog')->myPlay($allPoints));
}
D('MyLog')->MyLogWrite("order-goodslist==>".D('MyLog')->myPlay($orders_goods->getLastSql()));
// 商品积分增加
$poi=M('points');
$pdata['poi_points']=$allPoints;
$pdata['poi_form']='购买商品赠送积分';
$pdata['poi_remain_points']=$userInfo['user_points']+$allPoints;
$pdata['poi_get_time']=date('Y-m-d H:i:s',time());
$pdata['user_id']=session('user_id');
$pdata['poi_remark']=0;
$rese = $poi->add($pdata);
$userData['user_points']=$pdata['poi_remain_points'];
$resg = M('user')->where('user_id='.session('user_id'))->save($userData);
//消费记录
$consumeSum=$orderSum;
$this->comsumeNotes($consumeSum);
session('orderID',null);
$this->redirect('Mobile/Orders/orderNoReceive');
}else $this->redirect('Mobile/Orders/orderNoReceive');
}

//商户自行增加处理流程,
//例如:更新订单状态
//例如:数据库操作
//例如:推送支付完成信息
}
}
/**
* 服务器同步通知页面路径
*/
public function ReturnNotify() {
$this->redirect('Mobile/Orders/orderNoReceive');
}
/**
* 获取openid
*/
private function get_openid() {
$openid = $_COOKIE['apiopenid'];

if(empty($openid)) {
// 通过code获得openid
if (!isset($_GET['code'])) {
// 触发微信返回code码
$url = $this->createOauthUrlForCode($this->get_url());
Header("Location: " . $url);
} else {
// 获取code码,以获取openid
$code = $_GET['code'];
$openid = $this->getOpenId($code);
setcookie('apiopenid', $openid, time() + 86400);
}
}
return $openid;
}

/**
* 获取当前页面完整URL地址
*/
private function get_url() {
$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : $path_info);
return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relate_url;
}

/**
* 作用:生成可以获得code的url
*/
private function createOauthUrlForCode($redirectUrl) {
$urlObj["appid"] = $this->wxpayConfig['appid'];
$urlObj["redirect_uri"] = "$redirectUrl";
$urlObj["response_type"] = "code";
$urlObj["scope"] = "snsapi_base";
$urlObj["state"] = "STATE"."#wechat_redirect";
$bizString = $this->formatBizQueryParaMap($urlObj, false);
return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}

/**
* 作用:通过curl向微信提交code,以获取openid
*/
private function getOpenid($code) {
$url = $this->createOauthUrlForOpenid($code);
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $this->wxpayConfig['CURL_TIMEOUT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//运行curl,结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出openid
$data = json_decode($res,true);
$this->openid = $data['openid'];
return $this->openid;
}

/**
* 作用:生成可以获得openid的url
*/
private function createOauthUrlForOpenid($code) {
$urlObj["appid"] = $this->wxpayConfig['appid'];
$urlObj["secret"] = $this->wxpayConfig['appsecret'];
$urlObj["code"] = $code;
$urlObj["grant_type"] = "authorization_code";
$bizString = $this->formatBizQueryParaMap($urlObj, false);
return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
}

/**
* 作用:格式化参数,签名过程需要使用
*/
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v)
{
if($urlencode)
{
$v = urlencode($v);
}
//$buff .= strtolower($k) . "=" . $v . "&";
$buff .= $k . "=" . $v . "&";
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff)-1);
}
return $reqPar;
}

/**
* 作用:设置请求参数
*/
private function setParameter($parameter, $parameterValue) {
$this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
}

private function trimString($value) {
$ret = null;
if (null != $value) {
$ret = $value;
if (strlen($ret) == 0) {
$ret = null;
}
}
return $ret;
}

/**
* 获取prepay_id
*/
private function getCodeUrl() {
$response = $this->postXml();
$result = $this->xmlToArray($response);
$code_url = $result["code_url"];
return $code_url;
}

/**
* 作用:post请求xml
*/
private function postXml() {
$xml = $this->createXml();
$response = $this->postXmlCurl($xml,$this->wxpayConfig['url'],$this->wxpayConfig['CURL_TIMEOUT']);
return $response;
}

/**
* 生成接口参数xml
*/
private function createXml() {
try {
// 检测必填参数
if($this->parameters["out_trade_no"] == null) {
throw new \Exception("缺少统一支付接口必填参数out_trade_no!"."<br>");
}elseif($this->parameters["body"] == null){
throw new \Exception("缺少统一支付接口必填参数body!"."<br>");
}elseif ($this->parameters["total_fee"] == null ) {
throw new \Exception("缺少统一支付接口必填参数total_fee!"."<br>");
}elseif ($this->parameters["notify_url"] == null) {
throw new \Exception("缺少统一支付接口必填参数notify_url!"."<br>");
}elseif ($this->parameters["trade_type"] == null) {
throw new \Exception("缺少统一支付接口必填参数trade_type!"."<br>");
}elseif ($this->parameters["trade_type"] == "JSAPI" &&
$this->parameters["openid"] == NULL){
throw new \Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"."<br>");
}
$this->parameters["appid"] = $this->wxpayConfig['appid']; // 公众账号ID
$this->parameters["mch_id"] = $this->wxpayConfig['mchid']; // 商户号
$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];// 终端ip
$this->parameters["nonce_str"] = $this->createNoncestr(); // 随机字符串
$this->parameters["sign"] = $this->getSign($this->parameters); // 签名
return $this->arrayToXml($this->parameters);
} catch (\Exception $e) {
die($e->errorMessage());
}
}

/**
* 作用:产生随机字符串,不长于32位
*/
private function createNoncestr( $length = 32 ) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}

/**
* 作用:生成签名
*/
private function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'</br>';
//签名步骤二:在string后加入KEY
$String = $String."&key=".$this->wxpayConfig['key'];
//echo "【string2】".$String."</br>";
//签名步骤三:MD5加密
$String = md5($String);
//echo "【string3】 ".$String."</br>";
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
//echo "【result】 ".$result_."</br>";
return $result_;
}

/**
* 作用:array转xml
*/
private function arrayToXml($arr) {
$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;
}

/**
* 作用:以post方式提交xml到对应的接口url
*/
private function postXmlCurl($xml,$url,$second = 30) {
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
curl_close($ch);
//返回结果
if($data)
{
//curl_close($ch);
return $data;
}
else
{
$error = curl_errno($ch);
echo "curl出错,错误码:$error"."<br>";
echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch);
return false;
}
}

/**
* 作用:将xml转为array
*/
private function xmlToArray($xml) {
//将XML转为array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}

/**
* 作用:设置jsapi的参数
*/
private function getParameters($prepay_id) {
$jsApiObj["appId"] = $this->wxpayConfig['appid'];
$timeStamp = time();
$jsApiObj["timeStamp"] = "$timeStamp";
$jsApiObj["nonceStr"] = $this->createNoncestr();
$jsApiObj["package"] = "prepay_id=$prepay_id";
$jsApiObj["signType"] = "MD5";
$jsApiObj["paySign"] = $this->getSign($jsApiObj);
$this->parameters = json_encode($jsApiObj);
}

private function checkSign($data) {
$tmpData = $data;
unset($tmpData['sign']);
$sign = $this->getSign($tmpData);//本地签名
if ($data['sign'] == $sign) {
return TRUE;
}
return FALSE;
}

/**
* 设置返回微信的xml数据
*/
private function setReturnParameter($parameter, $parameterValue) {
$this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
}

/**
* 将xml数据返回微信
*/
private function returnXml() {
$returnXml = $this->arrayToXml($this->returnParameters);
return $returnXml;
}

/*----以下是JSSDK的文件----*/
private function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();

// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$timestamp = time();
$nonceStr = $this->createNonceStr2();

// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";

$signature = sha1($string);

$signPackage = array(
"appId" => $this->wxpayConfig['appid'],
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}

private function createNonceStr2($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}

private function getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
// $data = json_decode(file_get_contents("jsapi_ticket.json"));
$data = json_decode($_COOKIE['jsapi_ticket_json']);
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// 如果是企业号用以下 URL 获取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
//$data->expire_time = time() + 7000;
//$data->jsapi_ticket = $ticket;
//$fp = fopen("jsapi_ticket.json", "w");
//fwrite($fp, json_encode($data));
//fclose($fp);
$tempArr = array('jsapi_ticket' => $ticket, 'expire_time' => time() + 7000);
setcookie('jsapi_ticket_json', json_encode($tempArr), $tempArr['expire_time']);
}
} else {
$ticket = $data->jsapi_ticket;
}

return $ticket;
}

private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
// $data = json_decode(file_get_contents("access_token.json"));
$data = json_decode($_COOKIE["access_token_json"]);
if ($data->expire_time < time()) {
// 如果是企业号用以下URL获取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->wxpayConfig['appid'] . "&secret=" . $this->wxpayConfig['appsecret'];
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
//$data->expire_time = time() + 7000;
//$data->access_token = $access_token;
//$fp = fopen("access_token.json", "w");
//fwrite($fp, json_encode($data));
//fclose($fp);
$tempArr = array('access_token' => $access_token, 'expire_time' => time() + 7000);
setcookie('access_token_json', json_encode($tempArr), $tempArr['expire_time']);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}

private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);
curl_close($curl);

return $res;
}
//消费记录
function comsumeNotes($conSum){
$userSum=M('user')->where('user_id='.session('user_id'))->getField('user_sum');
$note['con_type']=-1;
$note['con_sum']=$conSum;
$note['con_arrive_sum']=$conSum;
$note['con_form']="微信支付购买商品";
$note['con_remain_sum']=$userSum;
$note['con_operate_time']=date('Y-m-d H:i:s');
$note['user_id']=session('user_id');
$res1=M("depo_cons_notes")->add($note);
}

}