阶段性提交代码

This commit is contained in:
2020-10-10 14:57:22 +08:00
parent c909b9b1ed
commit 7cfd45bf99
14 changed files with 540 additions and 211 deletions

View File

@@ -1,32 +0,0 @@
<?php
namespace XuanChen\Coupon;
use Illuminate\Support\ServiceProvider;
class CouponServiceProvider extends ServiceProvider
{
/**
* Register services.
* @return void
*/
public function register()
{
dd(1);
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/xuanchen_coupon.php' => config_path('xuanchen_coupon.php')]);
}
}
/**
* Bootstrap services.
* @return void
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__ . '/../config/xuanchen_coupon.php', 'xuanchen_coupon');
}
}

View File

@@ -22,7 +22,7 @@ class ZhyCreate implements CheckCouponContracts
"marketRuleNumber" => "001",
"perVoucherNumber" => "1",
"planVoucherNumber" => "10",
"queryCreateTime" => "2019-11-11 18=> 39=> 49",
"queryCreateTime" => "2019-11-11 18:39:49",
"similarStack" => "Y",
"voucherEndTime" => "2019-12-15",
"voucher_ruleCode" => "$01-VC-1573468710381900",
@@ -35,8 +35,8 @@ class ZhyCreate implements CheckCouponContracts
"limitFrequency" => "",
"mabIsRefund" => "",
"marketCode" => "20191213154203514",
"marketExEndTime" => "2019-12-15 00=> 00=> 00",
"marketExStartTime" => "2019-12-13 00=> 00=> 00",
"marketExEndTime" => "2019-12-15 00:00:00",
"marketExStartTime" => "2019-12-13 00:00:00",
"marketHierarchy" => "3311",
"marketName" => "行为推送活动-3",
"marketProvince" => "51",

View File

@@ -7,7 +7,7 @@ use App\Models\User;
use Illuminate\Support\Facades\DB;
/**
* 自有卡券系统
* 中石化接口
*/
class Sinopec
{

View File

@@ -2,13 +2,18 @@
namespace XuanChen\UnionPay\Action;
use App\Exceptions\ApiException;
use App\Exceptions\ApiUnionpayException;
use App\Models\Log as LogModel;
use Illuminate\Http\Request;
class Init
{
//传入的参数
public $params;
//传入的签名
public $sign;
public $msg_type = '00';
@@ -30,6 +35,15 @@ class Init
//网点id
public $outlet_id;
//渠道id
public $agent_id;
//日志
public $log;
//幂等数据
public $info;
/**
* Notes: 验签
* @Author: 玄尘
@@ -37,22 +51,22 @@ class Init
* @param false $self 是否是自己的证书
* @return bool|string
*/
public function checkSign($self = false)
public function checkSign($out = true, $self = false)
{
$sign = $this->sign;
$sign = base64_decode($sign);
$sign = hex2bin($this->sign);
$public_key = $this->getPublic($self);
$pub_key_id = openssl_get_publickey($public_key);
$signStr = $this->getSignString($this->params);
$signStr = $this->getSignString($out);
if ($pub_key_id) {
$result = (bool)openssl_verify($signStr, $sign, $pub_key_id);
openssl_free_key($pub_key_id);
} else {
return '钥格式有误!';
throw new \Exception('钥格式有误');
}
return $result;
@@ -61,8 +75,10 @@ class Init
/**
* Notes: 签名
* @Author: 玄尘
* @Date : 2020/9/29 9:56
* @Date : 2020/10/9 15:52
* @param bool $self
* @return string
* @throws \Exception
*/
public function getSign($out = true)
{
@@ -71,13 +87,18 @@ class Init
$privKeyId = openssl_pkey_get_private($private_key);
if (!$privKeyId) {
return '私钥格式有误';
throw new \Exception('私钥格式有误');
}
if (openssl_sign($signStr, $signature, $privKeyId)) {
$signature = bin2hex($signature);
} else {
throw new \Exception('签名错误');
}
openssl_sign($signStr, $signature, $privKeyId);
openssl_free_key($privKeyId);
return base64_encode($signature);
return $signature;
}
/**
@@ -87,7 +108,7 @@ class Init
* @param $out 是否是获取返回值的数据
* @return string
*/
public function getSignString($out)
public function getSignString($out = false)
{
if ($out) {
$params = array_filter($this->outdata);
@@ -95,10 +116,15 @@ class Init
$params = array_filter($this->params);
}
ksort($params);
$signStr = http_build_query($params);
if (empty($params)) {
throw new \Exception('缺少数据');
}
ksort($params);
//http_build_query 会自动urlencode 需要转换
return $this->str2utf8(urldecode(http_build_query($params)));
return $signStr;
}
//获取私钥
@@ -125,13 +151,48 @@ class Init
return file_get_contents($public);
}
//更新返回值
public function updateOutData()
/**
* Notes: 插入日志
* @Author: 玄尘
* @Date : 2020/10/9 14:38
* @return mixed
*/
public function addLog()
{
$this->outdata['sign'] = $this->getSign();
$this->model->out_source = $this->outdata;
$this->model->save();
$log_type = config('unionpay.log_type');
$data = [
'path' => request()->url(),
'method' => request()->method(),
'type' => $log_type[$this->msg_txn_code],
'in_source' => $this->params,
];
return $this->log = LogModel::create($data);
}
/**
* Notes: 更新日志
* @Author: 玄尘
* @Date : 2020/10/9 14:46
*/
public function updateLog()
{
$this->log->out_source = $this->outdata;
$this->log->save();
}
/**
* 将字符串编码转为 utf8
* @param $str
* @return string
*/
public function str2utf8($str)
{
$encode = mb_detect_encoding($str, ['ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5']);
$str = $str ? $str : mb_convert_encoding($str, 'UTF-8', $encode);
$str = is_string($str) ? $str : '';
return $str;
}
//输出数据

View File

@@ -22,8 +22,9 @@ class UnionPay extends Init
{
$this->params = $params;
$this->sign = $sign;
$this->msg_txn_code = $params['msg_txn_code'];
$this->msg_txn_code = $params['msg_txn_code'] ?? '';
$this->msg_sender = config('unionpay.msg_sender');
$this->agent_id = config('unionpay.agent_id');
$this->outlet_id = config('unionpay.outlet_id');
}
@@ -34,65 +35,153 @@ class UnionPay extends Init
*/
public function start()
{
$this->updateInputData();
$this->out_data();
//设置基础数据
$this->getOutBaseData();
try {
//校验数据
$this->checkInData();
//查询是否是幂等 就是重复查询
$this->idempotent();
//入库请求参数
$this->InputData();
//返回值
$this->out_data();
//更新数据
$this->updateOutData();
} catch (\Exception $e) {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $e->getMessage();
if (empty($this->model->out_source)) {
$this->updateOutData();
}
}
}
//处理流程
public function out_data()
{
$this->getOutBaseData();
if ($this->msg_rsp_code == '0000') {
switch ($this->msg_txn_code) {
//聚合营销优惠查询接口
case '002025':
$res = Coupon::Query($this->params['mkt_code'], $this->outlet_id);
if (is_array($res)) {
$this->outdata['pos_display'] = $res['name'];
$this->outdata['discount'] = $res['price'];
$this->outdata['actual_amt'] = bcsub($this->params['amount'], $res['price'], 4);
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $res;
}
break;
//销账交易接口
case '002100':
//查询聚合信息
$info = UnionpayLog::where('msg_crrltn_id', $this->params['msg_crrltn_id'])
->where('msg_txn_code', '002025')
->latest()
->first();
//获取配置数据
$conf_unionpay = config('unionpay');
//获取银联渠道
$user = User::find($conf_unionpay['agent_id']);
if ($info) {
$coupon = Coupon::Redemption($user, $info->mkt_code, $this->params['orig_amt'], $conf_unionpay['outlet_id'], '');
if (!is_array($coupon)) {
//是幂等
if ($this->info && !empty($this->info->out_source)) {
$this->outdata = $this->info->out_source;
} else {
if ($this->msg_rsp_code == '0000') {
switch ($this->msg_txn_code) {
//聚合营销优惠查询接口
case '002025':
$res = Coupon::Query($this->params['mkt_code'], $this->outlet_id);
if (is_array($res)) {
$this->outdata['pos_display'] = $res['name'];
$this->outdata['discount'] = $res['price'] * 100;
$this->outdata['actual_amt'] = (int)bcsub($this->params['amount'], $res['price'] * 100);
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $coupon;
$this->outdata['msg_rsp_desc'] = $res;
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。';
}
break;
case '002101':
break;
case '002102':
break;
default:
break;
}
break;
//销账交易接口
case '002100':
//查询聚合信息
$query = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no'])
->where('msg_txn_code', '002025')
->latest()
->first();
$this->outdata['orig_amt'] = $query->in_source['amount']; //订单金额 原始金额
$this->outdata['discount_amt'] = $query->out_source['discount']; //折扣金额
$this->outdata['pay_amt'] = $query->out_source['actual_amt']; //折扣后金额
//获取银联渠道
$user = User::find($this->agent_id);
if ($query) {
$coupon = [];
$coupon = Coupon::Redemption($user, $query->mkt_code, $this->params['orig_amt'] / 100, $this->outlet_id, '');
if (!is_array($coupon)) {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $coupon;
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。';
}
break;
case '002101':
break;
case '002102':
//查询聚合信息
$info = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no'])
->where('msg_txn_code', '002025')
->latest()
->first();
if ($info) {
if ($info->coupon) {
$ret = $info->coupon->reprofit();
if ($ret !== true) {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = $ret;
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '为查询到优惠券信息。';
}
} else {
$this->outdata['msg_rsp_code'] = '9999';
$this->outdata['msg_rsp_desc'] = '未查询到前置接口,获取券码失败。';
}
break;
default:
break;
}
}
}
}
/**
* Notes: 入库数据
* @Author: 玄尘
* @Date : 2020/9/30 8:46
*/
private function InputData()
{
//获取基础数据
$base = config('unionpay.regular')[$this->msg_txn_code];
$data = [];
//循环获取入库数据
foreach ($this->params as $key => $param) {
if (in_array($key, $base)) {
$data[$key] = $param;
} else {
$data['in_source'][$key] = $param;
}
}
$this->model = UnionpayLog::create($data);
if (empty($this->model)) {
throw new \Exception('数据入库失败');
}
}
/**
* Notes: 校验输入的数据
* @Author: 玄尘
* @Date : 2020/9/30 14:46
*/
public function checkInData()
{
if ($this->params['msg_txn_code'] == '002025' && (!isset($this->params['mkt_code']) || empty($this->params['mkt_code']))) {
$this->msg_rsp_code = 9999;
$this->msg_rsp_desc = '聚合营销码不能为空';
}
}
/**
* Notes: 返回的基础数据
* @Author: 玄尘
@@ -107,7 +196,7 @@ class UnionPay extends Init
"msg_flg" => 1,
"msg_sender" => $this->msg_sender,
"msg_time" => now()->format('YmdHis'),
"msg_sys_sn" => $this->params['msg_sys_sn'],
"msg_sys_sn" => $this->params['msg_sys_sn'] ?? '',
"msg_rsp_code" => $this->msg_rsp_code,
"msg_rsp_desc" => $this->msg_rsp_desc,
];
@@ -130,65 +219,52 @@ class UnionPay extends Init
'discount_amt' => $this->params['discount_amt'],
'pay_amt' => $this->params['pay_amt'],
'serv_chg' => config('unionpay.serv_chg'),
'commission' => $this->params['orig_amt'],
'event_no' => '',//什么是活动号
'commission' => config('unionpay.commission'),
'event_no' => '',//活动号 直接为空就可以
]);
break;
case '002101':
$basics = array_merge($basics, [
'msg_ver' => 0.1,
]);
break;
case '002102':
$basics = array_merge($basics, [
'msg_ver' => 0.1,
]);
break;
default:
break;
}
$this->outdata = $basics;
}
/**
* Notes: 入库数据
* @Author: 玄尘
* @Date : 2020/9/30 8:46
*/
private function updateInputData()
{
try {
if (!isset($this->params['msg_sys_sn'])) {
$this->params['msg_sys_sn'] = date('ymdHis') . sprintf('%06d', rand(1, 99999999));
}
//获取基础数据
$base = config('unionpay.regular')[$this->msg_txn_code];
$data = [];
foreach ($this->params as $key => $param) {
if (in_array($key, $base)) {
$data[$key] = $param;
} else {
$data['in_source'][$key] = $param;
}
}
$this->model = UnionpayLog::create($data);
return true;
} catch (\Exception $e) {
$this->msg_rsp_code = 9999;
$this->msg_rsp_desc = $e->getMessage();
return false;
}
return $this->outdata = $basics;
}
/**
* Notes: 校验输入的数据
* Notes: 查询是否是幂等
* @Author: 玄尘
* @Date : 2020/9/30 14:46
* @Date : 2020/10/10 13:25
*/
public function checkInData()
public function idempotent()
{
if ($this->params['msg_txn_code'] == '002025' && (!isset($this->params['mkt_code']) || empty($this->params['mkt_code']))) {
$this->msg_rsp_code = 9999;
$this->msg_rsp_desc = '聚合营销码不能为空';
$this->info = UnionpayLog::where('req_serial_no', $this->params['req_serial_no'])
->where('msg_txn_code', $this->msg_txn_code)
->where('status', 1)
->latest()
->first();
}
//更新返回值
public function updateOutData()
{
$this->outdata['sign'] = $this->getSign();
$this->model->out_source = $this->outdata;
if ($this->outdata['msg_rsp_code'] == '9999') {
$this->model->status = 0;
}
$this->model->save();
}
}