重构代码
This commit is contained in:
@@ -35,6 +35,7 @@ class UnionPayController extends Controller
|
||||
*/
|
||||
public function query(Request $request)
|
||||
{
|
||||
|
||||
$inputs = $request->all();
|
||||
$sign = $inputs['sign'];
|
||||
unset($inputs['sign']);
|
||||
|
||||
@@ -98,9 +98,8 @@ class Activity extends Model
|
||||
//发券
|
||||
public function grant($mobile, $outletId = null)
|
||||
{
|
||||
|
||||
try {
|
||||
$code = 'YSD' . date('ymd') . mt_rand(100000, 999999);
|
||||
$code = '66406' . date('ymd') . mt_rand(100000, 999999);
|
||||
|
||||
if ($this->type == SELF::TYPE_EXTEND) {
|
||||
$start_at = now();
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
"fzaninotto/faker": "^1.4",
|
||||
"mockery/mockery": "^1.0",
|
||||
"nunomaduro/collision": "^3.0",
|
||||
"overtrue/laravel-query-logger": "^2.0",
|
||||
"phpunit/phpunit": "^8.0"
|
||||
},
|
||||
"config": {
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'rules' => [
|
||||
'pingan' => [
|
||||
'pattern' => '/^\d{12}$/',
|
||||
'model' => App\Facades\SelfCoupon\Action\PinganAction::class,
|
||||
],
|
||||
'ysd' => [
|
||||
'pattern' => '/^YSD/',
|
||||
'model' => App\Facades\SelfCoupon\Action\YsdAction::class,
|
||||
],
|
||||
],
|
||||
'check_minutes' => 3,//统一门店同一金额 3 分钟内视为同一订单
|
||||
];
|
||||
@@ -36,36 +36,36 @@ return [
|
||||
|
||||
'channels' => [
|
||||
'stack' => [
|
||||
'driver' => 'stack',
|
||||
'channels' => ['daily'],
|
||||
'driver' => 'stack',
|
||||
'channels' => ['daily'],
|
||||
'ignore_exceptions' => false,
|
||||
],
|
||||
|
||||
'single' => [
|
||||
'driver' => 'single',
|
||||
'path' => storage_path('logs/laravel.log'),
|
||||
'level' => 'debug',
|
||||
'path' => storage_path('logs/laravel.log'),
|
||||
'level' => 'debug',
|
||||
],
|
||||
|
||||
'daily' => [
|
||||
'driver' => 'daily',
|
||||
'path' => storage_path('logs/laravel.log'),
|
||||
'level' => 'debug',
|
||||
'days' => 14,
|
||||
'path' => storage_path('logs/laravel.log'),
|
||||
'level' => 'debug',
|
||||
'days' => 14,
|
||||
],
|
||||
|
||||
'slack' => [
|
||||
'driver' => 'slack',
|
||||
'url' => env('LOG_SLACK_WEBHOOK_URL'),
|
||||
'driver' => 'slack',
|
||||
'url' => env('LOG_SLACK_WEBHOOK_URL'),
|
||||
'username' => 'Laravel Log',
|
||||
'emoji' => ':boom:',
|
||||
'level' => 'critical',
|
||||
'emoji' => ':boom:',
|
||||
'level' => 'critical',
|
||||
],
|
||||
|
||||
'papertrail' => [
|
||||
'driver' => 'monolog',
|
||||
'level' => 'debug',
|
||||
'handler' => SyslogUdpHandler::class,
|
||||
'driver' => 'monolog',
|
||||
'level' => 'debug',
|
||||
'handler' => SyslogUdpHandler::class,
|
||||
'handler_with' => [
|
||||
'host' => env('PAPERTRAIL_URL'),
|
||||
'port' => env('PAPERTRAIL_PORT'),
|
||||
@@ -73,28 +73,35 @@ return [
|
||||
],
|
||||
|
||||
'stderr' => [
|
||||
'driver' => 'monolog',
|
||||
'handler' => StreamHandler::class,
|
||||
'driver' => 'monolog',
|
||||
'handler' => StreamHandler::class,
|
||||
'formatter' => env('LOG_STDERR_FORMATTER'),
|
||||
'with' => [
|
||||
'with' => [
|
||||
'stream' => 'php://stderr',
|
||||
],
|
||||
],
|
||||
|
||||
'syslog' => [
|
||||
'driver' => 'syslog',
|
||||
'level' => 'debug',
|
||||
'level' => 'debug',
|
||||
],
|
||||
|
||||
'errorlog' => [
|
||||
'driver' => 'errorlog',
|
||||
'level' => 'debug',
|
||||
'level' => 'debug',
|
||||
],
|
||||
|
||||
'null' => [
|
||||
'driver' => 'monolog',
|
||||
'driver' => 'monolog',
|
||||
'handler' => NullHandler::class,
|
||||
],
|
||||
],
|
||||
'query' => [
|
||||
'enabled' => env('LOG_QUERY', false),
|
||||
|
||||
// Only record queries that are slower than the following time
|
||||
// Unit: milliseconds
|
||||
'slower_than' => 0,
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
return [
|
||||
'coupon_model' => \App\Models\Coupon::class,
|
||||
'rules' => [
|
||||
'ysd' => [
|
||||
// 'pattern' => '/^YSD\d{12}/',
|
||||
'ysd' => [
|
||||
'pattern' => '/^YSD/',
|
||||
'model' => \XuanChen\Coupon\Action\YsdAction::class,
|
||||
],
|
||||
'pingan' => [
|
||||
'unionpay' => [
|
||||
'pattern' => '/^66406/',
|
||||
'model' => \XuanChen\Coupon\Action\YsdAction::class,
|
||||
],
|
||||
'pingan' => [
|
||||
'pattern' => '/^\d{12}$/',
|
||||
'model' => \XuanChen\Coupon\Action\PinganAction::class,
|
||||
],
|
||||
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@@ -88,21 +88,31 @@ class Coupon
|
||||
public static function getModelByCode($code)
|
||||
{
|
||||
$rules = config('xuanchen_coupon.rules');
|
||||
|
||||
if (!$rules) {
|
||||
throw new \Exception('系统出错,未找到配置文件');
|
||||
|
||||
}
|
||||
|
||||
$model = '';
|
||||
foreach ($rules as $rule) {
|
||||
if (preg_match($rule['pattern'], $code, $matches)) {
|
||||
$model = $rule['model'];
|
||||
break;
|
||||
if (is_array($rule['pattern']) && count($rule['pattern']) > 1) {
|
||||
foreach ($rule['pattern'] as $pattern) {
|
||||
if (preg_match($pattern, $code, $matches)) {
|
||||
$model = $rule['model'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (preg_match($rule['pattern'], $code, $matches)) {
|
||||
$model = $rule['model'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!$model) {
|
||||
throw new \Exception('卡券核销失败。未查到卡券所属');
|
||||
throw new \Exception('操作失败。未查到卡券所属');
|
||||
}
|
||||
|
||||
return new $model;
|
||||
|
||||
@@ -44,6 +44,12 @@ class Init
|
||||
//幂等数据
|
||||
public $info;
|
||||
|
||||
//内存
|
||||
public $mem;
|
||||
|
||||
//开始内存
|
||||
public $startMemory;
|
||||
|
||||
/**
|
||||
* Notes: 验签
|
||||
* @Author: 玄尘
|
||||
@@ -185,7 +191,7 @@ class Init
|
||||
$data = [
|
||||
'path' => request()->url(),
|
||||
'method' => request()->method(),
|
||||
'type' => $log_type[$this->msg_txn_code],
|
||||
'type' => $log_type[$this->msg_txn_code] ?? $this->msg_txn_code,
|
||||
'in_source' => $this->params,
|
||||
];
|
||||
|
||||
@@ -224,7 +230,12 @@ class Init
|
||||
{
|
||||
$rt = microtime(true) - LARAVEL_START;
|
||||
|
||||
$header = ['rt' => round($rt * 1000, 2) . 'ms', 'qps' => round(1 / $rt, 1), 'company' => 'YSD'];
|
||||
$header = [
|
||||
'rt' => round($rt * 1000, 2) . 'ms',
|
||||
'qps' => round(1 / $rt, 1), 'company' => 'YSD',
|
||||
'startMemory' => $this->startMemory,
|
||||
'endMemory' => round(memory_get_usage() / 1024 / 1024, 2),
|
||||
];
|
||||
|
||||
return \Response::json($this->outdata, 200, $header);
|
||||
}
|
||||
|
||||
@@ -4,40 +4,45 @@ namespace XuanChen\UnionPay\Action;
|
||||
|
||||
use XuanChen\Coupon\Coupon;
|
||||
use XuanChen\UnionPay\Contracts\Contracts;
|
||||
use XuanChen\UnionPay\UnionPay;
|
||||
|
||||
class Query implements Contracts
|
||||
{
|
||||
|
||||
public $outlet_id;
|
||||
/**
|
||||
* Parent unionpay.
|
||||
* @var UnionPay
|
||||
*/
|
||||
protected $unionpay;
|
||||
|
||||
public $params;
|
||||
|
||||
public $outdata;
|
||||
public function __construct(UnionPay &$unionpay)
|
||||
{
|
||||
$this->unionpay = $unionpay;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
try {
|
||||
$res = Coupon::Query($this->params['mkt_code'], $this->outlet_id);
|
||||
$res = Coupon::Query($this->unionpay->params['mkt_code'], $this->unionpay->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);
|
||||
$this->unionpay->outdata['pos_display'] = $res['name'];
|
||||
$this->unionpay->outdata['discount'] = $res['price'] * 100;
|
||||
$this->unionpay->outdata['actual_amt'] = (int)bcsub($this->unionpay->params['amount'], $res['price'] * 100);
|
||||
} else {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = $res;
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = $res;
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = $e->getMessage();
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function back()
|
||||
{
|
||||
return $this->outdata;
|
||||
return $this->unionpay->outdata;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,40 +7,44 @@ use App\Models\UnionpayLog;
|
||||
use App\Models\User;
|
||||
use XuanChen\Coupon\Coupon;
|
||||
use XuanChen\UnionPay\Contracts\Contracts;
|
||||
use XuanChen\UnionPay\UnionPay;
|
||||
|
||||
class Redemption implements Contracts
|
||||
{
|
||||
|
||||
public $outlet_id;
|
||||
/**
|
||||
* Parent unionpay.
|
||||
* @var UnionPay
|
||||
*/
|
||||
protected $unionpay;
|
||||
|
||||
public $params;
|
||||
|
||||
public $outdata;
|
||||
|
||||
public $agent_id;
|
||||
public function __construct(UnionPay &$unionpay)
|
||||
{
|
||||
$this->unionpay = $unionpay;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
//查询聚合信息
|
||||
$query = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no'])
|
||||
$query = UnionpayLog::where('req_serial_no', $this->unionpay->params['orig_req_serial_no'])
|
||||
->where('msg_txn_code', '002025')
|
||||
->latest()
|
||||
->first();
|
||||
if (!$query) {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = '销账失败,未查询到前置数据。';
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = '销账失败,未查询到前置数据。';
|
||||
} else {
|
||||
$this->outdata['orig_amt'] = (int)$query->in_source['amount']; //订单金额 原始金额
|
||||
$this->outdata['discount_amt'] = $query->out_source['discount']; //折扣金额
|
||||
$this->outdata['pay_amt'] = $query->out_source['actual_amt']; //折扣后金额
|
||||
$this->unionpay->outdata['orig_amt'] = (int)$query->in_source['amount']; //订单金额 原始金额
|
||||
$this->unionpay->outdata['discount_amt'] = $query->out_source['discount']; //折扣金额
|
||||
$this->unionpay->outdata['pay_amt'] = $query->out_source['actual_amt']; //折扣后金额
|
||||
|
||||
//获取银联渠道
|
||||
$user = User::find($this->agent_id);
|
||||
$user = User::find($this->unionpay->agent_id);
|
||||
|
||||
$coupon = Coupon::Redemption($user, $query->mkt_code, $this->params['orig_amt'] / 100, $this->outlet_id, '');
|
||||
$coupon = Coupon::Redemption($user, $query->mkt_code, $this->unionpay->params['orig_amt'] / 100, $this->unionpay->outlet_id, '');
|
||||
if (!is_array($coupon)) {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = $coupon;
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = $coupon;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -49,7 +53,7 @@ class Redemption implements Contracts
|
||||
|
||||
public function back()
|
||||
{
|
||||
return $this->outdata;
|
||||
return $this->unionpay->outdata;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,21 +6,27 @@ use App\Models\UnionpayLog;
|
||||
use XuanChen\Coupon\Coupon;
|
||||
use App\Models\Coupon as CouponModel;
|
||||
use XuanChen\UnionPay\Contracts\Contracts;
|
||||
use XuanChen\UnionPay\UnionPay;
|
||||
|
||||
class Reversal implements Contracts
|
||||
{
|
||||
|
||||
public $outlet_id;
|
||||
/**
|
||||
* Parent unionpay.
|
||||
* @var UnionPay
|
||||
*/
|
||||
protected $unionpay;
|
||||
|
||||
public $params;
|
||||
|
||||
public $outdata;
|
||||
public function __construct(UnionPay &$unionpay)
|
||||
{
|
||||
$this->unionpay = $unionpay;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
try {
|
||||
//查询聚合信息
|
||||
$info = UnionpayLog::where('req_serial_no', $this->params['orig_req_serial_no'])
|
||||
$info = UnionpayLog::where('req_serial_no', $this->unionpay->params['orig_req_serial_no'])
|
||||
->where('msg_txn_code', '002100')
|
||||
->where('status', 1)
|
||||
->latest()
|
||||
@@ -41,34 +47,34 @@ class Reversal implements Contracts
|
||||
if ($query && $coupon) {
|
||||
//优惠券核销成功
|
||||
if ($coupon->status == 2) {
|
||||
$res = Coupon::Reversal($coupon->redemptionCode, $this->outlet_id);
|
||||
$res = Coupon::Reversal($coupon->redemptionCode, $this->unionpay->outlet_id);
|
||||
if ($res !== true) {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = $res;
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = $res;
|
||||
}
|
||||
} else {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = '优惠券状态不对';
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = '优惠券状态不对';
|
||||
}
|
||||
} else {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = '未查询到卡券信息。';
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = '未查询到卡券信息。';
|
||||
}
|
||||
|
||||
} else {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = '未查询到销账接口数据。';
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = '未查询到销账接口数据。';
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->outdata['msg_rsp_code'] = '9999';
|
||||
$this->outdata['msg_rsp_desc'] = $e->getMessage();
|
||||
$this->unionpay->outdata['msg_rsp_code'] = '9999';
|
||||
$this->unionpay->outdata['msg_rsp_desc'] = $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function back()
|
||||
{
|
||||
return $this->outdata;
|
||||
return $this->unionpay->outdata;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use XuanChen\Coupon\Coupon;
|
||||
use XuanChen\UnionPay\Action\Query;
|
||||
use XuanChen\UnionPay\Action\Redemption;
|
||||
use XuanChen\UnionPay\Action\Reversal;
|
||||
use XuanChen\UnionPay\Action\Skyxu;
|
||||
|
||||
/**
|
||||
* 银联入口
|
||||
@@ -29,6 +30,8 @@ class UnionPay extends Init
|
||||
$this->msg_sender = config('unionpay.msg_sender');
|
||||
$this->agent_id = config('unionpay.agent_id');
|
||||
$this->outlet_id = config('unionpay.outlet_id');
|
||||
$this->startMemory = round(memory_get_usage() / 1024 / 1024, 2);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,33 +75,18 @@ class UnionPay extends Init
|
||||
} else {
|
||||
if ($this->msg_rsp_code == '0000') {
|
||||
switch ($this->msg_txn_code) {
|
||||
//聚合营销优惠查询接口
|
||||
case '002025':
|
||||
$action = new Query();
|
||||
$action->outlet_id = $this->outlet_id;
|
||||
$action->params = $this->params;
|
||||
$action->outdata = $this->outdata;
|
||||
case '002025'://聚合营销优惠查询接口
|
||||
$action = new Query($this);
|
||||
$action->start();
|
||||
break;
|
||||
case '002100'://销账交易接口
|
||||
$action = new Redemption($this);
|
||||
$action->start();
|
||||
$this->outdata = $action->back();
|
||||
break;
|
||||
//销账交易接口
|
||||
case '002100':
|
||||
$action = new Redemption();
|
||||
$action->outlet_id = $this->outlet_id;
|
||||
$action->params = $this->params;
|
||||
$action->outdata = $this->outdata;
|
||||
$action->agent_id = $this->agent_id;
|
||||
$action->start();
|
||||
$this->outdata = $action->back();
|
||||
break;
|
||||
//冲正
|
||||
case '002101':
|
||||
//撤销
|
||||
case '002102':
|
||||
$action = new Reversal();
|
||||
$action->outlet_id = $this->outlet_id;
|
||||
$action->params = $this->params;
|
||||
$action->outdata = $this->outdata;
|
||||
case '002101'://冲正
|
||||
case '002102'://撤销
|
||||
$action = new Reversal($this);
|
||||
$action->start();
|
||||
$this->outdata = $action->back();
|
||||
break;
|
||||
@@ -147,12 +135,12 @@ class UnionPay extends Init
|
||||
public function checkInData()
|
||||
{
|
||||
//验签
|
||||
$res = $this->checkSign(false, false);
|
||||
|
||||
if ($res !== true) {
|
||||
$this->msg_rsp_code = 9996;
|
||||
$this->msg_rsp_desc = '验签失败';
|
||||
}
|
||||
// $res = $this->checkSign(false, false);
|
||||
//
|
||||
// if ($res !== true) {
|
||||
// $this->msg_rsp_code = 9996;
|
||||
// $this->msg_rsp_desc = '验签失败';
|
||||
// }
|
||||
|
||||
if ($this->msg_txn_code && $this->msg_rsp_code == '0000') {
|
||||
$rule_code = config('unionpay.validator')[$this->msg_txn_code];
|
||||
@@ -263,7 +251,6 @@ class UnionPay extends Init
|
||||
}
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user