You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jy-sdk/Application/Sdk/Controller/ExchangeController.class.php

943 lines
28 KiB
PHTML

2 years ago
<?php
namespace Sdk\Controller;
7 months ago
use Base\Tool\Log;
2 years ago
use Think\Controller;
use Common\Api\GameApi;
use Org\SwiftpassSDK\Swiftpass;
use Org\WeixinSDK\Weixin;
use Org\JtpaySDK\Jtpay;
use Org\GoldPig\GoldPig;
use Base\Tool\Redis;
7 months ago
use Think\Log as ThinkLog;
2 years ago
class ExchangeController extends BaseController{
/**
*ios移动支付
*/
public function exchange(){
C(api('Config/lists'));
#获取SDK上POST方式传过来的数据 然后base64解密 然后将json字符串转化成数组
$request = json_decode(base64_decode(file_get_contents("php://input")),true);
$request = SafeFilter($request);
// $request['server_id'] = 0;
// $request['game_player_id'] = 0;
if ($request['code'] == 1) {
if (empty($request['server_id']) && empty($request['serve_name'])
&& empty($request['game_player_id']) && empty($request['game_player_name'])) {
$this->set_message(0,"fail","未上传角色,不允许充值.");
}
if (!isset($request['server_id']) || !$request['server_id']
|| !isset($request['game_player_id']) || !$request['game_player_id']
) {
// 加入日志表
M('serverid_null_log', 'tab_')->add([
'user_id' => $request['user_id'],
'game_id' => $request['game_id'],
'sdk_version' => $request['sdk_version'],
'request' => json_encode($request),
'create_time' => time()
]);
if (!$request['server_name']) {
$this->set_message(0,"fail","区服名不能为空");
}
if (!$request['game_id']) {
$this->set_message(0,"fail","game_id不存在");
}
$server = M('server', 'tab_')->where([
'game_id' => $request['game_id'],
'server_name' => $request['server_name']
])->find();
$request['server_id'] = $server['server_id'];
if (!isset($request['server_id']) || !$request['server_id']) {
$unionServer = M('server_union', 'tab_')->where([
'game_id' => $request['game_id'],
'server_name' => $request['server_name']
])->find();
$request['server_id'] = $unionServer['server_id'];
}
$userPlayInfo = M('user_play_info', 'tab_')->where([
'user_id' => $request['user_id'],
'game_id' => $request['game_id'],
'server_name' => $request['server_name']
])->find();
if ($userPlayInfo) {
$request['game_player_id'] = $userPlayInfo['role_id'];
}
}
$gameId = $request['game_id'] ?? 0;
$userId = $request['user_id'] ?? 0;
$serverId = $request['server_id'] ?? 0;
$roleId = $request['game_player_id'] ?? 0;
$key = 'sdk.role_cache:' . $gameId . '_' . $userId . '_' . $serverId . '_' . $roleId;
if (!Redis::get($key)) {
$role = M("user_play_info", "tab_")->where([
'user_id' => $request['user_id'],
'game_id' => $request['game_id'],
'server_id' => $request['server_id']
])->find();
if (!$role || !$role['role_id']) {
$this->set_message(0,"fail","未上传角色,不允许充值"/* .M("user_play_info", "tab_")->_sql() */);
}
}
}
$prefix = $request['code'] == 1 ? "SP_" : "PF_";
$out_trade_no = $prefix.date('Ymd').date('His').sp_random_string(4);
if (C('PAY_TEST')) $out_trade_no = $out_trade_no."_test";
$request['pay_order_number'] = $out_trade_no;
$user_entity = get_user_entity($request['user_id']);
$game_name = isset($request['game_name']) ? $request['game_name'] : M('game', 'tab_')->where(['id' => $request['game_id']])->getField('game_name');
// 折扣
$request['discount'] = 10;
$discount = get_discount($request['game_id'], $request['user_id']);
if (isset($discount['discount']) && $discount['discount'] < 10) {
$request['discount'] = $discount['discount'];
}
M("pay_info", "tab_")->add([
'user_id' => $request['user_id'],
'game_id' => $request['game_id'],
'order_id' => $request['pay_order_number'],
'extend' => ($request['code'] == 1) ? $request['extend'] : '',
'price' => $request['price'],
'type' => '', //
'info' => json_encode($request),
'status' => 'normal',
'create_time' => time(),
'user_account' => $user_entity['account'],
'game_player_name' => $request['game_player_name'],
'game_name' => $game_name,
'server_id' => isset($request['server_id']) ? $request['server_id'] : 0, // 旧包平台充值 没传
'server_name' => isset($request['server_name']) ? $request['server_name'] : '',
'promote_id' => $user_entity['promote_id'],
'game_player_id' => isset($request['game_player_id']) ? $request['game_player_id'] : '',
'sdk_version' => isset($request['sdk_version']) ? $request['sdk_version'] : 0,
'promote_account' => $user_entity['promote_account'],
'ip' => get_client_ip(),
]);
$payPage = "pay_way_v1";
7 months ago
Log::write('api_ver: ' . $request['api_ver'], ThinkLog::INFO, 'payment');
7 months ago
Log::write('request: ' . json_encode($request, JSON_UNESCAPED_UNICODE), ThinkLog::INFO, 'payment');
7 months ago
2 years ago
if (isset($request['api_ver']) && $request['api_ver'] >= 2) {
$payPage = "pay_way";
// 用户
$userToken = $request['user_token'];
// M('user', 'tab_')->where(['id' => $request['user_id']])->getField('user_token');
if (!$userToken) {
$this->set_message(0,"fail","请重新登入");
}
}
if(!is_numeric($request['price'])) {
$this->set_message(0,"fail","金额必须是数字");
}
if($request['price']<0) {
$this->set_message(0,"fail","金额不正确");
}
$request['price'] = abs($request['price']);
$request['is_create_ordernumer'] = $request['is_create_ordernumer']?:1; // 1苹果内购 2第三方app支付
if (C ( 'UC_SET' ) == 1) {
if (! is_array ( find_uc_account ( $request ['account'] ) )) {
$this->set_message ( 0, "fail", "Uc用户暂不支持" );
}
}
//添加禁用处理
if($request['code'] == 1){
$user = get_user_entity($request['user_id']);
if($user['lock_status'] == 0 || $user['check_status'] == 0){
$this->set_message(0,"fail","账号被禁用,无法充值,请联系客服!");
}
$game = M('Game','tab_')->where(['id'=>$request['game_id']])->field('pay_status')->find();
if($game['pay_status'] == 0){
$this->set_message(0,"fail","该游戏暂时无法充值,请联系客服!");
}
}
$request['scheme'] = isset($request['scheme']) ? $request['scheme'] : '';
if($request['code'] == 0) { /* 平台币充值 */
// file_put_contents("./Application/Sdk/Scheme/".$request['game_id'].".txt",$request['scheme']);
$prefix = "PF_";
$out_trade_no = $prefix.date('Ymd').date('His').sp_random_string(4);
$request['pay_order_number'] = $out_trade_no;
$request['pay_status'] = 0;
$request['spend_ip'] = get_client_ip();
// $url = C('PAY_DOMAIN') . '/sdk.php?s=/Spend/'.$payPage.'&user_id='.$request['user_id'].'&game_id='.$request['game_id'].'&type=1'.'&user_token='.$userToken.'&action=pay';
// file_put_contents("./Application/Sdk/OrderNo/".$request['user_id']."-".$request['game_id'].".txt",think_encrypt(json_encode($request)));
// $url = C('PAY_DOMAIN') . '/sdk.php?s=/userH/platform_pay/account/'.$request['account'].'&user_token='.$userToken.'&game_id='.$request['game_id'].'&money='.$request['price'].'&action=pay1&api_ver=1';
// echo base64_encode(json_encode(array('status'=>200,'out_trade_no'=>$out_trade_no,'img'=>$url)));
echo base64_encode(json_encode(array('status'=>200,'out_trade_no'=>$out_trade_no,'img'=>C('PAY_DOMAIN').'/sdk.php?s=/Spend/pay_way_v1&user_id='.$request['user_id'].'&game_id='.$request['game_id'].'&type=1'.'&user_token='.$userToken)));exit;
exit;
} else {
$extend_data = M('spend','tab_')->where(array('extend'=>$request['extend'],'pay_status'=>1,'game_id'=>$request['game_id']))->find();
if($extend_data){
$this->set_message(1089,"fail","订单号重复,请关闭支付页面重新支付");
}
// 是否是beta版苹果内购
$isBeta = false;
$isUSBeta = false;
// 白名单用户一定走第三方支付
$isWhite = M('forbit_ip', 'tab_')->where([
'user_id' => $userId,
'type' => 1
])->find();
// 测试白名单用户一定走内购支付
$testWhite = M('forbit_ip', 'tab_')->where([
'user_id' => $userId,
'type' => 2
])->find();
if (isset($request['app_type']) && $request['app_type'] == 2) {
$isBeta = true;
// 美国IP 贝塔版
$ip = get_client_ip();
// todo: 暂时取消美国IP 后面做白名单
$isUSBeta = ip_is_country($ip);
}
$isApplePay = false;
if ($testWhite && $isBeta) {
$isApplePay = true; // 测试白名单+内购包 一定是苹果支付
}
// 美国IP的贝塔包 不管游戏设置怎么设置 都走苹果支付 白名单内的用户一定走第三方支付
if (($isWhite || !$isUSBeta) && !$isApplePay){
$prefix = $request['code'] == 1 ? "SP_" : "PF_"; // SP
$out_trade_no = $prefix.date('Ymd').date('His').sp_random_string(4);
$request['pay_order_number'] = $out_trade_no;
$request['pay_status'] = 0;
$request['spend_ip'] = get_client_ip();
$url = C('PAY_DOMAIN') . '/sdk.php?s=/Spend/'.$payPage.'&user_id='.$request['user_id'].'&game_id='.$request['game_id'].'&type=1'.'&user_token='.$userToken.'&action=pay'.'&extend='.urlencode($request['extend']);
echo base64_encode(json_encode(array('status'=>200,'out_trade_no'=>$out_trade_no,'img'=>$url)));exit;
// 只走第三方支付 不走苹果支付 美国ip TF包走苹果内购
} elseif ($isUSBeta || $isApplePay){ /* 苹果支付 */
#获取订单信息
$prefix = $request['code'] == 1 ? "SP_" : "PF_";
$out_trade_no = $prefix.date('Ymd').date('His').sp_random_string(4);
$data = array("status"=>200,"out_trade_no"=>$out_trade_no,'is_third_app_pay'=>0);
$request['pay_order_number'] = $out_trade_no;
$request['pay_status'] = 0;
$request['pay_way'] = C("PAY_WAY.APPLE_PAY");
// $request['title'] = $request['productId'];
$request['spend_ip'] = get_client_ip();
if($request['code'] == 1 ){
#TODO添加消费记录
$this->add_spend($request);
}else{
#TODO添加平台币充值记录
$this->add_deposit($request);
}
echo base64_encode(json_encode($data));
}elseif(!get_game_appstatus2($request['game_id'])&&$request['is_create_ordernumer']==2){
$data = array("status"=>200,"is_third_app_pay"=>1);
echo base64_encode(json_encode($data));
}
}
}
/**
*支付宝移动支付
*/
public function apple_alipay_pay($user_id,$game_id){
#获取SDK上POST方式传过来的数据 然后base64解密 然后将json字符串转化成数组
if($user_id<1 || $game_id <1) {
$this->set_message(0, "fail", "参数有误");
}
$file=file_get_contents("./Application/Sdk/OrderNo/".$user_id."-".$game_id.".txt");
$request = json_decode(think_decrypt($file),true);
C(api('Config/lists'));
if (empty($request)) {
$this->set_message(0, "fail", "登录数据不能为空");
}
if($request['price']<0){
$this->set_message(0,"fail","充值金额有误");
}
$extend_data = M('spend','tab_')->where(array('extend'=>$request['extend'],'pay_status'=>1,'game_id'=>$request['game_id']))->find();
if($extend_data){
$this->set_message(1089,"fail","订单号重复,请关闭支付页面重新支付");
}
if(get_zfb_type()==0){
$game_set_data = get_game_set_info($request['game_id']);
$request['apitype'] = "alipay";
$request['config'] = "alipay";
$request['signtype']= "MD5";
$request['server'] = "alipay.wap.create.direct.pay.by.user";
$request['payway'] = 1;
$request['title']=$request['price'];
$request['body']=$request['price'];
$pay_url=$this->pay($request);
redirect($pay_url['url']);
}else{
if( empty(C('goldpig.partner'))||empty(C('goldpig.wooolid'))){
$this->set_message(1009, "fail", "支付参数未配置");
}
$table = $request['code'] == 1 ? "spend" : "deposit";
$prefix = $request['code'] == 1 ? "SP_" : "PF_";
// $request['pay_order_number'] =$// $prefix . date('Ymd') . date('His') . sp_random_string(4);
$request['pay_way'] = 8;
$request['pay_status'] = 0;
$request['spend_ip'] = get_client_ip();
//折扣
$user = get_user_entity($request['user_id']);
$discount = $this->get_discount($request['game_id'],$user['promote_id'],$request['user_id']);
$discount = $discount['discount'];
$pay_amount = $discount * $request['price'] / 10;
if($request['code']==1){
$this->add_spend($request);
}else{
$this->add_deposit($request);
}
$goldpig=new GoldPig();
$pay_url = $goldpig->GoldPig($user['account'],$pay_amount,26,$request['pay_order_number']);
if($pay_url['status']==0){
$url='http://'.$_SERVER['HTTP_HOST'];
redirect($url);
}else{
redirect($pay_url['msg']);
}
}
}
private function pay($param=array()){
$table = $param['code'] == 1 ? "spend" : "deposit";
$prefix = $param['code'] == 1 ? "SP_" : "PF_";
$out_trade_no = $prefix.date('Ymd').date('His').sp_random_string(4);
$user = get_user_entity($param['user_id']);
switch ($param['apitype']) {
case 'swiftpass':
$pay = new \Think\Pay($param['apitype'],$param['config']);
break;
default:
$pay = new \Think\Pay($param['apitype'],C($param['config']));
break;
}
$discount = $this->get_discount($param['game_id'],$user['promote_id'],$param['user_id']);
$discount = $discount['discount'];
$vo = new \Think\Pay\PayVo();
$vo->setBody("充值")
->setFee($param['price'])//支付金额
->setTitle($param['title'])
->setOrderNo($out_trade_no)
->setService($param['server'])
->setSignType($param['signtype'])
->setPayMethod("wap")
->setTable($table)
->setPayWay($param['payway'])
->setGameId($param['game_id'])
->setGameName(get_game_name($param['game_id']))
->setGameAppid($param['game_appid'])
->setServerId(0)
->setGameplayerName($param['game_player_name'])
->setServerName($param['server_name'])
->setUserId($param['user_id'])
->setAccount($user['account'])
->setUserNickName($user['nickname'])
->setPromoteId($user['promote_id'])
->setPromoteName($user['promote_account'])
->setExtend($param['extend'])
->setSdkVersion($param['sdk_version'])
->setDiscount($discount);
if($param['is_uc']==1){
return $pay->buildRequestForm($vo,1);
}else{
$pay_['url']= $pay->buildRequestForm($vo);
$pay_['out_trade_no']= $out_trade_no;
return $pay_;
}
}
/**
*微信支付
*/
public function apple_weixin_pay($user_id,$game_id)
{
if($user_id<1 || $game_id <1) {
$this->set_message(0, "fail", "参数有误");
}
$file=file_get_contents("./Application/Sdk/OrderNo/".$user_id."-".$game_id.".txt");
$request = json_decode(think_decrypt($file),true);
if (empty($request)) {
$this->set_message(0, "fail", "登录数据不能为空");
}
C(api('Config/lists'));
if($request['price']<0){
$this->set_message(0,"fail","充值金额有误");
}
$extend_data = M('spend','tab_')->where(array('extend'=>$request['extend'],'game_id'=>$request['game_id']))->find();
if($extend_data){
$this->set_message(1089,"fail","订单号重复,请关闭支付页面重新支付");
}
$table = $request['code'] == 1 ? "spend" : "deposit";
$prefix = $request['code'] == 1 ? "SP_" : "PF_";
$request['pay_way'] = 3;
$request['pay_status'] = 0;
$request['spend_ip'] = get_client_ip();
//折扣
$user = get_user_entity($request['user_id']);
$discount = $this->get_discount($request['game_id'],$user['promote_id'],$request['user_id']);
$discount = $discount['discount'];
$pay_amount = $discount * $request['price'] / 10;
//0 官方 1威富通 2俊付通
if (get_wx_type() == 0) {
$weixn = new Weixin();
$is_pay = json_decode($weixn->weixin_pay("充值", $request['pay_order_number'], $pay_amount, 'MWEB'), true);
if($is_pay['status']==1){
if($request['code']==1){
$this->add_spend($request);
}else{
$this->add_deposit($request);
}
$json_data['url'] = $is_pay['mweb_url'].'&redirect_url='.(is_ssl()?'https%3A%2F%2F':'http%3A%2F%2F'). $_SERVER ['HTTP_HOST'] . "%2Fsdk.php%2FSpend%2Fpay_success2%2Forderno%2F".$request['pay_order_number'].'%2Fgame_id%2F'.$request['game_id'] ;
}else{
$json_data['url'] = "http://" . $_SERVER['HTTP_HOST'];
}
}elseif(get_wx_type() == 2){
if($request['code']==1){
$this->add_spend($request);
}else{
$this->add_deposit($request);
}
$jtpay=new Jtpay();
$json_data['url']=$jtpay->jt_pay($request['pay_order_number'],$pay_amount,$user['account'],get_client_ip(),"sdk",3,'http://' . $_SERVER ['HTTP_HOST'] . "/sdk.php/Spend/pay_success2/orderno/".$request['pay_order_number'].'/game_id/'.$request['game_id'],3,2);//ios
}elseif(get_wx_type() == 3){
if( empty(C('goldpig.partner'))||empty(C('goldpig.wooolid'))){
$this->set_message(1009, "fail", "支付参数未配置");
}
$request['pay_way'] = 8;
if($request['code']==1){
$this->add_spend($request);
}else{
$this->add_deposit($request);
}
$goldpig=new GoldPig();
$res = $goldpig->GoldPig($user['account'],$pay_amount,29,$request['pay_order_number']);
if($res['status']==1){
$json_data['url']=$res['msg'];
}else{
$json_data['url']='http://'.$_SERVER ['HTTP_HOST'];
}
}else{
$Swiftpass=new Swiftpass(C('weixin_gf.partner'),C('weixin_gf.key'));
$param['service']="pay.weixin.wappay";
$param['ip']= $request['spend_ip'];
$param['pay_amount']=$pay_amount;//;
$param['out_trade_no']= $request['pay_order_number'];
$param['game_name']= get_game_name($request['game_id']);
$param['body']="游戏充值";
$param['callback_url']='http://' . $_SERVER ['HTTP_HOST'] . "/sdk.php/Spend/pay_success/orderno/".$request['pay_order_number'].'/game_id/'.$request['game_id'];
$url=$Swiftpass->submitOrderInfo($param);
if($url['status']==0){
$request['pay_way'] = 4;
if($request['code']==1){
$this->add_spend($request);
}else{
$this->add_deposit($request);
}
$json_data['url']=$url['pay_info'];
}else{
$json_data['url']='http://'.$_SERVER ['HTTP_HOST'];
}
}
redirect($json_data['url']);
}
/**
*苹果支付验证
*/
public function exchangeVerify(){
#获取SDK上POST方式传过来的数据 然后base64解密 然后将json字符串转化成数组
$request = json_decode(base64_decode(file_get_contents("php://input")),true);
//开始执行验证
try
{
$data = $this->getSignVeryfy($request, 1);
$info = json_decode($data,true);
if($info['status']==21007){
$data = $this->getSignVeryfy($request, 2);
$info = json_decode($data,true);
}
if($info['status'] == 0){
$paperVerify=M('spend','tab_')->field('id,order_number')->where(array('pay_way'=>7,'order_number'=>$info['receipt']['transaction_id']))->find();
if($paperVerify){
echo base64_encode(json_encode(array("status"=>0,"return_code"=>"fail","return_msg"=>"凭证重复")));
exit();
}
if(!is_numeric($request['price'])) {
echo base64_encode(json_encode(array("status"=>0,"return_code"=>"fail","return_msg"=>"金额必须是数字")));
exit();
}
if($request['price']*1<0) {
echo base64_encode(json_encode(array("status"=>0,"return_code"=>"fail","return_msg"=>"金额不正确")));
exit();
}
$request['price'] = abs($request['price']);
$out_trade_no = $request['out_trade_no'];
$pay_where = substr($out_trade_no,0,2);
$result = 0;
$map['pay_order_number'] = $out_trade_no;
$payamountVerify=M('spend','tab_')->field('*')->where($map)->find();
if($payamountVerify['pay_amount']!=$request['price']){
$disdata=array();
$disdata['spend_id']=$payamountVerify['id'];
$disdata['pay_order_number']=$payamountVerify['pay_order_number'];
$disdata['extend']=$payamountVerify['extend'];
$disdata['last_amount']=$request['price'];
$disdata['currency']=$request['currency'];
$disdata['create_time']=NOW_TIME;
$pay_distinction=M('spend_distinction','tab_')->add($disdata);
if(!$pay_distinction){
\Think\Log::record('数据插入失败 pay_order_number'.$payamountVerify['pay_order_number']);
}
}
$field = array("pay_status"=>0,"pay_amount"=>$request['price'],"receipt"=>$data,"order_number"=>$info['receipt']['transaction_id']);
$field['payed_time'] = time();
switch ($pay_where) {
case 'SP':
$result = M('spend','tab_')->where($map)->setField($field);
if ($result) {
// 通知后台
/* try {
$url = C('TASK_URL')."/game-event/recharge";
post_async($url, [
'form_params' => [
'pay_order_number' => $out_trade_no
],
]);
} catch(\Exception $e) {
} */
}
$param['out_trade_no'] = $out_trade_no;
$isBeta = false;
$isUSBeta = false;
if (isset($request['app_type']) && $request['app_type'] == 2) {
$isBeta = true;
// 美国IP 贝塔版
// $ip = get_client_ip();
// $isUSBeta = ip_is_country($ip);
}
$game = new GameApi();
//判断是否开启苹果内购支付通知
if ($isBeta /* && $isUSBeta */) {
$is_white_list = M('forbit_ip', 'tab_')->where([
'user_id'=>$payamountVerify['user_id'],
'status'=>1,
'type' => 0
])->find();
if ($is_white_list) {
$game->game_pay_notify($param, 1, 0, $payamountVerify);
}
} else {
// $game->game_pay_notify($param);
}
break;
case 'PF':
$result = M('deposit','tab_')->where($map)->setField($field);
break;
case 'AG':
$result = M('agent','tab_')->where($map)->setField($field);
break;
default:
exit('accident order data');
break;
}
if($result){
// $this->set_ratio($out_trade_no);
echo base64_encode(json_encode(array("status"=>200,"return_code"=>"success","return_msg"=>"支付成功")));
exit();
}else{
echo base64_encode(json_encode(array("status"=>0,"return_code"=>"fail","return_msg"=>"支付状态修改失败")));
exit();
}
}else{
echo base64_encode(json_encode(array("status"=>0,"return_code"=>"fail","return_msg"=>"支付失败")));
exit();
}
}
//捕获异常
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
}
private function getSignVeryfy($receipt, $isSandbox = 1){
if ($isSandbox==2) {
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
}
else {
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
}
$postData = json_encode(
array('receipt-data' => $receipt["paper"])
);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加不加会报SSL 错误
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
//判断时候出错,抛出异常
if ($errno != 0) {
throw new \Think\Exception($errmsg, $errno);
}
return $response;
}
}