<?php
|
error_reporting(0);
|
require_once "../../../m.ttgw.tv/Store/phpmvcomcn/jingnei/lib/WxPay.Api.php";
|
|
/**
|
*
|
* 刷卡支付实现类
|
* 该类实现了一个刷卡支付的流程,流程如下:
|
* 1、提交刷卡支付
|
* 2、根据返回结果决定是否需要查询订单,如果查询之后订单还未变则需要返回查询(一般反复查10次)
|
* 3、如果反复查询10订单依然不变,则发起撤销订单
|
* 4、撤销订单需要循环撤销,一直撤销成功为止(注意循环次数,建议10次)
|
*
|
* 该类是微信支付提供的样例程序,商户可根据自己的需求修改,或者使用lib中的api自行开发,为了防止
|
* 查询时hold住后台php进程,商户查询和撤销逻辑可在前端调用
|
*
|
* @author widy
|
*
|
*/
|
class MicroPay
|
{
|
/**
|
*
|
* 提交刷卡支付,并且确认结果,接口比较慢
|
* @param WxPayMicroPay $microPayInput
|
* @throws WxpayException
|
* @return 返回查询接口的结果
|
*/
|
public function pay($microPayInput,$ShopOrdID,$sfxf)
|
{
|
//①、提交被扫支付
|
$result = WxPayApi::micropay($microPayInput, 5);
|
//如果返回成功
|
/* if(!array_key_exists("return_code", $result)
|
|| !array_key_exists("out_trade_no", $result)
|
|| !array_key_exists("result_code", $result))
|
{
|
// echo "接口调用失败,请确认是否输入是否有误!";
|
// throw new WxPayException("接口调用失败!");
|
} */
|
|
//签名验证
|
$out_trade_no = $microPayInput->GetOut_trade_no();
|
|
//②、接口调用成功,明确返回调用失败
|
if($result["return_code"] == "SUCCESS" &&
|
$result["result_code"] == "FAIL" &&
|
$result["err_code"] != "USERPAYING" &&
|
$result["err_code"] != "SYSTEMERROR")
|
{
|
return false;
|
}
|
//③、确认支付是否成功
|
$queryTimes = 10;
|
$i = 0;
|
while($queryTimes > 0)
|
{
|
|
$succResult = 0;
|
$queryResult = $this->query($out_trade_no, $succResult,$sfxf);
|
//如果需要等待2s后继续
|
if($succResult == 2){
|
sleep(2);
|
$queryTimes--;
|
continue;
|
} else if($succResult == 1){//查询成功
|
return $queryResult;
|
}else {//订单交易失败
|
return $queryResult;
|
}
|
|
}
|
return 3;
|
|
}
|
/**
|
*
|
* 查询订单情况
|
* @param string $out_trade_no 商户订单号
|
* @param int $succCode 查询订单结果
|
* @return 0 订单不成功,1表示订单成功,2表示继续等待
|
*/
|
public function query($out_trade_no, &$succCode,$sfxf)
|
{
|
$queryOrderInput = new WxPayOrderQuery();
|
$queryOrderInput->SetOut_trade_no($out_trade_no);
|
$result = WxPayApi::orderQuery($queryOrderInput);
|
// var_dump($result);exit;
|
if($result["return_code"] == "SUCCESS"
|
&& $result["result_code"] == "SUCCESS")
|
{
|
//支付成功
|
if($result["trade_state"] == "SUCCESS"){
|
//解锁
|
$key='apivcomcn20170421';
|
|
$access_token = json_decode(file_get_contents("http://api.v.com.cn/weixin/?method=weixin_Appid&weixinAPP=1"),true)["access_token"];
|
|
|
|
$attach = json_decode($result["attach"],true);
|
$ShopOrdID = $attach["ShopOrdID"];
|
|
//刷新支付信息
|
$method = "Order_PayNotify";
|
$transaction_id = $result["transaction_id"];
|
$mch_id = $result["mch_id"];
|
$openid = $result["openid"];
|
|
$unionid_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
|
$curl = curl_init(); // 启动一个CURL会话
|
curl_setopt($curl, CURLOPT_URL, $unionid_url);
|
curl_setopt($curl, CURLOPT_HEADER, 0);
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
|
$tmpInfo = curl_exec($curl); //返回api的json对象
|
//关闭URL请求
|
curl_close($curl);
|
|
$unioninfo = json_decode($tmpInfo,true);
|
|
$unionid = $unioninfo["unionid"];
|
|
$out_trade_no = $result["out_trade_no"];
|
$total_fee = number_format($result["total_fee"]/100,2,'.','');
|
$PayMono = urlencode("微信支付");
|
if(!empty($attach["ShopUserID"])){
|
$new_openid = $attach["ShopUserID"];
|
$arr = array("method"=>$method,"transaction_id"=>$transaction_id,"mch_id"=>$mch_id,"out_trade_no"=>$out_trade_no,"total_fee"=>$total_fee,"ShopOrdID"=>$ShopOrdID,"PayMono"=>$PayMono,"openid"=>$new_openid);
|
}else{
|
$arr = array("method"=>$method,"transaction_id"=>$transaction_id,"mch_id"=>$mch_id,"out_trade_no"=>$out_trade_no,"total_fee"=>$total_fee,"ShopOrdID"=>$ShopOrdID,"PayMono"=>$PayMono,"openid"=>$openid);
|
}
|
|
|
|
|
ksort($arr);
|
$i=0;
|
$stringA='';
|
|
foreach($arr as $k=>$v){
|
$stringA=$stringA.$k.$v;
|
}
|
$stringSignTemp=$stringA.$key;
|
$stringSignTemp=str_replace(" ", "%20", $stringSignTemp);
|
|
$sign=MD5($stringSignTemp);
|
if(!empty($attach["ShopUserID"])){
|
$content = 'method='.$method.'&transaction_id='.$transaction_id.'&mch_id='.$mch_id.'&out_trade_no='.$out_trade_no.'&total_fee='.$total_fee.'&ShopOrdID='.$ShopOrdID.'&PayMono='.$PayMono."&openid=".$new_openid;
|
}else{
|
$content = 'method='.$method.'&transaction_id='.$transaction_id.'&mch_id='.$mch_id.'&out_trade_no='.$out_trade_no.'&total_fee='.$total_fee.'&ShopOrdID='.$ShopOrdID.'&PayMono='.$PayMono."&openid=".$openid;
|
}
|
|
$content .= '&Sign='.$sign;
|
$order_url='http://api.v.com.cn/order/?'.$content;
|
$order_url=str_replace(" ", "%20", $order_url);
|
$orderinfo=file_get_contents($order_url);
|
$orderinfo=json_decode($orderinfo,true);
|
|
|
|
$ShopUserID = urlencode($orderinfo["ShopUserID"]);
|
|
|
|
|
|
|
if($sfxf == "2"){
|
$succCode = 1;
|
return 1;
|
}else{
|
|
|
$method = "Order_detail";
|
$arr = array("method"=>$method,"ShopOrdID"=>$ShopOrdID);
|
ksort($arr);
|
$i=0;
|
$stringA='';
|
foreach($arr as $k=>$v){
|
$stringA=$stringA.$k.$v;
|
}
|
$stringSignTemp=$stringA.$key;
|
$stringSignTemp=str_replace(" ", "%20", $stringSignTemp);
|
$sign=MD5($stringSignTemp);
|
$content = 'method='.$method.'&ShopOrdID='.$ShopOrdID;
|
$content .= '&Sign='.$sign;
|
$order_url='http://api.v.com.cn/order/?'.$content;
|
$order_url=str_replace(" ", "%20", $order_url);
|
// var_dump($url);exit;
|
$orderinfo=file_get_contents($order_url);
|
$orderinfo=json_decode($orderinfo,true);
|
|
$details = $orderinfo["details"];
|
foreach($details as $kd=>$vd){
|
if($vd["strItmID"] == "1120-005691"){
|
$you = "1";
|
}
|
}
|
if($you && $you == "1"){
|
$ShopUserID = $orderinfo["ShopUserID"];
|
$ShopUserID = urlencode($ShopUserID);
|
$method = "User_Recharge";
|
$RechargeType = "Store";
|
$RechargeMoney = intval($total_fee);
|
$arr = array("method"=>$method,"ShopOrdID"=>$ShopOrdID,"ShopUserID"=>$ShopUserID,"RechargeType"=>$RechargeType,"RechargeMoney"=>$RechargeMoney);
|
ksort($arr);
|
$i=0;
|
$stringA='';
|
foreach($arr as $k=>$v){
|
$stringA=$stringA.$k.$v;
|
}
|
$stringSignTemp=$stringA.$key;
|
$stringSignTemp=str_replace(" ", "%20", $stringSignTemp);
|
$sign=MD5($stringSignTemp);
|
$content = 'method='.$method.'&ShopOrdID='.$ShopOrdID.'&ShopUserID='.$ShopUserID.'&RechargeType='.$RechargeType.'&RechargeMoney='.$RechargeMoney;
|
$content .= '&Sign='.$sign;
|
$user_url='http://api.v.com.cn/User/?'.$content;
|
$user_url=str_replace(" ", "%20", $user_url);
|
// var_dump($url);exit;
|
$userinfo=file_get_contents($user_url);
|
$userinfo=json_decode($userinfo,true);
|
|
$pdo = new PDO('mysql:host=120.24.62.151;dbname=wxordidsave', 'yyjj', 'zsh56ynyj!2w',array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
|
$sql = "UPDATE `order` SET `order_send` = '2' WHERE `order_long` = '$out_trade_no'";
|
|
$pdo->exec($sql);
|
$succCode = 1;
|
return 1;
|
|
}
|
$succCode = 1;
|
return 1;
|
}
|
}
|
//用户支付中
|
else if($result["trade_state"] == "USERPAYING"){
|
$succCode = 2;
|
return $succCode;
|
}
|
}
|
|
//如果返回错误码为“此交易订单号不存在”则直接认定失败
|
elseif($result["err_code"] == "ORDERNOTEXIST")
|
{
|
$succCode = 0;
|
return $result;
|
}else{
|
//如果是系统错误,则后续继续
|
$succCode = 3;
|
return 0;
|
}
|
}
|
|
/**
|
*
|
* 撤销订单,如果失败会重复调用10次
|
* @param string $out_trade_no
|
* @param 调用深度 $depth
|
*/
|
public function cancel($out_trade_no, $depth = 0)
|
{
|
if($depth > 10){
|
return false;
|
}
|
|
$clostOrder = new WxPayReverse();
|
$clostOrder->SetOut_trade_no($out_trade_no);
|
$result = WxPayApi::reverse($clostOrder);
|
|
//接口调用失败
|
if($result["return_code"] != "SUCCESS"){
|
return false;
|
}
|
|
//如果结果为success且不需要重新调用撤销,则表示撤销成功
|
if($result["result_code"] != "SUCCESS"
|
&& $result["recall"] == "N"){
|
return true;
|
} else if($result["recall"] == "Y") {
|
return $this->cancel($out_trade_no, ++$depth);
|
}
|
return false;
|
}
|
}
|