<?php
/*
* 作用:QQ获取订单号和扣除游戏币支付方式接口.
*/
require_once('../global/common.php');
require_once(DAL_DIR.'charge.class.php');
require_once(DAL_DIR.'user.class.php');
define("QQ_PAY_URL", 'http://msdktest.qq.com/mpay/pay_m'); //扣除游戏币接口(沙箱测试)
// define("QQ_PAY_URL", 'https://openapi.tencentyun.com/mpay/pay_m'); //扣除游戏币接口(现网正式1)
// define("QQ_PAY_URL", 'http://msdk.qq.com/mpay/pay_m'); //扣除游戏币接口(现网正式2)
//定义微信和QQ登陆请求充值接口时的cookie值
define('QQ_SESSION_ID', 'openid');
define('QQ_SESSION_TYPE', 'kp_actoken');
define('WX_SESSION_ID', 'hy_gameid');
define('WX_SESSION_TYPE', 'wc_actoken');
//C1W应用宝QQ和微信登陆支付APPID和APPKEY---------------------------------------------------------------------------------------------
define("C1WYYB_QQ_APPID", '1104311500');
define("C1WYYB_QQ_APPKEY", 'cIUubaAdBNMPQnCX');
//星月应用宝QQ和微信登陆支付APPID和APPKEY---------------------------------------------------------------------------------------------
define("XYYYB_QQ_APPID", '1104445410');
define("XYYYB_QQ_APPKEY", 'gpGCsGwnplcrAoLh');
//外部订单号申请时间
$apply_time = time();
//登录令牌验证
if (!CheckTicket($_REQUEST, $error_code)) {
$return = array(
'code' => $error_code,
'str' => 'fail',
'msg' => '',
);
die(json_encode($return));
}
$ac = empty($_REQUEST['ac']) ? '' : trim($_REQUEST['ac']);
$mode = $_REQUEST['mode'];
switch ($ac) {
case 'apply_exorderno':
$plat_name = dstripslashes(urldecode($_REQUEST['plat_user_name'])); //获取平台账号名
$user_id = $_REQUEST['user_id']; //获取角色ID
$user_name = dstripslashes(urldecode($_REQUEST['user_name'])); //获取角色名
$money = empty($_REQUEST['money']) ? 0 : intval($_REQUEST['money']); //获取角色充值金额
//验证金额
if (empty($money)) { //充值金额出错
$return = array(
'code' => $g_global['client_error']['param_exorderno_money_error'],
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//验证角色合法性(判断角色是否存在)
if (User::is_user_exist($user_name)) {
$tmp = json_decode($_REQUEST['extrainfo'], 1);
$plat_form = $tmp['plat_form'];
$appid = get_appid($mode);
$appkey = get_appkey($mode);
$arr = array(
'openid' => $tmp['openid'],
'openkey' => $tmp['openkey'],
'pf' => $tmp['pf'],
'pfkey' => $tmp['pfkey'],
'pay_token' => ($plat_form == 'weixin') ? '' : $tmp['pay_token'],
'appid' => $appid,
'ts' => time(),
'zoneid' => 1,
'amt' => $tmp['amt'],
);
$session_id = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_ID) : rawurlencode(QQ_SESSION_ID);
$session_type = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_TYPE) : rawurlencode(QQ_SESSION_TYPE);
$org_loc = rawurlencode('/mpay/pay_m');
$cookie ="session_id={$session_id};session_type={$session_type};org_loc={$org_loc}";
$arr['sig'] = makeSig('GET', '/mpay/pay_m', $arr, $appkey .'&');
$str = '';
foreach ($arr as $key => $value) {
$str .= $key.'='.$value.'&';
}
$url = QQ_PAY_URL .'?'.rtrim($str, '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_COOKIE , $cookie );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($curl);
if ( curl_errno($curl) != 0 )
{
$curl_errno = curl_errno($curl);
curl_close($curl);
$return = array(
'code' => $g_global['client_error']['success'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "none","msg"=>"{$curl_errno}"),
);
die(json_encode($return));
}
curl_close($curl);
$responseData = json_decode($result, 1);
//插入到外部订单号表charge_exorderno
$insert_arr['plat_name'] = $plat_name; //平台账号名
$insert_arr['user_id'] = $user_id; //角色ID
$insert_arr['user_name'] = $user_name; //角色名
$insert_arr['mode'] = $mode; //充值方式
$insert_arr['money'] = $money; //充值金额
$insert_arr['apply_time'] = $apply_time; //申请时间
$insert_arr['result'] = 'false'; //结果
$insert_arr['receipt_data'] = json_encode($arr); //请求参数
$insert_arr['ret_result'] = $result; //请求腾讯扣除游戏币结果
$passwd = md5($plat_name.$user_id.$user_name.$money.$apply_time.$g_c['pay']['exorderno_key'], false);
$exorderno_id = Charge::insert_charge_exorderno($insert_arr);
if (empty($exorderno_id)) {
$return = array(
'code' => $g_global['client_error']['insert_charge_exorderno_error'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//构造外部订单号exorderno
$exorderno = $_REQUEST['s_id'].'_'.$exorderno_id.'_'.$passwd;
$update_arr['exorderno'] = $exorderno;
Charge::update_charge_exorderno($update_arr, array('id' => $exorderno_id));
/*游戏币接口*/
if(is_array($responseData) && !empty($responseData) && $responseData['ret'] === 0){
//成功 请求发货
$data['exorderno'] = $data['orderId'] = $exorderno;
$data['money'] = $money / 100;
$data['PlatSign'] = GetSign($data);
$response_data = SendGold($_REQUEST['s_id'], $data);
$msg = json_encode($response_data);
/*
//---记录日志-------------------------------------
$log_arr['exorderno'] = $data['exorderno'];
$log_arr['money'] = $data['money'];
$log_arr['billno'] = $responseData['billno'];
$log_arr['ret'] = $responseData['ret'];
$log_arr['msg'] = $responseData['msg'];
$log_arr['exorderno'] = $data['exorderno'];
$log_arr['my_response'] = $msg;
$content = FileUtil::filelog_format($log_arr);
FileUtil::writeFile(PAY_LOG_DIR,'yyb.log',$content);
//---记录日志-------------------------------------
*/
if ( $response_data === "true" || $response_data === "paynum_exist")
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'success',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}","billno"=>"{$responseData['billno']}","msg"=>"{$msg}"),
);
}else
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "元宝发放失败({$msg})"),
);
}
}
else{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}","msg"=>"{$responseData['msg']}"),
);
}
}
else{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}","msg"=>"user_not_exist"),
);
}
echo json_encode($return);
break;
//申请外部订单号
case 'apply_monthcard':
$plat_name = dstripslashes(urldecode($_REQUEST['plat_user_name'])); //获取平台账号名
$user_id = $_REQUEST['user_id']; //获取角色ID
$user_name = dstripslashes(urldecode($_REQUEST['user_name'])); //获取角色名
$money = empty($_REQUEST['money']) ? 0 : intval($_REQUEST['money']); //获取角色充值金额
//验证金额
if (empty($money)) { //充值金额出错
$return = array(
'code' => $g_global['client_error']['param_exorderno_money_error'],
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//验证角色合法性(判断角色是否存在)
if (User::is_user_exist($user_name)) {
$tmp = json_decode($_REQUEST['extrainfo'], 1);
$plat_form = $tmp['plat_form'];
$appid = get_appid($mode);
$appkey = get_appkey($mode);
$arr = array(
'openid' => $tmp['openid'],
'openkey' => ($plat_form == 'weixin') ? $_REQUEST['openkey'] : $tmp['openkey'],
'pf' => $tmp['pf'],
'pfkey' => $tmp['pfkey'],
'pay_token' => ($plat_form == 'weixin') ? '' : $tmp['pay_token'],
'appid' => $appid,
'ts' => time(),
'zoneid' => 1,
'amt' => $tmp['amt'],
);
$session_id = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_ID) : rawurlencode(QQ_SESSION_ID);
$session_type = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_TYPE) : rawurlencode(QQ_SESSION_TYPE);
$org_loc = rawurlencode('/mpay/pay_m');
$cookie ="session_id={$session_id};session_type={$session_type};org_loc={$org_loc}";
$arr['sig'] = makeSig('GET', '/mpay/pay_m', $arr, $appkey .'&');
$str = '';
foreach ($arr as $key => $value) {
$str .= $key.'='.$value.'&';
}
$url = QQ_PAY_URL .'?'.rtrim($str, '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_COOKIE , $cookie );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($curl);
if ( curl_errno($curl) != 0 )
{
$curl_errno = curl_errno($curl);
curl_close($curl);
$return = array(
'code' => $g_global['client_error']['success'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "none","msg"=>"{$curl_errno}"),
);
die(json_encode($return));
}
curl_close($curl);
$responseData = json_decode($result, 1);
//插入到外部订单号表charge_exorderno
$insert_arr['plat_name'] = $plat_name; //平台账号名
$insert_arr['user_id'] = $user_id; //角色ID
$insert_arr['user_name'] = $user_name; //角色名
$insert_arr['mode'] = $mode; //充值方式
$insert_arr['money'] = $money; //充值金额
$insert_arr['apply_time'] = $apply_time; //申请时间
$insert_arr['result'] = 'false'; //结果
$insert_arr['receipt_data'] = json_encode($arr); //请求参数
$insert_arr['ret_result'] = $result; //请求腾讯扣除游戏币结果
// $passwd = md5($plat_name.$user_id.$user_name.$money.$apply_time.$g_c['pay']['exorderno_key'], false);
$exorderno_id = Charge::insert_charge_exorderno($insert_arr);
if (empty($exorderno_id)) {
$return = array(
'code' => $g_global['client_error']['insert_charge_exorderno_error'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//构造外部订单号exorderno
$package_id = intval($_REQUEST['package_id']);
$exorderno = $_REQUEST['s_id'].'_'.$exorderno_id.'_1_'.$package_id;
$update_arr['exorderno'] = $exorderno;
Charge::update_charge_exorderno($update_arr, array('id' => $exorderno_id));
/*游戏币接口*/
if(is_array($responseData) && !empty($responseData) && $responseData['ret'] === 0){
//成功 请求发货
$data['exorderno'] = $data['orderId'] = $exorderno;
$data['money'] = $money / 100;
$data['PlatSign'] = GetSign($data);
$response_data = SendGold($_REQUEST['s_id'], $data);
if ( $response_data === "true" || $response_data === "paynum_exist")
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'success',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}",'billno' => "{$responseData['billno']}"),
);
}else
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => '元宝发放失败',),
);
}
}else
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}",'msg' => "{$responseData['msg']}"),
);
}
echo json_encode($return);
}
break;
default:
break;
}
//根据请求的mode参数返回对应的appid和appkey
function get_appid($mode){
switch ($mode) {
case 'c1wyyb':
return C1WYYB_QQ_APPID;
case 'xyyyb':
return XYYYB_QQ_APPID;
}
}
function get_appkey($mode){
switch ($mode) {
case 'c1wyyb':
return C1WYYB_QQ_APPKEY;
case 'xyyyb':
return XYYYB_QQ_APPKEY;
}
}
//腾讯签名方法-----------------------------------------------------------------------------------------------------------------
/*内部加密*/
function GetSign($array=array())
{
global $g_c;
if(!empty($array) && is_array($array)) {
ksort($array);
$sign_str='';
foreach($array as $key=>$value){
if(empty($value)){
continue;
}
$sign_str.=$value;
}
$sign_str.=$g_c['game_login_key'];
}
$ticket = md5($sign_str,false);
return $ticket;
}
function SendGold($ServerId,$data=array(),$postName="receive_exorderno")
{
$post_data = json_encode($data);
$sub_url = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], "."));
$post_url = "http://s".$ServerId.$sub_url."/gametool/api/".$postName.".php";
$curl = curl_init($post_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response_data = curl_exec($curl);
if ( curl_errno($curl) != 0 )
{
curl_close($curl);
return "curl_error";
}
curl_close($curl);
return $response_data;
}
/**
* 生成签名
*
* @param string $method 请求方法 "get" or "post"
* @param string $url_path
* @param array $params 表单参数
* @param string $secret 密钥
*/
function makeSig($method, $url_path, $params, $secret)
{
$mk = makeSource($method, $url_path, $params);
$my_sign = hash_hmac("sha1", $mk, strtr($secret, '-_', '+/'), true);
$my_sign = base64_encode($my_sign);
return $my_sign;
}
function makeSource($method, $url_path, $params)
{
$strs = strtoupper($method) . '&' . rawurlencode($url_path) . '&';
ksort($params);
$query_string = array();
foreach ($params as $key => $val )
{
array_push($query_string, $key . '=' . $val);
}
$query_string = join('&', $query_string);
return $strs . str_replace('~', '%7E', rawurlencode($query_string));
}
/**
* 验证回调发货URL的签名 (注意和普通的OpenAPI签名算法不一样,详见@refer的说明)
*
* @param string $method 请求方法 "get" or "post"
* @param string $url_path
* @param array $params 腾讯调用发货回调URL携带的请求参数
* @param string $secret 密钥
* @param string $sig 腾讯调用发货回调URL时传递的签名
*
* @refer
* http://wiki.open.qq.com/wiki/%E5%9B%9E%E8%B0%83%E5%8F%91%E8%B4%A7URL%E7%9A%84%E5%8D%8F%E8%AE%AE%E8%AF%B4%E6%98%8E_V3
*/
function verifySig($method, $url_path, $params, $secret, $sig)
{
unset($params['sig']);
// 先使用专用的编码规则对value编码
foreach ($params as $k => $v)
{
$params[$k] = encodeValue($v);
}
// 再计算签名
$sig_new = makeSig($method, $url_path, $params, $secret);
return $sig_new == $sig;
}
/**
* 回调发货URL专用的编码算法
* 编码规则为:除了 0~9 a~z A~Z !*()之外其他字符按其ASCII码的十六进制加%进行表示,例如"-"编码为"%2D"
* @refer
* http://wiki.open.qq.com/wiki/%E5%9B%9E%E8%B0%83%E5%8F%91%E8%B4%A7URL%E7%9A%84%E5%8D%8F%E8%AE%AE%E8%AF%B4%E6%98%8E_V3
*/
function encodeValue($value)
{
$rst = '';
$len = strlen($value);
for ($i=0; $i<$len; $i++)
{
$c = $value[$i];
if (preg_match ("/[a-zA-Z0-9!\(\)*]{1,1}/", $c))
{
$rst .= $c;
}
else
{
$rst .= ("%" . sprintf("%02X", ord($c)));
}
}
return $rst;
}
?>
/*
* 作用:QQ获取订单号和扣除游戏币支付方式接口.
*/
require_once('../global/common.php');
require_once(DAL_DIR.'charge.class.php');
require_once(DAL_DIR.'user.class.php');
define("QQ_PAY_URL", 'http://msdktest.qq.com/mpay/pay_m'); //扣除游戏币接口(沙箱测试)
// define("QQ_PAY_URL", 'https://openapi.tencentyun.com/mpay/pay_m'); //扣除游戏币接口(现网正式1)
// define("QQ_PAY_URL", 'http://msdk.qq.com/mpay/pay_m'); //扣除游戏币接口(现网正式2)
//定义微信和QQ登陆请求充值接口时的cookie值
define('QQ_SESSION_ID', 'openid');
define('QQ_SESSION_TYPE', 'kp_actoken');
define('WX_SESSION_ID', 'hy_gameid');
define('WX_SESSION_TYPE', 'wc_actoken');
//C1W应用宝QQ和微信登陆支付APPID和APPKEY---------------------------------------------------------------------------------------------
define("C1WYYB_QQ_APPID", '1104311500');
define("C1WYYB_QQ_APPKEY", 'cIUubaAdBNMPQnCX');
//星月应用宝QQ和微信登陆支付APPID和APPKEY---------------------------------------------------------------------------------------------
define("XYYYB_QQ_APPID", '1104445410');
define("XYYYB_QQ_APPKEY", 'gpGCsGwnplcrAoLh');
//外部订单号申请时间
$apply_time = time();
//登录令牌验证
if (!CheckTicket($_REQUEST, $error_code)) {
$return = array(
'code' => $error_code,
'str' => 'fail',
'msg' => '',
);
die(json_encode($return));
}
$ac = empty($_REQUEST['ac']) ? '' : trim($_REQUEST['ac']);
$mode = $_REQUEST['mode'];
switch ($ac) {
case 'apply_exorderno':
$plat_name = dstripslashes(urldecode($_REQUEST['plat_user_name'])); //获取平台账号名
$user_id = $_REQUEST['user_id']; //获取角色ID
$user_name = dstripslashes(urldecode($_REQUEST['user_name'])); //获取角色名
$money = empty($_REQUEST['money']) ? 0 : intval($_REQUEST['money']); //获取角色充值金额
//验证金额
if (empty($money)) { //充值金额出错
$return = array(
'code' => $g_global['client_error']['param_exorderno_money_error'],
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//验证角色合法性(判断角色是否存在)
if (User::is_user_exist($user_name)) {
$tmp = json_decode($_REQUEST['extrainfo'], 1);
$plat_form = $tmp['plat_form'];
$appid = get_appid($mode);
$appkey = get_appkey($mode);
$arr = array(
'openid' => $tmp['openid'],
'openkey' => $tmp['openkey'],
'pf' => $tmp['pf'],
'pfkey' => $tmp['pfkey'],
'pay_token' => ($plat_form == 'weixin') ? '' : $tmp['pay_token'],
'appid' => $appid,
'ts' => time(),
'zoneid' => 1,
'amt' => $tmp['amt'],
);
$session_id = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_ID) : rawurlencode(QQ_SESSION_ID);
$session_type = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_TYPE) : rawurlencode(QQ_SESSION_TYPE);
$org_loc = rawurlencode('/mpay/pay_m');
$cookie ="session_id={$session_id};session_type={$session_type};org_loc={$org_loc}";
$arr['sig'] = makeSig('GET', '/mpay/pay_m', $arr, $appkey .'&');
$str = '';
foreach ($arr as $key => $value) {
$str .= $key.'='.$value.'&';
}
$url = QQ_PAY_URL .'?'.rtrim($str, '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_COOKIE , $cookie );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($curl);
if ( curl_errno($curl) != 0 )
{
$curl_errno = curl_errno($curl);
curl_close($curl);
$return = array(
'code' => $g_global['client_error']['success'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "none","msg"=>"{$curl_errno}"),
);
die(json_encode($return));
}
curl_close($curl);
$responseData = json_decode($result, 1);
//插入到外部订单号表charge_exorderno
$insert_arr['plat_name'] = $plat_name; //平台账号名
$insert_arr['user_id'] = $user_id; //角色ID
$insert_arr['user_name'] = $user_name; //角色名
$insert_arr['mode'] = $mode; //充值方式
$insert_arr['money'] = $money; //充值金额
$insert_arr['apply_time'] = $apply_time; //申请时间
$insert_arr['result'] = 'false'; //结果
$insert_arr['receipt_data'] = json_encode($arr); //请求参数
$insert_arr['ret_result'] = $result; //请求腾讯扣除游戏币结果
$passwd = md5($plat_name.$user_id.$user_name.$money.$apply_time.$g_c['pay']['exorderno_key'], false);
$exorderno_id = Charge::insert_charge_exorderno($insert_arr);
if (empty($exorderno_id)) {
$return = array(
'code' => $g_global['client_error']['insert_charge_exorderno_error'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//构造外部订单号exorderno
$exorderno = $_REQUEST['s_id'].'_'.$exorderno_id.'_'.$passwd;
$update_arr['exorderno'] = $exorderno;
Charge::update_charge_exorderno($update_arr, array('id' => $exorderno_id));
/*游戏币接口*/
if(is_array($responseData) && !empty($responseData) && $responseData['ret'] === 0){
//成功 请求发货
$data['exorderno'] = $data['orderId'] = $exorderno;
$data['money'] = $money / 100;
$data['PlatSign'] = GetSign($data);
$response_data = SendGold($_REQUEST['s_id'], $data);
$msg = json_encode($response_data);
/*
//---记录日志-------------------------------------
$log_arr['exorderno'] = $data['exorderno'];
$log_arr['money'] = $data['money'];
$log_arr['billno'] = $responseData['billno'];
$log_arr['ret'] = $responseData['ret'];
$log_arr['msg'] = $responseData['msg'];
$log_arr['exorderno'] = $data['exorderno'];
$log_arr['my_response'] = $msg;
$content = FileUtil::filelog_format($log_arr);
FileUtil::writeFile(PAY_LOG_DIR,'yyb.log',$content);
//---记录日志-------------------------------------
*/
if ( $response_data === "true" || $response_data === "paynum_exist")
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'success',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}","billno"=>"{$responseData['billno']}","msg"=>"{$msg}"),
);
}else
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "元宝发放失败({$msg})"),
);
}
}
else{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}","msg"=>"{$responseData['msg']}"),
);
}
}
else{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}","msg"=>"user_not_exist"),
);
}
echo json_encode($return);
break;
//申请外部订单号
case 'apply_monthcard':
$plat_name = dstripslashes(urldecode($_REQUEST['plat_user_name'])); //获取平台账号名
$user_id = $_REQUEST['user_id']; //获取角色ID
$user_name = dstripslashes(urldecode($_REQUEST['user_name'])); //获取角色名
$money = empty($_REQUEST['money']) ? 0 : intval($_REQUEST['money']); //获取角色充值金额
//验证金额
if (empty($money)) { //充值金额出错
$return = array(
'code' => $g_global['client_error']['param_exorderno_money_error'],
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//验证角色合法性(判断角色是否存在)
if (User::is_user_exist($user_name)) {
$tmp = json_decode($_REQUEST['extrainfo'], 1);
$plat_form = $tmp['plat_form'];
$appid = get_appid($mode);
$appkey = get_appkey($mode);
$arr = array(
'openid' => $tmp['openid'],
'openkey' => ($plat_form == 'weixin') ? $_REQUEST['openkey'] : $tmp['openkey'],
'pf' => $tmp['pf'],
'pfkey' => $tmp['pfkey'],
'pay_token' => ($plat_form == 'weixin') ? '' : $tmp['pay_token'],
'appid' => $appid,
'ts' => time(),
'zoneid' => 1,
'amt' => $tmp['amt'],
);
$session_id = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_ID) : rawurlencode(QQ_SESSION_ID);
$session_type = ($plat_form == 'weixin') ? rawurlencode(WX_SESSION_TYPE) : rawurlencode(QQ_SESSION_TYPE);
$org_loc = rawurlencode('/mpay/pay_m');
$cookie ="session_id={$session_id};session_type={$session_type};org_loc={$org_loc}";
$arr['sig'] = makeSig('GET', '/mpay/pay_m', $arr, $appkey .'&');
$str = '';
foreach ($arr as $key => $value) {
$str .= $key.'='.$value.'&';
}
$url = QQ_PAY_URL .'?'.rtrim($str, '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_COOKIE , $cookie );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($curl);
if ( curl_errno($curl) != 0 )
{
$curl_errno = curl_errno($curl);
curl_close($curl);
$return = array(
'code' => $g_global['client_error']['success'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "none","msg"=>"{$curl_errno}"),
);
die(json_encode($return));
}
curl_close($curl);
$responseData = json_decode($result, 1);
//插入到外部订单号表charge_exorderno
$insert_arr['plat_name'] = $plat_name; //平台账号名
$insert_arr['user_id'] = $user_id; //角色ID
$insert_arr['user_name'] = $user_name; //角色名
$insert_arr['mode'] = $mode; //充值方式
$insert_arr['money'] = $money; //充值金额
$insert_arr['apply_time'] = $apply_time; //申请时间
$insert_arr['result'] = 'false'; //结果
$insert_arr['receipt_data'] = json_encode($arr); //请求参数
$insert_arr['ret_result'] = $result; //请求腾讯扣除游戏币结果
// $passwd = md5($plat_name.$user_id.$user_name.$money.$apply_time.$g_c['pay']['exorderno_key'], false);
$exorderno_id = Charge::insert_charge_exorderno($insert_arr);
if (empty($exorderno_id)) {
$return = array(
'code' => $g_global['client_error']['insert_charge_exorderno_error'], //插入外部订单号表charge_exorderno出错
'str' => 'error',
'msg' => '',
);
die(json_encode($return));
}
//构造外部订单号exorderno
$package_id = intval($_REQUEST['package_id']);
$exorderno = $_REQUEST['s_id'].'_'.$exorderno_id.'_1_'.$package_id;
$update_arr['exorderno'] = $exorderno;
Charge::update_charge_exorderno($update_arr, array('id' => $exorderno_id));
/*游戏币接口*/
if(is_array($responseData) && !empty($responseData) && $responseData['ret'] === 0){
//成功 请求发货
$data['exorderno'] = $data['orderId'] = $exorderno;
$data['money'] = $money / 100;
$data['PlatSign'] = GetSign($data);
$response_data = SendGold($_REQUEST['s_id'], $data);
if ( $response_data === "true" || $response_data === "paynum_exist")
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'success',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}",'billno' => "{$responseData['billno']}"),
);
}else
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => '元宝发放失败',),
);
}
}else
{
$return = array(
'code' => $g_global['client_error']['success'],
'str' => 'error',
'msg' => array('exorderno' => $exorderno,'url_params' => "{$responseData['ret']}",'msg' => "{$responseData['msg']}"),
);
}
echo json_encode($return);
}
break;
default:
break;
}
//根据请求的mode参数返回对应的appid和appkey
function get_appid($mode){
switch ($mode) {
case 'c1wyyb':
return C1WYYB_QQ_APPID;
case 'xyyyb':
return XYYYB_QQ_APPID;
}
}
function get_appkey($mode){
switch ($mode) {
case 'c1wyyb':
return C1WYYB_QQ_APPKEY;
case 'xyyyb':
return XYYYB_QQ_APPKEY;
}
}
//腾讯签名方法-----------------------------------------------------------------------------------------------------------------
/*内部加密*/
function GetSign($array=array())
{
global $g_c;
if(!empty($array) && is_array($array)) {
ksort($array);
$sign_str='';
foreach($array as $key=>$value){
if(empty($value)){
continue;
}
$sign_str.=$value;
}
$sign_str.=$g_c['game_login_key'];
}
$ticket = md5($sign_str,false);
return $ticket;
}
function SendGold($ServerId,$data=array(),$postName="receive_exorderno")
{
$post_data = json_encode($data);
$sub_url = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], "."));
$post_url = "http://s".$ServerId.$sub_url."/gametool/api/".$postName.".php";
$curl = curl_init($post_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response_data = curl_exec($curl);
if ( curl_errno($curl) != 0 )
{
curl_close($curl);
return "curl_error";
}
curl_close($curl);
return $response_data;
}
/**
* 生成签名
*
* @param string $method 请求方法 "get" or "post"
* @param string $url_path
* @param array $params 表单参数
* @param string $secret 密钥
*/
function makeSig($method, $url_path, $params, $secret)
{
$mk = makeSource($method, $url_path, $params);
$my_sign = hash_hmac("sha1", $mk, strtr($secret, '-_', '+/'), true);
$my_sign = base64_encode($my_sign);
return $my_sign;
}
function makeSource($method, $url_path, $params)
{
$strs = strtoupper($method) . '&' . rawurlencode($url_path) . '&';
ksort($params);
$query_string = array();
foreach ($params as $key => $val )
{
array_push($query_string, $key . '=' . $val);
}
$query_string = join('&', $query_string);
return $strs . str_replace('~', '%7E', rawurlencode($query_string));
}
/**
* 验证回调发货URL的签名 (注意和普通的OpenAPI签名算法不一样,详见@refer的说明)
*
* @param string $method 请求方法 "get" or "post"
* @param string $url_path
* @param array $params 腾讯调用发货回调URL携带的请求参数
* @param string $secret 密钥
* @param string $sig 腾讯调用发货回调URL时传递的签名
*
* @refer
* http://wiki.open.qq.com/wiki/%E5%9B%9E%E8%B0%83%E5%8F%91%E8%B4%A7URL%E7%9A%84%E5%8D%8F%E8%AE%AE%E8%AF%B4%E6%98%8E_V3
*/
function verifySig($method, $url_path, $params, $secret, $sig)
{
unset($params['sig']);
// 先使用专用的编码规则对value编码
foreach ($params as $k => $v)
{
$params[$k] = encodeValue($v);
}
// 再计算签名
$sig_new = makeSig($method, $url_path, $params, $secret);
return $sig_new == $sig;
}
/**
* 回调发货URL专用的编码算法
* 编码规则为:除了 0~9 a~z A~Z !*()之外其他字符按其ASCII码的十六进制加%进行表示,例如"-"编码为"%2D"
* @refer
* http://wiki.open.qq.com/wiki/%E5%9B%9E%E8%B0%83%E5%8F%91%E8%B4%A7URL%E7%9A%84%E5%8D%8F%E8%AE%AE%E8%AF%B4%E6%98%8E_V3
*/
function encodeValue($value)
{
$rst = '';
$len = strlen($value);
for ($i=0; $i<$len; $i++)
{
$c = $value[$i];
if (preg_match ("/[a-zA-Z0-9!\(\)*]{1,1}/", $c))
{
$rst .= $c;
}
else
{
$rst .= ("%" . sprintf("%02X", ord($c)));
}
}
return $rst;
}
?>