REAPI/app/merchant/service/MerchantService.php

1217 lines
44 KiB
PHP

<?php
namespace app\merchant\service;
use app\core\BtWafService;
use app\core\Service;
use app\merchant\service\OrderService;
use app\order\service\Kami91OrderService;
use think\admin\extend\CodeExtend;
use app\gateway\service\RedisService;
use app\merchant\service\MerchantLogService;
use app\merchant\service\PercentService;
//use app\channel\service\PercentService as ChannelPercent;
use app\merchant\service\ProjectPercentService;
//use app\merchant\service\OrderService;
use app\channel\service\ProductService;
use dever\Log;
use think\event\LogWrite;
/**
* 商户服务
* Class MerchantService
* @package app\merchant\service
*/
class MerchantService extends Service
{
/**
* 设置默认操作表
* @var string
*/
public $table = 'merchant_list';
/**
* 重置商户秘钥
* @param $mid 商户id
*/
public function resetApi($mid)
{
$data['appid'] = CodeExtend::random(15,1,'app');
$data['appsecret'] = strtoupper(CodeExtend::random(32,3));
return $data;
}
# 获取真实的折扣
public function getPercent($percent, $account, $product_key, $key, $rule, $isp = -1)
{
if ($product_key == 'dhcz') {
# 验证运营商
if ($isp && $isp > 0) {
} else {
$isp = $this->isp($account);
}
$key = $isp;
}
if ($rule) {
$rule = json_decode($rule, true);
$key1 = intval($key);
if (isset($rule[$key]) && $rule[$key]) {
$percent = $rule[$key];
} elseif (isset($rule[$key1]) && $rule[$key1]) {
$percent = $rule[$key1];
}
}
return $percent;
}
# 计算实际的消费
private function cash($percent, $cash, $mid, $pid, $product_key, $project_id, $account = '', $isp = -1)
{
if ($project_id) {
$info = ProjectPercentService::instance()->get($mid, $project_id, $product_key);
} else {
$info = PercentService::instance()->get($mid, $product_key);
}
$percent_type = 1;
if ($info && $info['percent']) {
$percent = $info['percent'];
if (isset($info['percent_type']) && $info['percent_type']) {
$percent_type = $info['percent_type'];
}
$percent = $this->getPercent($percent, $account, $product_key, $cash, $info['rule'], $isp);
}
$new_cash = $cash;
$product_cash = $profit = 0;
if ($percent > 0) {
if ($percent_type == 2) {
$new_cash = $percent;
} else {
$new_cash = $percent*$cash;
}
}
Log::write('test', 'test', $new_cash);
# 得到产品分成
if ($pid) {
$product = ProductService::instance()->get($mid, $pid);
if ($product) {
if ($product['rule']) {
$product['percent'] = $this->getPercent($product['percent'], $account, $product_key, $cash, $product['rule'], $isp);
}
$product_percent_type = 1;
if (isset($product['percent_type']) && $product['percent_type']) {
$product_percent_type = $product['percent_type'];
}
if ($product_percent_type == 2) {
$product_cash = $product['percent'];
} else {
$product_cash = $product['percent']*$cash;
}
$profit = $new_cash - $product_cash;
}
}
$no_profit_merchant = sysconf('no_profit_merchant');
if ($no_profit_merchant) {
$no_profit_merchant = explode(',', $no_profit_merchant);
if (in_array($mid, $no_profit_merchant)) {
$new_cash = $product_cash;
$profit = 0;
}
}
return array($cash, $new_cash, $product_cash, $profit);
}
# 扣费 冻结 冻结已去掉
public function setFrozenAccount($oid, $order_id, $mid, $pid, $product_key, $cash, $project_id = false, $account = '', $isp = -1, $time = false, $fushu = false)
{
$info = $this->getInfo($mid);
if ($info) {
list($cash, $new_cash, $product_cash, $profit) = $this->cash($info['percent'], $cash, $mid, $pid, $product_key, $project_id, $account, $isp);
# 先扣余额
$info = $this->getInfo($mid);
if ($info['account_surplus'] >= $new_cash) {
$account_type = 1;
} elseif ($info['credit_surplus'] >= $new_cash) {
$account_type = 2;
} elseif (!$fushu) {
return false;
}
if ($product_key == 'dxdc' || $product_key == 'xtdc') {
$name = '代充';
} elseif (str_contains($product_key, '_cardbuy') || $product_key == 'cardbuy') {
$name = '采购';
} else {
$name = '充值';
}
MerchantLogService::instance()->add($mid, $new_cash, $account_type, 3, $name . '业务余额扣除', 1, $order_id, $product_cash, $profit, $pid, true, $time, $time);
//$this->get($info['appid'], false);
//$this->get($info['id'], false);
return array($new_cash, $product_cash, $account_type);
}
}
# 扣费
public function setAccount($oid, $order_id, $mid, $pid, $product_key, $cash, $project_id = false)
{
$order = OrderService::instance()->get($oid);
if (!$order) {
return false;
}
if ($order && $order['error_account_oper'] == 2) {
return false;
}
if ($order['actual_cash'] <= 0) {
/*
$state = $this->setFrozenAccount($oid, $order_id, $mid, $pid, $product_key, $cash, $project_id, $account, $isp);
if ($state) {
$update = array();
$update['actual_cash'] = $state[0];
$update['product_cash'] = $state[1];
OrderService::instance()->where(array('id' => $order['id']))->update($update);
}
*/
}
OrderService::instance()->upError($oid);
# 没有冻结了
return;
/*
$info = $this->getInfo($mid);
if ($info) {
$where['id'] = $mid;
$update['account_frozen'] = $info['account_frozen'] - $cash;
$update['account_consum'] = $info['account_consum'] + $cash;
$this->db()->where($where)->update($update);
$this->get($info['appid'], false);
$this->get($info['id'], false);
}
*/
}
# 恢复冻结的费用
public function resFrozenAccount($oid, $order_id, $mid, $pid, $product_key, $cash, $project_id = false, $account = '', $isp = -1)
{
if ($product_key == 'dxdc') {
# 短信代充不恢复扣费
//return false;
}
$info = $this->getInfo($mid);
if ($info) {
if (is_array($oid)) {
$order = $oid;
} else {
$order = OrderService::instance()->get($oid);
if (!$order) {
return false;
}
if ($order && $order['error_account_oper'] == 2) {
return false;
}
if ($order['actual_cash'] <= 0) {
//return false;
}
OrderService::instance()->upError($oid);
}
if (isset($order['order_id'])) {
$order_id = $order['order_id'];
}
$account_type = MerchantLogService::instance()->getAccountType($mid, $order_id);
/*
if (isset($order['account_type']) && $order['account_type']) {
$account_type = $order['account_type'];
} else {
$account_type = MerchantLogService::instance()->getAccountType($mid, $order_id);
}
*/
if (!$account_type) {
return false;
}
list($cash, $new_cash, $product_cash, $profit) = $this->cash($info['percent'], $cash, $mid, $pid, $product_key, $project_id, $account, $isp);
MerchantLogService::instance()->add($mid, $new_cash,$account_type, 4, '充值业务失败增加余额', 1, $order_id, $product_cash, $profit, $pid, true, $order['create_at'], $order['create_at']);
//$this->get($info['appid'], false);
//$this->get($info['id'], false);
return $new_cash;
}
return false;
}
# 获取商户信息
public function getInfo($appid)
{
if (str_contains($appid, 'PT_')) {
$where['agentId'] = str_replace('PT_', '', $appid);
} elseif (is_numeric($appid)) {
$where['id'] = $appid;
} else {
$where['appid'] = $appid;
}
//$where['status'] = 1;
//$where['is_deleted'] = 0;
$data = $this->db()->where($where)->find();
if ($data && isset($data['account_baitiao']) && $data['account_baitiao'] > 0) {
$data['account_surplus'] += $data['account_baitiao'];
}
return $data;
}
#获取商户类型
public function getTypeInfo($TypeId,$other_status = '-1')
{
$where['merchant_type'] = $TypeId;
$where['status'] = 1;
if($other_status != '-1'){
$where['other_status'] = $other_status;
}
//$where['is_deleted'] = 0;
$data = $this->db()->where($where)->select()->toArray();
if ($data && isset($data['account_baitiao']) && $data['account_baitiao'] > 0) {
$data['account_surplus'] += $data['account_baitiao'];
}
return $data;
}
# 获取缓存
public function get($key, $get = true)
{
if (!$get) {
return false;
}
$cache_key = 'merchantv2_' . $key;
$data = !$get ? false : $this->cache($cache_key);
if (!$data) {
$data = $this->getInfo($key);
if ($data) {
$this->cache($cache_key, $data);
}
}
if ($data) {
if ($data['status'] != 1) {
return false;
}
if ($data['is_deleted'] != 0) {
return false;
}
}
return $data;
}
# 商户数据更新
/*
public function up($oid, $mid, $pid, $product_key, $result)
{
$this->upAction(array($oid, $mid, $pid, $product_key, $result));
//$this->commit(array($this, 'upAction'), array($oid, $mid, $pid, $product_key, $result));
}
*/
# 商户数据更新
public function up($oid, $mid, $pid, $product_key, $result)
{
if (!isset($result['project_id'])) {
$result['project_id'] = false;
}
if (!isset($result['isp'])) {
$result['isp'] = -1;
}
//list($oid, $mid, $pid, $product_key, $result) = $data;
if (is_array($mid)) {
$merchant = $mid;
} else {
$merchant = $this->get($mid);
}
if (!$merchant) {
return;
}
$mid = $merchant['id'];
$notify = false;
if ($result['status'] == 2) {
# 成功,真实扣费,发通知
$notify = true;
$this->setAccount($oid, $result['order_id'], $mid, $pid, $product_key, $result['cash'], $result['project_id'], $result['account'], $result['isp']);
if (isset($result['card_id']) && $result['card_id']) {
$card = \app\merchant\service\CardService::instance()->getData($result['card_id'], 7);
if ($card) {
foreach ($card as $k => $v) {
if ($v['order_id'] != $result['order_id']) {
# 前几张失败
\app\order\service\OrderService::instance()->setCallback(1, $v['order_id'], 3);
}
}
}
}
/*
$log['type'] = 'account_success';
$log['data'] = $result;
$log['msg'] = '扣费';
$this->log($log);
*/
} elseif ($result['status'] == 3) {
# 失败,恢复扣费,发通知
$notify = true;
$state = $this->resFrozenAccount($oid, $result['order_id'], $mid, $pid, $product_key, $result['cash'], $result['project_id'], $result['account'], $result['isp']);
if ($state) {
$log['type'] = 'account_fail';
$log['data'] = $result;
$log['msg'] = '恢复扣费';
$this->log($log);
}
} elseif ($result['status'] == 4) {
# 处理中
//$notify = true;
} elseif ($result['status'] == 5) {
//$result['status'] == 4;
# 存疑 只有短信代充和系统代充才发回调
if ($product_key == 'dxdc' || $product_key == 'xtdc') {
# 无需恢复扣费
OrderService::instance()->upError($oid);
$notify = true;
}
} elseif ($result['status'] == 7) {
# 提卡成功待处理
//$notify = true;
}
if ($notify) {
$this->notify($oid, $merchant, $pid, $product_key, $result);
}
}
# 发送通知
public function notify($oid, $merchant, $pid, $product_key, $result, $type = 1, $limit = true)
{
$notify = false;
if (isset($result['status']) && $result['status']) {
if ($result['status'] == 2 || $result['status'] == 3) {
$notify = true;
}
if ($result['status'] == 5 && ($product_key == 'dxdc' || $product_key == 'xtdc')) {
$notify = true;
}
}
if (!$notify) {
return;
}
# 如果是子订单 就要拆分处理
/*
if ($result['order_id'] && strstr($result['order_id'], '_')) {
$temp = explode('_', $result['order_id']);
$parent_order = $temp[0];
# 获取所有子订单的状态
$order = OrderService::instance()->getChildOrder($parent_order);
$total = count($order);
$error = $success = $handle = 0;
$s_nubmer = array();
if ($order) {
foreach ($order as $k => $v) {
if ($v['status'] == 4) {
$handle += 1;
} elseif ($v['status'] == 3) {
$error += 1;
} elseif ($v['status'] == 2) {
$success += 1;
}
if ($v['channel_callback_msg']) {
$msg = json_decode($v['channel_callback_msg'], true);
if (isset($msg['s_nubmer']) && $msg['s_nubmer']) {
$s_nubmer[] = $msg['s_nubmer'];
}
}
}
# 如果有一个还在处理中,就依然是处理中
$result['order_id'] = $parent_order;
if ($handle > 0) {
$result['status'] = 4;
} elseif ($success >= $total) {
# 全部成功,直接发送成功即可
$result['status'] = 2;
} elseif ($success < $total && $success >= 1) {
# 部分成功,也发送成功吧?
$result['status'] = 2;
} else {
# 其他情况就是失败
$result['status'] = 3;
}
if ($s_nubmer) {
$result['s_nubmer'] = implode(';', $s_nubmer);
} else {
$result['s_nubmer'] = '';
}
}
print_r($result);die;
}
*/
if (is_numeric($merchant)) {
$merchant = $this->get($merchant);
}
if (isset($result['notify']) && $result['notify']) {
$url = $result['notify'];
$url = urldecode($url);
} else {
$url = $merchant['notify_url'];
if (strstr($url, '|')) {
$temp = explode('|', $url);
$url = $temp[0];
if (strstr($product_key, 'zsycz')) {
$url = $temp[1];
}
}
}
# 最大通知次数
if ($limit) {
$num = 5;
} else {
$num = 500;
}
if ($url && $result['notify_num'] < $num) {
$param['appid'] = $merchant['appid'];
$param['order_id'] = $result['order_id'];
$param['merchant_order_id'] = $result['merchant_order_id'];
$param['status'] = $result['status'];
$param['cash'] = $result['cash'];
if(isset($merchant['api_version']) &&$merchant['api_version']){
$param['version'] = $merchant['api_version'];
if($param['version'] == 'v3'){
unset($param['version']);
}
}elseif(sysconf('api_version')){
$param['version'] = sysconf('api_version');
}
$number = '';
if (isset($result['s_nubmer']) && $result['s_nubmer']) {
$number = $result['s_nubmer'];
} elseif (isset($result['s_number']) && $result['s_number']) {
$number = $result['s_number'];
}
if ($merchant['id'] == 48) {
$number = '';
if (isset($result['msg'])) {
unset($result['msg']);
}
}
$order_name = sysconf('order_name');
if ($number && $order_name) {
$param[$order_name] = $number;
} elseif ($number) {
$param['s_nubmer'] = $number;
}
if (isset($result['code']) && $result['code']) {
//$param['code'] = $result['code'];
}
if (isset($result['msg']) && $result['msg'] && is_string($result['msg'])) {
$param['msg'] = $result['msg'];
} else {
/*
$array = array('提单超限','卡号错误','已无库存','系统超时');
$key = mt_rand(0, count($array) - 1);
if (isset($array[$key])) {
$param['msg'] = $array[$key];
}
*/
}
if ($result['status'] == 2) {
$cash = $result['cash'];
list($cash, $new_cash, $product_cash, $profit) = $this->cash($merchant['percent'], $cash, $merchant['id'], false, $product_key, $result['project_id']);
$cash = $new_cash;
} else {
$cash = 0;
}
if ($cash > 0) {
$param['actual_cash'] = $cash;
}
$sign_type = sysconf('sign_type');
if ($sign_type == 2) {
$token = $merchant['appsecret'];
} else {
$token = $merchant['appid'] . '|' . $merchant['appsecret'];
}
if($merchant['kami_status'] == 1 && $merchant['merchant_type'] == 1 && $param['status'] == 2){
$ksorderService = OrderService::instance();
$ksdata = $ksorderService->get('', $result['merchant_order_id'], $merchant['id']);
if(isset($ksdata['response']) && $ksdata['response']){
$responseK = json_decode($ksdata['response'],true);
if(isset($responseK['kami'])) {
$cardno = $responseK['kami']['cardno'];
$cardpwd = $responseK['kami']['cardpwd'];
$expire_time= $responseK['kami']['expired'];
$kami = [
'cardno' =>$cardno,
'cardpwd' =>$cardpwd,
'expire_time' =>$expire_time,
];
if($merchant['id'] != 7){
$kami=implode(',',$kami);
}
}elseif(isset($ksdata['channel_callback_msg']) && str_contains($ksdata['channel_callback_msg'], "kami")) {
$msgK = json_decode($ksdata['channel_callback_msg'], true);
$cardno = $msgK['kami']['cardno'];
$cardpwd = $msgK['kami']['cardpwd'];
$expire_time = $msgK['kami']['expired'];
$kami = [
'cardno' => $cardno,
'cardpwd' => $cardpwd,
'expire_time' => $expire_time,
];
if($merchant['id'] != 7){
$kami=implode(',',$kami);
}
}
if($merchant['id'] != 7){
$seacret=strtolower(md5(md5($token).$param['order_id']));
// $v=substr($seacret, 0,strlen($seacret)/2);
$str=openssl_encrypt($kami, 'DES-EDE3', $seacret, OPENSSL_RAW_DATA);
$param['card'] = base64_encode($str);
}else{
$param['card'] = json_encode($kami);
}
}
}
$param = $this->getParam($param, $token, $sign_type);
switch ($merchant['merchant_type'])
{
case "5":
#分析是否为快手店铺
$param = array();
$orderkshd = '';
$ksorderService = OrderService::instance();
$ksdata = $ksorderService->get($orderkshd, $result['merchant_order_id'], $merchant['id']);
$param['orderId'] = $result['merchant_order_id'];
$param['mobile'] = $result['account'];
$tdtime = strtotime($ksdata['create_at']);
$tdtime = date('YmdHis',$tdtime);
$tdtime = substr($tdtime,2);
\dever\Log::write('gateway', 'test1', $ksdata);
#快手订单状态转换
if($result['status'] == 2){
#成功
$param['status'] = 'SUCCESS';
$param['bizType'] = '20';
} elseif ($result['status'] == 3) {
# 失败...
$param['status'] = 'FAILED';
$param['bizType'] = '20';
$param['failCode'] = '4013024';
$param['failMsg'] = '上游通道充值失败';
} else {
# 其他终止...
return;
}
if (substr($merchant['agentId'], -3) === "_KC") {
$param['bizType'] = '10';
$merchant['agentId'] = substr($merchant['agentId'], 0, -3);
}
$param = json_encode($param);
$signtime = time();
$sign = 'access_token='.$merchant['access_token'].'&appkey='.$merchant['agentId'].'&method=integration.virtual.topup.mobile.order.callback&param='.$param.'&signMethod=MD5&timestamp='.$signtime.'&version=1';
$signmd5 = md5($sign);
$param = urlencode($param);
$geturl = '?access_token='.$merchant['access_token'].'&appkey='.$merchant['agentId'].'&method=integration.virtual.topup.mobile.order.callback&param='.$param.'&signMethod=MD5&timestamp='.$signtime.'&version=1&sign='.$signmd5;
break;
case "7":
//
if($result['status'] == 2){
#成功
$orderStatus = '9';
$rsp_info = '做单成功';
} elseif ($result['status'] == 3) {
# 失败...
$orderStatus = '8';
$rsp_info = '做单失败';
} else {
# 其他终止...
return;
}
$params = [
'data' => [
'user_order_id' => $result['merchant_order_id'],
'rsp_info' => $rsp_info, //可选,具体定义详见文档
'status' => $orderStatus,
]
];
ksort($params['data']);
$params['data'] = json_encode($params['data'], JSON_UNESCAPED_UNICODE);
// $mkeyinfo = $merchant['agentId'];
$param = $this->getmfSign($params,$merchant);
break;
case "8":
#天猫游戏直充
// $ksdata = OrderService::instance()->get($result['order_id'], $result['merchant_order_id']);
// var_dump($result);die;
if($result['status'] == 2){
#成功
$apiParams['coop_order_status'] = 'SUCCESS';
// $rsp_info = '';
} elseif ($result['status'] == 3) {
# 失败...
$apiParams['coop_order_status'] = 'FAILED';
$apiParams['failed_code'] = '0503';
$apiParams['failed_reason'] = '充值失败';
} else {
# 其他终止...
return;
}
// $taobao=new \app\openapi\controller\Taobao($this->app);
// $msg = $taobao ->notify($url,$param['merchant_order_id'] ,$merchant,$apiParams)
$check = true;
if (isset($merchant['other_status'] )&& $merchant['other_status'] == '1'){
// var_dump('123');die;
#根据其他特殊状态字段判断是否链接91卡库
$kami91Service = Kami91OrderService::instance();
$getkami91info = $kami91Service->getOrder(1, $result['order_id']);
// var_dump($getkami91info);die;
if($result['status'] == 3 ){
} elseif(isset($getkami91info)){
if ($getkami91info['status'] == '6'){
// var_dump($result['data']['kami']);die;
#订单卡密同步
// $qudaomsg =json_decode($result['data']['kami'],true);
$qudaomsg = $result['data'];
if(isset($qudaomsg['kami'])){
$kami91Service->upkami($result['order_id'],$qudaomsg['kami']);
$notify91 = (new \app\openapi\controller\Kami91($this->app))->kaminotify($result['order_id'],$result['status'] );
if($notify91 != 'ok' ){
return;
}
}elseif ($result['status'] == 2 ){
// $notify91 = (new \app\openapi\controller\Kami91($this->app))->kaminotify($result['order_id'],$result['status'] );
// if($notify91 != 'ok' ){
// return;
// }
// return;
}
// var_dump('123');die;
}elseif($getkami91info['status'] == '2' && $result['status'] == '2'){
#不进行任何操作
}elseif($getkami91info['status'] == '3' && $result['status'] == '3'){
#不进行任何操作,发送失败回调
} else {
return;
}
}elseif($result['status'] == 3 || $result['status'] == 2){
} else{
return;
}
}
break;
case "9":
#淘宝直充
// $ksdata = OrderService::instance()->get($result['order_id'], $result['merchant_order_id']);
// var_dump($result);die;
$orderService = OrderService::instance();
$tb_data = $orderService->get('', $result['merchant_order_id'], $merchant['id']);
$tb_data_paramJSON = $tb_data['param'];
$tb_data_param = json_decode($tb_data_paramJSON,true);
$tbParams = [
'tbOrderNo' =>$result['merchant_order_id'],
'bizType' =>$tb_data_param['bizType'],
'supplierId' => $merchant['agentId'],
'coopOrderNo' => $result['order_id'],
// 'coopOrderStatus' => '',
// 'errCode' => '',
// 'errDesc' => '',
'ts' => time() * 1000,
];
if($result['status'] == 2){
#成功
$tbParams['coopOrderStatus'] = 'SUCCESS';
//
} elseif ($result['status'] == 3) {
# 失败...
$tbParams['coopOrderStatus'] = 'FAILED';
$tbParams['errCode'] = 'STATUS_REFUNDED';
$tbParams['errDesc'] = '充值失败';
} else {
# 其他终止...
return;
}
// $taobao=new \app\openapi\controller\Taobao($this->app);
// $msg = $taobao ->notify($url,$param['merchant_order_id'] ,$merchant,$apiParams)
$check = true;
if (isset($merchant['other_status'] )&& $merchant['other_status'] == '1'){
// var_dump('123');die;
#根据其他特殊状态字段判断是否链接91卡库
$kami91Service = Kami91OrderService::instance();
$getkami91info = $kami91Service->getOrder(1, $result['order_id']);
// var_dump($getkami91info);die;
if($result['status'] == 3 ){
} elseif(isset($getkami91info)){
if ($getkami91info['status'] == '6'){
// var_dump($result['data']['kami']);die;
#订单卡密同步
// $qudaomsg =json_decode($result['data']['kami'],true);
$qudaomsg = $result['data'];
if(isset($qudaomsg['kami'])){
$kami91Service->upkami($result['order_id'],$qudaomsg['kami']);
$notify91 = (new \app\openapi\controller\Kami91($this->app))->kaminotify($result['order_id'],$result['status'] );
if($notify91 != 'ok' ){
return;
}
}elseif ($result['status'] == 2 ){
return;
}
// var_dump('123');die;
}elseif($getkami91info['status'] == '2' && $result['status'] == '2'){
#不进行任何操作
}elseif($getkami91info['status'] == '3' && $result['status'] == '3'){
#不进行任何操作,发送失败回调
} else {
return;
}
}elseif($result['status'] == 3 || $result['status'] == 2){
} else{
return;
}
}
ksort($tbParams);
$sign_str = '';
foreach ($tbParams as $key => $value) {
$sign_str .= $key.$value;
}
$sign_str .= $merchant['agentkey'];
$tbParams['sign'] = md5($sign_str);
break;
}
try {
if (isset($merchant['notify_type']) && $merchant['notify_type']) {
if($merchant['merchant_type'] == '5'){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url.$geturl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'User-Agent: Apifox/1.0.0 (https://apifox.com)',
'Accept: */*',
'Host: openapi.kwaixiaodian.com',
'Connection: keep-alive',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
// var_dump($response);exit;
$response = json_decode($response, true);
$response = $response['result'];
if ($response == '1') {
$response = 'success';
} else {
$response = 'fail';
}
$msg = $response;
} elseif ($merchant['merchant_type'] == 7) {
# post 发送
// print_r('111');die;
// print_r($params);die;
$resultces = $this->send_curl_post($url, $param);
$resultArray =json_decode($resultces, true);
// print_r($resultArray);die;
if($resultArray['code'] == '0'){
$msg = 'success';
}else{
$msg = $resultArray;
}
} elseif ($merchant['merchant_type'] == 8) {
# 如果为8
// print_r('111');die;
// print_r($params);die;
// $taobao = new \app\openapi\controller\Taobao();
$taobao=new \app\openapi\controller\Taobao($this->app);
$msg = $taobao ->notify($url,$param['merchant_order_id'] ,$merchant,$apiParams);
} elseif ($merchant['merchant_type'] == 9) {
# 如果为9
$result_send = $this->send_curl_post($url, $tbParams);
$xmlObject = simplexml_load_string($result_send);
$json = json_encode($xmlObject);
$array = json_decode($json, true);
if($array['result'] == 'T'){
$msg = 'success';
}else{
$msg = $array;
}
// $msg = ;
} elseif ($merchant['notify_type'] == 2) {
# post json
$header['Content-Type'] = 'application/x-www-form-urlencoded';
$msg = $this->curl('post', $url, http_build_query($param), false, $header);
} elseif ($merchant['notify_type'] == 3) {
# get
$msg = $this->curl('get', $url, $param);
} elseif ($merchant['notify_type'] == 4) {
# get json
$msg = $this->curl('get', $url, $param, true);
} else {
# post
$msg = $this->curl('post', $url, $param);
}
} else {
if ($merchant['id'] == 31 || $merchant['id'] == 40) {
$msg = $this->curl('get', $url, $param);
} else {
$msg = $this->curl('post', $url, $param);
}
}
} catch (\Exception $e) {
$msg = 'error';
}
if ($msg == 'success') {
$log['type'] = 'merchant_notify_success';
$log['send'] = $param;
$log['data'] = $msg;
$this->log($log);
$error = 1;
} else {
$log['type'] = 'merchant_notify_fail';
$log['send'] = $param;
$log['data'] = $msg;
$this->log($log);
# 这里要加入到队列里,重新开始请求
/*
$send['order_id'] = $result['order_id'];
if (isset($result['data'])) {
$send['data'] = $result['data'];
}
$this->queue('notify', $send);
*/
$msg = 'error';
$error = 2;
}
if ($result['status'] == 2 || $result['status'] == 3 || $result['status'] == 5) {
if ($type == 1) {
OrderService::instance()->upMerchantMsg($result['order_id'], $result['notify_num'], $msg, $error);
} elseif($type == 5) {
OrderLastHistoryService::instance()->upMerchantMsg($result['order_id'], $result['notify_num'], $msg, $error);
}elseif($type == 6) {
OrderTwoHistoryService::instance()->upMerchantMsg($result['order_id'], $result['notify_num'], $msg, $error);
}elseif($type == 7) {
OrderLastweekHistoryService::instance()->upMerchantMsg($result['order_id'], $result['notify_num'], $msg, $error);
}else {
OrderHistoryService::instance()->upMerchantMsg($result['order_id'], $result['notify_num'], $msg, $error);
}
}
}
}
public function send_curl_post($url, $data)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data,
));
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
if($httpCode == 404){
$response = '404 Page Not Found';
}
curl_close($curl);
return $response;
}
public function getmfSign($params,$mkeyinfo)
{
$appkey = $mkeyinfo['agentId'];
$appSecret = $mkeyinfo['agentkey'];
if(empty($params) || empty($appkey) || empty($appSecret)){
$this->log('签名参数缺失'.date("Y-m-d,H:i:s",time()));
throw new \Exception('参数错误');
}
$params = array_merge($params, ['app_key' => $appkey, 'timestamp' => time()]);
ksort($params);
$buff = '';
foreach ($params as $k => $v) {
if ($k != "sign") {
$buff .= $k . $v;
}
}
$buff .= $appSecret;
$params['sign'] = md5($buff);
// var_dump($params);die;
return $params;
}
# 记录日志
protected function log($data)
{
Log::write('gateway', 'merchant', $data);
}
# 获取所有商户
public function getAll()
{
$where['is_deleted'] = 0;
$where['status'] = 1;
return $this->db()->where($where)->select()->toArray();
}
# 校正数据 每天凌晨0点10分处理
public function init($day = false)
{
OrderService::instance()->kou($day);
$data = $this->db()->select()->toArray();
foreach ($data as $k => $v) {
$this->initOne($v['id']);
}
}
#商户写入新的refresh_token
public function edit_refresh_token($mid,$refresh_token)
{
$update = array(
'refresh_token' =>$refresh_token
);
$this->db()->where(array('id' => $mid))->update($update);
}
public function get_refresh_token($mid)
{
$result= $this->db()->where(array('id' => $mid))->field('refresh_token')->find();
return $result?$result['refresh_token']:'';
}
public function initOne($mid)
{
$w['mid'] = $mid;
$w['account_type'] = 1;
$w['oper'] = 1;
$w['is_yue'] = 1;
$total = MerchantLogService::instance()->db()->where($w)->sum('num');
$w['oper'] = 2;
$consum = MerchantLogService::instance()->db()->where($w)->sum('num');
$update['account_surplus'] = round(($total - $consum), 2);
$w['oper'] = 1;
$w['account_type'] = 2;
$total = MerchantLogService::instance()->db()->where($w)->sum('num');
$w['oper'] = 2;
$consum = MerchantLogService::instance()->db()->where($w)->sum('num');
$update['credit_surplus'] = round(($total - $consum), 2);
$w = array();
$w['mid'] = $mid;
$w['account_type'] = 1;
$w['type'] = 1;
$update['account_total'] = MerchantLogService::instance()->db()->where($w)->sum('num');
$w = array();
$w['mid'] = $mid;
$w['account_type'] = 2;
$w['type'] = 6;
$update['credit_total'] = MerchantLogService::instance()->db()->where($w)->sum('num');
$this->db()->where(array('id' => $mid))->update($update);
}
public function update_ipWhite($mid,$ip_white_data='')
{
$is_btWaf = sysconf('settingBtWafStatus');
$api_domain = sysconf('BtWaf_domain');
if(isset($is_btWaf) && $is_btWaf == 1) {
$ip_white_array = $ip_white_data?explode(',', $ip_white_data):[];
$bt = new BtWafService();
$list = $bt->get_customize_list();
$ip_white_id = '';
if($list){
$rule_list = $list['msg']['rules'];
$name = 'ip白名单-'.$mid;
foreach ($rule_list as $k=>$v){
if($v['name'] == $name){
$ip_white_id = $k;
break;
}
}
}
if($ip_white_id){
$data = $bt->update_waf_whiteIp($ip_white_id,$name,$api_domain,$ip_white_array);
} else{
$data = $bt->update_waf_whiteIp('', $name, $api_domain, $ip_white_array);
}
if($data && isset($data['status']) && !$data['status']){
return '添加防火墙错误';
}
}
return $this->db()->where(array('id' => $mid))->update(['ip_white'=>$ip_white_data]);
}
}