diff --git a/app/Api/Controllers/UnionPayController.php b/app/Api/Controllers/UnionPayController.php index dba84d2..e8480fc 100644 --- a/app/Api/Controllers/UnionPayController.php +++ b/app/Api/Controllers/UnionPayController.php @@ -35,18 +35,12 @@ class UnionPayController extends Controller */ public function query(Request $request) { - $inputs = $request->all(); - - $app = app('xuanchen.unionpay'); + $app = app('xuanchen.unionpay'); $app->setParams($inputs); - - $app->addLog(); $app->start(); - $app->updateLog(); return $app->respond(); - } } diff --git a/packages/xuanchen/unionpay/.gitignore b/packages/xuanchen/unionpay/.gitignore deleted file mode 100644 index c73cdc8..0000000 --- a/packages/xuanchen/unionpay/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/.idea/ -/.git/ -/vendor diff --git a/packages/xuanchen/unionpay/README.md b/packages/xuanchen/unionpay/README.md deleted file mode 100644 index f8db1cb..0000000 --- a/packages/xuanchen/unionpay/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Unionpay 对接银联接口 - -002025 聚合营销优惠查询接口 - -002100 销账交易接口 - -002101 销账冲正通知接口 - -002102 销账撤销通知接口 - diff --git a/packages/xuanchen/unionpay/composer.json b/packages/xuanchen/unionpay/composer.json deleted file mode 100644 index fd48e91..0000000 --- a/packages/xuanchen/unionpay/composer.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "xuanchen/unionpay", - "description": "第三方银联对接", - "license": "MIT", - "authors": [ - { - "name": "玄尘", - "email": "122383162@qq.com" - } - ], - "require": { - "php": ">=7.1.3", - "laravel/framework": "*" - }, - "autoload": { - "psr-4": { - "XuanChen\\UnionPay\\": "src/" - } - }, - "extra": { - "laravel": { - "providers": [ - "XuanChen\\UnionPay\\ServiceProvider" - ] - } - } -} diff --git a/packages/xuanchen/unionpay/config/unionpay.php b/packages/xuanchen/unionpay/config/unionpay.php deleted file mode 100644 index dcbb5f9..0000000 --- a/packages/xuanchen/unionpay/config/unionpay.php +++ /dev/null @@ -1,339 +0,0 @@ - '660134', - //打印在小票上,由活动标题、优惠金额、原始金额组合而成 - 'pos_receipt' => '本时生活,优惠生活', - //广告,用于打印在小票上 - 'pos_ad' => '', - //营销联盟广告,用于打印在小票上 - 'pos_mkt_ad' => '本时生活,优惠生活', - //银联渠道id - 'agent_id' => '299', - //银联网点id - 'outlet_id' => '2009300919918', - //用于银商与sp分润的金额(是佣金的一部分), 以分为单位 - 'serv_chg' => 0, - //佣金 - 'commission' => 0, - //证书 - 'check' => [ - 'self' => [ - 'private' => storage_path('cert/unionpay/self/private_rsa.pem'), - 'public' => storage_path('cert/unionpay/self/public_rsa.pem'), - ], - 'unionpay' => [ - 'public' => storage_path('cert/unionpay/public_rsa.pem'), - ], - ], - 'type' => [ - '002025' => '查询', - '002100' => '交易', - '002101' => '冲正', - '002102' => '撤销', - ], - 'log_type' => [ - '002025' => 'query', - '002100' => 'freezecoupon', - '002101' => 'reversal', - '002102' => 'annul', - ], - //需要校验的数据 - 'validator' => [ - '002025' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - "msg_flg", - "msg_sender", - "msg_time", - "msg_ver", - "msg_sys_sn", - "req_serial_no", - "mkt_code", - "amount", - "avl_amt", - ], - '002100' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - "msg_flg", - "msg_sender", - "msg_time", - "msg_sys_sn", - "msg_ver", - "req_serial_no", - "orig_req_serial_no", - "sett_date", - "txn_date", - "txn_time", - "orig_amt", - "discount_amt", - ], - '002101' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - "msg_flg", - "msg_sender", - "msg_time", - "msg_sys_sn", - "msg_ver", - "req_serial_no", - "orig_req_serial_no", - ], - '002102' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - "msg_flg", - "msg_sender", - "msg_time", - "msg_sys_sn", - "msg_ver", - "req_serial_no", - "orig_req_serial_no", - ], - ], - //入库基础数据 - 'regular' => [ - '002025' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - // "msg_flg", - "msg_sender", - "msg_time", - "msg_ver", - "msg_sys_sn",//自己添加的基础数据 - "req_serial_no",//自己添加的基础数据 - "mkt_code",//自己添加的基础数据 - ], - '002100' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - // "msg_flg", - "msg_sender", - "msg_time", - "msg_sys_sn", - "msg_ver", - "req_serial_no",//自己添加的基础数据 - "orig_req_serial_no",//自己添加的基础数据 - "sett_date",//自己添加的基础数据 - ], - '002101' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - // "msg_flg", - "msg_sender", - "msg_time", - "msg_sys_sn", - "msg_ver", - "req_serial_no",//自己添加的基础数据 - "orig_req_serial_no",//自己添加的基础数据 - ], - '002102' => [ - "msg_type", - "msg_txn_code", - "msg_crrltn_id", - // "msg_flg", - "msg_sender", - "msg_time", - "msg_sys_sn", - "msg_ver", - "req_serial_no",//自己添加的基础数据 - "orig_req_serial_no",//自己添加的基础数据 - ], - ], - 'fields' => [ - //聚合营销优惠查询接口 - '002025' => [ - 'in' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "报文流水号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "req_serial_no" => "查询流水号", - "shop_no" => "门店号", - "term_no" => "受理终端号", - "service_code" => "受理方式", - "voucher_no" => "受理凭证号", - "mkt_code" => "聚合营销码", - "mkt_mode" => "聚合营销类型", - "embedded_mchnt_no" => "发起渠道商户号", - "currency_code" => "货币代码", - "amount" => "消费金额", - "avl_amt" => "可优惠金额", - "term_sp_chnl_no" => "终端指定SP渠道号", - "func_code" => "功能码", - "times" => "次数", - "pay_mode" => "支付方式", - //用户附加信息 - "user_ext_info" => [ - "mobile_no" => "手机号", - "user_code" => "用户号", - "user_code_type" => "用户号类型", - "dev_id" => "设备id", - ], - "sign" => "签名域", - ], - 'out' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "报文流水号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_rsp_code" => "响应码", - "msg_rsp_desc" => "响应码描述", - "discount" => "折扣金额", - "actual_amt" => "折后应收金额", - "pos_display" => "POS显示", - "pos_receipt" => "POS小票", - "pos_ad" => "POS广告", - "pos_mkt_ad" => "Pos_营销联盟广告", - "sign" => "签名域", - ], - ], - //销账交易接口 - '002100' => [ - 'in' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "消息关联号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "shop_no" => "门店号", - "term_no" => "终端号", - "req_serial_no" => "销券流水号", - "orig_req_serial_no" => "原查询流水号", - "enc_card_no" => "加密卡号", - "part_card_no" => "部分卡号", - "acq_term_sn" => "受理终端流水号", - "refer_no" => "检索参考号", - "sett_date" => "清算日期", - "txn_date" => "交易日期", - "txn_time" => "交易时间", - "orig_amt" => "原始金额", - "discount_amt" => "优惠的金额", - "pay_amt" => "支付金额", - "pay_mode" => "支付方式", - "order_no" => "订单号", - "trans_crrltn_no" => "交易关联流水号", - "equity_no" => "权益号", - "card_no" => "全卡号", - "sign" => "签名数据", - ], - 'out' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "消息关联号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "msg_rsp_code" => "响应码", - "msg_rsp_desc" => "响应码描述", - "orig_amt" => "原始金额", - "discount_amt" => "折扣金额", - "pay_amt" => "支付金额", - "serv_chg" => "服务费", - "commission" => "佣金", - "ad" => "广告", - "pos_receipt" => "POS优惠", - "coupon_no" => "凭证号", - "coupon_type" => "凭证类型", - "sp_biz_code" => "SP统计码", - "charge_code" => "计费码", - "pos_event_title" => "SP活动主题", - "sp_contact" => "SP联系电话", - "sp_name" => "SP名称", - "event_no" => "活动号", - "td_code" => "二维码", - "memo" => "附言", - "mkt_sp_chnl_no" => "营销渠道号", - "point_amt" => "积分抵扣金额", - "sign" => "签名数据", - ], - ], - //销账冲正通知接口 - '002101' => [ - 'in' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "消息关联号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "shop_no" => "门店号", - "term_no" => "终端号", - "req_serial_no" => "冲正流水号", - "orig_req_serial_no" => "原始销账流水号", - "trans_crrltn_no" => "交易关联流水号", - "sign" => "签名数据", - ], - 'out' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "消息关联号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "msg_rsp_code" => "响应码", - "msg_rsp_desc" => "响应码描述", - "sign" => "签名数据", - ], - ], - '002102' => [ - 'in' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "消息关联号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "shop_no" => "门店号", - "term_no" => "终端号", - "req_serial_no" => "撤销流水号", - "orig_req_serial_no" => "原始销账流水号", - "trans_crrltn_no" => "交易关联流水号", - "sign" => "签名数据", - ], - 'out' => [ - "msg_type" => "报文类型", - "msg_txn_code" => "交易代码", - "msg_crrltn_id" => "消息关联号", - "msg_flg" => "报文请求应答标志", - "msg_sender" => "报文发送方", - "msg_time" => "报文日期", - "msg_sys_sn" => "平台流水号", - "msg_ver" => "报文版本号", - "msg_rsp_code" => "响应码", - "msg_rsp_desc" => "响应码描述", - "ad" => "广告", - "td_code" => "二维码", - "sign" => "签名数据", - ], - ], - - ], -]; diff --git a/packages/xuanchen/unionpay/src/Action/Init.php b/packages/xuanchen/unionpay/src/Action/Init.php deleted file mode 100644 index ec8af27..0000000 --- a/packages/xuanchen/unionpay/src/Action/Init.php +++ /dev/null @@ -1,243 +0,0 @@ -hexXbin($this->sign); - if (!$sign) { - throw new \Exception('签名错误'); - } - $public_key = $this->getPublic($self); - - $pub_key_id = openssl_get_publickey($public_key); - - $signStr = $this->getSignString($out); - - if ($pub_key_id) { - $result = (bool)openssl_verify($signStr, $sign, $pub_key_id); - openssl_free_key($pub_key_id); - } else { - throw new \Exception('私钥格式有误'); - - } - - return $result; - } - - /** - * Notes: 校验sign - * @Author: 玄尘 - * @Date : 2020/10/13 15:21 - * @param $data - * @param false $types - * @return int|string - */ - public function hexXbin($sign, $types = false) - { - // 过滤非16进制字符 - $checkStr = strspn($sign, '0123456789abcdefABCDEF'); - //字符串长度不是偶数时pack来处理 - if (strlen($checkStr) % 2) { - return pack("H*", $sign); - } else { - return hex2bin($sign); - } - } - - /** - * Notes: 签名 - * @Author: 玄尘 - * @Date : 2020/10/9 15:52 - * @param bool $self - * @return string - * @throws \Exception - */ - public function getSign($out = true) - { - $signStr = $this->getSignString($out); - $private_key = $this->getPrivate(); - - $privKeyId = openssl_pkey_get_private($private_key); - if (!$privKeyId) { - throw new \Exception('私钥格式有误'); - } - - if (openssl_sign($signStr, $signature, $privKeyId)) { - $signature = bin2hex($signature); - } else { - throw new \Exception('签名错误'); - } - - openssl_free_key($privKeyId); - - return $signature; - } - - /** - * Notes: 获取待签名字符串 - * @Author: 玄尘 - * @Date : 2020/9/30 9:38 - * @param $out 是否是获取返回值的数据 - * @return string - */ - public function getSignString($out = false) - { - if ($out) { - $params = array_filter($this->outdata); - } else { - $params = array_filter($this->params); - } - - if (empty($params)) { - throw new \Exception('缺少数据'); - } - - ksort($params); - - //http_build_query 会自动urlencode 需要转换 - return $this->str2utf8(urldecode(http_build_query($params))); - - } - - //获取私钥 - public function getPrivate() - { - $private = config('unionpay.check.self.private'); - - if (!file_exists($private)) { - throw new \Exception('缺少私钥文件'); - } - - return file_get_contents($private); - } - - //获取公钥 - public function getPublic($self = false) - { - $public = config('unionpay.check.unionpay.public'); - - if ($self) { - $public = config('unionpay.check.self.public'); - } - - return file_get_contents($public); - } - - /** - * Notes: 插入日志 - * @Author: 玄尘 - * @Date : 2020/10/9 14:38 - * @return mixed - */ - public function addLog() - { - $log_type = config('unionpay.log_type'); - $data = [ - 'path' => request()->url(), - 'method' => request()->method(), - 'type' => $log_type[$this->msg_txn_code] ?? $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']); - if ($encode != 'UTF-8') { - $str = $str ? $str : mb_convert_encoding($str, 'UTF-8', $encode); - } - $str = is_string($str) ? $str : ''; - - return $str; - } - - //输出数据 - public function respond() - { - $rt = microtime(true) - LARAVEL_START; - - $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); - } - -} diff --git a/packages/xuanchen/unionpay/src/Action/Query.php b/packages/xuanchen/unionpay/src/Action/Query.php deleted file mode 100644 index 5adfcaf..0000000 --- a/packages/xuanchen/unionpay/src/Action/Query.php +++ /dev/null @@ -1,48 +0,0 @@ -unionpay = $unionpay; - } - - public function start() - { - try { - $res = Coupon::Query($this->unionpay->params['mkt_code'], $this->unionpay->outlet_id); - - if (is_array($res)) { - $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->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = $res; - } - } catch (\Exception $e) { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = $e->getMessage(); - } - - } - - public function back() - { - return $this->unionpay->outdata; - } - -} diff --git a/packages/xuanchen/unionpay/src/Action/Redemption.php b/packages/xuanchen/unionpay/src/Action/Redemption.php deleted file mode 100644 index 3f54a56..0000000 --- a/packages/xuanchen/unionpay/src/Action/Redemption.php +++ /dev/null @@ -1,59 +0,0 @@ -unionpay = $unionpay; - } - - public function start() - { - //查询聚合信息 - $query = UnionpayLog::where('req_serial_no', $this->unionpay->params['orig_req_serial_no']) - ->where('msg_txn_code', '002025') - ->latest() - ->first(); - if (!$query) { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = '销账失败,未查询到前置数据。'; - } else { - $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->unionpay->agent_id); - - $coupon = Coupon::Redemption($user, $query->mkt_code, $this->unionpay->params['orig_amt'] / 100, $this->unionpay->outlet_id, ''); - if (!is_array($coupon)) { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = $coupon; - } - - } - - } - - public function back() - { - return $this->unionpay->outdata; - } - -} \ No newline at end of file diff --git a/packages/xuanchen/unionpay/src/Action/Reversal.php b/packages/xuanchen/unionpay/src/Action/Reversal.php deleted file mode 100644 index b51287f..0000000 --- a/packages/xuanchen/unionpay/src/Action/Reversal.php +++ /dev/null @@ -1,80 +0,0 @@ -unionpay = $unionpay; - } - - public function start() - { - try { - //查询聚合信息 - $info = UnionpayLog::where('req_serial_no', $this->unionpay->params['orig_req_serial_no']) - ->where('msg_txn_code', '002100') - ->where('status', 1) - ->latest() - ->first(); - - if ($info) { - $query = UnionpayLog::where('req_serial_no', $info->orig_req_serial_no) - ->where('msg_txn_code', '002025') - ->where('status', 1) - ->latest() - ->first(); - - $coupon = CouponModel::where('redemptionCode', $query->mkt_code) - ->where('status', 2) - ->latest() - ->first(); - - if ($query && $coupon) { - //优惠券核销成功 - if ($coupon->status == 2) { - $res = Coupon::Reversal($coupon->redemptionCode, $this->unionpay->outlet_id); - if ($res !== true) { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = $res; - } - } else { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = '优惠券状态不对'; - } - } else { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = '未查询到卡券信息。'; - } - - } else { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = '未查询到销账接口数据。'; - } - } catch (\Exception $e) { - $this->unionpay->outdata['msg_rsp_code'] = '9999'; - $this->unionpay->outdata['msg_rsp_desc'] = $e->getMessage(); - } - - } - - public function back() - { - return $this->unionpay->outdata; - } - -} \ No newline at end of file diff --git a/packages/xuanchen/unionpay/src/Contracts/Contracts.php b/packages/xuanchen/unionpay/src/Contracts/Contracts.php deleted file mode 100644 index c87b633..0000000 --- a/packages/xuanchen/unionpay/src/Contracts/Contracts.php +++ /dev/null @@ -1,16 +0,0 @@ -app->bind('xuanchen.unionpay', function ($app) { - $unionpay = new UnionPay(); - $unionpay->setConfig(); - - return $unionpay; - }); - } - - /** - * Bootstrap services. - * @return void - */ - public function boot() - { - $this->setConfig(); - } - - public function setConfig() - { - $path = __DIR__ . '/../config/unionpay.php'; - if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { - $this->publishes([ - $path => config_path('unionpay.php'), - ]); - } elseif ($this->app instanceof LumenApplication) { - $this->app->configure('unionpay'); - } - - $this->mergeConfigFrom($path, 'unionpay'); - - } - -} diff --git a/packages/xuanchen/unionpay/src/UnionPay.php b/packages/xuanchen/unionpay/src/UnionPay.php deleted file mode 100644 index 7b26031..0000000 --- a/packages/xuanchen/unionpay/src/UnionPay.php +++ /dev/null @@ -1,270 +0,0 @@ -params = $params; - $this->sign = $params['sign']; - $this->msg_txn_code = $params['msg_txn_code'] ?? ''; - } - - /** - * Notes: 设置基础参数 - * @Author: 玄尘 - * @Date : 2020/11/10 10:46 - */ - public function setConfig() - { - $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); - } - - /** - * Notes: 入口 - * @Author: 玄尘 - * @Date : 2020/10/9 9:33 - */ - public function start() - { - //设置基础数据 - $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() - { - //是幂等 - 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'://聚合营销优惠查询接口 - $action = new Query($this); - $action->start(); - $this->outdata = $action->back(); - break; - case '002100'://销账交易接口 - $action = new Redemption($this); - $action->start(); - $this->outdata = $action->back(); - break; - case '002101'://冲正 - case '002102'://撤销 - $action = new Reversal($this); - $action->start(); - $this->outdata = $action->back(); - break; - default: - break; - } - - } else { - $this->outdata['msg_rsp_code'] = $this->msg_rsp_code; - $this->outdata['msg_rsp_desc'] = $this->msg_rsp_desc; - } - } - - } - - /** - * 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() - { - try { - //验签 - $res = $this->checkSign(false, false); - if ($res !== true) { - $this->msg_rsp_code = 9996; - $this->msg_rsp_desc = '验签失败'; - } - } catch (\Exception $e) { - $this->msg_rsp_code = 9996; - $this->msg_rsp_desc = $e->getMessage(); - } - - if ($this->msg_txn_code && $this->msg_rsp_code == '0000') { - $rule_code = config('unionpay.validator')[$this->msg_txn_code]; - $rule_msg = config('unionpay.fields')[$this->msg_txn_code]['in']; - - foreach ($rule_code as $item) { - $rule[$item] = 'required'; - $msg[$item . '.required'] = $rule_msg[$item] . '不能为空'; - } - $validator = \Validator::make($this->params, $rule, $msg); - - if ($validator->fails()) { - $this->msg_rsp_code = 9996; - $this->msg_rsp_desc = $validator->errors()->first(); - - } - - } else { - - $this->msg_rsp_code = 9996; - $this->msg_rsp_desc = $this->msg_rsp_code == '0000' ? '平台流水号不能为空。' : $this->msg_rsp_desc; - } - - } - - /** - * Notes: 返回的基础数据 - * @Author: 玄尘 - * @Date : 2020/9/30 14:48 - */ - public function getOutBaseData() - { - $basics = [ - "msg_type" => $this->msg_type, - "msg_txn_code" => $this->msg_txn_code, - "msg_crrltn_id" => $this->params['msg_crrltn_id'], - "msg_flg" => 1, - "msg_sender" => $this->msg_sender, - "msg_time" => now()->format('YmdHis'), - "msg_sys_sn" => $this->params['msg_sys_sn'] ?? '', - "msg_rsp_code" => $this->msg_rsp_code, - "msg_rsp_desc" => $this->msg_rsp_desc, - ]; - - switch ($this->msg_txn_code) { - //查询 - case '002025': - $basics = array_merge($basics, [ - "discount" => 0, - "actual_amt" => 0, - "pos_display" => "", - // "pos_receipt" => config('unionpay.pos_receipt'), - // "pos_ad" => config('unionpay.pos_ad'), - "pos_mkt_ad" => config('unionpay.pos_receipt'), - ]); - break; - //销账 - case '002100': - $basics = array_merge($basics, [ - 'msg_ver' => 0.1, - 'orig_amt' => $this->params['orig_amt'], - 'discount_amt' => $this->params['discount_amt'], - 'pay_amt' => $this->params['pay_amt'], - 'serv_chg' => config('unionpay.serv_chg'), - 'commission' => config('unionpay.commission'), - 'event_no' => '',//活动号 直接为空就可以 - ]); - break; - //冲正 - case '002101': - //撤销 - case '002102': - $basics = array_merge($basics, [ - 'msg_ver' => 0.1, - ]); - break; - default: - break; - } - - return $this->outdata = $basics; - - } - - /** - * Notes: 查询是否是幂等 - * @Author: 玄尘 - * @Date : 2020/10/10 13:25 - */ - public function idempotent() - { - $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(); - //如果有入库模型 - if ($this->model) { - $this->model->out_source = $this->outdata; - if ($this->outdata['msg_rsp_code'] != '0000') { - $this->model->status = 0; - } - $this->model->save(); - } - } - -}