382 lines
10 KiB
PHP
382 lines
10 KiB
PHP
<?php
|
||
|
||
namespace app\channel\service\system;
|
||
use app\merchant\service\MerchantService;
|
||
use app\merchant\service\OrderService;
|
||
|
||
/**
|
||
* 讯银服务
|
||
* Class Bodingcheng
|
||
* @package app\channel\service
|
||
*/
|
||
class Shengxiakami extends Core
|
||
{
|
||
# 这几个可以后台设置
|
||
protected $host = 'http://58.220.211.41:12009/';
|
||
protected $mid = '61';
|
||
protected $token = '31HHu6R4';
|
||
protected $api = '';
|
||
protected $deskey = 'V79x42C7a45zXMn6v85b44r2';
|
||
protected $goodid = '';
|
||
// public function __construct($async = false, $data = array(), $channel = array(), $product = array())
|
||
// {
|
||
// parent::__construct($async, $data, $channel, $product);
|
||
// $actionName = request()->action();
|
||
// if(strpos($actionName, '_cardbuy') !== false){
|
||
// if(!method_exists($this,$actionName)){
|
||
// $param = request()->param();
|
||
// $this->cardbuy($param);
|
||
// }
|
||
// }
|
||
//
|
||
// }
|
||
|
||
|
||
|
||
# 天龙卡
|
||
public function kdb_tlk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
# 金虎卡
|
||
public function kdb_jhk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
#乐富卡
|
||
public function kdb_lfk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
#龙华卡
|
||
public function kdb_lhk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
#东升卡
|
||
public function kdb_dsk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
#宁森卡
|
||
public function kdb_nsk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
#火星卡
|
||
public function kdb_hxk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
#掌心卡
|
||
public function kdb_zxk_cardbuy($param)
|
||
{
|
||
return $this->cardbuy($param,);
|
||
}
|
||
|
||
|
||
|
||
# 购买卡密
|
||
public function cardbuy($param)
|
||
{
|
||
// var_dump($param);die;
|
||
$check = array
|
||
(
|
||
'proid' => 'card_kind',
|
||
'cash' => 'card_price',
|
||
'num' => 'card_num',
|
||
'timestamp' => 'bill_time'
|
||
);
|
||
if(empty($param['timestamp'])){
|
||
$param['timestamp'] = date('YmdHis');;
|
||
}
|
||
|
||
|
||
$this->api = 'api/BrandUCardFetchSubmit';
|
||
return $this->submit($param, $check);
|
||
}
|
||
|
||
# 购买智选卡卡密
|
||
public function jkzx_cardbuy($param)
|
||
{
|
||
// var_dump($param);die;
|
||
$check = array
|
||
(
|
||
'proid' => 'card_kind',
|
||
'cash' => 'card_price',
|
||
'num' => 'card_num',
|
||
'timestamp' => 'bill_time'
|
||
);
|
||
if(empty($param['timestamp'])){
|
||
$param['timestamp'] = date('YmdHis');;
|
||
}
|
||
|
||
|
||
$this->api = 'api/BrandUCardFetchSubmit';
|
||
return $this->submit($param, $check);
|
||
}
|
||
|
||
# 购买大象卡卡卡密
|
||
public function daxk_cardbuy($param)
|
||
{
|
||
// var_dump($param);die;
|
||
$check = array
|
||
(
|
||
'proid' => 'card_kind',
|
||
'cash' => 'card_price',
|
||
'num' => 'card_num',
|
||
'timestamp' => 'bill_time'
|
||
);
|
||
if(empty($param['timestamp'])){
|
||
$param['timestamp'] = date('YmdHis');;
|
||
}
|
||
|
||
|
||
$this->api = 'api/BrandUCardFetchSubmit';
|
||
return $this->submit($param, $check);
|
||
}
|
||
|
||
# 购买金鑫卡卡卡密
|
||
public function jxk_cardbuy($param)
|
||
{
|
||
// var_dump($param);die;
|
||
$check = array
|
||
(
|
||
'proid' => 'card_kind',
|
||
'cash' => 'card_price',
|
||
'num' => 'card_num',
|
||
'timestamp' => 'bill_time'
|
||
);
|
||
if(empty($param['timestamp'])){
|
||
$param['timestamp'] = date('YmdHis');;
|
||
}
|
||
|
||
|
||
$this->api = 'api/BrandUCardFetchSubmit';
|
||
return $this->submit($param, $check);
|
||
}
|
||
|
||
# 数据响应格式处理
|
||
public function response($data)
|
||
{
|
||
|
||
$log['type'] = 'response';
|
||
$log['data'] = $data;
|
||
$log['config'] = $this->data;
|
||
$this->log($log);
|
||
|
||
// ret_code=0&ret_msg=提卡成功&shop_id=61&bill_id=2065056746662580766&mm_bill_no=F24022318215784910&purchase_amt=10&card_no_data=bf1c26db1ed2e0d201db11f0918032c12fd1d f3cde46612578dc4838f45b2e94b84463c3f516e4afda0c02395570524fdbc38260ff951280&sign=eb5c380044c1b57b81ff6123f7c32289
|
||
|
||
//$array = $this->json_decode($data);
|
||
|
||
// $str = "key1=value1&key2=value2";
|
||
|
||
// 按照 & 分割字符串
|
||
$pairs = explode('&', $data);
|
||
|
||
// 将每个键值对分割并创建数组
|
||
$array = [];
|
||
foreach ($pairs as $pair) {
|
||
// 分割键和值
|
||
list($key, $value) = explode('=', $pair);
|
||
// 解码键和值
|
||
$array[urldecode($key)] = urldecode($value);
|
||
}
|
||
|
||
|
||
if (!$array) {
|
||
$msg = 'error';
|
||
} elseif (isset($array['ret_code']) && $array['ret_code'] == '0' || $array['ret_code'] == '999') {
|
||
# 正确
|
||
$msg = 'ok';
|
||
} else {
|
||
# 错误
|
||
$msg = $array['ret_msg'] ?? 'error';
|
||
}
|
||
|
||
return array
|
||
(
|
||
'msg' => $msg,
|
||
'data' => $array,
|
||
'array' => $array,
|
||
);
|
||
}
|
||
|
||
# 查询接口
|
||
public function query($order)
|
||
{
|
||
$orderRequest = json_decode($order['request'], true);
|
||
$request['shop_id'] = $this->mid;
|
||
$request['bill_id'] = $order['merchant_order_id'];
|
||
$request['time_stamp'] =date('YmdHis');
|
||
|
||
$request['sign'] = $this->_sign($request, array('shop_id', 'bill_id', 'time_stamp'));
|
||
$url = $this->host . 'api/UCardFetchQuery';
|
||
$response = $this->curl('get', $url, $request);
|
||
|
||
$log['type'] = 'query_response';
|
||
$log['data'] = $response;
|
||
$log['config'] = $this->data;
|
||
$this->log($log);
|
||
$pairs = explode('&', $response);
|
||
$array = [];
|
||
foreach ($pairs as $pair) {
|
||
// 分割键和值
|
||
list($key, $value) = explode('=', $pair);
|
||
// 解码键和值
|
||
$array[urldecode($key)] = urldecode($value);
|
||
}
|
||
|
||
|
||
|
||
$result = array();
|
||
// $result['status'] = 4;
|
||
if (isset($array['ret_code']) && $array['ret_code'] == '0') {
|
||
if(isset($array['card_no_data'])){
|
||
$kami = $this->decrypt_3des($array['card_no_data']);
|
||
$kami = explode(',', $kami);
|
||
$kamidata = [
|
||
'cardno' => $kami[0],
|
||
'cardpwd' => $kami[1],
|
||
'expired' => $kami[2],
|
||
|
||
];
|
||
|
||
$this->upOrderResponse($kamidata,$order['merchant_order_id'],$order['response']);
|
||
$result['status'] = 7;
|
||
$result['kami'] = $kamidata;
|
||
}else{
|
||
$result['status'] = 4;
|
||
}
|
||
|
||
} else{
|
||
$result['status'] = 3;
|
||
}
|
||
|
||
if (isset($array['mm_bill_no']) && $array['mm_bill_no']) {
|
||
# 流水号
|
||
// $result['status'] = 3;
|
||
//$data['s_nubmer'] = $result['s_nubmer'] = $data['result']['code'];
|
||
}
|
||
|
||
|
||
|
||
return $result;
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
# 通知处理 主要返回状态 2是成功 3是失败
|
||
public function notify($data)
|
||
{
|
||
$result = array();
|
||
$result['cash'] = 1;
|
||
$result['status'] = $data['status'] ;
|
||
if (isset($data['kami']) && $data['kami']['cardno']) {
|
||
# 流水号
|
||
$data['s_nubmer'] = $result['s_nubmer'] = $data['kami']['cardno'];
|
||
}
|
||
|
||
|
||
$result['yes'] = 'ok';
|
||
|
||
$result['data'] = $data;
|
||
|
||
return $result;
|
||
}
|
||
|
||
# 提交数据
|
||
private function submit($param, $check)
|
||
{
|
||
$param = $this->param($param, $check);
|
||
if (is_string($param) || (is_array($param) && $param['status'] == 1)) {
|
||
return $param;
|
||
}
|
||
// var_dump($param);die;
|
||
|
||
|
||
$request = $param['detail'];
|
||
$request['shop_id'] = $this->mid;
|
||
$request['card_kind'] = $this->getGid($param['cash']);
|
||
$request['bill_id'] = $param['merchant_order'];
|
||
|
||
$request['bill_time'] = date('YmdHis');
|
||
$request['time_stamp'] = date('YmdHis');
|
||
|
||
$request['version'] = '1';
|
||
|
||
//$request['returnurl'] = $this->getNotify($param['order'], 1);
|
||
$request['sign'] = $this->_sign($request, array('shop_id', 'bill_id', 'card_kind', 'card_num', 'card_price', 'time_stamp','version'));
|
||
$url = $this->host . $this->api;
|
||
$response = $this->curl('get', $url, $request);
|
||
|
||
$response = $this->response($response);
|
||
|
||
$channel_order_id = '';
|
||
if (isset($response['array']['mm_bill_no'])) {
|
||
$channel_order_id = $response['array']['mm_bill_no'];
|
||
}
|
||
|
||
|
||
|
||
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
|
||
return $response['msg'];
|
||
}
|
||
|
||
private function _sign($request, $param)
|
||
{
|
||
//shop_id={$强自竟平台商户id}&bill_id={$订单编号}&card_kind={$卡种类}&card_num={$发货数量}&card_price={$卡面值}&time_stamp={$时间戳A}&version=1|||{$强自竟平台md5}
|
||
$signature_string = '';
|
||
foreach ($param as $k => $v) {
|
||
// if (isset($request[$v]) && $request[$v]) {
|
||
|
||
$signature_string .= $v . '=' . $request[$v] . '&';
|
||
|
||
}
|
||
|
||
$signature_string = substr($signature_string, 0, -1);
|
||
|
||
$signature_string .= '|||'.($this->token);
|
||
// var_dump($signature_string);die;
|
||
// $signature_string = iconv("UTF-8", "gbk", $signature_string);
|
||
|
||
return md5($signature_string);
|
||
}
|
||
|
||
#3des解密
|
||
public function decrypt_3des($encrypted_string){
|
||
|
||
$cipher = "des-ede3"; // 3des-ebc 使用的是des-ede3算法
|
||
$mode = "ebc"; // 工作模式为ebc
|
||
|
||
$iv = ""; // EBC模式不需要IV,留空即可
|
||
$json = $this->channel['describe'];
|
||
$array = $this->json_decode($json);
|
||
if(isset($array['deskey'])){
|
||
$this->deskey = $array['deskey'];
|
||
}
|
||
|
||
return openssl_decrypt(hex2bin($encrypted_string), $cipher, $this->deskey, $options=OPENSSL_RAW_DATA, $iv); // 输出解密后的字符串
|
||
}
|
||
public function upOrderResponse($kami,$merchant_order_id,$orderResponse)
|
||
{
|
||
$orderService = OrderService::instance();
|
||
|
||
$response = json_decode($orderResponse,true);
|
||
$response['kami'] = $kami;
|
||
$data = ['response' => json_encode($response)];
|
||
|
||
$orderService->db()->where(array('merchant_order_id' => $merchant_order_id))->update($data);
|
||
}
|
||
|
||
|
||
} |