313 lines
9.9 KiB
PHP
Raw Permalink Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\channel\service\system;
use app\channel\service\CardService;
use app\merchant\service\OrderService;
/**
* 乐购代发
* Class Tiancheng
* @package app\channel\service
*/
class Lorp extends Core
{
# 这几个可以后台设置
protected $host = 'http://lorp-api.shujubuy.com/';
protected $mid = 'yizhanhongtu';
protected $token = '709b62b6680248f8ad8c4949920144c4';
protected $card = array();
protected $order_id = '';
protected $api = '';
protected $sign = array();
protected $lprefix = '';
# 系统代充
public function xtdc($param)
{
$check = array
(
'card' => 'number',
'cash' => 'money',
'cnum' => 'cardNum',
'cpwd' => 'cardPwd',
);
if (!isset($param['cnum'])) {
$data = CardService::instance()->getData($param['cash'], $this->data['merchant']);
if ($data) {
$this->data['param']['cnum'] = $param['cnum'] = $data['cnum'];
$this->data['param']['cpwd'] = $param['cpwd'] = $data['cpwd'];
$this->card = $data;
$this->card_id = $this->card['id'];
} else {
$this->card_id = -1;
}
}
$this->sign = array('username', 'orderNumber', 'number', 'cardNum', 'cardPwd', 'productNo', 'money', 'orderTime', 'privateKey');
$this->api = 'api/v2/submitOrder';
return $this->submit($param, $check);
}
# 电话充值
public function common($param)
{
$check = array
(
'account' => 'number',
'cash' => 'money',
);
$this->sign = array('username', 'orderNumber', 'number', 'productNo', 'money', 'orderTime', 'privateKey');
$this->api = 'api/v1/submitOrder';
return $this->submit($param, $check);
}
# 电话充值
public function dhcz($param)
{
$check = array
(
'mobile' => 'number',
'cash' => 'money',
);
$this->sign = array('username', 'orderNumber', 'number', 'productNo', 'money', 'orderTime', 'privateKey');
$this->api = 'api/v1/submitOrder';
if (isset($param['isp']) && $param['isp']) {
if ($param['isp'] == 1) {
$this->lprefix = 'ZGYD';
} elseif ($param['isp'] == 2) {
$this->lprefix = 'ZGLT';
} elseif ($param['isp'] == 3) {
$this->lprefix = 'ZGDX';
}
}
return $this->submit($param, $check);
}
# 中石化油卡充值
public function zshcz($param)
{
$check = array
(
'card' => 'number',
'cash' => 'money',
);
$this->sign = array('username', 'orderNumber', 'number', 'productNo', 'money', 'orderTime', 'privateKey');
$this->api = 'api/v1/submitOrder';
return $this->submit($param, $check);
}
# 中石化油卡充值
public function zsycz($param)
{
$check = array
(
'card' => 'number',
'cash' => 'money',
);
$this->sign = array('username', 'orderNumber', 'number', 'productNo', 'money', 'orderTime', 'privateKey');
$this->api = 'api/v1/submitOrder';
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
$result = array();
$result['status'] = 4;
$result['cash'] = 1;
if ($data['status'] == 5) {
$result['status'] = 2;
} elseif ($data['status'] == 6) {
$result['status'] = 3;
} elseif ($data['status'] == 7) {
//存疑
$result['status'] = 5;
} else {
$result['status'] = 4;
}
$msg = '';
if (isset($data['voucher']) && $data['voucher']) {
# 错误信息
//$data['s_nubmer'] = $result['s_nubmer'] = $data['voucher'];
$msg = $result['msg'] = $data['msg'] = $data['voucher'];
}
if (isset($data['msg']) && $data['msg']) {
# 错误信息
$msg = $result['msg'] = $data['msg'];
}
if ($result['status'] == 2 && isset($result['msg']) && $result['msg']) {
# 充$值成功,分析短信
if (strstr($result['msg'], ':')) {
$temp = explode(':', $result['msg']);
$order = $temp[1];
if (strstr($order, '')) {
$temp = explode('', $order);
$order = $temp[0];
}
$data['s_nubmer'] = $result['s_nubmer'] = $order;
}
}
$result['data'] = $data;
$order = OrderService::instance()->get($data['orderNumber']);
if ($order && $order['card_id'] > 0) {
if ($result['status'] == 2) {
CardService::instance()->log($order, 5, '充值成功', $msg);
} elseif ($result['status'] == 3) {
CardService::instance()->log($order, 6, '充值失败', $msg);
} elseif ($result['status'] == 5) {
if (isset($data['code']) && $data['code'] == 82) {
CardService::instance()->log($order, 8, '充值卡无效或密码错误', $msg);
} elseif (isset($data['code']) && $data['code'] == 83) {
CardService::instance()->log($order, 9, '卡密已经有使用记录', $msg);
} elseif (isset($data['code']) && $data['code'] == 81) {
CardService::instance()->log($order, 10, '提交次数过多', $msg);
} else {
CardService::instance()->log($order, 7, '疑似:卡密充值失败', $msg);
}
}
}
$result['yes'] = 'SUCCESS';
return $result;
}
# 数据响应格式处理
public function response($data)
{
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
$array = $this->json_decode($data);
if (!$array) {
$msg = 'error';
} elseif (isset($array['status']) && $array['status'] == '122600') {
# 正确
$msg = 'ok';
} elseif (isset($array['status']) && $array['status'] == '122610') {
# 疑似
$msg = 'ys';
} elseif (isset($array['status']) && $array['status'] == '122616') {
# 疑似
$msg = 'ys';
} else {
# 错误
$msg = isset($array['desc']) ? $array['desc'] : 'error';
}
# 增加卡密日志
$order = array();
if ($this->order_id) {
$order = OrderService::instance()->get($this->order_id);
if ($order && $order['card_id'] > 0) {
if ($msg == 'ok') {
CardService::instance()->log($order, 3, '渠道下单成功');
} else {
CardService::instance()->log($order, 4, '渠道下单失败');
}
}
}
return array
(
'msg' => $msg,
'data' => $data,
'array' => $array,
);
}
public function encrypt($value, $key = false, $iv = false)
{
if (!$key) {
//$key = str_replace('-', '', $this->token);
$key = $this->token;
$iv = substr($key, -16, 16);
$key = substr($key, 0, 16);
}
$data = openssl_encrypt($value, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
$data = base64_encode($data);
return $data;
}
# 查询接口
public function query($param)
{
}
# 提交数据
private function submit($param, $check)
{
$param = $this->param($param, $check);
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
return $param;
}
$request = $param['detail'];
$request['username'] = $this->mid;
$this->order_id = $request['orderNumber'] = $param['order'];
$request['orderTime'] = date('Y-m-d H:i:s');
if ($this->lprefix) {
$request['productNo'] = $this->getCode($this->lprefix, $param['cash']);
} else {
$request['productNo'] = $this->getGid($param['cash']);
}
$request['sign'] = $this->_sign($request, $this->sign);
if (isset($request['cardNum']) && $request['cardNum']) {
$request['cardNum'] = $this->encrypt($request['cardNum']);
}
if (isset($request['cardPwd']) && $request['cardPwd']) {
$request['cardPwd'] = $this->encrypt($request['cardPwd']);
}
$request['notifyUrl'] = $this->getNotify($param['order'], 1);
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request, false, array('X-Submit-Username' => $request['username']));
$response = $this->response($response);
//$response['data'] = '';
//$response['msg'] = 'ok';
$channel_order_id = '';
/*
if (isset($response['array']['obj']) && $response['array']['obj']) {
$channel_order_id = $response['array']['obj'];
}
*/
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account'], '', $this->card_id);
return $response['msg'];
}
private function _sign($request, $param)
{
$request['privateKey'] = $this->token;
$signature_string = '';
foreach ($param as $k => $v) {
if (isset($request[$v]) && $request[$v]) {
$signature_string .= $request[$v];
}
}
return md5($signature_string);
}
private function getCode($prefix, $cash)
{
return $prefix . $cash;
}
}