447 lines
12 KiB
PHP
447 lines
12 KiB
PHP
<?php
|
||
|
||
namespace app\openapi\controller;
|
||
|
||
use app\gateway\service\CurlService;
|
||
use app\gateway\service\RedisService as redisObj;
|
||
use app\merchant\service\MerchantService;
|
||
use app\merchant\service\OrderHistoryService;
|
||
use app\merchant\service\OrderService;
|
||
use app\order\service\Kami91OrderService;
|
||
use think\admin\Controller;
|
||
use app\core\Service as CoreService;
|
||
use think\facade\Log;
|
||
|
||
class Kami91 extends Controller
|
||
{
|
||
|
||
# 是否检测数据
|
||
// protected bool $check = false;
|
||
protected $merchant_id;
|
||
|
||
# code码定义
|
||
protected $code = array
|
||
(
|
||
# 成功
|
||
1 => 'ok',
|
||
# 小于0为失败
|
||
-1 => 'appid为空',
|
||
-2 => '产品错误',
|
||
-3 => 'appid无效',
|
||
-4 => '余额不足或者没有传入价格',
|
||
|
||
-5 => 'signature不能为空',
|
||
-6 => 'nonce不能为空',
|
||
-7 => 'time不能为空',
|
||
-8 => 'signature已失效',
|
||
-9 => 'signature验证失败',
|
||
|
||
-100 => '请求错误',
|
||
-101 => '订单不存在',
|
||
-102 => '订单号重复',
|
||
|
||
-103 => '正在处理中',
|
||
|
||
-1000 => '系统维护中',
|
||
|
||
);
|
||
# 查询接口 一般用于查询数据,同步执行
|
||
|
||
|
||
# 提交接口 一般用于提交数据,异步执行
|
||
|
||
|
||
public function query()
|
||
{
|
||
$data = input();
|
||
if(empty($data['api_product']) || $data['api_product'] != 'query'){
|
||
$this->no(-2);
|
||
}
|
||
|
||
if (empty($data['appid'])) {
|
||
$this->no(-1);
|
||
}
|
||
|
||
if (empty($data['merchant_order_id'])) {
|
||
$this->no(-100);
|
||
}
|
||
// http_post($url, $data)
|
||
|
||
$getMerchantInfo = MerchantService::instance()->get($data['appid']);
|
||
// $mid = $getMerchantInfo['id'];
|
||
$info = Kami91OrderService::instance()->db()->where(['merchant_order_id' => $data['merchant_order_id']])->find();
|
||
if(!$info){
|
||
$this->no(-100);
|
||
}
|
||
$order = $this->getOrder('', $data['merchant_order_id'] ,$getMerchantInfo['id']);
|
||
|
||
if(!$order){
|
||
$this->no(-101);
|
||
}
|
||
|
||
if($order['status'] == 2){
|
||
if(empty($info['cardno'])){
|
||
$data = ['msg' => '正在处理中'];
|
||
$this -> no(-103);
|
||
}
|
||
$data = [
|
||
'CardNum' => $info['cardno'],
|
||
'CardPassword' => $info['cardpwd'],
|
||
'expireDate' => $info['expire_time']
|
||
];
|
||
}elseif ($order['status'] == 7){
|
||
|
||
|
||
if($info['status'] == '1'){
|
||
$data =array();
|
||
$data['order_id'] = $order['order_id'];
|
||
$data['status'] = 4;
|
||
Kami91OrderService::instance()->db()->where(array('merchant_order_id' => $order['merchant_order_id']))->update($data);
|
||
}
|
||
|
||
$this->huidiao($order['order_id']);
|
||
|
||
if(empty($info['cardno'])){
|
||
$response = json_decode($order['response'],true);
|
||
//
|
||
if(isset($response['kami'])) {
|
||
|
||
$cardno = $response['kami']['cardno'];
|
||
$cardpwd = $response['kami']['cardpwd'];
|
||
$expire_time= $response['kami']['expired'];
|
||
}else{
|
||
$this->no(-103);
|
||
}
|
||
}else{
|
||
$cardno = $info['cardno'];
|
||
$cardpwd = $info['cardpwd'];
|
||
$expire_time= $info['expire_time'];
|
||
}
|
||
$data = [
|
||
'CardNum' => $cardno,
|
||
'CardPassword' => $cardpwd,
|
||
'expireDate' => $expire_time
|
||
];
|
||
|
||
}else {
|
||
$this->no(-100);
|
||
}
|
||
|
||
|
||
|
||
return $this->yes($data);
|
||
}
|
||
|
||
protected function getOrder($order_id, $merchant_order_id = false ,$mid = false)
|
||
{
|
||
$orderService = OrderService::instance();
|
||
$order = $orderService->get($order_id, $merchant_order_id ,$mid);
|
||
if (!$order) {
|
||
$orderHistoryService = OrderHistoryService::instance();
|
||
$order = $orderHistoryService->get($order_id, $merchant_order_id ,$mid);
|
||
}
|
||
return $order;
|
||
}
|
||
|
||
public function submit()
|
||
{
|
||
#注意修改id
|
||
// $this->mid = '2';
|
||
|
||
$input = input();
|
||
|
||
|
||
$data = [];
|
||
|
||
if (!$input) {
|
||
$this->no(-100);
|
||
}
|
||
|
||
if (!$input['appid']) {
|
||
$this->no(-1);
|
||
}
|
||
|
||
if (!$input['order']) {
|
||
$this->no(-100);
|
||
}
|
||
|
||
$order = '';
|
||
$merchant_order = $input['order'];
|
||
|
||
|
||
if (isset($input['proid'])) {
|
||
# 根据产品id 获取信息
|
||
$pinfo = \app\channel\service\ProductBaseService::instance()->getOne($input['proid']);
|
||
if (!$pinfo) {
|
||
$this->no(-2);
|
||
}
|
||
|
||
|
||
$data['product_key'] = $pinfo['key'];
|
||
$data['cash'] = $pinfo['value'] ;
|
||
$pid = \app\channel\service\ProductBaseService::instance()->getProductId($pinfo['service_id']);
|
||
|
||
$data['pid'] = $pid['id'];
|
||
} else {
|
||
$this->no(-2);
|
||
}
|
||
|
||
|
||
$data['status'] = '1';
|
||
$data['param'] = json_encode($input);
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// if ($data['request'] && $data['status'] == '-7') {
|
||
// $data['request'] = json_decode($data['request'], true);
|
||
//// \app\gateway\controller\api\Task::run($data['request'], true);
|
||
//
|
||
// }
|
||
|
||
$getMerchantInfo = MerchantService::instance()->get($input['appid']);
|
||
$data['mid'] = $getMerchantInfo['id'];
|
||
$orderdata = $this->getOrder($order, $merchant_order ,$getMerchantInfo['id']);
|
||
|
||
if(!$orderdata){
|
||
|
||
}else{
|
||
$data['status'] = '4';
|
||
$data['order_id'] = $orderdata['order_id'];
|
||
}
|
||
|
||
$where = [
|
||
'mid' => $data['mid'],
|
||
'merchant_order_id' => $merchant_order
|
||
];
|
||
$data['merchant_order_id'] = $merchant_order;
|
||
$data['notifyurl'] = $input['notify'];
|
||
|
||
$result = Kami91OrderService::instance()->up($where,$data);
|
||
|
||
if(!is_numeric($result)){
|
||
$this->no(-1000);
|
||
}
|
||
// var_dump($orderdata);die;
|
||
|
||
if(isset($orderdata['status']) && $orderdata['status'] == '7'){
|
||
$this->huidiao($orderdata['order_id']);
|
||
}
|
||
$this->yes('ok');
|
||
|
||
}
|
||
|
||
public function huidiao($order_id)
|
||
{
|
||
// var_dump(123);die;
|
||
$order = $this->getOrder($order_id);
|
||
|
||
if(isset($order['channel_callback_msg'])){
|
||
|
||
$msg = json_decode($order['channel_callback_msg'],true);
|
||
if(isset($msg['kami'])){
|
||
OrderService::instance()->upStatus($order_id);
|
||
|
||
}else{
|
||
return 'error';
|
||
}
|
||
}else{
|
||
|
||
$response = json_decode($order['response'],true);
|
||
//
|
||
if(isset($response['kami'])){
|
||
// var_dump($response['kami']);die;
|
||
$kami = [
|
||
'cardno' =>$response['kami']['cardno'],
|
||
'cardpwd' =>$response['kami']['cardpwd'],
|
||
'expire_time' =>$response['kami']['expired'],
|
||
];
|
||
|
||
$where = [
|
||
'order_id' => $order_id
|
||
];
|
||
|
||
$result = Kami91OrderService::instance()->up($where,$kami);
|
||
OrderService::instance()->upChannelMsg($order_id,'2',['kami' => $response['kami'],'s_nubmer'=>$response['kami']['cardno']]);
|
||
|
||
}else{
|
||
return 'error';
|
||
}
|
||
}
|
||
if ( $order['merchant_callback_error'] != 1) {
|
||
// var_dump(123);die;
|
||
//$update['order'] = $order;
|
||
$update = [];
|
||
$update['status'] = $order['status'];
|
||
|
||
$update['account'] = $order['account'];
|
||
$update['cash'] = $order['cash'];
|
||
$update['merchant_order_id'] = $order['merchant_order_id'];
|
||
$update['order_id'] = $order_id;
|
||
$update['notify_num'] = $order['merchant_callback_num'];
|
||
$order['param'] = json_decode($order['param'],true);
|
||
|
||
if ($order['param'] && isset($order['param']['notify']) && $order['param']['notify']) {
|
||
$update['notify'] = $order['param']['notify'];
|
||
}
|
||
|
||
# 商户后续操作
|
||
|
||
if (isset($order['error_account_oper']) && $order['error_account_oper'] == 1) {
|
||
# 需要进行扣费
|
||
MerchantService::instance()->up($order['id'], $order['mid'], $order['pid'], $order['product_key'], $update);
|
||
} else {
|
||
# 直接发送
|
||
MerchantService::instance()->notify($order['id'], $order['mid'], $order['pid'], $order['product_key'], $update);
|
||
}
|
||
|
||
}
|
||
return 'ok';
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
# 提交接口 一般用于提交数据,异步执行
|
||
public function dc_charge(): void
|
||
{
|
||
$this->submit();
|
||
}
|
||
// protected function getMerchant(): void
|
||
// {
|
||
// $this->merchant = MerchantService::instance()->get('PT_'.$this->agentId);
|
||
// $this->mid = $this->merchant['id'];
|
||
//
|
||
//// $this->agentId = $this->merchant['agentId'];
|
||
//
|
||
// $this->agentkey = $this->merchant['agentkey'];
|
||
//
|
||
// $this->callnotify = $this->merchant['notify_url'];
|
||
//// $this->signkey = $this->merchant['other_key'];
|
||
//
|
||
//
|
||
// }
|
||
|
||
|
||
|
||
protected function curl($method, $url, $param = array(), $json = false, $header = false):mixed
|
||
{
|
||
if ($param) {
|
||
$log['type'] = 'request';
|
||
$log['url'] = $url;
|
||
$log['param'] = $param;
|
||
$this->log($log);
|
||
}
|
||
|
||
$curl = CurlService::getInstance($url, $param, $method, $json, $header);
|
||
$curl->setTimeOut(3600);
|
||
return $curl->result();
|
||
}
|
||
|
||
protected static function log($data, $type = 'request'):void
|
||
{
|
||
\dever\Log::write('kami91', $type, $data);
|
||
// \dever\Log::write('jingdong', $type, $data);
|
||
}
|
||
|
||
public function kaminotify( $order_id ,$status)
|
||
{
|
||
$kami91Service = Kami91OrderService::instance();
|
||
$getkami91info = $kami91Service->getOrder(1, $order_id);
|
||
$url = $getkami91info['notifyurl'];
|
||
$param = array();
|
||
$param['status'] = $status;
|
||
if ($status == 2){
|
||
$param['cardno'] = $getkami91info['cardno'];
|
||
$param['cardpwd'] = $getkami91info['cardpwd'];
|
||
$param['expire_time'] = $getkami91info['expire_time'];
|
||
}
|
||
|
||
|
||
|
||
$header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||
// var_dump($param);die;
|
||
// $msg = http_post( $url, $param);
|
||
|
||
// $msg = $this->send_post( $url, $param, 'POST');
|
||
$msg = 'success';
|
||
// var_dump($msg);die;
|
||
$this->log($msg,'msg');#写入到日志里
|
||
|
||
|
||
if($msg != 'success'){
|
||
$this->log($msg,'errormsg');#写入到日志里
|
||
$callbalck_status = '3';
|
||
}else{
|
||
$callbalck_status = '2';
|
||
}
|
||
$upstatus = $kami91Service->upStatus($order_id, $callbalck_status ,$msg);
|
||
|
||
if ($callbalck_status == 3){
|
||
die;
|
||
}
|
||
|
||
|
||
|
||
|
||
return $upstatus;
|
||
}
|
||
|
||
public function send_post($notify_url, $post_data, $type): mixed
|
||
{
|
||
|
||
$postdate = http_build_query($post_data);
|
||
|
||
$options = array(
|
||
'http' => array(
|
||
'method' => $type,
|
||
'header' => 'Content-type:application/x-www-form-urlencoded',
|
||
'content' => $postdate,
|
||
'timeout' => 15 * 60 // 超时时间(单位:s)
|
||
)
|
||
);
|
||
$context = stream_context_create($options);
|
||
return file_get_contents($notify_url, false, $context);
|
||
}
|
||
|
||
/**
|
||
* 返回失败的消息
|
||
* @param mixed $info
|
||
* @param string $data
|
||
* @param integer $code
|
||
*/
|
||
protected function no($code = 0, $info = '', $data = '{-null-}')
|
||
{
|
||
$msg = $this->code[$code] ?? 'error';
|
||
if ($info) {
|
||
$msg .= ':' . $info;
|
||
}
|
||
$data = '{-null-}';
|
||
$this->error($msg, $data, $code);
|
||
}
|
||
|
||
/**
|
||
* 返回成功的消息
|
||
* @param mixed $info
|
||
* @param string $data
|
||
* @param integer $code
|
||
*/
|
||
protected function yes($data = '{-null-}', $info = 'ok', $code = 1)
|
||
{
|
||
if (is_string($data) && $data != 'ok' && $data != 'success') {
|
||
if ($data == '订单号重复') {
|
||
return $this->no(-102);
|
||
}
|
||
return $this->no(-100, $data);
|
||
}
|
||
$this->success($info, $data, $code);
|
||
}
|
||
|
||
|
||
} |