添加后台代理代码
This commit is contained in:
72
codes/agent/game/api/payment/swiftpass/Utils.class.php
Normal file
72
codes/agent/game/api/payment/swiftpass/Utils.class.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
class Utils{
|
||||
/**
|
||||
* 将数据转为XML
|
||||
*/
|
||||
public static function toXml($array){
|
||||
$xml = '<xml>';
|
||||
forEach($array as $k=>$v){
|
||||
$xml.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
|
||||
}
|
||||
$xml.='</xml>';
|
||||
return $xml;
|
||||
}
|
||||
|
||||
public static function dataRecodes($title,$data){
|
||||
$handler = fopen('result.txt','a+');
|
||||
$content = "================".$title."===================\n";
|
||||
if(is_string($data) === true){
|
||||
$content .= $data."\n";
|
||||
}
|
||||
if(is_array($data) === true){
|
||||
forEach($data as $k=>$v){
|
||||
$content .= "key: ".$k." value: ".$v."\n";
|
||||
}
|
||||
}
|
||||
$flag = fwrite($handler,$content);
|
||||
fclose($handler);
|
||||
return $flag;
|
||||
}
|
||||
|
||||
public static function parseXML($xmlSrc){
|
||||
if(empty($xmlSrc)){
|
||||
return false;
|
||||
}
|
||||
$array = array();
|
||||
$xml = simplexml_load_string($xmlSrc);
|
||||
$encode = Utils::getXmlEncode($xmlSrc);
|
||||
|
||||
if($xml && $xml->children()) {
|
||||
foreach ($xml->children() as $node){
|
||||
//有子节点
|
||||
if($node->children()) {
|
||||
$k = $node->getName();
|
||||
$nodeXml = $node->asXML();
|
||||
$v = substr($nodeXml, strlen($k)+2, strlen($nodeXml)-2*strlen($k)-5);
|
||||
|
||||
} else {
|
||||
$k = $node->getName();
|
||||
$v = (string)$node;
|
||||
}
|
||||
|
||||
if($encode!="" && $encode != "UTF-8") {
|
||||
$k = iconv("UTF-8", $encode, $k);
|
||||
$v = iconv("UTF-8", $encode, $v);
|
||||
}
|
||||
$array[$k] = $v;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
//获取xml编码
|
||||
function getXmlEncode($xml) {
|
||||
$ret = preg_match ("/<?xml[^>]* encoding=\"(.*)\"[^>]* ?>/i", $xml, $arr);
|
||||
if($ret) {
|
||||
return strtoupper ( $arr[1] );
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 后台应答类
|
||||
* ============================================================================
|
||||
* api说明:
|
||||
* getKey()/setKey(),获取/设置密钥
|
||||
* getContent() / setContent(), 获取/设置原始内容
|
||||
* getParameter()/setParameter(),获取/设置参数值
|
||||
* getAllParameters(),获取所有参数
|
||||
* isTenpaySign(),是否威富通签名,true:是 false:否
|
||||
* getDebugInfo(),获取debug信息
|
||||
*
|
||||
* ============================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
class ClientResponseHandler {
|
||||
|
||||
/** 密钥 */
|
||||
var $key;
|
||||
|
||||
/** 应答的参数 */
|
||||
var $parameters;
|
||||
|
||||
/** debug信息 */
|
||||
var $debugInfo;
|
||||
|
||||
//原始内容
|
||||
var $content;
|
||||
|
||||
function __construct() {
|
||||
$this->ClientResponseHandler();
|
||||
}
|
||||
|
||||
function ClientResponseHandler() {
|
||||
$this->key = "";
|
||||
$this->parameters = array();
|
||||
$this->debugInfo = "";
|
||||
$this->content = "";
|
||||
}
|
||||
|
||||
/**
|
||||
*获取密钥
|
||||
*/
|
||||
function getKey() {
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
*设置密钥
|
||||
*/
|
||||
function setKey($key) {
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
//设置原始内容
|
||||
function setContent($content) {
|
||||
$this->content = $content;
|
||||
|
||||
$xml = simplexml_load_string($this->content);
|
||||
$encode = $this->getXmlEncode($this->content);
|
||||
|
||||
if($xml && $xml->children()) {
|
||||
foreach ($xml->children() as $node){
|
||||
//有子节点
|
||||
if($node->children()) {
|
||||
$k = $node->getName();
|
||||
$nodeXml = $node->asXML();
|
||||
$v = substr($nodeXml, strlen($k)+2, strlen($nodeXml)-2*strlen($k)-5);
|
||||
|
||||
} else {
|
||||
$k = $node->getName();
|
||||
$v = (string)$node;
|
||||
}
|
||||
|
||||
if($encode!="" && $encode != "UTF-8") {
|
||||
$k = iconv("UTF-8", $encode, $k);
|
||||
$v = iconv("UTF-8", $encode, $v);
|
||||
}
|
||||
|
||||
$this->setParameter($k, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//获取原始内容
|
||||
function getContent() {
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取参数值
|
||||
*/
|
||||
function getParameter($parameter) {
|
||||
return isset($this->parameters[$parameter])?$this->parameters[$parameter] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
*设置参数值
|
||||
*/
|
||||
function setParameter($parameter, $parameterValue) {
|
||||
$this->parameters[$parameter] = $parameterValue;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取所有请求的参数
|
||||
*@return array
|
||||
*/
|
||||
function getAllParameters() {
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
*是否威富通签名,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
|
||||
*true:是
|
||||
*false:否
|
||||
*/
|
||||
function isTenpaySign() {
|
||||
$signPars = "";
|
||||
ksort($this->parameters);
|
||||
foreach($this->parameters as $k => $v) {
|
||||
if("sign" != $k && "" != $v) {
|
||||
$signPars .= $k . "=" . $v . "&";
|
||||
}
|
||||
}
|
||||
$signPars .= "key=" . $this->getKey();
|
||||
|
||||
$sign = strtolower(md5($signPars));
|
||||
|
||||
$tenpaySign = strtolower($this->getParameter("sign"));
|
||||
|
||||
//debug信息
|
||||
$this->_setDebugInfo($signPars . " => sign:" . $sign .
|
||||
" tenpaySign:" . $this->getParameter("sign"));
|
||||
|
||||
return $sign == $tenpaySign;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*获取debug信息
|
||||
*/
|
||||
function getDebugInfo() {
|
||||
return $this->debugInfo;
|
||||
}
|
||||
|
||||
//获取xml编码
|
||||
function getXmlEncode($xml) {
|
||||
$ret = preg_match ("/<?xml[^>]* encoding=\"(.*)\"[^>]* ?>/i", $xml, $arr);
|
||||
if($ret) {
|
||||
return strtoupper ( $arr[1] );
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*设置debug信息
|
||||
*/
|
||||
function _setDebugInfo($debugInfo) {
|
||||
$this->debugInfo = $debugInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否财付通签名
|
||||
* @param signParameterArray 签名的参数数组
|
||||
* @return boolean
|
||||
*/
|
||||
function _isTenpaySign($signParameterArray) {
|
||||
|
||||
$signPars = "";
|
||||
foreach($signParameterArray as $k) {
|
||||
$v = $this->getParameter($k);
|
||||
if("sign" != $k && "" != $v) {
|
||||
$signPars .= $k . "=" . $v . "&";
|
||||
}
|
||||
}
|
||||
$signPars .= "key=" . $this->getKey();
|
||||
|
||||
$sign = strtolower(md5($signPars));
|
||||
|
||||
$tenpaySign = strtolower($this->getParameter("sign"));
|
||||
|
||||
//debug信息
|
||||
$this->_setDebugInfo($signPars . " => sign:" . $sign .
|
||||
" tenpaySign:" . $this->getParameter("sign"));
|
||||
|
||||
return $sign == $tenpaySign;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* http、https通信类
|
||||
* ============================================================================
|
||||
* api说明:
|
||||
* setReqContent($reqContent),设置请求内容,无论post和get,都用get方式提供
|
||||
* getResContent(), 获取应答内容
|
||||
* setMethod($method),设置请求方法,post或者get
|
||||
* getErrInfo(),获取错误信息
|
||||
* setCertInfo($certFile, $certPasswd, $certType="PEM"),设置证书,双向https时需要使用
|
||||
* setCaInfo($caFile), 设置CA,格式未pem,不设置则不检查
|
||||
* setTimeOut($timeOut), 设置超时时间,单位秒
|
||||
* getResponseCode(), 取返回的http状态码
|
||||
* call(),真正调用接口
|
||||
*
|
||||
* ============================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
class PayHttpClient {
|
||||
//请求内容,无论post和get,都用get方式提供
|
||||
var $reqContent = array();
|
||||
//应答内容
|
||||
var $resContent;
|
||||
|
||||
//错误信息
|
||||
var $errInfo;
|
||||
|
||||
//超时时间
|
||||
var $timeOut;
|
||||
|
||||
//http状态码
|
||||
var $responseCode;
|
||||
|
||||
function __construct() {
|
||||
$this->PayHttpClient();
|
||||
}
|
||||
|
||||
|
||||
function PayHttpClient() {
|
||||
$this->reqContent = "";
|
||||
$this->resContent = "";
|
||||
|
||||
$this->errInfo = "";
|
||||
|
||||
$this->timeOut = 120;
|
||||
|
||||
$this->responseCode = 0;
|
||||
|
||||
}
|
||||
|
||||
//设置请求内容
|
||||
function setReqContent($url,$data) {
|
||||
$this->reqContent['url']=$url;
|
||||
$this->reqContent['data']=$data;
|
||||
}
|
||||
|
||||
//获取结果内容
|
||||
function getResContent() {
|
||||
return $this->resContent;
|
||||
}
|
||||
|
||||
//获取错误信息
|
||||
function getErrInfo() {
|
||||
return $this->errInfo;
|
||||
}
|
||||
|
||||
//设置超时时间,单位秒
|
||||
function setTimeOut($timeOut) {
|
||||
$this->timeOut = $timeOut;
|
||||
}
|
||||
|
||||
//执行http调用
|
||||
function call() {
|
||||
//启动一个CURL会话
|
||||
$ch = curl_init();
|
||||
|
||||
// 设置curl允许执行的最长秒数
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut);
|
||||
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
|
||||
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
|
||||
// 获取的信息以文件流的形式返回,而不是直接输出。
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
|
||||
|
||||
//发送一个常规的POST请求。
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_URL, $this->reqContent['url']);
|
||||
//要传送的所有数据
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->reqContent['data']);
|
||||
|
||||
// 执行操作
|
||||
$res = curl_exec($ch);
|
||||
$this->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
if ($res == NULL) {
|
||||
$this->errInfo = "call http err :" . curl_errno($ch) . " - " . curl_error($ch) ;
|
||||
curl_close($ch);
|
||||
return false;
|
||||
} else if($this->responseCode != "200") {
|
||||
$this->errInfo = "call http err httpcode=" . $this->responseCode ;
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
$this->resContent = $res;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getResponseCode() {
|
||||
return $this->responseCode;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/**
|
||||
* 请求类
|
||||
* ============================================================================
|
||||
* api说明:
|
||||
* init(),初始化函数,默认给一些参数赋值,如cmdno,date等。
|
||||
* getGateURL()/setGateURL(),获取/设置入口地址,不包含参数值
|
||||
* getKey()/setKey(),获取/设置密钥
|
||||
* getParameter()/setParameter(),获取/设置参数值
|
||||
* getAllParameters(),获取所有参数
|
||||
* getRequestURL(),获取带参数的请求URL
|
||||
* getDebugInfo(),获取debug信息
|
||||
*
|
||||
* ============================================================================
|
||||
*
|
||||
*/
|
||||
class RequestHandler {
|
||||
|
||||
/** 网关url地址 */
|
||||
var $gateUrl;
|
||||
|
||||
/** 密钥 */
|
||||
var $key;
|
||||
|
||||
/** 请求的参数 */
|
||||
var $parameters;
|
||||
|
||||
/** debug信息 */
|
||||
var $debugInfo;
|
||||
|
||||
function __construct() {
|
||||
$this->RequestHandler();
|
||||
}
|
||||
|
||||
function RequestHandler() {
|
||||
$this->gateUrl = "https://pay.swiftpass.cn/pay/gateway";
|
||||
$this->key = "";
|
||||
$this->parameters = array();
|
||||
$this->debugInfo = "";
|
||||
}
|
||||
|
||||
/**
|
||||
*初始化函数。
|
||||
*/
|
||||
function init() {
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
/**
|
||||
*获取入口地址,不包含参数值
|
||||
*/
|
||||
function getGateURL() {
|
||||
return $this->gateUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
*设置入口地址,不包含参数值
|
||||
*/
|
||||
function setGateURL($gateUrl) {
|
||||
$this->gateUrl = $gateUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取密钥
|
||||
*/
|
||||
function getKey() {
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
*设置密钥
|
||||
*/
|
||||
function setKey($key) {
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取参数值
|
||||
*/
|
||||
function getParameter($parameter) {
|
||||
return isset($this->parameters[$parameter])?$this->parameters[$parameter]:'';
|
||||
}
|
||||
|
||||
/**
|
||||
*设置参数值
|
||||
*/
|
||||
function setParameter($parameter, $parameterValue) {
|
||||
$this->parameters[$parameter] = $parameterValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次性设置参数
|
||||
*/
|
||||
function setReqParams($post,$filterField=null){
|
||||
if($filterField !== null){
|
||||
forEach($filterField as $k=>$v){
|
||||
unset($post[$v]);
|
||||
}
|
||||
}
|
||||
|
||||
//判断是否存在空值,空值不提交
|
||||
forEach($post as $k=>$v){
|
||||
if(empty($v)){
|
||||
unset($post[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->parameters = $post;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取所有请求的参数
|
||||
*@return array
|
||||
*/
|
||||
function getAllParameters() {
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
*获取带参数的请求URL
|
||||
*/
|
||||
function getRequestURL() {
|
||||
|
||||
$this->createSign();
|
||||
|
||||
$reqPar = "";
|
||||
ksort($this->parameters);
|
||||
foreach($this->parameters as $k => $v) {
|
||||
$reqPar .= $k . "=" . urlencode($v) . "&";
|
||||
}
|
||||
|
||||
//去掉最后一个&
|
||||
$reqPar = substr($reqPar, 0, strlen($reqPar)-1);
|
||||
|
||||
$requestURL = $this->getGateURL() . "?" . $reqPar;
|
||||
|
||||
return $requestURL;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*获取debug信息
|
||||
*/
|
||||
function getDebugInfo() {
|
||||
return $this->debugInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
*创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
|
||||
*/
|
||||
function createSign() {
|
||||
$signPars = "";
|
||||
ksort($this->parameters);
|
||||
foreach($this->parameters as $k => $v) {
|
||||
if("" != $v && "sign" != $k) {
|
||||
$signPars .= $k . "=" . $v . "&";
|
||||
}
|
||||
}
|
||||
$signPars .= "key=" . $this->getKey();
|
||||
$sign = strtoupper(md5($signPars));
|
||||
$this->setParameter("sign", $sign);
|
||||
|
||||
//debug信息
|
||||
$this->_setDebugInfo($signPars . " => sign:" . $sign);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*设置debug信息
|
||||
*/
|
||||
function _setDebugInfo($debugInfo) {
|
||||
$this->debugInfo = $debugInfo;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
14
codes/agent/game/api/payment/swiftpass/config/config.php
Normal file
14
codes/agent/game/api/payment/swiftpass/config/config.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
class Config{
|
||||
private $cfg = array(
|
||||
'url'=>'https://pay.swiftpass.cn/pay/gateway',
|
||||
'mchId'=>'7551000001',
|
||||
'key'=>'9d101c97133837e13dde2d32a5054abb',
|
||||
'version'=>'2.0'
|
||||
);
|
||||
|
||||
public function C($cfgName){
|
||||
return $this->cfg[$cfgName];
|
||||
}
|
||||
}
|
||||
?>
|
||||
385
codes/agent/game/api/payment/swiftpass/request.php
Normal file
385
codes/agent/game/api/payment/swiftpass/request.php
Normal file
@@ -0,0 +1,385 @@
|
||||
<?php
|
||||
/**
|
||||
* 支付接口调测例子
|
||||
* ================================================================
|
||||
* index 进入口,方法中转
|
||||
* submitOrderInfo 提交订单信息
|
||||
* queryOrder 查询订单
|
||||
*
|
||||
* ================================================================
|
||||
*/
|
||||
require('Utils.class.php');
|
||||
require('config/config.php');
|
||||
require('class/RequestHandler.class.php');
|
||||
require('class/ClientResponseHandler.class.php');
|
||||
require('class/PayHttpClient.class.php');
|
||||
|
||||
Class Request
|
||||
{
|
||||
//$url = 'http://192.168.1.185:9000/pay/gateway';
|
||||
/**
|
||||
* @var ClientResponseHandler
|
||||
*/
|
||||
private $resHandler = null;
|
||||
/**
|
||||
* @var RequestHandler
|
||||
*/
|
||||
private $reqHandler = null;
|
||||
/**
|
||||
* @var PayHttpClient
|
||||
*/
|
||||
private $pay = null;
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $cfg = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->Request();
|
||||
}
|
||||
|
||||
public function Request()
|
||||
{
|
||||
$this->resHandler = new ClientResponseHandler();
|
||||
$this->reqHandler = new RequestHandler();
|
||||
$this->pay = new PayHttpClient();
|
||||
$this->cfg = new Config();
|
||||
|
||||
$this->reqHandler->setGateUrl($this->cfg->C('url'));
|
||||
$this->reqHandler->setKey($this->cfg->C('key'));
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$method = isset($_REQUEST['method']) ? $_REQUEST['method'] : 'submitOrderInfo';
|
||||
switch ($method)
|
||||
{
|
||||
case 'submitOrderInfo'://提交订单
|
||||
$this->submitOrderInfo();
|
||||
break;
|
||||
case 'queryOrder'://查询订单
|
||||
$this->queryOrder();
|
||||
break;
|
||||
case 'submitRefund'://提交退款
|
||||
$this->submitRefund();
|
||||
break;
|
||||
case 'queryRefund'://查询退款
|
||||
$this->queryRefund();
|
||||
break;
|
||||
case 'callback':
|
||||
$this->callback();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交订单信息
|
||||
*/
|
||||
public function submitOrderInfo()
|
||||
{
|
||||
$this->reqHandler->setReqParams($_POST, array('method'));
|
||||
$this->reqHandler->setParameter('service', 'pay.weixin.jspay'); /// 接口类型
|
||||
$this->reqHandler->setParameter('mch_id', $this->cfg->C('mchId')); /// 必填项,商户号,由平台分配
|
||||
$this->reqHandler->setParameter('version', $this->cfg->C('version'));
|
||||
$this->reqHandler->setParameter('limit_credit_pay', '1');
|
||||
$this->reqHandler->setParameter('op_user_id', 'yida');
|
||||
//$this->reqHandler->setParameter('appid','wx50bb165fb00ef149');
|
||||
$this->reqHandler->setParameter('op_shop_id', 'yida1');
|
||||
$this->reqHandler->setParameter('op_device_id', 'yida2');
|
||||
$this->reqHandler->setParameter('device_info', 'yida3');
|
||||
//$this->reqHandler->setParameter('time_expire','20170306152200');
|
||||
$this->reqHandler->setParameter('is_raw', '1');
|
||||
/// 通知地址,必填项,接收平台通知的URL,需给绝对路径,255字符内格式如:http://wap.tenpay.com/tenpay.asp
|
||||
//$notify_url = 'http://'.$_SERVER['HTTP_HOST'];
|
||||
//$this->reqHandler->setParameter('notify_url', $notify_url.'/payInterface/request.php?method=callback');
|
||||
$this->reqHandler->setParameter('notify_url', 'http://zhangwei.dev.swiftpass.cn/payInterface_gzzh1/request.php?method=callback');//
|
||||
$this->reqHandler->setParameter('callback_url', 'https://www.baidu.com/');
|
||||
$this->reqHandler->setParameter('nonce_str', mt_rand(time(), time() + rand())); /// 随机字符串,必填项,不长于 32 位
|
||||
$this->reqHandler->setParameter('sub_openid', 'oZ1CSjktOzQRa-q2feBrBXg9VXuM');
|
||||
$this->reqHandler->createSign(); /// 创建签名
|
||||
|
||||
$data = Utils::toXml($this->reqHandler->getAllParameters());
|
||||
//var_dump($data);
|
||||
|
||||
$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
|
||||
if ($this->pay->call())
|
||||
{
|
||||
$this->resHandler->setContent($this->pay->getResContent());
|
||||
$this->resHandler->setKey($this->reqHandler->getKey());
|
||||
if ($this->resHandler->isTenpaySign())
|
||||
{
|
||||
/// 当返回状态与业务结果都为0时才返回,其它结果请查看接口文档
|
||||
if ($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0)
|
||||
{
|
||||
echo json_encode(array(
|
||||
'token_id' => $this->resHandler->getParameter('token_id'),
|
||||
'pay_info' => $this->resHandler->getParameter('pay_info'),
|
||||
));
|
||||
exit();
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Error Code:' . $this->resHandler->getParameter('status') . ' Error Message:' . $this->resHandler->getParameter('message'),
|
||||
));
|
||||
exit();
|
||||
}
|
||||
}
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Error Code:' . $this->resHandler->getParameter('status') . ' Error Message:' . $this->resHandler->getParameter('message'),
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Response Code:' . $this->pay->getResponseCode() . ' Error Info:' . $this->pay->getErrInfo(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询订单
|
||||
*/
|
||||
public function queryOrder()
|
||||
{
|
||||
$this->reqHandler->setReqParams($_POST, array('method'));
|
||||
$reqParam = $this->reqHandler->getAllParameters();
|
||||
if (empty($reqParam['transaction_id']) && empty($reqParam['out_trade_no']))
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => '请输入商户订单号,平台订单号!',
|
||||
));
|
||||
exit();
|
||||
}
|
||||
$this->reqHandler->setParameter('version', $this->cfg->C('version'));
|
||||
$this->reqHandler->setParameter('service', 'unified.trade.query');//接口类型
|
||||
$this->reqHandler->setParameter('mch_id', $this->cfg->C('mchId'));//必填项,商户号,由平台分配
|
||||
$this->reqHandler->setParameter('nonce_str', mt_rand(time(), time() + rand()));//随机字符串,必填项,不长于 32 位
|
||||
$this->reqHandler->createSign();//创建签名
|
||||
$data = Utils::toXml($this->reqHandler->getAllParameters());
|
||||
|
||||
$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
|
||||
if ($this->pay->call())
|
||||
{
|
||||
$this->resHandler->setContent($this->pay->getResContent());
|
||||
$this->resHandler->setKey($this->reqHandler->getKey());
|
||||
if ($this->resHandler->isTenpaySign())
|
||||
{
|
||||
$res = $this->resHandler->getAllParameters();
|
||||
Utils::dataRecodes('查询订单', $res);
|
||||
//支付成功会输出更多参数,详情请查看文档中的7.1.4返回结果
|
||||
echo json_encode(array(
|
||||
'status' => 200,
|
||||
'msg' => '查询订单成功,请查看result.txt文件!',
|
||||
'data' => $res,
|
||||
));
|
||||
exit();
|
||||
}
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Error Code:' . $this->resHandler->getParameter('status') . ' Error Message:' . $this->resHandler->getParameter('message'),
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Response Code:' . $this->pay->getResponseCode() . ' Error Info:' . $this->pay->getErrInfo(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 提交退款
|
||||
*/
|
||||
public function submitRefund()
|
||||
{
|
||||
$this->reqHandler->setReqParams($_POST, array('method'));
|
||||
$reqParam = $this->reqHandler->getAllParameters();
|
||||
if (empty($reqParam['transaction_id']) && empty($reqParam['out_trade_no']))
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => '请输入商户订单号或平台订单号!',
|
||||
));
|
||||
exit();
|
||||
}
|
||||
$this->reqHandler->setParameter('version', $this->cfg->C('version'));
|
||||
$this->reqHandler->setParameter('service', 'unified.trade.refund');//接口类型
|
||||
$this->reqHandler->setParameter('mch_id', $this->cfg->C('mchId'));//必填项,商户号,由平台分配
|
||||
$this->reqHandler->setParameter('nonce_str', mt_rand(time(), time() + rand()));//随机字符串,必填项,不长于 32 位
|
||||
$this->reqHandler->setParameter('op_user_id', $this->cfg->C('mchId'));//必填项,操作员帐号,默认为商户号
|
||||
|
||||
$this->reqHandler->createSign();//创建签名
|
||||
$data = Utils::toXml($this->reqHandler->getAllParameters());//将提交参数转为xml,目前接口参数也只支持XML方式
|
||||
var_dump($data);
|
||||
$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);
|
||||
if ($this->pay->call())
|
||||
{
|
||||
$this->resHandler->setContent($this->pay->getResContent());
|
||||
$this->resHandler->setKey($this->reqHandler->getKey());
|
||||
if ($this->resHandler->isTenpaySign())
|
||||
{
|
||||
//当返回状态与业务结果都为0时才返,其它结果请查看接口文档
|
||||
if ($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0)
|
||||
{
|
||||
/*$res = array('transaction_id'=>$this->resHandler->getParameter('transaction_id'),
|
||||
'out_trade_no'=>$this->resHandler->getParameter('out_trade_no'),
|
||||
'out_refund_no'=>$this->resHandler->getParameter('out_refund_no'),
|
||||
'refund_id'=>$this->resHandler->getParameter('refund_id'),
|
||||
'refund_channel'=>$this->resHandler->getParameter('refund_channel'),
|
||||
'refund_fee'=>$this->resHandler->getParameter('refund_fee'),
|
||||
'coupon_refund_fee'=>$this->resHandler->getParameter('coupon_refund_fee'));*/
|
||||
$res = $this->resHandler->getAllParameters();
|
||||
Utils::dataRecodes('提交退款', $res);
|
||||
echo json_encode(array(
|
||||
'status' => 200,
|
||||
'msg' => '退款成功,请查看result.txt文件!',
|
||||
'data' => $res,
|
||||
));
|
||||
exit();
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Error Code:' . $this->resHandler->getParameter('err_code') . ' Error Message:' . $this->resHandler->getParameter('err_msg'),
|
||||
));
|
||||
exit();
|
||||
}
|
||||
}
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Error Code:' . $this->resHandler->getParameter('status') . ' Error Message:' . $this->resHandler->getParameter('message'),
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Response Code:' . $this->pay->getResponseCode() . ' Error Info:' . $this->pay->getErrInfo(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款
|
||||
*/
|
||||
public function queryRefund()
|
||||
{
|
||||
$this->reqHandler->setReqParams($_POST, array('method'));
|
||||
if (count($this->reqHandler->getAllParameters()) === 0)
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => '请输入商户订单号,平台订单号,商户退款单号,平台退款单号!',
|
||||
));
|
||||
exit();
|
||||
}
|
||||
$this->reqHandler->setParameter('version', $this->cfg->C('version'));
|
||||
$this->reqHandler->setParameter('service', 'unified.trade.refundquery');//接口类型
|
||||
$this->reqHandler->setParameter('mch_id', $this->cfg->C('mchId'));//必填项,商户号,由平台分配
|
||||
$this->reqHandler->setParameter('nonce_str', mt_rand(time(), time() + rand()));//随机字符串,必填项,不长于 32 位
|
||||
|
||||
$this->reqHandler->createSign();//创建签名
|
||||
$data = Utils::toXml($this->reqHandler->getAllParameters());//将提交参数转为xml,目前接口参数也只支持XML方式
|
||||
|
||||
$this->pay->setReqContent($this->reqHandler->getGateURL(), $data);//设置请求地址与请求参数
|
||||
if ($this->pay->call())
|
||||
{
|
||||
$this->resHandler->setContent($this->pay->getResContent());
|
||||
$this->resHandler->setKey($this->reqHandler->getKey());
|
||||
if ($this->resHandler->isTenpaySign())
|
||||
{
|
||||
//当返回状态与业务结果都为0时才返回,其它结果请查看接口文档
|
||||
if ($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0)
|
||||
{
|
||||
/*$res = array('transaction_id'=>$this->resHandler->getParameter('transaction_id'),
|
||||
'out_trade_no'=>$this->resHandler->getParameter('out_trade_no'),
|
||||
'refund_count'=>$this->resHandler->getParameter('refund_count'));
|
||||
for($i=0; $i<$res['refund_count']; $i++){
|
||||
$res['out_refund_no_'.$i] = $this->resHandler->getParameter('out_refund_no_'.$i);
|
||||
$res['refund_id_'.$i] = $this->resHandler->getParameter('refund_id_'.$i);
|
||||
$res['refund_channel_'.$i] = $this->resHandler->getParameter('refund_channel_'.$i);
|
||||
$res['refund_fee_'.$i] = $this->resHandler->getParameter('refund_fee_'.$i);
|
||||
$res['coupon_refund_fee_'.$i] = $this->resHandler->getParameter('coupon_refund_fee_'.$i);
|
||||
$res['refund_status_'.$i] = $this->resHandler->getParameter('refund_status_'.$i);
|
||||
}*/
|
||||
$res = $this->resHandler->getAllParameters();
|
||||
Utils::dataRecodes('查询退款', $res);
|
||||
echo json_encode(array(
|
||||
'status' => 200,
|
||||
'msg' => '查询成功,请查看result.txt文件!',
|
||||
'data' => $res,
|
||||
));
|
||||
exit();
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Error Code:' . $this->resHandler->getParameter('message'),
|
||||
));
|
||||
exit();
|
||||
}
|
||||
}
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => $this->resHandler->getContent(),
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
echo json_encode(array(
|
||||
'status' => 500,
|
||||
'msg' => 'Response Code:' . $this->pay->getResponseCode() . ' Error Info:' . $this->pay->getErrInfo(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台异步回调通知
|
||||
*/
|
||||
public function callback()
|
||||
{
|
||||
$xml = file_get_contents('php://input');
|
||||
file_put_contents('1.txt', $xml);
|
||||
$this->resHandler->setContent($xml);
|
||||
//var_dump($this->resHandler->setContent($xml));
|
||||
$this->resHandler->setKey($this->cfg->C('key'));
|
||||
if ($this->resHandler->isTenpaySign())
|
||||
{
|
||||
if ($this->resHandler->getParameter('status') == 0 && $this->resHandler->getParameter('result_code') == 0)
|
||||
{
|
||||
$tradeno = $this->resHandler->getParameter('out_trade_no');
|
||||
// 此处可以在添加相关处理业务,校验通知参数中的商户订单号out_trade_no和金额total_fee是否和商户业务系统的单号和金额是否一致,一致后方可更新数据库表中的记录。
|
||||
//更改订单状态
|
||||
|
||||
Utils::dataRecodes('接口回调收到通知参数', $this->resHandler->getAllParameters());
|
||||
ob_clean();
|
||||
echo 'success';
|
||||
file_put_contents('2.txt', 1);
|
||||
exit();
|
||||
}
|
||||
else
|
||||
{
|
||||
echo 'failure1';
|
||||
exit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
echo 'failure2';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$req = new Request();
|
||||
$req->index();
|
||||
?>
|
||||
Reference in New Issue
Block a user