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.

601 lines
26 KiB
PHTML

2 years ago
<?php
/**
* 通用支付接口类
* @author yunwuxin<448901948@qq.com>
*/
namespace Think;
use Org\UcenterSDK\Ucservice;
use Base\Service\PromoteService;
class Pay
{
/**
* 支付驱动实例
* @var Object
*/
private $payer;
/**
* 配置参数
* @var type
*/
private $config;
/**
* 支付类型
* @var type
*/
private $apitype;
/**
* 推广员服务
* @var PromoteService
*/
private $promoteService;
/**
* 构造方法,用于构造上传实例
* @param string $driver 要使用的支付驱动
* @param array $config 配置
*/
public function __construct($driver, $config = array())
{
/* 配置 */
$pos = strrpos($driver, '\\');
$pos = $pos === false ? 0 : $pos + 1;
$apitype = strtolower(substr($driver, $pos));
$this->apitype = $apitype;
$this->config['notify_url'] = 'http://' . $_SERVER ['HTTP_HOST'] . "/callback.php/Notify/notify/apitype/" . $apitype . '/method/notify';
$this->config['return_url'] = 'http://' . $_SERVER ['HTTP_HOST'] . "/callback.php/Notify/notify/apitype/" . $apitype . '/method/return/model/'.MODULE_NAME .'/c/'.CONTROLLER_NAME;
$config = array_merge($this->config, $config);
/* 设置支付驱动 */
$class = strpos($driver, '\\') ? $driver : 'Think\\Pay\\Driver\\' . ucfirst(strtolower($driver));
$this->setDriver($class, $config);
$this->promoteService = new PromoteService();
}
public function isKBCPartner($promoteId)
{
$promote = null;
$company = null;
if ($promoteId) {
$promote = M('promote', 'tab_')->field(['company_id', 'id'])->where(['id' => $promoteId])->find();
}
if ($promote) {
$company = M('promote_company', 'tab_')->field(['ali_partner', 'id'])->where(['id' => $promote['company_id']])->find();
}
if ($company && $company['ali_partner'] == 1) {
return true;
}
return false;
}
public function buildRequestForm(Pay\PayVo $vo, $uc = 0, $param=array(), $payConf=array())
{
/* $promoteId = 0;
if ($vo->getPromoteId()) {
$promoteId = $vo->getPromoteId();
} elseif (isset($param['promote_id']) && $param['promote_id']) {
$promoteId = $param['promote_id'];
}
$isKBCPartner = $this->isKBCPartner($promoteId); */
$this->payer->check();
$result = false;
if ($uc == 0) {
switch ($vo->getTable()) {
case 'spend':
$result = $this->add_spend($vo, $param, $payConf);
break;
case 'deposit':
$result = $this->add_deposit($vo, $param, $payConf);
break;
case 'agent':
$result = $this->add_agent($vo);
break;
case 'balance':
$result = $this->add_balance($vo);
break;
case 'RefundRecord':
$result = 1;
break;
case "bind_recharge":
$result = $this->add_bind_recharge($vo);
break;
case 'Withdraw':
$result=1;
break;
case 'order':{
$result = $this->add_order($vo);
};break;
case 'supersign' :
$result=1;
break;
default:
$result = false;
break;
}
if ($result !== false) {//$check !== false
if ($this->apitype == "alipay" && C('alipay.usenew')) {
if (C('alipay.appid')) {
return $this->newbuildRequestForm($vo, $payConf);
} else {
E("appid未设置");
exit('新版appid未填写');
}
} else {
return $this->payer->buildRequestForm($vo);
}
} else {
E(M($vo->getTable(), "tab_")->getDbError());
}
} else {
return $this->payer->buildRequestForm($vo);
}
}
/**
* 新版支付宝接口调用
* */
public function newbuildRequestForm(Pay\PayVo $vo, $payConf=array())
{
if (!$payConf) return false;
Vendor('Alipay.AopSdk');
$aop = new \AopClient();
$aop->appId = $payConf['app_id'];
$aop->signType = 'RSA2';
$aop->rsaPrivateKey = $payConf['private_key'];
$aop->alipayrsaPublicKey = $payConf['public_key'];
$type = $vo->getPayMethod();
//file_put_contents(dirname(__FILE__) . '/newbuildRequestForm.txt',json_encode($vo->getOrderNo()));
switch ($type) {
case 'direct':
$productcode = 'FAST_INSTANT_TRADE_PAY';
$request = new \AlipayTradePagePayRequest();
$request->setReturnUrl('http://' . $_SERVER ['HTTP_HOST'] . '/callback.php/Notify2/notify/apitype/alipay/methodtype/return');
break;
case 'direct_dc':
$productcode = 'FAST_INSTANT_TRADE_PAY';
$request = new \AlipayTradePagePayRequest();
$request->setReturnUrl('http://' . $_SERVER ['HTTP_HOST'] . '/callback.php/Notify2/notify/apitype/alipay/methodtype/return_dc');
break;
case 'direct_tb'://推广平台支付
$productcode = 'FAST_INSTANT_TRADE_PAY';
$request = new \AlipayTradePagePayRequest();
$request->setReturnUrl('http://' . $_SERVER ['HTTP_HOST'] . '/callback.php/Notify2/notify/apitype/alipay/methodtype/return_tb');
break;
case 'wap':
$returnurl = $vo->getCallback();
if (!$returnurl)
$returnurl = 'http://' . $_SERVER ['HTTP_HOST'] . "/sdk.php/Spend/pay_success";
$productcode = 'QUICK_WAP_PAY';
$request = new \AlipayTradeWapPayRequest();
$request->setReturnUrl($returnurl);
break;
case 'trade':
$productcode = 'QUICK_WAP_PAY';
$request = new \AlipayTradeWapPayRequest();
$request->setReturnUrl('http://' . $_SERVER ['HTTP_HOST'] . "/mobile.php/Trade/success");
break;
case 'mobile':
$productcode = 'QUICK_MSECURITY_PAY';
$request = new \AlipayTradeAppPayRequest();
break;
case 'refund':
$request = new \AlipayTradeRefundRequest();
break;
case 'transfer':
$request = new \AlipayFundTransToaccountTransferRequest ();
break;
default:
$productcode = 'FAST_INSTANT_TRADE_PAY';
$request = new \AlipayTradePagePayRequest();
break;
}
$notifyurl = $vo->getNotifyUrl();
if (!$notifyurl)
$notifyurl = 'http://' . $_SERVER ['HTTP_HOST'] . '/callback.php/Notify2/notify/apitype/alipay/methodtype/notify';
$request->setNotifyUrl($notifyurl);
switch ($type) {
case 'direct':
$request->setBizContent('{"product_code":"' . $productcode . '","body":"' . $vo->getBody() . '","subject":"' . $vo->getTitle() . '","total_amount":"' . $vo->getFee() . '","out_trade_no":"' . $vo->getOrderNo() . '"}');
return $aop->pageExecute($request, 'POST');
break;
case 'direct_dc':
$request->setBizContent('{"product_code":"' . $productcode . '","body":"' . $vo->getBody() . '","subject":"' . $vo->getTitle() . '","total_amount":"' . $vo->getFee() . '","out_trade_no":"' . $vo->getOrderNo() . '"}');
return $aop->pageExecute($request, 'POST');
break;
case 'direct_tb':
$request->setBizContent('{"product_code":"' . $productcode . '","body":"' . $vo->getBody() . '","subject":"' . $vo->getTitle() . '","total_amount":"' . $vo->getFee() . '","out_trade_no":"' . $vo->getOrderNo() . '"}');
return $aop->pageExecute($request, 'POST');
break;
case 'wap':
/*参数 out_trade_no系统订单号*/
$request->setBizContent('{"product_code":"' . $productcode . '","body":"' . $vo->getBody() . '","subject":"' . $vo->getTitle() . '","total_amount":"' . $vo->getFee() . '","out_trade_no":"' . $vo->getOrderNo() . '"}');
return $aop->pageExecute($request, 'GET');
break;
case 'trade':
/*参数 out_trade_no系统订单号*/
$request->setBizContent('{"product_code":"' . $productcode . '","body":"' . $vo->getBody() . '","subject":"' . $vo->getTitle() . '","total_amount":"' . $vo->getFee() . '","out_trade_no":"' . $vo->getOrderNo() . '"}');
return $aop->pageExecute($request, 'GET');
break;
case 'mobile':
/*参数 out_trade_no系统订单号*/
$request->setBizContent('{"body":"' . $vo->getBody() . '","subject":"' . $vo->getTitle() . '","out_trade_no":"' . $vo->getOrderNo() . '","timeout_express":"30m","total_amount":"' . $vo->getFee() . '","product_code":"' . $productcode . '"}');
$response = $aop->sdkExecute($request);
$sHtml['arg'] = $response['orderstr'];
$sHtml['sign'] = $response['sign'];
$sHtml['out_trade_no'] = $vo->getOrderNo();
return $sHtml;
break;
case 'refund':
$batch_no = $vo->getBatchNo();
$map['batch_no'] = $batch_no;
$refund = M('refund_record', 'tab_')->where($map)->find();
/*参数 out_trade_no系统订单号 trade_no支付宝订单号 refund_amount退款金额 out_request_no退款请求号 refund_reason退款原因*/
$request->setBizContent('{"out_trade_no":"","trade_no":"' . $vo->getOrderNo() . '","refund_amount":"' . $refund['pay_amount'] . '","out_request_no":"' . $batch_no . '","refund_reason":"调单"}');
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
$date['tui_status'] = 1;
$date['tui_time'] = time();
M('refund_record', 'tab_')->where($map)->save($date);
$map_spend['pay_order_number'] = get_refund_pay_order_number($batch_no);
$spen_date['sub_status'] = 1;
$spen_date['settle_check'] = 1;
M('spend', 'tab_')->where($map_spend)->save($spen_date);
return "10000";//退款成功
} else {
return "退款失败";
}
case 'transfer':
$settlement_number = $vo->getOrderNo();
$WidthdrawNo = $vo->getBatchNo();
$map['settlement_number'] = $settlement_number;
$widthdraw = M('withdraw', 'tab_')->where($map)->find();
$promote = get_promote_entity($widthdraw['promote_id']);
/*参数 out_biz_no打款单号 payee_type账户类型 payee_account支付账号 amount转账金额 remark备注*/
// var_dump('{"out_biz_no":"'.$WidthdrawNo.'","payee_type":"ALIPAY_LOGONID","payee_a1ccount":"'.$promote['alipay_account'].'","amount":"'.$widthdraw['sum_money'].'","payer_show_name":"","payee_real_name":"","remark":"'.$vo->getDetailData().'"}');exit;
$request->setBizContent('{"out_biz_no":"'.$WidthdrawNo.'","payee_type":"ALIPAY_LOGONID","payee_account":"'.$promote['alipay_account'].'","amount":"'.$widthdraw['sum_money'].'","payer_show_name":"","payee_real_name":"","remark":"'.$vo->getDetailData().'"}');
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
$remark = date('Y-m-d H:i:s')." 提现单号:".$WidthdrawNo.", 提现";
if(!empty($resultCode)&&$resultCode == 10000){
$data['status'] = 1;
$data['end_time'] = time();
$data['widthdraw_number'] = $WidthdrawNo;
$data['tx_account'] = $promote['alipay_account'];
$data['respond'] = $remark."成功<br/>".$widthdraw['respond'];
M('withdraw', 'tab_')->where($map)->save($data);
$se_data['ti_status']=1;
M('settlement','tab_')->where($map)->save($se_data);
return "10000";//提现成功
} else {
$msg = $result->$responseNode->sub_msg;
$data['respond'] = $remark."失败:".$msg."<br/>".$widthdraw['respond'];
M('withdraw', 'tab_')->where($map)->save($data);
return $msg;
}
break;
break;
}
}
/**
* 商品订单表添加数据
* @param PayVo $vo
* @author 鹿文学
*/
private function add_order(Pay\PayVo $vo) {
$order=M('order','tab_');
$da = $order->where(['merchandise_id'=>$vo->getParam(),'buyer_id'=>$vo->getBuyerId()])->find();
if(is_array($da)) {
if($da['pay_status']==1) {return false;}
$vo->setOrderNo($da['order_number']);
return $da['id'];
}
$order_data['order_number']=$vo->getOrderNo();
$order_data['buyer_id']=$vo->getBuyerId();
$order_data['buyer_account']=$vo->getBuyerAccount();
$order_data['seller_id']=$vo->getSellerId();
$order_data['seller_account']=$vo->getSellerAccount();
$order_data['seller_account']=$vo->getSellerAccount();
$order_data['order_small_id']=$vo->getSmallId();
$order_data['order_small_account']=$vo->getSmallAccount();
$order_data['merchandise_id']=$vo->getParam();
$order_data['order_price']=$vo->getFee();
$order_data['poundage']=$vo->getPoundage();
$order_data['real_money']=$vo->getMoney();
$order_data['pay_way'] = $vo->getPayWay();
$order_data['order_time'] = NOW_TIME;
$order_data['pay_status'] = 0;
$order_data['game_id'] = $vo->getGameId();
$result = $order->add($order_data);
return $result;
}
public function getPromoteById($promoteId)
{
return $this->promoteService->getPromoteById($promoteId, ['top']);
}
public function getMarketAdminId($promote)
{
return $this->promoteService->getMarketAdminIdByPromote($promote);
}
public function getIsCheckValue($promote, $gameId)
{
if (is_null($promote)) {
return 1;
}
if ($this->promoteService->isGameIdSettle($promote, $gameId)) {
return 1;
} else {
return 2;
}
}
/**
*消费表添加数据
*/
private function add_spend(Pay\PayVo $vo, $param=array(), $payConf = array())
{
$promote = $this->getPromoteById($vo->getPromoteId());
$spend = M("spend", "tab_");
$spend_data['user_id'] = $vo->getUserId();
$spend_data['user_account'] = $vo->getAccount();
$spend_data['user_nickname'] = $vo->getUserNickName();
$spend_data['game_id'] = $vo->getGameId();
$spend_data['game_appid'] = $vo->getGameAppid();
$spend_data['game_name'] = $vo->getGameName();
$spend_data['server_id'] = $vo->getServerId();
$spend_data['server_name'] = $vo->getServerName();
$spend_data['game_player_id'] = $vo->getGameplayerId();
$spend_data['game_player_name'] = $vo->getGameplayerName();
$spend_data['promote_id'] = $vo->getPromoteId();
$spend_data['promote_account'] = $vo->getPromoteName();
$spend_data['order_number'] = "";
$spend_data['selle_ratio'] = getGameSelleRatio($spend_data['promote_id'], $spend_data['game_id']);//$vo->getRatio();
$spend_data['pay_order_number'] = $vo->getOrderNo();
$spend_data['props_name'] = $vo->getTitle();
$spend_data['cost'] = $param['cost'];
/*$price = $vo->getFee(); //获取原价
$discount = 10;
$pay_amount = $price;
if ($param['discount'] < 10) {
$discount = $param['discount'];
$pay_amount = round($discount * $price / 10, 2); //计算折扣后的价格
$spend_data['discount_amount'] = round($price - $pay_amount, 2);
}
$vo->setFee($pay_amount);//构造表单 设置金额为折扣后的价格*/
$spend_data['pay_amount'] = $param['actual_amount'];
$spend_data['pay_way'] = $vo->getPayWay();
$spend_data['pay_time'] = 0;
$spend_data['spend_time'] = NOW_TIME;
$spend_data['pay_status'] = 0;
$spend_data['pay_game_status'] = 0;
$spend_data['extend'] = $vo->getExtend();
$spend_data['spend_ip'] = get_client_ip();
$spend_data['sdk_version'] = $vo->getSdkVersion();
$spend_data['market_admin_id'] = $this->getMarketAdminId($promote);
$spend_data['is_check'] = $this->getIsCheckValue($promote, $vo->getGameId());
/* if ($isKBCPartner) {
$spend_data['partner_type'] = 1;
} */
if($vo->getSmallId() != $vo->getUserId()) {
$spend_data['small_id'] = $vo->getSmallId();
$spend_data['small_account'] = $vo->getSmallAccount();
}
if ($param) {
$spend_data['deduction_amount'] = $param['deduction_amount'];
$spend_data['discount_amount'] = $param['discount_amount'];
}
$spend_data['merchant_id'] = isset($payConf['merchant_id']) ? $payConf['merchant_id'] : 0;
$spend_data['merchant_way'] = isset($payConf['merchant_way']) ? $payConf['merchant_way'] : 0;
$spend_data['partner_type'] = isset($payConf['main_id']) ? $payConf['main_id'] : 0;
$result = $spend->add($spend_data);
return $result;
}
/**
*平台币充值记录
*/
private function add_deposit(Pay\PayVo $vo, $param=array(), $payConf=array())
{
if (!empty($vo->getUc())) {
$uc = new Ucservice();
$uc_user = $uc->get_user_from_uid($vo->getUserId());
$uc_id = $uc->uc_deposit($vo->getUserId(), $vo->getAccount(), $vo->getUserNickName(), $vo->getGameId(), $vo->getGameAppid(), $vo->getGameName(), 0, '', $vo->getPromoteId(), $vo->getPromoteName(), "", $vo->getOrderNo(), $vo->getFee(), time(), $vo->getExtend(), $vo->getPayWay(), get_client_ip(), '', 5, $uc_user['platform'], '', '');
} else {
$promote = $this->getPromoteById($vo->getPromoteId());
$deposit = M("deposit", "tab_");
// $ordercheck = $deposit->where(array('pay_order_number'=>$data["order_no"]))->find();
// if($ordercheck)$this->error("订单已经存在,请刷新充值页面重新下单!");
$deposit_data['order_number'] = "";
$deposit_data['pay_order_number'] = $vo->getOrderNo();
$deposit_data['user_id'] = $vo->getUserId();
$deposit_data['user_account'] = $vo->getAccount();
$deposit_data['user_nickname'] = $vo->getUserNickName();
$deposit_data['promote_id'] = $vo->getPromoteId();
$deposit_data['promote_account'] = $vo->getPromoteName();
$deposit_data['pay_amount'] = $vo->getFee();
$deposit_data['reality_amount'] = $vo->getFee();
$deposit_data['pay_status'] = 0;
$deposit_data['pay_way'] = $vo->getPayWay();
// $deposit_data['pay_source'] = 0;
$deposit_data['pay_ip'] = get_client_ip();
$deposit_data['pay_source'] = 2;
$deposit_data['create_time'] = NOW_TIME;
$deposit_data['sdk_version'] = $vo->getSdkVersion();
$deposit_data['market_admin_id'] = $this->getMarketAdminId($promote);
/* if ($isKBCPartner) {
$deposit_data['partner_type'] = 1;
} */
$deposit_data['merchant_id'] = isset($payConf['merchant_id']) ? $payConf['merchant_id'] : 0;
$deposit_data['merchant_way'] = isset($payConf['merchant_way']) ? $payConf['merchant_way'] : 0;
$deposit_data['partner_type'] = isset($payConf['main_id']) ? $payConf['main_id'] : 0;
$result = $deposit->add($deposit_data);
return $result;
}
}
/**
*添加代充记录 支付宝 /威富通
*/
private function add_agent(Pay\PayVo $vo)
{
$agent = M("agent", "tab_");
$agnet_data['order_number'] = "";
$agnet_data['pay_order_number'] = $vo->getOrderNo();
$agnet_data['game_id'] = $vo->getGameId();
$agnet_data['game_appid'] = $vo->getGameAppid();
$agnet_data['game_name'] = $vo->getGameName();
$agnet_data['promote_id'] = $vo->getPromoteId();
$agnet_data['promote_account'] = $vo->getPromoteName();
$agnet_data['user_id'] = $vo->getUserId();
$agnet_data['user_account'] = $vo->getAccount();
$agnet_data['user_nickname'] = $vo->getUserNickName();
$agnet_data['pay_type'] = 0;//代充 转移
$agnet_data['amount'] = $vo->getMoney();
$agnet_data['real_amount'] = $vo->getFee();
$agnet_data['pay_status'] = 0;
$agnet_data['pay_way'] = $vo->getPayWay();
$agnet_data['create_time'] = time();
$agnet_data['zhekou'] = $vo->getParam();
// $agnet_data['sdk_version'] = $vo->getSdkVersion();
$agent->create($agnet_data);
$result = $agent->add();
return $result;
}
/**
* @函数或方法说明
* @支付宝查询订单信息
* @param $order_no
*
* @author: 郭家屯
* @since: 2019/5/7 9:31
*/
public function alipay_orderquery($order_no,$out_trade_no){
Vendor('Alipay.AopSdk');
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = C('alipay.appid');
$aop->signType = 'RSA2';
$aop->rsaPrivateKey = file_get_contents("./Application/Sdk/SecretKey/alipay/rsa2_private_key.txt");
$aop->alipayrsaPublicKey = file_get_contents("./Application/Sdk/SecretKey/alipay/alipay2_public_key.txt");
$aop->apiVersion = '1.0';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayTradeQueryRequest();
$data['out_trade_no'] = $out_trade_no;
$data['trade_no'] = $order_no;
$request->setBizContent(json_encode($data));
$result = $aop->execute ($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
return $result->$responseNode->total_amount;
} else {
return $result->$responseNode->sub_msg;
}
}
/**
* 绑币充值记录
* @param Pay\PayVo $vo
* @return mixed
* author: xmy 280564871@qq.com
*/
private function add_bind_recharge(Pay\PayVo $vo)
{
$agent = M("bind_recharge", "tab_");
$agnet_data['order_number'] = "";
$agnet_data['pay_order_number'] = $vo->getOrderNo();
$agnet_data['game_id'] = $vo->getGameId();
$agnet_data['game_appid'] = $vo->getGameAppid();
$agnet_data['game_name'] = $vo->getGameName();
$agnet_data['promote_id'] = $vo->getPromoteId();
$agnet_data['promote_account'] = $vo->getPromoteName();
$agnet_data['user_id'] = $vo->getUserId();
$agnet_data['user_account'] = $vo->getAccount();
$agnet_data['user_nickname'] = $vo->getUserNickName();
$agnet_data['pay_type'] = 0;
$agnet_data['amount'] = $vo->getMoney();
$agnet_data['real_amount'] = $vo->getFee();
$agnet_data['pay_status'] = 0;
$agnet_data['pay_way'] = $vo->getPayWay();
$agnet_data['create_time'] = time();
$agnet_data['zhekou'] = $vo->getDiscount();
$agnet_data['recharge_ip'] = get_client_ip();
$agent->create($agnet_data);
$result = $agent->add();
return $result;
}
private function add_balance(Pay\PayVo $vo) {
$balance = M("balance", "tab_");
$balance_data['order_number'] = "";
$balance_data['pay_order_number'] = $vo->getOrderNo();
$balance_data['promote_id'] = $vo->getPromoteId();
$balance_data['promote_account'] = $vo->getPromoteName();
$balance_data['recharge_id'] = $vo->getUserId();
$balance_data['recharge_account'] = $vo->getAccount();
$balance_data['balance'] = $vo->getMoney();
$balance_data['money'] = $vo->getFee();
$balance_data['pay_status'] = 0;
$balance_data['recharge_type'] = $vo->getPayWay();
$balance_data['create_time'] = time();
$balance->create($balance_data);
$result = $balance->add();
return $result;
}
/**
* 设置支付驱动
* @param string $class 驱动类名称
*/
private function setDriver($class, $config)
{
$this->payer = new $class($config);
if (!$this->payer) {
E("不存在支付驱动:{$class}");
}
}
public function __call($method, $arguments)
{
if (method_exists($this, $method)) {
return call_user_func_array(array(&$this, $method), $arguments);
} elseif (!empty($this->payer) && $this->payer instanceof Pay\Pay && method_exists($this->payer, $method)) {
return call_user_func_array(array(&$this->payer, $method), $arguments);
}
}
}