first commit
This commit is contained in:
357
src/Action/Init.php
Normal file
357
src/Action/Init.php
Normal file
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
|
||||
namespace XuanChen\WoUnicomWeb\Action;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class Init
|
||||
{
|
||||
|
||||
public $version = '3.0.0.0';
|
||||
|
||||
public $merNo;
|
||||
|
||||
//传入的参数
|
||||
public $params;
|
||||
|
||||
//输出的数据
|
||||
public $data = [];
|
||||
|
||||
//私钥
|
||||
public $private;
|
||||
|
||||
//自己的公钥
|
||||
public $public;
|
||||
|
||||
//沃支付公钥
|
||||
public $unicom_public;
|
||||
|
||||
//请求地址
|
||||
public $url;
|
||||
|
||||
//密钥
|
||||
public $key;
|
||||
|
||||
//标识码
|
||||
public $code = true;
|
||||
|
||||
//信息
|
||||
public $message;
|
||||
|
||||
//签名字符串
|
||||
public $sign;
|
||||
|
||||
//参数是否需要转换成小写
|
||||
public $is_lower = true;
|
||||
|
||||
/**
|
||||
* Notes: 设置静态数据
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/4/30 10:46
|
||||
*/
|
||||
public function setConfig()
|
||||
{
|
||||
$this->merNo = config('wounicomweb.merNo');
|
||||
$this->private = config('wounicomweb.certificate.private');
|
||||
$this->public = config('wounicomweb.certificate.public');
|
||||
$this->unicom_public = config('wounicomweb.certificate.unicom_public');
|
||||
$this->key = config('wounicomweb.key');
|
||||
//检查数据
|
||||
$this->checkDefaultData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 设置参数是否需要小写
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/5/12 11:07
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setIsLower($value)
|
||||
{
|
||||
$this->is_lower = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 检查基础数据
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/4/30 10:56
|
||||
*/
|
||||
public function checkDefaultData()
|
||||
{
|
||||
if (empty($this->merNo) || empty($this->private) || empty($this->public)) {
|
||||
$this->code = false;
|
||||
$this->message = '缺少基础参数.';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 设置数据
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/4/30 10:47
|
||||
* @param $data
|
||||
* @return $this
|
||||
*/
|
||||
public function setParams($data)
|
||||
{
|
||||
$this->params = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 处理传入参数
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/4/30 11:28
|
||||
*/
|
||||
public function setSignData()
|
||||
{
|
||||
$sign = $this->getSign();
|
||||
$this->params['signMsg'] = $sign;
|
||||
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 验签
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/5/11 11:42
|
||||
* @param bool $out
|
||||
* @param false $self
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function checkSign($self = false)
|
||||
{
|
||||
$sign = base64_decode($this->sign);
|
||||
if (! $sign) {
|
||||
throw new \Exception('签名错误');
|
||||
}
|
||||
|
||||
$public_key = $this->getPublic($self);
|
||||
$pub_key_id = openssl_get_publickey($public_key);
|
||||
|
||||
$signStr = $this->getSignString();
|
||||
|
||||
if ($pub_key_id) {
|
||||
$result = (bool) openssl_verify($signStr, $sign, $pub_key_id, 'SHA256');
|
||||
openssl_free_key($pub_key_id);
|
||||
} else {
|
||||
throw new \Exception('公钥格式有误');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 签名
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2020/10/9 15:52
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getSign(): string
|
||||
{
|
||||
$signStr = $this->getSignString();
|
||||
$private_key = $this->getPrivate();
|
||||
$privKeyId = openssl_get_privatekey($private_key);
|
||||
|
||||
if (! $privKeyId) {
|
||||
throw new \Exception('私钥格式有误');
|
||||
}
|
||||
if (openssl_sign($signStr, $signature, $privKeyId, "SHA256")) {
|
||||
$signature = base64_encode($signature);
|
||||
} else {
|
||||
throw new \Exception('签名错误');
|
||||
}
|
||||
|
||||
openssl_free_key($privKeyId);
|
||||
|
||||
return $signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取待签名字符串
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2020/9/30 9:38
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getSignString()
|
||||
{
|
||||
$params = $this->checkSignData($this->params);
|
||||
|
||||
if (empty($params)) {
|
||||
throw new \Exception('获取校验数据失败,缺少数据..');
|
||||
}
|
||||
|
||||
ksort($params);
|
||||
$signStr = $this->str2utf8(urldecode(http_build_query($params, '', '|')));
|
||||
|
||||
return $signStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取下单数据
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/5/6 8:32
|
||||
*/
|
||||
public function getUnicomData($prefix = '$')
|
||||
{
|
||||
$str = '';
|
||||
|
||||
foreach ($this->params as $key => $param) {
|
||||
$str .= $prefix.strtolower($key).'='.$param;
|
||||
}
|
||||
|
||||
return $this->merNo.trim($str, $prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 格式化需要校验的数据
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/2/18 15:47
|
||||
* @param $params
|
||||
*/
|
||||
public function checkSignData($params): array
|
||||
{
|
||||
if (isset($params['signMsg'])) {
|
||||
unset($params['signMsg']);
|
||||
}
|
||||
|
||||
if (isset($params['signmsg'])) {
|
||||
unset($params['signmsg']);
|
||||
}
|
||||
|
||||
if (isset($params['hmac'])) {
|
||||
unset($params['hmac']);
|
||||
}
|
||||
|
||||
ksort($params);
|
||||
if ($this->is_lower) {
|
||||
$new = [];
|
||||
|
||||
foreach ($params as $key => $param) {
|
||||
if (strlen($param) > 0 && ! is_null($param)) {
|
||||
$key = strtolower($key);
|
||||
$new[$key] = $param;
|
||||
}
|
||||
}
|
||||
|
||||
return $new;
|
||||
} else {
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
||||
//获取私钥
|
||||
public function getPrivate()
|
||||
{
|
||||
return file_get_contents($this->private);
|
||||
}
|
||||
|
||||
//获取公钥
|
||||
public function getPublic($self = false)
|
||||
{
|
||||
if ($self) {
|
||||
return file_get_contents($this->public);
|
||||
}
|
||||
|
||||
return file_get_contents($this->unicom_public);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串编码转为 utf8
|
||||
*
|
||||
* @param $str
|
||||
* @return string
|
||||
*/
|
||||
public function str2utf8($str)
|
||||
{
|
||||
$encode = mb_detect_encoding($str, ['ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5']);
|
||||
if ($encode != 'UTF-8') {
|
||||
$str = $str ? $str : mb_convert_encoding($str, 'UTF-8', $encode);
|
||||
}
|
||||
$str = is_string($str) ? $str : '';
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2020/12/15 11:23
|
||||
* @param $portUrl
|
||||
* @param $paramArray
|
||||
* @return array|mixed
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function sendPost($paramArray, $url = null)
|
||||
{
|
||||
if ($this->code !== true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $url) {
|
||||
$url = $this->url;
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
try {
|
||||
$response = $client->request('POST', $url, [
|
||||
'form_params' => $paramArray,
|
||||
'http_errors' => false,
|
||||
'timeout' => 3,
|
||||
]);
|
||||
|
||||
if ($response->getStatusCode() == 200) {
|
||||
$body = $response->getBody();
|
||||
$content = str_replace(["\r\n", "\n", "\r", '/n', "\\n",], '&', $body->getContents());
|
||||
parse_str($content, $data);
|
||||
|
||||
$this->data = $data;
|
||||
|
||||
if (isset($data['queryResult']) && $data['queryResult'] != 'SUCCESS') {
|
||||
throw new \Exception('返回的数据错误');
|
||||
}
|
||||
|
||||
if (isset($data['transRet']) && $data['transRet'] != 'SUCCESS') {
|
||||
throw new \Exception($data['resultDis']);
|
||||
}
|
||||
|
||||
$this->code = true;
|
||||
} else {
|
||||
throw new \Exception('未正常返回数据');
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->code = false;
|
||||
$this->message = '接口错误 '.$exception->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
//输出数据
|
||||
public function respond()
|
||||
{
|
||||
$data = [
|
||||
'code' => $this->code,
|
||||
'message' => $this->message,
|
||||
'data' => $this->data,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
46
src/Action/Order.php
Normal file
46
src/Action/Order.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace XuanChen\WoUnicomWeb\Action;
|
||||
|
||||
/**
|
||||
* Class Order 沃支付下单
|
||||
* @Author : 玄尘
|
||||
* @Date : 2021/4/30 9:34
|
||||
* @package XuanChen\Coupon\Action
|
||||
*/
|
||||
class Order extends Init
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes: 开始执行
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/4/30 10:49
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$this->addData();
|
||||
$this->setSignData();
|
||||
|
||||
return $this->getUnicomData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 增加基础数据
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/4/30 11:24
|
||||
*/
|
||||
public function addData()
|
||||
{
|
||||
$params = $this->params;
|
||||
$params = array_merge($params, [
|
||||
'version' => $this->version,
|
||||
'merNo' => $this->merNo,
|
||||
'storeName' => config('wounicomweb.storeName'),
|
||||
'signType' => config('wounicomweb.signType'),
|
||||
]);
|
||||
|
||||
$this->params = $params;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
41
src/Action/Query.php
Normal file
41
src/Action/Query.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace XuanChen\WoUnicomWeb\Action;
|
||||
|
||||
/**
|
||||
* Class Order 查询
|
||||
* @Author : 玄尘
|
||||
* @Date : 2021/4/30 9:34
|
||||
* @package XuanChen\Coupon\Action
|
||||
*/
|
||||
class Query extends Init
|
||||
{
|
||||
|
||||
public function start()
|
||||
{
|
||||
$this->setIsLower(false);
|
||||
$this->addData();
|
||||
$this->setSignData();
|
||||
$url = config('wounicomweb.uri.unicom.query');
|
||||
$this->sendPost($this->params, $url);
|
||||
|
||||
return $this->respond();
|
||||
}
|
||||
|
||||
public function addData()
|
||||
{
|
||||
$order = $this->params;
|
||||
|
||||
$data = [
|
||||
'merNo' => $this->merNo,
|
||||
'orderNo' => $order->orderid,
|
||||
'orderDate' => $order->created_at->format('Ymd'),
|
||||
'charSet' => 'UTF-8',
|
||||
'signType' => config('wounicomweb.signType'),
|
||||
];
|
||||
|
||||
$this->params = $data;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
46
src/Action/Refund.php
Normal file
46
src/Action/Refund.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace XuanChen\WoUnicomWeb\Action;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* Class Order 退款
|
||||
* @Author : 玄尘
|
||||
* @Date : 2021/4/30 9:34
|
||||
* @package XuanChen\Coupon\Action
|
||||
*/
|
||||
class Refund extends Init
|
||||
{
|
||||
|
||||
public function start()
|
||||
{
|
||||
$this->reSetParams();
|
||||
$this->setIsLower(false);
|
||||
$this->setSignData();
|
||||
$url = config('wounicomweb.uri.unicom.refund');
|
||||
$this->sendPost($this->params, $url);
|
||||
|
||||
return $this->respond();
|
||||
}
|
||||
|
||||
public function reSetParams()
|
||||
{
|
||||
$order = $this->params;
|
||||
|
||||
$data = [
|
||||
'refundReqJournl' => 'R' . date('YmdHis') . sprintf('%06d', rand(1, 999999999)),
|
||||
'merNo' => $this->merNo,
|
||||
'orderNo' => $order->orderid,
|
||||
'orderDate' => $order->created_at->format('Ymd'),
|
||||
'payJournl' => $order->payment->out_trade_no,
|
||||
'merReqTime' => Carbon::now()->format('YmdHis'),
|
||||
'amount' => $order->payment->amount * 100,
|
||||
'reason' => '退款',
|
||||
'signType' => 'RSA_SHA256',
|
||||
];
|
||||
|
||||
$this->params = $data;
|
||||
}
|
||||
|
||||
}
|
||||
25
src/Action/Sign.php
Normal file
25
src/Action/Sign.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace XuanChen\WoUnicomWeb\Action;
|
||||
|
||||
/**
|
||||
* Class Order 验证签名
|
||||
* @Author : 玄尘
|
||||
* @Date : 2021/4/30 9:34
|
||||
* @package XuanChen\Coupon\Action
|
||||
*/
|
||||
class Sign extends Init
|
||||
{
|
||||
|
||||
public function start()
|
||||
{
|
||||
$signMsg = $this->params['signmsg'];
|
||||
// $signMsg = str_replace('\\', '', $signMsg);
|
||||
$signMsg = str_replace(' ', '+', $signMsg);
|
||||
$this->sign = $signMsg;
|
||||
|
||||
return $this->checkSign();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user