213 lines
6.5 KiB
PHP
213 lines
6.5 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
namespace app\channel\service\system;
|
||
|
|
||
|
/**
|
||
|
* cheyouma服务
|
||
|
* Class Bodingcheng
|
||
|
* @package app\channel\service
|
||
|
*/
|
||
|
class Manxiangyou extends Core
|
||
|
{
|
||
|
# 这几个可以后台设置
|
||
|
protected $host = 'host';
|
||
|
protected $mid = 'mid';
|
||
|
protected $token = 'token';
|
||
|
protected $api = 'api';
|
||
|
protected $youka_type = 1;
|
||
|
protected $private_key = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIljNDv8ApN+hRbvNYnzEPJyxLCLq5xF7iJgftbjkyfUTXqK9jiB+Zs81N97TCtO/dfWQ/mj+ihpk7PVE9Pvl/SJC9U5oNd2z++9t4kskZbMSH2alOj0XvTdIdP12WRLwmi5ZK8lMWUrL6cRH51h0qnAzxge2PQblrB0dK6kTG2BAgMBAAECgYAuJhKfq5E59h/CimuTgQBKUF7dZq0oexz4ky1IBX2ivV+hMBhw9sffa1meEA6P5aLC44hFzNz0wjIJECPu5uZFjHdUGiV6b6zLnJ5e74Z3ACg6ehjSjPqPkqrJV7dUjQvA9GyvU2J31y7EExKeiT3mafU9HbSBvft4kKYCgcDf+QJBAPsQHJ51LBBuzwyUQrvXLJedj9tsANW0M0snLDL/LZ+xdKQ+0JdVPtDqqs9rQkaZvDPCXzTKzoE9bDOXrSnYok8CQQCMFtVQAX8CqImvXtIVB3ZW0b0iRRxt3t7oTvEiUYAX0RBct315iPEkfXbZ34D+v+XOx9fWGrZsofzfl4tOvA8vAkEAyPaFogUyp1i7XVtT6536cMZLf5z9MhKTOGX7yDpgX8KhFlO9jlVK+dnNrkgx/edTMa81nEF2eRcchFO15ZglmwJAPUvwYmWFMaCVPkr7GYB+Y1a6rzgWTGzslDGWiTGoHsO3qRoQqREmkpTjYEnPo6hL4cLHykM99yKaByvEJNtcuQJBAPl/NgTgaF/npH76ppOCFk9jHjbKkdeY7UvRkJDzDdkaNdXWtdezbWhkXJaymsVdakIutkzV2cLJmEQTLbA5HPQ=';
|
||
|
|
||
|
# 加油券
|
||
|
public function jyqcz($param)
|
||
|
{
|
||
|
$check = array
|
||
|
(
|
||
|
'mobile' => 'mobile',
|
||
|
'cash' => 'amount',
|
||
|
);
|
||
|
$this->api = 'api/oil_card/get_oilCardCode_notInvoice';
|
||
|
return $this->submit($param, $check);
|
||
|
}
|
||
|
|
||
|
# 通知处理 主要返回状态 2是成功 3是失败
|
||
|
public function notify($data)
|
||
|
{
|
||
|
$request = $data;
|
||
|
$result = array();
|
||
|
$result['cash'] = 1;
|
||
|
if ($data['status'] == 1) {
|
||
|
$result['status'] = 2;
|
||
|
} elseif ($data['status'] == 2) {
|
||
|
$result['status'] = 3;
|
||
|
} else {
|
||
|
$result['status'] = 4;
|
||
|
}
|
||
|
|
||
|
$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['status']) && $array['status'] == '0') {
|
||
|
# 正确
|
||
|
$msg = 'ok';
|
||
|
} else {
|
||
|
# 错误
|
||
|
$msg = isset($array['errorMessage']) ? $array['errorMessage'] : '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['pnsn'] = $this->mid;
|
||
|
$request['sn'] = $param['order'];
|
||
|
$mtimestamp = sprintf("%.3f", microtime(true));
|
||
|
$timestamp = floor($mtimestamp);
|
||
|
$milliseconds = round(($mtimestamp - $timestamp) * 1000);
|
||
|
$request['dt'] = date("YmdHis", $timestamp) . $milliseconds;
|
||
|
$url = $this->host . $this->api;
|
||
|
$args = $this->_sign($request, array('dt', 'amount', 'mobile', 'sn', 'pnsn'));
|
||
|
|
||
|
$response = $this->curl('get', $url . '?args=' . $args);
|
||
|
$response = $this->response($response);
|
||
|
|
||
|
$channel_order_id = '';
|
||
|
|
||
|
$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)
|
||
|
{
|
||
|
$string = '';
|
||
|
foreach ($param as $k => $v) {
|
||
|
$string .= $v . '=' . $request[$v] . '&';
|
||
|
}
|
||
|
|
||
|
$public_key = RsaUtil::url_safe_decode($this->token);
|
||
|
$public_key = "-----BEGIN PUBLIC KEY-----\n" . $public_key . "\n-----END PUBLIC KEY-----";
|
||
|
$result = RsaUtil::publicEncrypt($string, $public_key);
|
||
|
|
||
|
|
||
|
$result = urlencode($result);
|
||
|
|
||
|
$result = $this->mid . ',' . $result;
|
||
|
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function decode($data)
|
||
|
{
|
||
|
$private_key = RsaUtil::url_safe_decode($this->private_key);
|
||
|
$private_key = "-----BEGIN PRIVATE KEY-----\n" . $private_key . "\n-----END PRIVATE KEY-----";
|
||
|
$result = RsaUtil::privateDecrypt($string, $private_key);
|
||
|
|
||
|
parse_str($result, $result);
|
||
|
|
||
|
return $result;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class RsaUtil
|
||
|
{
|
||
|
private static function getPublicKey($publicKey)
|
||
|
{
|
||
|
return openssl_pkey_get_public($publicKey);
|
||
|
}
|
||
|
|
||
|
private static function getPrivateKey($privateKey)
|
||
|
{
|
||
|
return openssl_pkey_get_private($privateKey);
|
||
|
}
|
||
|
|
||
|
public static function publicEncrypt($data, $publicKey)
|
||
|
{
|
||
|
if (!is_string($data)) {
|
||
|
return null;
|
||
|
}
|
||
|
return openssl_public_encrypt($data, $encrypted, self::getPublicKey($publicKey)) ? base64_encode($encrypted) : null;
|
||
|
}
|
||
|
|
||
|
public static function publicDecrypt($encrypted, $publicKey)
|
||
|
{
|
||
|
if (!is_string($encrypted)) {
|
||
|
return null;
|
||
|
}
|
||
|
return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey($publicKey))) ? $decrypted : null;
|
||
|
}
|
||
|
|
||
|
public static function privateEncrypt($data, $privateKey)
|
||
|
{
|
||
|
if(!is_string($data)){
|
||
|
return null;
|
||
|
}
|
||
|
return openssl_private_encrypt($data, $encrypted, self::getPrivateKey($privateKey)) ? base64_encode($encrypted) : null;
|
||
|
}
|
||
|
|
||
|
public static function privateDecrypt($encrypted, $privateKey)
|
||
|
{
|
||
|
if(!is_string($encrypted)){
|
||
|
return null;
|
||
|
}
|
||
|
return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey($privateKey)))? $decrypted : null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param $string
|
||
|
* @return string|string[]
|
||
|
* url安全解码
|
||
|
*/
|
||
|
public static function url_safe_decode($string)
|
||
|
{
|
||
|
$data = str_replace(['-','_'], ['+','/'], $string);
|
||
|
$mod4 = strlen($data) % 4;
|
||
|
if ($mod4)
|
||
|
{
|
||
|
$data .= substr('====', $mod4);
|
||
|
}
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param $string
|
||
|
* @return string|string[]
|
||
|
* url安全转码
|
||
|
*/
|
||
|
public static function url_safe_encode($string)
|
||
|
{
|
||
|
return str_replace(['+','/','='], ['-','_',''], $string);
|
||
|
}
|
||
|
}
|