REAPI/app/channel/service/system/Chuangha.php

207 lines
5.8 KiB
PHP
Raw 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 Shoukala
* @package app\channel\service
*/
class Chuangha extends Core
{
# 这几个可以后台设置
protected $host = 'host';
protected $mid = 'mid';
protected $token = 'token';
protected $api = 'api';
protected $aes = '51356a585b26';
protected $card = -1;
# 中石化收卡
public function zshsk($param)
{
$check = array
(
'card' => 'cardNo',
'pwd' => 'cardPwd',
'cash' => 'faceAmount',
'subclassType' => 'subclassType',
);
$param['subclassType'] = 'Sinopec';
return $this->submit($param, $check);
}
# 中石化收卡
public function zsysk($param)
{
$check = array
(
'card' => 'cardNo',
'pwd' => 'cardPwd',
'cash' => 'faceAmount',
'subclassType' => 'subclassType',
);
$param['subclassType'] = 'PetroChina';
return $this->submit($param, $check);
}
# 通知处理 主要返回状态 2是成功 3是失败
public function notify($data)
{
$request = $data;
unset($request['sign']);
unset($request['s_order']);
$sign = $this->_sign($request);
if ($sign != $data['sign']) {
return false;
}
$result = array();
$result['cash'] = 1;
if ($data['status'] == 1) {
$result['status'] = 2;
} elseif ($data['status'] == 2) {
$result['status'] = 3;
} else {
$result['status'] = 4;
}
$order = OrderService::instance()->get($request['mOrderNo']);
$msg = $request['msg'];
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);
}
}
$result['yes'] = 'success';
$result['data'] = $data;
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['code']) && $array['code'] == '0') {
# 正确
$msg = 'ok';
} else {
# 错误
$msg = isset($array['msg']) ? $array['msg'] : '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 query($param)
{
}
# 提交数据
private function submit($param, $check)
{
$this->card_id = -1;
$data = array();
if (!isset($param['card'])) {
if (is_numeric($this->data['merchant'])) {
$mid = $this->data['merchant'];
} else {
$mid = false;
}
$data = CardService::instance()->getData($param['cash'], $mid);
} elseif (isset($param['card'])) {
$data = CardService::instance()->getOne($param['card'], $param['pwd'], 1);
}
if ($data) {
$this->card_id = $data['id'];
$param['card'] = $data['cnum'];
$param['pwd'] = $data['cpwd'];
} else {
return 'order';
}
$this->api = 'api/order/recharge';
$param = $this->param($param, $check);
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
return $param;
}
$request = $param['detail'];
$request['mId'] = $this->mid;
$request['cardPwd'] = $this->aes($request['cardPwd']);
$request['timestamp'] = date('YmdHis');
$this->order_id = $request['mOrderNo'] = $param['order'];
$request['notifyUrl'] = $this->getNotify($param['order'], 1);
$request['sign'] = $this->_sign($request);
//$request['cardPwd'] = urlencode($request['cardPwd']);
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request);
$response = $this->response($response);
$channel_order_id = '';
if (isset($response['array']['orderNo'])) {
$channel_order_id = $response['array']['orderNo'];
}
$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)
{
ksort($request);
$signature_string = '';
foreach ($request as $k => $v) {
$signature_string .= $k . '=' . $v . '&';
}
$signature_string .= 'key=' . $this->token;
return md5($signature_string);
}
private function aes($data)
{
$key = md5($this->aes);
$key = substr($key, 8, 16);
$data = openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
return base64_encode($data);
}
}