diff --git a/app/Admin/Controllers/Coupon/IndexController.php b/app/Admin/Controllers/Coupon/IndexController.php index 2277197..98241d5 100644 --- a/app/Admin/Controllers/Coupon/IndexController.php +++ b/app/Admin/Controllers/Coupon/IndexController.php @@ -41,7 +41,7 @@ class IndexController extends AdminController $users = User::whereHas('identity', function ($query) { $query->where('identity_id', 1); })->get()->pluck('nickname', 'id'); - + $filter->equal('user_id', '渠道')->select($users); $filter->equal('thirdPartyGoodsId', '优惠政策')->select(ActivityRule::pluck('title', 'code')); }); diff --git a/app/Admin/Controllers/Unionpay/IndexController.php b/app/Admin/Controllers/Unionpay/IndexController.php new file mode 100644 index 0000000..14f2eb4 --- /dev/null +++ b/app/Admin/Controllers/Unionpay/IndexController.php @@ -0,0 +1,94 @@ + + * @Date : 2019/9/18 14:50 + * @return Grid + */ + protected function grid() + { + $grid = new Grid(new UnionpayLog); + + $grid->disableActions(); + + $grid->model()->orderBy('id', 'desc'); + + $grid->filter(function ($filter) { + $filter->column(1 / 2, function ($filter) { + $filter->equal('msg_txn_code', '交易类型')->select(config('unionpay.type')); + }); + }); + + $grid->column('id', '#ID#'); + $grid->column('msg_txn_code', '交易类型') + ->using(config('unionpay.type')) + ->label(); + + $grid->column('msg_crrltn_id', '消息关联号'); + $grid->column('msg_time', '报文日期'); + $grid->column('mkt_code', '券码'); + $grid->column('msg_sys_sn', '平台流水号'); + $grid->column('req_serial_no', '查询流水号'); + + // $grid->column('请求参数')->display(function ($title, $column) { + // return '点击展开'; + // })->modal(function ($model) { + // $in_source = $this->in_source; + // if (is_array($in_source) && count($in_source) > 1) { + // foreach ($in_source as $key => $item) { + // dump($this->id); + // $title[] = $key; + // $data[] = $item; + // } + // + // return new Table($title, $data); + // } + // }); + + $grid->column('请求参数')->display(function () { + $in_source = $this->in_source; + + return $in_source; + })->hide(); + + $grid->column('返回信息')->display(function () { + $out_source = $this->out_source; + + return $out_source; + })->hide(); + + $grid->column('created_at', '注册时间'); + $grid->disableExport(false); + + $grid->export(function ($export) { + $export->filename($this->title . date("YmdHis")); + }); + + return $grid; + } + +} diff --git a/app/Admin/routes.php b/app/Admin/routes.php index 9010744..41217c7 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -55,4 +55,7 @@ Route::group([ $router->resource('activitycoupons', 'Activity\CouponController'); $router->resource('activitycouponlogs', 'Activity\LogController'); + //银联日志 + $router->resource('unionpays', 'Unionpay\IndexController'); + }); diff --git a/app/Api/Controllers/UnionPayController.php b/app/Api/Controllers/UnionPayController.php index ab5cde6..e8fff8f 100644 --- a/app/Api/Controllers/UnionPayController.php +++ b/app/Api/Controllers/UnionPayController.php @@ -1,54 +1,98 @@ - '15663876870', - 'user_code' => '1', - 'user_code_type' => '8521475DDDeeee', - 'dev_id' => '1', - ]; - dd(json_encode($user_ext_info)); - $str = 'msg_type=00&msg_txn_code=002100&msg_crrltn_id=12345678901234567890123456789000&msg_flg=0&msg_sender=01&msg_time=20130318162412&msg_sys_sn=12345678900987654321&msg_ver=0.1&mchnt_no=8981231234567890&term_no=12345678&shop_no=086123456123456789&req_serial_no=12345678900987654321&orig_req_serial_no=12345678900987654321&enc_card_no=123123123123123123123sdfadqerqr&acq_term_sn=123456&refer_no=123456789012&sett_date=20130318&txn_date=20130318&txn_time=162850&orig_amt=10000&discount_amt=2000&pay_amt=8000&pay_mode=1'; - parse_str($str, $arr_str); - - $action = new UnionPay($arr_str); - $sign = $action->getSign(); - $action->sign = $sign; - $res = $action->checkSign(true); - dump($sign); - dump($res); - - } - - /** - * Notes: 银联接口 - * @Author: 玄尘 - * @Date : 2020/9/28 16:31 - * @param Request $request - * @return mixed - */ - public function query(Request $request) - { - $inputs = $request->all(); - $sign = $inputs['sign']; - unset($inputs['sign']); - - $action = new UnionPay($inputs, $sign); - $action->start(); - $action->updateOutData(); - - return $action->respond(); - - } - -} +all()); + // $sign = $action->getSign(false); + $sign = 'd8e5bf46d0d9f1da702170c2e141d85cf3ca785106886dbfedb3310ee9ce2ca3f18a2e6c179ec9908fc4f41d05df463634106918bdbefc63b8f199c7d2f3b0d45510b4dd6ccdf4549e11a8551a5098b14c01fdaa5840a4608f462fdafdc14b8f2a35471da315d8245a4ef6281b6e04bd22d5a266500a6caf6e5203202c37111d'; + $action->sign = $sign; + $res = $action->checkSign(false, false); + dump('签名: ' . $sign); + $res_str = ($res === true) ? '成功' : '失败'; + dump('验签结果:' . $res_str); + dd($action); + + } + + //签名 + public function toSign($sign_data) + { + //签名字段 + // $sign_data['msg_type'] = $array['msg_type']; + // $sign_data['msg_txn_code'] = $array['msg_txn_code']; + // $sign_data['msg_crrltn_id'] = $array['msg_crrltn_id']; + // $sign_data['msg_flg'] = $array['msg_flg']; + // $sign_data['msg_sender'] = $array['msg_sender']; + // $sign_data['msg_time'] = $array['msg_time']; + // $sign_data['msg_sys_sn'] = $array['msg_sys_sn']; + // $sign_data['msg_ver'] = $array['msg_ver']; + + // if (isset($array['msg_rsp_code'])) { + // $sign_data['msg_rsp_code'] = $array['msg_rsp_code']; + // } + + // if (isset($array['msg_rsp_desc'])) { + // $sign_data['msg_rsp_desc'] = $array['msg_rsp_desc']; + // } + + //将字段名按字母表顺序排序 + ksort($sign_data); + + //将post的数据utf-8编码 + $str_data = utf8_encode(http_build_query($sign_data)); + //获取私钥资源ID + $private = config('unionpay.check.self.private'); + + if (!file_exists($private)) { + throw new \Exception('缺少私钥文件'); + } + + $private_key = file_get_contents($private); + $pkeyid = openssl_get_privatekey($private_key); + + //签名赋值sign + if (openssl_sign($str_data, $sign, $pkeyid)) { + $sign_data = bin2hex($sign); + } else { + $sign_data = ''; + } + //释放ssl资源 + openssl_free_key($pkeyid); + + return $sign_data; + } + + /** + * Notes: 银联接口 + * @Author: 玄尘 + * @Date : 2020/9/28 16:31 + * @param Request $request + * @return mixed + */ + public function query(Request $request) + { + $inputs = $request->all(); + $sign = $inputs['sign']; + unset($inputs['sign']); + $action = new UnionPay($inputs, $sign); + + // $action->addLog(); + $action->start(); + + // $action->updateLog(); + + return $action->respond(); + + } + +} diff --git a/app/Exceptions/ApiUnionpayException.php b/app/Exceptions/ApiUnionpayException.php new file mode 100644 index 0000000..2c1fa39 --- /dev/null +++ b/app/Exceptions/ApiUnionpayException.php @@ -0,0 +1,45 @@ +data = $data; + } + + /** + * @return array + */ + public function getData() + { + return $this->data; + } + + /** + * 异常输出 + */ + public function render($request) + { + return response()->json($this->getData(), 200); + } + +} \ No newline at end of file diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 043cad6..a94e5d4 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -7,18 +7,17 @@ use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { + /** * A list of the exception types that are not reported. - * * @var array */ protected $dontReport = [ - // + ApiException::class, ]; /** * A list of the inputs that are never flashed for validation exceptions. - * * @var array */ protected $dontFlash = [ @@ -28,24 +27,24 @@ class Handler extends ExceptionHandler /** * Report or log an exception. - * - * @param \Exception $exception + * @param \Exception $exception * @return void */ public function report(Exception $exception) { + info($exception->getMessage()); parent::report($exception); } /** * Render an exception into an HTTP response. - * - * @param \Illuminate\Http\Request $request - * @param \Exception $exception + * @param \Illuminate\Http\Request $request + * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { return parent::render($request, $exception); } + } diff --git a/app/Models/Coupon.php b/app/Models/Coupon.php index 9c58368..5831f90 100644 --- a/app/Models/Coupon.php +++ b/app/Models/Coupon.php @@ -88,6 +88,24 @@ class Coupon extends Model } } + /** + * Notes: 撤销分润 + * @Author: 玄尘 + * @Date : 2020/10/9 14:07 + */ + public function reprofit() + { + if ($this->profit > 1) { + return $this->user->account->rule('refreeze', -$this->profit, false, [ + 'coupon_id' => $this->id, + 'redemptionCode' => $this->redemptionCode, + ]); + } else { + return '不需要操作'; + } + + } + /** * 打款 * @author 玄尘 2020-03-13 diff --git a/app/Models/UnionpayLog.php b/app/Models/UnionpayLog.php index 254b874..4ac1bde 100644 --- a/app/Models/UnionpayLog.php +++ b/app/Models/UnionpayLog.php @@ -10,4 +10,9 @@ class UnionpayLog extends Model 'out_source' => 'array', ]; + public function coupon() + { + return $this->hasOne(Coupon::class, 'redemptionCode', 'mkt_code'); + } + } diff --git a/config/unionpay.php b/config/unionpay.php index ef62bf6..cfc82cc 100644 --- a/config/unionpay.php +++ b/config/unionpay.php @@ -2,13 +2,13 @@ return [ //分配的渠道号 - 'msg_sender' => 01, + 'msg_sender' => '660134', //打印在小票上,由活动标题、优惠金额、原始金额组合而成 - 'pos_receipt' => '', + 'pos_receipt' => '本时生活,优惠生活', //广告,用于打印在小票上 'pos_ad' => '', //营销联盟广告,用于打印在小票上 - 'pos_mkt_ad' => '', + 'pos_mkt_ad' => '本时生活,优惠生活', //银联渠道id 'agent_id' => '299', //银联网点id @@ -17,6 +17,7 @@ return [ 'serv_chg' => 0, //佣金 'commission' => 0, + //证书 'check' => [ 'self' => [ 'private' => storage_path('cert/unionpay/self/private_rsa.pem'), @@ -26,6 +27,18 @@ return [ 'public' => storage_path('cert/unionpay/public_rsa.pem'), ], ], + 'type' => [ + '002025' => '查询', + '002100' => '交易', + '002101' => '冲正', + '002102' => '撤销', + ], + 'log_type' => [ + '002025' => 'query', + '002100' => 'freezecoupon', + '002101' => 'reversal', + '002102' => 'annul', + ], //基础数据 'regular' => [ '002025' => [ @@ -50,6 +63,8 @@ return [ "msg_sys_sn", "msg_ver", "req_serial_no",//自己添加的基础数据 + "orig_req_serial_no",//自己添加的基础数据 + "sett_date",//自己添加的基础数据 ], '002101' => [ "msg_type", @@ -61,6 +76,7 @@ return [ "msg_sys_sn", "msg_ver", "req_serial_no",//自己添加的基础数据 + "orig_req_serial_no",//自己添加的基础数据 ], '002102' => [ "msg_type", @@ -72,7 +88,7 @@ return [ "msg_sys_sn", "msg_ver", "req_serial_no",//自己添加的基础数据 - + "orig_req_serial_no",//自己添加的基础数据 ], ], 'fields' => [ diff --git a/packages/coupon/src/CouponServiceProvider.php b/packages/coupon/src/CouponServiceProvider.php deleted file mode 100644 index cc39a11..0000000 --- a/packages/coupon/src/CouponServiceProvider.php +++ /dev/null @@ -1,32 +0,0 @@ -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'); - - } - -} diff --git a/packages/sinopec/src/Action/sinopec/ZhyCreate.php b/packages/sinopec/src/Action/sinopec/ZhyCreate.php index 9a290b6..60514bd 100644 --- a/packages/sinopec/src/Action/sinopec/ZhyCreate.php +++ b/packages/sinopec/src/Action/sinopec/ZhyCreate.php @@ -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", diff --git a/packages/sinopec/src/Sinopec.php b/packages/sinopec/src/Sinopec.php index 9c325fc..efa6325 100644 --- a/packages/sinopec/src/Sinopec.php +++ b/packages/sinopec/src/Sinopec.php @@ -7,7 +7,7 @@ use App\Models\User; use Illuminate\Support\Facades\DB; /** - * 自有卡券系统 + * 中石化接口 */ class Sinopec { diff --git a/packages/unionpay/src/Action/Init.php b/packages/unionpay/src/Action/Init.php index f3df95d..3912264 100644 --- a/packages/unionpay/src/Action/Init.php +++ b/packages/unionpay/src/Action/Init.php @@ -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; } //输出数据 diff --git a/packages/unionpay/src/UnionPay.php b/packages/unionpay/src/UnionPay.php index 2cb33f4..a5d9e55 100644 --- a/packages/unionpay/src/UnionPay.php +++ b/packages/unionpay/src/UnionPay.php @@ -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(); + } }