添加后台代理代码
This commit is contained in:
678
codes/agent/game/api/payment/wechat/lib/WxPay.Api.php
Normal file
678
codes/agent/game/api/payment/wechat/lib/WxPay.Api.php
Normal file
@@ -0,0 +1,678 @@
|
||||
<?php
|
||||
require_once "WxPay.Exception.php";
|
||||
require_once "WxPay.Config.php";
|
||||
require_once "WxPay.Data.php";
|
||||
//require_once '../log.php';
|
||||
|
||||
//初始化日志
|
||||
//$logHandler= new CLogFileHandler("../../logs/".date('Y-m-d').'.log');
|
||||
//$log = Log::Init($logHandler, 15);
|
||||
|
||||
/**
|
||||
*
|
||||
* 接口访问类,包含所有微信支付API列表的封装,类中方法为static方法,
|
||||
* 每个接口有默认超时时间(除提交被扫支付为10s,上报超时时间为1s外,其他均为6s)
|
||||
* @author widyhu
|
||||
*
|
||||
*/
|
||||
class WxPayApi
|
||||
{
|
||||
/**
|
||||
* 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayUnifiedOrder $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return array 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function unifiedOrder($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsOut_trade_noSet())
|
||||
throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
|
||||
else if (!$inputObj->IsBodySet())
|
||||
throw new WxPayException("缺少统一支付接口必填参数body!");
|
||||
else if (!$inputObj->IsTotal_feeSet())
|
||||
throw new WxPayException("缺少统一支付接口必填参数total_fee!");
|
||||
else if (!$inputObj->IsTrade_typeSet())
|
||||
throw new WxPayException("缺少统一支付接口必填参数trade_type!");
|
||||
|
||||
//关联参数
|
||||
if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet())
|
||||
throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
|
||||
|
||||
if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet())
|
||||
throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
|
||||
|
||||
|
||||
// 异步通知url未设置,则使用配置文件中的url
|
||||
if (!$inputObj->IsNotify_urlSet())
|
||||
$inputObj->SetNotify_url(WxPayConfig::NOTIFY_URL); // 异步通知url
|
||||
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']); // 终端ip
|
||||
//$inputObj->SetSpbill_create_ip("1.1.1.1");
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
// 签名
|
||||
$inputObj->SetSign($wechatInfo);
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
// 提交统一订单返回的数据
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); // 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 查询订单,WxPayOrderQuery中out_trade_no、transaction_id至少填一个
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayOrderQuery $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function orderQuery($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/pay/orderquery";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet())
|
||||
throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
|
||||
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond();//请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
|
||||
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); // 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 关闭订单,WxPayCloseOrder中out_trade_no必填
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayCloseOrder $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function closeOrder($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/pay/closeorder";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsOut_trade_noSet())
|
||||
{
|
||||
throw new WxPayException("订单查询接口中,out_trade_no必填!");
|
||||
}
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo);//签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result);//上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 申请退款,WxPayRefund中out_trade_no、transaction_id至少填一个且
|
||||
* out_refund_no、total_fee、refund_fee、op_user_id为必填参数
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayRefund $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $weixinInfo
|
||||
* @param int $timeOut
|
||||
* @throws WxPayException
|
||||
* @return array 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function refund($inputObj, $timeOut = 6, $weixinInfo, $dbObj)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
|
||||
// 检测必填参数
|
||||
if (!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet())
|
||||
throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
|
||||
else if (!$inputObj->IsOut_refund_noSet())
|
||||
throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
|
||||
else if (!$inputObj->IsTotal_feeSet())
|
||||
throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
|
||||
else if (!$inputObj->IsRefund_feeSet())
|
||||
throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
|
||||
else if (!$inputObj->IsOp_user_idSet())
|
||||
throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
|
||||
|
||||
$inputObj->SetAppid($weixinInfo["appid"]); // 公众账号ID
|
||||
$inputObj->SetMch_id($weixinInfo["mchid"]); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($weixinInfo);//签名
|
||||
$xml = $inputObj->ToXml();
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, true, $timeOut, $weixinInfo);
|
||||
//Log::DEBUG("退款回调消息:" . $response);
|
||||
$result = WxPayResults::Init($response, $weixinInfo, 1, true, $dbObj);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); // 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 查询退款
|
||||
* 提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,
|
||||
* 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
|
||||
* WxPayRefundQuery中out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayRefundQuery $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function refundQuery($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/pay/refundquery";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsOut_refund_noSet() &&
|
||||
!$inputObj->IsOut_trade_noSet() &&
|
||||
!$inputObj->IsTransaction_idSet() &&
|
||||
!$inputObj->IsRefund_idSet()
|
||||
)
|
||||
{
|
||||
throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
|
||||
}
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); // 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载对账单,WxPayDownloadBill中bill_date为必填参数
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayDownloadBill $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function downloadBill($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/pay/downloadbill";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsBill_dateSet())
|
||||
{
|
||||
throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
|
||||
}
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
if (substr($response, 0, 5) == "<xml>")
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交被扫支付API
|
||||
* 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
|
||||
* 由商户收银台或者商户后台调用该接口发起支付。
|
||||
* WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayWxPayMicroPay $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function micropay($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/pay/micropay";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsBodySet())
|
||||
throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
|
||||
else if (!$inputObj->IsOut_trade_noSet())
|
||||
throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
|
||||
else if (!$inputObj->IsTotal_feeSet())
|
||||
throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
|
||||
else if (!$inputObj->IsAuth_codeSet())
|
||||
throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
|
||||
|
||||
$inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); // 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 撤销订单API接口,WxPayReverse中参数out_trade_no和transaction_id必须填写一个
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayReverse $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
*/
|
||||
public static function reverse($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet())
|
||||
throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
|
||||
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr());//随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond();//请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, true, $timeOut);
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result);//上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 测速上报,该方法内部封装在report中,使用时请注意异常流程
|
||||
* WxPayReport中interface_url、return_code、result_code、user_ip、execute_time_必填
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayReport $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function report($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/payitil/report";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsInterface_urlSet())
|
||||
throw new WxPayException("接口URL,缺少必填参数interface_url!");
|
||||
if (!$inputObj->IsReturn_codeSet())
|
||||
throw new WxPayException("返回状态码,缺少必填参数return_code!");
|
||||
if (!$inputObj->IsResult_codeSet())
|
||||
throw new WxPayException("业务结果,缺少必填参数result_code!");
|
||||
if (!$inputObj->IsUser_ipSet())
|
||||
throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
|
||||
if (!$inputObj->IsExecute_time_Set())
|
||||
throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
|
||||
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetUser_ip($_SERVER['REMOTE_ADDR']);//终端ip
|
||||
$inputObj->SetTime(date("YmdHis")); // 商户上报时间
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 生成二维码规则,模式一生成支付二维码
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayBizPayUrl $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function bizpayurl($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
if (!$inputObj->IsProduct_idSet())
|
||||
throw new WxPayException("生成二维码,缺少必填参数product_id!");
|
||||
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetTime_stamp(time()); // 时间戳
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
|
||||
return $inputObj->GetValues();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 转换短链接
|
||||
* 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),
|
||||
* 减小二维码数据量,提升扫描速度和精确度。
|
||||
* appid、mchid、spbill_create_ip、nonce_str不需要填入
|
||||
* @param WxPayShortUrl $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $wechatInfo
|
||||
* @throws WxPayException
|
||||
* @return 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function shorturl($inputObj, $timeOut, $wechatInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/tools/shorturl";
|
||||
//检测必填参数
|
||||
if (!$inputObj->IsLong_urlSet())
|
||||
throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
|
||||
|
||||
$inputObj->SetAppid($wechatInfo['appid']); // 公众账号ID
|
||||
$inputObj->SetMch_id($wechatInfo['mchid']); // 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); // 随机字符串
|
||||
|
||||
$inputObj->SetSign($wechatInfo); // 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
|
||||
$startTimeStamp = self::getMillisecond(); // 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, false, $timeOut);
|
||||
$result = WxPayResults::Init($response, $wechatInfo);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); // 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 支付结果通用通知
|
||||
* @param function $callback
|
||||
* 直接回调函数使用方法: notify(you_function);
|
||||
* 回调类成员函数方法:notify(array($this, you_function));
|
||||
* $callback 原型为:function function_name($data){}
|
||||
*/
|
||||
public static function notify($callback, &$msg, $xml, $isXML, $db = null)
|
||||
{
|
||||
//如果返回成功则验证签名
|
||||
try
|
||||
{
|
||||
// 假如是XML格式的话
|
||||
if ($isXML)
|
||||
$result = WxPayResults::Init($xml, "", 2, true, $db);
|
||||
else
|
||||
$result = WxPayResults::Init($_GET, "", 2, false, $db);
|
||||
}
|
||||
catch (WxPayException $e)
|
||||
{
|
||||
$msg = $e->errorMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
return call_user_func($callback, $result, $db);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 发送普通红包
|
||||
* @param WxPaySendredpack $inputObj
|
||||
* @param int $timeOut
|
||||
* @param array $weixinInfo
|
||||
* @throws WxPayException
|
||||
* @return array 成功时返回,其他抛异常
|
||||
*/
|
||||
public static function sendredpack($inputObj, $timeOut, $weixinInfo)
|
||||
{
|
||||
$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
|
||||
// 检测必填参数
|
||||
if (!$inputObj->IsMch_billnoSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数mch_billno!");
|
||||
else if (!$inputObj->IsSend_nameSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数send_name!");
|
||||
else if (!$inputObj->IsRe_openidSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数re_openid!");
|
||||
else if (!$inputObj->IsTotal_amountSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数total_amount!");
|
||||
else if (!$inputObj->IsTotal_numSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数total_num!");
|
||||
else if (!$inputObj->IsWishingSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数wishing!");
|
||||
else if (!$inputObj->IsClient_ipSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数client_ip!");
|
||||
else if (!$inputObj->IsAct_nameSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数act_name!");
|
||||
else if (!$inputObj->IsRemarkSet())
|
||||
throw new WxPayException("发送红包接口中,缺少必填参数remark!");
|
||||
|
||||
$inputObj->SetAppid($weixinInfo["appid"]); /// 公众账号ID
|
||||
$inputObj->SetMch_id($weixinInfo["mchid"]); /// 商户号
|
||||
$inputObj->SetNonce_str(self::getNonceStr()); /// 随机字符串
|
||||
|
||||
$inputObj->SetSign($weixinInfo); /// 签名
|
||||
$xml = $inputObj->ToXml();
|
||||
//Log::DEBUG("发送普通红包消息:" . $xml);
|
||||
$startTimeStamp = self::getMillisecond(); /// 请求开始时间
|
||||
$response = self::postXmlCurl($xml, $url, true, $timeOut, $weixinInfo);
|
||||
//Log::DEBUG("发送普通红包消息:" . $response);
|
||||
$result = WxPayResults::Init($response, $weixinInfo, 1, true, null, false);
|
||||
self::reportCostTime($url, $startTimeStamp, $result); /// 上报请求花费时间
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 产生随机字符串,不长于32位
|
||||
* @param int $length
|
||||
* @return 产生的随机字符串
|
||||
*/
|
||||
public static function getNonceStr($length = 32)
|
||||
{
|
||||
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
$str = "";
|
||||
for ($i = 0; $i < $length; $i++)
|
||||
{
|
||||
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接输出xml
|
||||
* @param string $xml
|
||||
*/
|
||||
public static function replyNotify($xml)
|
||||
{
|
||||
echo $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 上报数据, 上报的时候将屏蔽所有异常流程
|
||||
* @param string $usrl
|
||||
* @param int $startTimeStamp
|
||||
* @param array $data
|
||||
*/
|
||||
private static function reportCostTime($url, $startTimeStamp, $data)
|
||||
{
|
||||
//如果不需要上报数据
|
||||
if (WxPayConfig::REPORT_LEVENL == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//如果仅失败上报
|
||||
if (WxPayConfig::REPORT_LEVENL == 1 &&
|
||||
array_key_exists("return_code", $data) &&
|
||||
$data["return_code"] == "SUCCESS" &&
|
||||
array_key_exists("result_code", $data) &&
|
||||
$data["result_code"] == "SUCCESS"
|
||||
)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//上报逻辑
|
||||
$endTimeStamp = self::getMillisecond();
|
||||
$objInput = new WxPayReport();
|
||||
$objInput->SetInterface_url($url);
|
||||
$objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
|
||||
//返回状态码
|
||||
if (array_key_exists("return_code", $data))
|
||||
$objInput->SetReturn_code($data["return_code"]);
|
||||
//返回信息
|
||||
if (array_key_exists("return_msg", $data))
|
||||
$objInput->SetReturn_msg($data["return_msg"]);
|
||||
//业务结果
|
||||
if (array_key_exists("result_code", $data))
|
||||
$objInput->SetResult_code($data["result_code"]);
|
||||
//错误代码
|
||||
if (array_key_exists("err_code", $data))
|
||||
$objInput->SetErr_code($data["err_code"]);
|
||||
//错误代码描述
|
||||
if (array_key_exists("err_code_des", $data))
|
||||
$objInput->SetErr_code_des($data["err_code_des"]);
|
||||
//商户订单号
|
||||
if (array_key_exists("out_trade_no", $data))
|
||||
$objInput->SetOut_trade_no($data["out_trade_no"]);
|
||||
//设备号
|
||||
if (array_key_exists("device_info", $data))
|
||||
$objInput->SetDevice_info($data["device_info"]);
|
||||
|
||||
try
|
||||
{
|
||||
self::report($objInput);
|
||||
}
|
||||
catch (WxPayException $e)
|
||||
{
|
||||
//不做任何处理
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以post方式提交xml到对应的接口url
|
||||
*
|
||||
* @param string $xml 需要post的xml数据
|
||||
* @param string $url url
|
||||
* @param bool $useCert 是否需要证书,默认不需要
|
||||
* @param int $second url执行超时时间,默认30s
|
||||
* @throws WxPayException
|
||||
*/
|
||||
private static function postXmlCurl($xml, $url, $useCert = false, $second = 30, $weixinInfo = null)
|
||||
{
|
||||
// return ihttp_request($url,$xml,false);
|
||||
$ch = curl_init();
|
||||
//设置超时
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
|
||||
|
||||
//如果有配置代理这里就设置代理
|
||||
if (WxPayConfig::CURL_PROXY_HOST != "0.0.0.0" && WxPayConfig::CURL_PROXY_PORT != 0)
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
|
||||
curl_setopt($ch, CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
// edit by benton begin
|
||||
//curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
|
||||
//curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//严格校验
|
||||
// edit by benton end
|
||||
//设置header
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
//要求结果为字符串且输出到屏幕上
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
if ($useCert == true)
|
||||
{
|
||||
//设置证书
|
||||
//使用证书:cert 与 key 分别属于两个.pem文件
|
||||
if (!empty($weixinInfo) && count($weixinInfo) > 0)
|
||||
{
|
||||
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
|
||||
//curl_setopt($ch, CURLOPT_SSLCERT, str_replace("WEIXINMCHID", $weixinInfo["mchid"], WxPayConfig::SSLCERT_PATH));
|
||||
curl_setopt($ch, CURLOPT_SSLCERT, sprintf(WxPayConfig::SSLCERT_PATH(), $weixinInfo['mchid']));
|
||||
|
||||
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
|
||||
//curl_setopt($ch, CURLOPT_SSLKEY, str_replace("WEIXINMCHID", $weixinInfo["mchid"], WxPayConfig::SSLKEY_PATH));
|
||||
curl_setopt($ch, CURLOPT_SSLKEY, sprintf(WxPayConfig::SSLKEY_PATH(), $weixinInfo['mchid']));
|
||||
|
||||
///var_dump(sprintf(WxPayConfig::SSLCERT_PATH(), $weixinInfo['mchid']));
|
||||
///echo '<br>', PHP_EOL;
|
||||
///var_dump(sprintf(WxPayConfig::SSLKEY_PATH(), $weixinInfo['mchid']));
|
||||
}
|
||||
else
|
||||
throw new WxPayException("未传入公众号相关信息。");
|
||||
}
|
||||
//post提交方式
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
|
||||
//运行curl
|
||||
$data = curl_exec($ch);
|
||||
//返回结果
|
||||
if ($data)
|
||||
{
|
||||
curl_close($ch);
|
||||
return $data;
|
||||
}
|
||||
else
|
||||
{
|
||||
$error = curl_errno($ch);
|
||||
$message = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new WxPayException("curl出错,错误码:{$error},错误信息:{$message}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取毫秒级别的时间戳
|
||||
*/
|
||||
private static function getMillisecond()
|
||||
{
|
||||
//获取毫秒的时间戳
|
||||
$time = explode(" ", microtime());
|
||||
$time = $time[1] . ($time[0] * 1000);
|
||||
$time2 = explode(".", $time);
|
||||
$time = $time2[0];
|
||||
return $time;
|
||||
}
|
||||
}
|
||||
|
||||
80
codes/agent/game/api/payment/wechat/lib/WxPay.Config.php
Normal file
80
codes/agent/game/api/payment/wechat/lib/WxPay.Config.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 配置账号信息
|
||||
*/
|
||||
class WxPayConfig
|
||||
{
|
||||
public function init($appid = '', $mchid = '', $key = '', $appsecret = '')
|
||||
{
|
||||
$this->appid = $appid;
|
||||
$this->mchid = $mchid;
|
||||
$this->key = $key;
|
||||
$this->appsecret = $appsecret;
|
||||
}
|
||||
//=======【基本信息设置】=====================================
|
||||
//
|
||||
/**
|
||||
* TODO: 修改这里配置为您自己申请的商户信息
|
||||
* 微信公众号信息配置
|
||||
*
|
||||
* APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
|
||||
*
|
||||
* MCHID:商户号(必须配置,开户邮件中可查看)
|
||||
*
|
||||
* KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
|
||||
* 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
|
||||
*
|
||||
* APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
|
||||
* 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
|
||||
* @var string
|
||||
*/
|
||||
public $appid = '';
|
||||
public $mchid = '';
|
||||
public $key = '';
|
||||
public $appsecret = '';
|
||||
|
||||
//=======【证书路径设置】=====================================
|
||||
/**
|
||||
* TODO:设置商户证书路径
|
||||
* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要,可登录商户平台下载,
|
||||
* API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书)
|
||||
* @var path
|
||||
*/
|
||||
//const SSLCERT_PATH = 'E:\\java\\apache24www\\syapi\\payment\\wechat\\cert\\WEIXINMCHID\\apiclient_cert.pem';
|
||||
//const SSLKEY_PATH = 'E:\\java\\apache24www\\syapi\\payment\\wechat\\cert\\WEIXINMCHID\\apiclient_key.pem';
|
||||
static public function SSLCERT_PATH()
|
||||
{
|
||||
return dirname(dirname(__FILE__)) . '\\cert\\%s\\apiclient_cert.pem';
|
||||
}
|
||||
|
||||
static public function SSLKEY_PATH()
|
||||
{
|
||||
return dirname(dirname(__FILE__)) . '\\cert\\%s\\apiclient_key.pem';
|
||||
}
|
||||
|
||||
//=======【curl代理设置】===================================
|
||||
/**
|
||||
* TODO:这里设置代理机器,只有需要代理的时候才设置,不需要代理,请设置为0.0.0.0和0
|
||||
* 本例程通过curl使用HTTP POST方法,此处可修改代理服务器,
|
||||
* 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置)
|
||||
* @var unknown_type
|
||||
*/
|
||||
const CURL_PROXY_HOST = "0.0.0.0";//"10.152.18.220";
|
||||
const CURL_PROXY_PORT = 0;//8080;
|
||||
|
||||
//=======【上报信息配置】===================================
|
||||
/**
|
||||
* TODO:接口调用上报等级,默认紧错误上报(注意:上报超时间为【1s】,上报无论成败【永不抛出异常】,
|
||||
* 不会影响接口调用流程),开启上报之后,方便微信监控请求调用的质量,建议至少
|
||||
* 开启错误上报。
|
||||
* 上报等级,0.关闭上报; 1.仅错误出错上报; 2.全量上报
|
||||
* @var int
|
||||
*/
|
||||
const REPORT_LEVENL = 1;
|
||||
|
||||
/**
|
||||
* todo: 默认的通知地址
|
||||
*/
|
||||
const NOTIFY_URL = '';
|
||||
}
|
||||
3948
codes/agent/game/api/payment/wechat/lib/WxPay.Data.php
Normal file
3948
codes/agent/game/api/payment/wechat/lib/WxPay.Data.php
Normal file
File diff suppressed because it is too large
Load Diff
13
codes/agent/game/api/payment/wechat/lib/WxPay.Exception.php
Normal file
13
codes/agent/game/api/payment/wechat/lib/WxPay.Exception.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* 微信支付API异常类
|
||||
* @author widyhu
|
||||
*
|
||||
*/
|
||||
class WxPayException extends Exception {
|
||||
public function errorMessage()
|
||||
{
|
||||
return $this->getMessage();
|
||||
}
|
||||
}
|
||||
112
codes/agent/game/api/payment/wechat/lib/WxPay.Notify.php
Normal file
112
codes/agent/game/api/payment/wechat/lib/WxPay.Notify.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
use phprs\ezsql\Sql;
|
||||
|
||||
/**
|
||||
*
|
||||
* 回调基础类
|
||||
* @author widyhu
|
||||
*
|
||||
*/
|
||||
class WxPayNotify extends WxPayNotifyReply
|
||||
{
|
||||
/**
|
||||
*
|
||||
* 回调入口
|
||||
* @param bool $needSign 是否需要签名输出
|
||||
*/
|
||||
final public function Handle($needSign = true, $db = null)
|
||||
{
|
||||
$msg = "OK";
|
||||
|
||||
// 当返回false的时候,表示notify中调用NotifyCallBack回调失败获取签名校验失败,此时直接回复失败
|
||||
$input = file_get_contents('php://input');
|
||||
$isxml = true;
|
||||
if (!empty($input) && empty($_GET['out_trade_no']))
|
||||
{
|
||||
$result = WxpayApi::notify(array($this,'NotifyCallBack',), $msg, $input, true, $db);
|
||||
}
|
||||
else
|
||||
{
|
||||
$isxml = false;
|
||||
$result = WxpayApi::notify(array($this,'NotifyCallBack',), $msg, $_GET, false, $db);
|
||||
}
|
||||
|
||||
if ($result == false)
|
||||
{
|
||||
$this->SetReturn_code("FAIL");
|
||||
$this->SetReturn_msg($msg);
|
||||
$this->ReplyNotify(false);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 该分支在成功回调到NotifyCallBack方法,处理完成之后流程
|
||||
$this->SetReturn_code("SUCCESS");
|
||||
$this->SetReturn_msg("OK");
|
||||
}
|
||||
$this->ReplyNotify($needSign, $isxml);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 回调方法入口,子类可重写该方法
|
||||
* 注意:
|
||||
* 1、微信回调超时时间为2s,建议用户使用异步处理流程,确认成功之后立刻回复微信服务器
|
||||
* 2、微信服务器在调用失败或者接到回包为非确认包的时候,会发起重试,需确保你的回调是可以重入
|
||||
* @param array $data 回调解释出的参数
|
||||
* @param string $msg 如果回调处理失败,可以将错误信息输出到该方法
|
||||
* @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
|
||||
*/
|
||||
public function NotifyProcess($data, &$msg)
|
||||
{
|
||||
//TODO 用户基础该类之后需要重写该方法,成功的时候返回true,失败返回false
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* notify回调方法,该方法中需要赋值需要输出的参数,不可重写
|
||||
* @param array $data
|
||||
* @return true回调出来完成不需要继续回调,false回调处理未完成需要继续回调
|
||||
*/
|
||||
final public function NotifyCallBack($data, $db = null)
|
||||
{
|
||||
$msg = "OK";
|
||||
$result = $this->NotifyProcess($data, $msg, $db);
|
||||
if ($result == true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 回复通知
|
||||
* @param bool $needSign 是否需要签名输出
|
||||
*/
|
||||
final private function ReplyNotify($needSign = true, $isXML)
|
||||
{
|
||||
if ($isXML)
|
||||
{
|
||||
//如果需要签名
|
||||
if ($needSign == true &&
|
||||
$this->GetReturn_code($return_code) == "SUCCESS"
|
||||
)
|
||||
{
|
||||
$this->SetSign();
|
||||
}
|
||||
WxpayApi::replyNotify($this->ToXml());
|
||||
}
|
||||
else
|
||||
{
|
||||
exit('success');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user