REAPI/app/channel/service/system/Xunyinkami.php
2024-09-29 15:43:18 +08:00

166 lines
5.7 KiB
PHP

<?php
namespace app\channel\service\system;
use app\merchant\service\MerchantService;
/**
* 讯银服务
* Class Bodingcheng
* @package app\channel\service
*/
class Xunyinkami extends Core
{
# 这几个可以后台设置
protected $host = 'http://openapi.xunyin.com/';
protected $mid = '8146';
protected $token = 'def16b4a01d33e03efd131aff1a9f38d';
protected $api = '';
protected $goodid = '';
# 购买卡密
public function cardbuy($param)
{
$check = array
(
'proid' => 'gamegoodid',
'num' => 'buynum',
);
$this->api = 'opencardbuyapi/submit';
return $this->submit($param, $check);
}
# 数据响应格式处理
public function response($data)
{
//$data = '<Order><cpid>9313</cpid><orderid>Q202211210610423376962187</orderid><order_no>92446200</order_no><msg>成功</msg><code>0000</code><cardlist><cardlist><cardno>BpwQYs759WBmODjU3QoRNGuz5GwTVEG8</cardno><cardpwd>Z+7lcC3ApgXSEujRU8BExjF/Vr3oJrYD</cardpwd><expired>2024-04-30 00:00:00</expired><facevalue>10.0000</facevalue></cardlist></cardlist></Order>';
$log['type'] = 'response';
$log['data'] = $data;
$log['config'] = $this->data;
$this->log($log);
//$array = $this->json_decode($data);
$array = (array) simplexml_load_string($data);
if (isset($array['Order']) && $array['Order']) {
$array = $array['Order'];
}
if (!$array) {
$msg = 'error';
} elseif (isset($array['code']) && $array['code'] == '0000' && isset($array['order_no']) && $array['order_no']) {
# 正确
$msg = 'success';
if (isset($array['cardlist'])) {
$array['cardlist'] = (array) $array['cardlist'];
$cardlist = $array['cardlist']['cardlist'];
$merchant = MerchantService::instance()->get($this->data['merchant']);
if (is_array($cardlist)) {
foreach ($cardlist as $k => $v) {
$v = (array) $v;
$array['card'][] = array
(
'cardno' => $this->encrypt($this->decrypt($v['cardno']), $merchant['appsecret']),
'cardpwd' => $this->encrypt($this->decrypt($v['cardpwd']), $merchant['appsecret']),
'expired' => $v['expired'],
'value' => $v['facevalue'],
);
}
} else {
$cardlist = (array) $cardlist;
$array['card'][] = array
(
'cardno' => $this->encrypt($this->decrypt($cardlist['cardno']), $merchant['appsecret']),
'cardpwd' => $this->encrypt($this->decrypt($cardlist['cardpwd']), $merchant['appsecret']),
'expired' => $cardlist['expired'],
'value' => $cardlist['facevalue'],
);
}
}
} else {
# 错误
$msg = $array['msg'] ?? 'error';
}
return array
(
'msg' => $msg,
'data' => $array,
'array' => $array,
);
}
# 查询接口
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['cpid'] = $this->mid;
$request['gamegoodid'] = $this->getGid($param['cash']);
$request['orderid'] = $param['order'];
$request['buyerIp'] = '8.141.69.21';
$request['createtime'] = date('YmdHis');
//$request['returnurl'] = $this->getNotify($param['order'], 1);
$request['sign'] = $this->_sign($request, array('cpid', 'gamegoodid', 'createtime', 'account', 'orderid', 'buynum'));
$url = $this->host . $this->api;
$response = $this->curl('post', $url, $request);
$response = $this->response($response);
//$response['data'] = '';
//$response['msg'] = 'ok';
$channel_order_id = '';
if (isset($response['array']['order_no'])) {
$channel_order_id = $response['array']['order_no'];
}
$param['account'] = $request['gamegoodid'];
$this->create($param['order'], $channel_order_id, $param['merchant_order'], $param['cash'], $url, $request, $response, 1, $param['account']);
return $response['array']['card'] ?? $response['msg'];
}
private function _sign($request, $param)
{
//ksort($request);
$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;
return md5($signature_string);
}
public function encrypt($value, $token = false)
{
$key = $token ? $token : $this->token;
$iv = substr($key, -8, 8);
$key = substr($key, 0, 8);
$data = openssl_encrypt($value, 'DES-CBC', $key, false, $iv);
//$data = base64_encode($data);
return $data;
}
public function decrypt($value, $token = false)
{
//$value = base64_decode($value);
$key = $token ? $token : $this->token;
$iv = substr($key, -8, 8);
$key = substr($key, 0, 8);
$data = openssl_decrypt($value, 'DES-CBC', $key, false, $iv);
return $data;
}
}