2024-09-29 15:43:18 +08:00

303 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\merchant\service\system;
use app\core\Service;
use app\channel\service\ChannelService;
use app\merchant\service\OrderService;
use app\merchant\service\OrderHistoryService;
use dever\Log;
/**
* 商户自动提单
* Class Qianmi
* @package app\channel\service
*/
class Jdoil extends Service
{
# 这几个可以后台设置
protected $host = 'https://public-robot.open.fulu.com/';
protected $mid = '11948388';
protected $token = 'b335768c1e9de4dded4f358b932414aa';
protected $merchant_id = 67;
protected $product = 'zshcz';
protected $iv = '1111111111111111';
protected $check = false;
public function setMid($mid)
{
$this->merchant_id = $mid;
return $this;
}
# 查询卡号
public function card()
{
$input = input();
$this->log($input, 'card');
$result = array();
$sign = $this->_sign($input);
if ($sign && isset($input['sign']) && $sign == $input['sign']) {
$type = $input['cardType'];
$card = $input['cardNum'];
$status = '04';
if ($type == 1) {
# 验证是否中石油加油卡
$check = "/^(?:7|9)\d{15}$/";
if (!preg_match($check, $card)) {
$status = '03';
}
} elseif ($type == 2) {
$check = "/^1\d{18}$/";
if (!preg_match($check, $card)) {
$status = '03';
}
} elseif ($type == 5) {
$result['errorCode'] = 'JDI_00011';
$status = '11';
}
$result['cardType'] = $type;
$result['cardNum'] = $card;
$result['cardStatus'] = $status;
$result['ownName'] = '';
$result['customerName'] = '';
$result['customerType'] = 1;
//$result['location'] = '';
//中石化加油卡根据第7-8位区分归属地100011XX北京11天津12河北13山西14内蒙古15辽宁21吉林22黑龙江23上海31江苏32浙江33安徽34福建35江西36山东37河南41湖北42湖南43广东44广西45海南46重庆50四川51贵州52云南53西藏54陕西61甘肃62青海63宁夏64新疆65
} else {
$result['errorCode'] = 'JDI_00002';
}
return $this->getResult($result);
}
# 查询订单
public function query()
{
$input = input();
$this->log($input, 'query');
$result = array();
$sign = $this->_sign($input);
if ($sign && isset($input['sign']) && $sign == $input['sign']) {
$order_id = '';
if (isset($input['fillOrderNo'])) {
$result['fillOrderNo'] = $input['fillOrderNo'];
$merchant_order_id = $input['fillOrderNo'];
$orderService = OrderService::instance();
$order = $orderService->get($order_id, $merchant_order_id, $this->merchant_id);
if (!$order) {
$orderHistoryService = OrderHistoryService::instance();
$order = $orderHistoryService->get($order_id, $merchant_order_id, $this->merchant_id);
}
if ($order) {
$result['agentOrderNo'] = $order['order_id'];
$result['fillTime'] = str_replace(array('-', ':', ' '), '', $order['create_at']);
if ($order['status'] == 2) {
$result['fillStatus'] = 1;
} elseif ($order['status'] == 3) {
$result['fillStatus'] = 2;
} else {
$result['fillStatus'] = 3;
}
} else {
$result['errorCode'] = 'JDI_00007';
}
} else {
$result['errorCode'] = 'JDI_00001';
}
} else {
$result['errorCode'] = 'JDI_00002';
}
return $this->getResult($result);
}
# 获取订单
public function apply()
{
$input = input();
/*
$input = '{"data":"eyJvcmRlcklkIjoiMjM4ODEwMjk0ODg2IiwiYnV5TnVtIjoiMSIsInRvdGFsUHJpY2UiOiI2My4wMCIsImJyYW5kSWQiOiI2NTc2ODYiLCJza3VJZCI6IjEwMDQ0MTcxMDI3NDQ4IiwidXNlcklwIjoiMTE0LjI1NS4yNDkuMTU3Iiwic291cmNlVHlwZSI6MCwiZ2FtZUFjY291bnQiOiI4MzgzMTA3NjAifQ==","customerId":"12058096","sign":"df706acda0119f4a30cfa61f3cbf8b96","timestamp":"20220212112733"}';
$input = json_decode($input, true);
*/
$this->log($input, 'apply');
$result = array();
$sign = $this->_sign($input);
if ($sign && isset($input['sign']) && $sign == $input['sign']) {
$this->log($input, 'request');
if (isset($input['fillOrderNo'])) {
$result = $this->push($input);
} else {
$result['errorCode'] = 'JDI_00001';
}
} else {
$result['errorCode'] = 'JDI_00002';
}
return $this->getResult($result);
}
private function push($item)
{
$result = array();
$param['cash'] = $item['faceAmount'];
$param['card'] = $item['cardNum'];
$param['order'] = $item['fillOrderNo'];
$param['notify_url'] = $item['notifyUrl'];
$param['mobile'] = $item['fillMobile'];
//$param['realName'] = $item['ownName'];
$result['fillOrderNo'] = $param['order'];
$type = $item['cardType'];
if ($type == 1) {
$product = 'zsycz';
} elseif ($type == 2) {
$product = 'zshcz';
} elseif ($type == 5) {
$result['errorCode'] = 'JDI_00003';
return $result;
}
# 验证是否可以充值
$yes = $this->yes($param['order']);
if ($yes) {
# 可以充值 去充值
$msg = ChannelService::instance()->use($this->merchant_id, $product, $param, true);
if (is_string($msg)) {
# 下单失败,回调失败
if ($msg == '订单号重复') {
$result['errorCode'] = 'JDI_00006';
} elseif ($msg == '面额无效') {
$result['errorCode'] = 'JDI_00004';
} else {
$result['errorCode'] = 'JDI_00003';
}
$item['errorCode'] = $result['errorCode'];
$this->log($item, 'apply_record');
} else {
$result['agentOrderNo'] = $msg['order'];
if ($this->check) {
# 下单成功,调用第三方接口
$url = 'http://103.239.246.254:9080/order/check?orderNo=' . $param['order'];
$return = $this->curl('get', $url, array());
$orderService = OrderService::instance();
}
}
return $result;
}
}
# 验证是否可以充值
public function yes($order)
{
return true;
}
# 通知处理
public function notify()
{
$log['type'] = 'merchant_notify';
$log['request'] = input();
if (!isset($log['request']['merchant_order_id'])) {
echo 'error';die;
}
$merchant_order_id = $log['request']['merchant_order_id'];
if (strstr($merchant_order_id, 't')) {
echo 'success';die;
}
if (isset($log['request']['status']) && ($log['request']['status'] == 2 || $log['request']['status'] == 3)) {
if ($log['request']['status'] == 2) {
$status = 1;
} elseif ($log['request']['status'] == 3) {
$status = 2;
} else {
$status = 3;
}
$order_id = '';
$merchant_order_id = $log['request']['merchant_order_id'];
$orderService = OrderService::instance();
$order = $orderService->get($order_id, $merchant_order_id, $this->merchant_id);
if (!$order) {
$orderHistoryService = OrderHistoryService::instance();
$order = $orderHistoryService->get($order_id, $merchant_order_id, $this->merchant_id);
}
if (!$order) {
echo 'error';die;
}
$param = json_decode($order['param'], true);
$url = $param['notify_url'];
$request['agentId'] = $this->mid;
$request['fillOrderNo'] = $merchant_order_id;
$request['agentOrderNo'] = $order['order_id'];
$request['fillStatus'] = $status;
$request['fillTime'] = str_replace(array('-', ':', ' '), '', $order['create_at']);
$request = $this->getResult($request, true);
$response = $this->curl('get', $url, $request);
if ($response) {
$response = json_decode($response, true);
if (isset($response['isSuccess']) && $response['isSuccess'] == 'T') {
echo 'success';die;
}
}
}
echo 'error';die;
}
private function getResult($result, $state = false)
{
$result['signType'] = 'MD5';
$result['timestamp'] = date('YmdHis');
$result['version'] = '1.0';
if (!$state) {
$result['isSuccess'] = 'T';
if (isset($result['errorCode']) && $result['errorCode']) {
$result['isSuccess'] = 'F';
}
}
//$result['errorCode'] = 'T';
//$result['fillOrderNo'] = 'T';
//$result['agentOrderNo'] = 'T';
$result['sign'] = $this->_sign($result);
if ($state) {
return $result;
}
return json_encode($result);
}
# 记录日志
protected function log($data, $type = 'request')
{
Log::write('jdoil', $type, $data);
}
private function _sign($request)
{
if (isset($request['sign'])) {
unset($request['sign']);
}
if (isset($request['signType'])) {
unset($request['signType']);
}
ksort($request);
$signature_string = '';
foreach ($request as $k => $v) {
if (strlen($v)) {
$signature_string .= $k .'' . $v . '';
}
}
$signature_string .= $this->token;
return md5($signature_string);
}
}