261 lines
9.7 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\merchant\service\system;
use app\core\Service;
use app\channel\service\ChannelService;
use dever\Log;
/**
* 福禄商户自动提单
* Class Qianmi
* @package app\channel\service
*/
class Fulu extends Service
{
# 这几个可以后台设置
protected $host = 'https://public-robot.open.fulu.com/';
protected $mid = '327f524f-b15a-4427-807d-8eae225b961b';
protected $token = '=B00zmo^jjPMw9!*';
protected $merchant_id = 42;
protected $product = 'zshcz';
protected $iv = '1111111111111111';
# 获取订单
public function apply()
{
$input = file_get_contents("php://input");
$this->log($input, 'apply');
/*
$input = 'Gh7LhtyyH7XAH0lBkcD/jNmbotFYt1bclMLbZML5TKL/yP797GrvzpFNsKwWHIrCIFyjecg8nKm836kS0CYBEfp+0yDdcSSHH2qUipUrmKlprxsq7t+ZP6a5FOUCoxlhTmmpwSPpIXIL7Tib66wwk4jpx/1V4NKnZkQxeeXmoF+gx5XrT8KreozYVkrMj7z+uiMX3h5wwyFD9SClaHF6KPE2+SyjK5xSe6sioEHtHdCeg9alBku0HHugK9a2+qL7xNwbwPaloSzGod0X8A9sgVlANobAv11VfIaOwMxWh6j/gzka1RiFsuHeBQq10QjHZQmGGu7o5uxPVRlXpW7sa8N14ot53lV/yskIJFG0VDZEyWYf7OMuEL4td15XWYeDezoKzgqnueZAm7RyPKwIkZogaMphuE4rbGldwMmXEpjBfJs8KgJ1yDK1jLfD+3cy/xGB5zJh6g8YKFKVh9Fc7vp0bL/k2p1Mc91V/4A2iehLl+N3ZvsUksTrHSTDWEvOIOPhsHJKNPFge60PvE5HgpDlDfuz9R+rVbbuv6ewQUTrfc5FkK5wui6usbQctbtP/Fqsxv+Q6IerB0u99/wz/P2w6VZIIy0u1T3LuV1uTxss4R6VkD7+uLKg5+88w671RGd9hA0zUmWlLDDlPacX7CZQBZMi9G6/j913XBNauE/4OOqSyoCRr4EOvnlkdQKlZgsTbSTD0BZqtVFKm4Q5g2GxOa0oof3fSsyrkNrjdahroPuhNBxVs/kd+h0OYf4tGOf9fl2EU8/yRgp8nPKBtn1dwRKfL2ub2ejJSHTMaflbp3OGv2xfpArhSFKkUC2PrOstfZV/93vgdpajyFUJ+7HsEviWKKSFBWquWQIhghU1FfPDsPp+USENY+up7wSodcY0+MWZjRsNcj4X1QxJcQ==';
*/
$data = $this->aes_decrypt($input);
if ($data && isset($data['SupBuyNum']) && isset($data['ChargeAccount'])) {
$this->log($data, 'request');
for ($num = 1; $num <= $data['SupBuyNum']; $num++) {
$this->push($data);
}
}
}
private function push($item)
{
/*
{"CustomerOrderNo":"PS2021052711011606505","ChargeAccount":"1000113300024812399","SupBuyNum":1,"ChargeValue":null,"ChargeIp":null,"Area":null,"OperatorName":"中国石化","OrderType":4,"PacketType":null,"SupMemberId":"327f524f-b15a-4427-807d-8eae225b961b","SupMemberName":"北京丙蕴科技","SupProductId":"500052553","SupProductName":"中国石化-中石化全国加油卡-500元-直充-【丙蕴】","SupProductPrice":491.5,"SupProductFaceValue":500,"SupProductType":"在线直
","Remarks":null,"ChargeGameName":null,"ChargeGameRegion":null,"ChargeGameSrv":null,"ChargeGameRole":null,"ChargePassword":null,"ChargeType":null,"PacketKind":null,"PacketSize":0,"CreateTime":"2021-05-27 11:02:01","Id":"104210527110118429389","ContactQQ":null,"ContactTel":null}
*/
$param['cash'] = isset($item['SupProductFaceValue']) ? $item['SupProductFaceValue'] : $item['ChargeValue'];
# 订单类型1话费 2流量 3卡密 4直充,
$item['OrderType'] = 4;
if ($item['OrderType'] == 1) {
# 话费
$this->product = 'dhcz';
} elseif ($item['OrderType'] == 3) {
# 卡密
$this->product = 'zshcz';
} elseif ($item['OrderType'] == 4) {
# 直冲
$this->product = 'zshcz';
} else {
return;
}
if ($this->product == 'dhcz') {
$key = 'mobile';
} else {
$key = 'card';
}
$param[$key] = '';
if (isset($item['ChargeAccount'])) {
$param[$key] = $item['ChargeAccount'];
}
if (!$param[$key]) {
return;
}
$param['order'] = $item['Id'];
# 验证是否可以充值
$yes = $this->yes($param['order']);
if ($yes) {
# 可以充值 去充值
$msg = ChannelService::instance()->use($this->merchant_id, $this->product, $param, true);
if (is_string($msg)) {
# 下单失败,回调失败
$this->updateStatus($param['order'], 2);
}
}
}
# 验证是否可以充值
public function yes($order)
{
return $this->updateStatusProcess($order, 1);
$header['UserID'] = $this->mid;
$header['OrderNo'] = $order;
$url = $this->host . 'api/OutOrder/StockInfo';
$request['PostData'] = $this->aes_encrypt($header);
$response = $this->curl('post', $url, $request, true, $header);
$response = $this->response($response);
if ($response['status'] == 1) {
# 可以充值,修改订单状态是为配送中
return $this->updateStatusProcess($order, 1);
}
return false;
}
public function updateStatus($order, $status, $num = 1)
{
if ($num >= 5) {
return false;
}
$header['UserID'] = $this->mid;
$header['OrderNo'] = $order;
$url = $this->host . 'api/OutOrder/UpdateStatus';
$send['OrderNo'] = $header['OrderNo'];
$send['Status'] = $status;
if ($status == 2) {
$array = array('提单超限','卡号错误','已无库存','系统超时');
$key = mt_rand(0, count($array) - 1);
if (isset($array[$key])) {
$send['Description'] = $array[$key];
}
}
$request['PostData'] = $this->aes_encrypt($send);
$response = $this->curl('post', $url, $request, true, $header);
$response = $this->response($response);
if ($response['status'] != 1) {
# 修改失败,继续修改
return false;
return $this->updateStatus($order, $status, $num+1);
}
return true;
}
# 修改订单状态为配送中
public function updateStatusProcess($order, $num = 1)
{
if ($num >= 5) {
return false;
}
$header['UserID'] = $this->mid;
$header['OrderNo'] = $order;
$url = $this->host . 'api/OutOrder/UpdateStatusProcess';
$send['OrderNo'] = $header['OrderNo'];
$send['Status'] = '0';
$request['PostData'] = $this->aes_encrypt($send);
$response = $this->curl('post', $url, $request, true, $header);
$response = $this->response($response);
if ($response['status'] != 1) {
# 修改失败,继续修改
return false;
return $this->updateStatusProcess($order, $num+1);
}
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 || $log['request']['status'] == 5)) {
if ($log['request']['status'] == 2) {
$status = 1;
} elseif ($log['request']['status'] == 3) {
$status = 2;
} elseif ($log['request']['status'] == 5) {
$status = 3;
} else {
return;
}
$state = $this->updateStatus($merchant_order_id, $status);
if ($state) {
echo 'success';die;
}
}
echo 'error';die;
}
# 数据响应格式处理
private function response($data, $test = 2)
{
if ($test == 1) {
$data = '{"status":"0000","data":{"reqId":"G202011205596181875482758","dataList":[{"product_id":"1507505","recharge_chargetype":null,"product_name":"全国联通话费50元直充","userid":"S115281","order_id":"20112015735638","id":"1069730237","esup_uid":"S688374","product_company":"联通","order_time":"2020-11-20 15:05:42","order_num":1,"order_ip":"123.125.23.211","product_par_value":"50","recharge_account":"18660828076","tplid":"MB2020110511251455"}],"fields":"product_id,recharge_chargetype,product_name,userid,order_id,id,esup_uid,product_company,order_time,order_num,order_ip,product_par_value,recharge_account,tplid"},"msg":null}';
}
$this->log($data, 'response');
$array = json_decode($data, true);
if ($array['code'] && $array['code'] == '200' && isset($array['data']) && $array['data']) {
# 写入订单
# 记录请求日志
$data = $array['data'];
$msg = 'ok';
$status = 1;
} else {
# 记录请求日志
$msg = $array['message'];
$status = 2;
}
return array
(
'msg' => $msg,
'data' => $data,
'status' => $status,
);
}
# 记录日志
protected function log($data, $type = 'request')
{
Log::write('fulu', $type, $data);
}
protected function aes_encrypt($data)
{
if (is_array($data)) {
//ksort($data);
$data = json_encode($data);
}
$str_padded = $data;
if (strlen($str_padded) % 16) {
$str_padded = str_pad($str_padded,strlen($str_padded) + 16 - strlen($str_padded) % 16, "\0");
}
$data = openssl_encrypt($str_padded, 'AES-128-CBC', $this->token, OPENSSL_NO_PADDING, $this->iv);
return base64_encode($data);
}
//AES-128-ECB解密 data 字符串
protected function aes_decrypt($data)
{
$encrypted = base64_decode($data);
$data = openssl_decrypt($encrypted, 'AES-128-CBC', $this->token, OPENSSL_NO_PADDING, $this->iv);
$data = str_replace("\0", '', $data);
$data = json_decode($data, true);
return $data;
}
}