354 lines
12 KiB
PHP
354 lines
12 KiB
PHP
![]() |
<?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 Qianmi extends Service
|
||
|
{
|
||
|
# 这几个可以后台设置
|
||
|
protected $host = 'http://supplyapi.bm001.com/';
|
||
|
protected $mid = 'S688374';
|
||
|
protected $token = 'ynrrwyzd8wxrantqhqki3oenspad6g64dbncoghsgd';
|
||
|
protected $merchant_id = 6;
|
||
|
protected $product = 'zshcz';
|
||
|
protected $tplid = 'MB2020101616251417';
|
||
|
|
||
|
/**
|
||
|
* 设置默认操作表
|
||
|
* @var string
|
||
|
*/
|
||
|
public $apply = 'merchant_qianmi_apply';
|
||
|
|
||
|
/**
|
||
|
* 设置默认操作表
|
||
|
* @var string
|
||
|
*/
|
||
|
public $order = 'merchant_qianmi_order';
|
||
|
|
||
|
|
||
|
# 获取订单
|
||
|
public function apply($tplid, $product)
|
||
|
{
|
||
|
$this->tplid = $tplid;
|
||
|
$this->product = $product;
|
||
|
|
||
|
$request['partner'] = $this->mid;
|
||
|
$request['tplid'] = $this->tplid;
|
||
|
|
||
|
$request['sign'] = strtoupper(md5($request['partner'] . $request['tplid'] . $this->token));
|
||
|
$request['reqid'] = $this->createOrder();
|
||
|
$request['format'] = 'json';
|
||
|
$url = $this->host . 'supply.do';
|
||
|
|
||
|
$response = $this->response($this->curl('get', $url, $request));
|
||
|
if ($response['status'] == 2) {
|
||
|
# 错误,漏单检查
|
||
|
$this->check($request['reqid']);
|
||
|
} else {
|
||
|
# 解析数据
|
||
|
if (isset($response['data']['dataList']) && $response['data']['dataList']) {
|
||
|
$this->addApply($url, $request['reqid'], $request, $response, 1);
|
||
|
$this->item($request['reqid'], $response['data']);
|
||
|
} else {
|
||
|
//$this->check($request['reqid']);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function addApply($url, $reqid, $request, $response, $status)
|
||
|
{
|
||
|
$data['product'] = $this->product;
|
||
|
$data['url'] = $url;
|
||
|
$data['reqid'] = $reqid;
|
||
|
$data['request'] = json_encode($request, JSON_UNESCAPED_UNICODE);
|
||
|
$data['response'] = json_encode($response, JSON_UNESCAPED_UNICODE);
|
||
|
$data['status'] = $status;
|
||
|
$this->db($this->apply)->insert($data);
|
||
|
}
|
||
|
|
||
|
private function item($reqid, $data)
|
||
|
{
|
||
|
if (isset($data['dataList']) && $data['dataList']) {
|
||
|
$item = $data['dataList'];
|
||
|
if (isset($item[0])) {
|
||
|
foreach ($item as $k => $v) {
|
||
|
$this->push($reqid, $v);
|
||
|
}
|
||
|
} else {
|
||
|
$this->push($reqid, $item);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function push($reqid, $item)
|
||
|
{
|
||
|
if (isset($item['idnumber'])) {
|
||
|
$param['idCard'] = $item['idnumber'];
|
||
|
}
|
||
|
if (isset($item['ownername'])) {
|
||
|
$param['realName'] = $item['ownername'];
|
||
|
}
|
||
|
if (isset($item['telpho'])) {
|
||
|
$param['mobile'] = $item['telpho'];
|
||
|
}
|
||
|
$num = $item['order_num'];
|
||
|
$id = $item['id'];
|
||
|
$param['cash'] = $item['product_par_value'];
|
||
|
|
||
|
if ($this->product == 'zshcz') {
|
||
|
$key = 'card';
|
||
|
} else {
|
||
|
$key = 'mobile';
|
||
|
}
|
||
|
$param[$key] = '';
|
||
|
if (isset($item['recharge_account'])) {
|
||
|
$param[$key] = $item['recharge_account'];
|
||
|
}
|
||
|
if (isset($item['RECHARGE_ACCOUNT'])) {
|
||
|
$param[$key] = $item['RECHARGE_ACCOUNT'];
|
||
|
}
|
||
|
|
||
|
$param['order'] = $reqid . '_' . $id . '_' . $item['order_id'] . '_' . $this->tplid;
|
||
|
# 验证是否可以充值
|
||
|
$yes = $this->yes($id, $item['order_id']);
|
||
|
if ($yes) {
|
||
|
$this->addOrder($reqid, $id, $item['order_id']);
|
||
|
# 可以充值 去充值
|
||
|
for ($i = 0; $i < $num; $i++) {
|
||
|
$msg = ChannelService::instance()->use($this->merchant_id, $this->product, $param, true);
|
||
|
if (is_string($msg)) {
|
||
|
# 下单失败,回调失败
|
||
|
$this->notify_error($reqid, $id, $item['order_id']);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function notify_error($reqid, $id, $orderid)
|
||
|
{
|
||
|
$request['partner'] = $this->mid;
|
||
|
$request['tplid'] = $this->tplid;
|
||
|
$request['reqid'] = $reqid;
|
||
|
$request['id'] = $id;
|
||
|
$request['orderid'] = $orderid;
|
||
|
$request['orderstate'] = 5;
|
||
|
$request['version'] = '2.4';
|
||
|
|
||
|
$request['sign'] = strtoupper(md5($request['partner'] . $request['id'] . $request['orderid'] . $request['orderstate'] . $this->token));
|
||
|
$request['format'] = 'json';
|
||
|
$url = $this->host . 'setOrders.do';
|
||
|
|
||
|
$response = $this->response($this->curl('get', $url, $request));
|
||
|
|
||
|
/*
|
||
|
$log = array();
|
||
|
$log['type'] = 'merchant_notify_response';
|
||
|
$log['response'] = $response;
|
||
|
$this->log($log, 'callback');
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
private function addOrder($reqid, $id, $order)
|
||
|
{
|
||
|
$data['reqid'] = $reqid;
|
||
|
$data['item_id'] = $id;
|
||
|
$data['item_order_id'] = $order;
|
||
|
$data['status'] = 1;
|
||
|
$this->db($this->order)->insert($data);
|
||
|
}
|
||
|
|
||
|
private function upOrderStatus($reqid, $id, $order, $status)
|
||
|
{
|
||
|
$where['reqid'] = $reqid;
|
||
|
$where['item_id'] = $id;
|
||
|
$where['item_order_id'] = $order;
|
||
|
$data['status'] = $status;
|
||
|
$this->db($this->order)->where($where)->update($data);
|
||
|
}
|
||
|
|
||
|
# 验证是否可以充值
|
||
|
public function yes($id, $order)
|
||
|
{
|
||
|
$request['orderid'] = $order;
|
||
|
$request['id'] = $id;
|
||
|
$request['format'] = 'json';
|
||
|
$url = $this->host . 'confirmRecharge.do';
|
||
|
|
||
|
$response = $this->response($this->curl('get', $url, $request));
|
||
|
|
||
|
if ($response['status'] == 1 && isset($response['data']['canRechaege']) && $response['data']['canRechaege']) {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
# 获取漏单
|
||
|
public function check($reqid, $orderids = '', $tplid = '', $product = '')
|
||
|
{
|
||
|
if ($tplid) {
|
||
|
$this->tplid = $tplid;
|
||
|
}
|
||
|
|
||
|
if ($product) {
|
||
|
$this->product = $product;
|
||
|
}
|
||
|
|
||
|
$request['partner'] = $this->mid;
|
||
|
$request['tplid'] = $this->tplid;
|
||
|
$request['reqid'] = $reqid;
|
||
|
$request['sign'] = strtoupper(md5($request['partner'] . $request['tplid'] . $request['reqid'] . $this->token));
|
||
|
$request['orderids'] = $orderids;
|
||
|
$request['format'] = 'json';
|
||
|
$url = $this->host . 'checkOrder.do';
|
||
|
|
||
|
$response = $this->response($this->curl('get', $url, $request));
|
||
|
|
||
|
if ($response['status'] == 1) {
|
||
|
# 解析数据
|
||
|
$this->item($reqid, $response['data']);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
# 通知处理
|
||
|
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'];
|
||
|
$config = explode('_', $merchant_order_id);
|
||
|
if (isset($config[3])) {
|
||
|
$this->tplid = $config[3];
|
||
|
}
|
||
|
|
||
|
$this->log($log, 'callback');
|
||
|
|
||
|
if (isset($log['request']['status']) && ($log['request']['status'] == 2 || $log['request']['status'] == 3)) {
|
||
|
# 成功
|
||
|
$status = $log['request']['status'];
|
||
|
$request['partner'] = $this->mid;
|
||
|
$request['tplid'] = $this->tplid;
|
||
|
$request['reqid'] = $config[0];
|
||
|
$request['id'] = $config[1];
|
||
|
$request['orderid'] = $config[2];
|
||
|
if ($status == 2) {
|
||
|
$request['orderstate'] = 4;
|
||
|
} else {
|
||
|
$request['orderstate'] = 5;
|
||
|
}
|
||
|
|
||
|
$request['version'] = '2.4';
|
||
|
|
||
|
$request['sign'] = strtoupper(md5($request['partner'] . $request['id'] . $request['orderid'] . $request['orderstate'] . $this->token));
|
||
|
$request['format'] = 'json';
|
||
|
$url = $this->host . 'setOrders.do';
|
||
|
|
||
|
$response = $this->response($this->curl('get', $url, $request));
|
||
|
|
||
|
$log = array();
|
||
|
$log['type'] = 'merchant_notify_response';
|
||
|
$log['response'] = $response;
|
||
|
$this->log($log, 'callback');
|
||
|
|
||
|
//$this->upOrderStatus($config[0], $config[1], $config[2], $status);
|
||
|
|
||
|
echo 'success';die;
|
||
|
}
|
||
|
echo 'error';die;
|
||
|
}
|
||
|
|
||
|
# 数据响应格式处理
|
||
|
private function response($data, $test = 2)
|
||
|
{
|
||
|
if ($test == 1) {
|
||
|
$data = '{"status":"0000","data":{"reqId":"G202011045568145046815429","dataList":[{"company_id":null,"recharge_server":null,"product_id":"64357111","product_name":"全国 中石化加油卡 直充100元","RECHARGE_EXT1":null,"userid":"S115281","RECHARGE_EXT2":null,"RECHARGE_EXT3":null,"RECHARGE_EXT4":null,"RECHARGE_ACCOUNT":"1000113200027253874","order_id":"20110410574480","id":"1068096463","recharge_area":null,"esup_uid":"S688374","order_time":"2020-11-04 10:07:42","product_company":null,"order_num":1,"RECHARGE_ACCOUNTTYPE":null,"order_ip":"52.77.237.77","product_par_value":"100","tplid":"MB2020101616251417"}],"fields":"company_id,recharge_server,product_id,product_name,RECHARGE_EXT1,userid,RECHARGE_EXT2,RECHARGE_EXT3,RECHARGE_EXT4,RECHARGE_ACCOUNT,order_id,id,recharge_area,esup_uid,order_time,product_company,order_num,RECHARGE_ACCOUNTTYPE,order_ip,product_par_value,tplid"},"msg":null}';
|
||
|
|
||
|
$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);
|
||
|
# 数据模拟
|
||
|
/*
|
||
|
$array = array
|
||
|
(
|
||
|
'status' => '0000',
|
||
|
'data' => array
|
||
|
(
|
||
|
'dataList' => array
|
||
|
(
|
||
|
'item' => array
|
||
|
(
|
||
|
'id' => 1,
|
||
|
'product_par_value' => 100,
|
||
|
'recharge_account' => 10000,
|
||
|
'order_id' => 100,
|
||
|
'idnumber' => 100,
|
||
|
'ownername' => 100,
|
||
|
'telpho' => 100,
|
||
|
'order_num' => 1,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
*/
|
||
|
if ($array['status'] && $array['status'] == '0000' && isset($array['data']) && $array['data']) {
|
||
|
# 写入订单
|
||
|
# 记录请求日志
|
||
|
$data = $array['data'];
|
||
|
$msg = 'ok';
|
||
|
$reqid = 0;
|
||
|
if (isset($data['reqId']) && $data['reqId']) {
|
||
|
$reqid = $data['reqId'];
|
||
|
}
|
||
|
$status = 1;
|
||
|
|
||
|
} else {
|
||
|
# 记录请求日志
|
||
|
$msg = $array['msg'];
|
||
|
$status = 2;
|
||
|
$reqid = 0;
|
||
|
}
|
||
|
|
||
|
return array
|
||
|
(
|
||
|
'msg' => $msg,
|
||
|
'data' => $data,
|
||
|
'status' => $status,
|
||
|
'reqid' => $reqid,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
protected function createOrder()
|
||
|
{
|
||
|
$order_id = $this->createOrderId();
|
||
|
$where['reqid'] = $order_id;
|
||
|
$info = $this->db($this->apply)->where($where)->find();
|
||
|
if ($info) {
|
||
|
return $this->createOrder();
|
||
|
}
|
||
|
return $order_id;
|
||
|
}
|
||
|
|
||
|
# 生成订单号
|
||
|
protected function createOrderId()
|
||
|
{
|
||
|
return 'G' . date('Ymd').substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(100000, 999999));
|
||
|
}
|
||
|
|
||
|
# 记录日志
|
||
|
protected function log($data, $type = 'request')
|
||
|
{
|
||
|
Log::write('qianmi_' . $this->tplid, $type, $data);
|
||
|
}
|
||
|
}
|