diff --git a/app/Api/Controllers/UnionPayController.php b/app/Api/Controllers/UnionPayController.php index 5350b4d..ab5cde6 100644 --- a/app/Api/Controllers/UnionPayController.php +++ b/app/Api/Controllers/UnionPayController.php @@ -4,6 +4,7 @@ namespace App\Api\Controllers; use Illuminate\Http\Request; use XuanChen\UnionPay\UnionPay; +use Response; class UnionPayController extends Controller { @@ -43,8 +44,11 @@ class UnionPayController extends Controller unset($inputs['sign']); $action = new UnionPay($inputs, $sign); - $res = $action->start(); - dd($res); + $action->start(); + $action->updateOutData(); + + return $action->respond(); + } } diff --git a/config/unionpay.php b/config/unionpay.php index 442d856..b1983ca 100644 --- a/config/unionpay.php +++ b/config/unionpay.php @@ -9,8 +9,15 @@ return [ 'pos_ad' => '', //营销联盟广告,用于打印在小票上 'pos_mkt_ad' => '', - - 'check' => [ + //银联渠道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'), @@ -19,7 +26,7 @@ return [ 'public' => storage_path('cert/unionpay/public_rsa.pem'), ], ], - 'regular' => [ + 'regular' => [ '002025' => [ "msg_type", "msg_txn_code", @@ -28,6 +35,9 @@ return [ "msg_sender", "msg_time", "msg_ver", + "msg_sys_sn",//自己添加的基础数据 + "req_serial_no",//自己添加的基础数据 + "mkt_code",//自己添加的基础数据 ], '002100' => [ "msg_type", @@ -38,6 +48,7 @@ return [ "msg_time", "msg_sys_sn", "msg_ver", + "req_serial_no",//自己添加的基础数据 ], '002101' => [ "msg_type", @@ -48,6 +59,7 @@ return [ "msg_time", "msg_sys_sn", "msg_ver", + "req_serial_no",//自己添加的基础数据 ], '002102' => [ "msg_type", @@ -58,9 +70,11 @@ return [ "msg_time", "msg_sys_sn", "msg_ver", + "req_serial_no",//自己添加的基础数据 + ], ], - 'fields' => [ + 'fields' => [ //聚合营销优惠查询接口 '002025' => [ 'in' => [ diff --git a/packages/unionpay/src/Action/Init.php b/packages/unionpay/src/Action/Init.php index bd8386c..f3df95d 100644 --- a/packages/unionpay/src/Action/Init.php +++ b/packages/unionpay/src/Action/Init.php @@ -11,16 +11,31 @@ class Init public $sign; + public $msg_type = '00'; + public $msg_txn_code; public $msg_sender; + public $msg_rsp_code = '0000'; + + public $msg_rsp_desc = '成功'; + + //入库的模型 + public $model; + + //返回的数据 + public $outdata; + + //网点id + public $outlet_id; + /** - * RSA验签 - * @param $params 待签名数据 - * @param $public_key 公钥字符串 - * @param $sign 要校对的的签名结果 - * return 验证结果 + * Notes: 验签 + * @Author: 玄尘 + * @Date : 2020/9/30 8:39 + * @param false $self 是否是自己的证书 + * @return bool|string */ public function checkSign($self = false) { @@ -49,9 +64,9 @@ class Init * @Date : 2020/9/29 9:56 * @return string */ - public function getSign() + public function getSign($out = true) { - $signStr = $this->getSignString(); + $signStr = $this->getSignString($out); $private_key = $this->getPrivate(); $privKeyId = openssl_pkey_get_private($private_key); @@ -66,13 +81,20 @@ class Init } /** - * 获取待签名字符串 - * @param array $params 参数数组 - * @return string + * Notes: 获取待签名字符串 + * @Author: 玄尘 + * @Date : 2020/9/30 9:38 + * @param $out 是否是获取返回值的数据 + * @return string */ - public function getSignString() + public function getSignString($out) { - $params = array_filter($this->params); + if ($out) { + $params = array_filter($this->outdata); + } else { + $params = array_filter($this->params); + } + ksort($params); $signStr = http_build_query($params); @@ -103,4 +125,23 @@ class Init return file_get_contents($public); } + //更新返回值 + public function updateOutData() + { + $this->outdata['sign'] = $this->getSign(); + $this->model->out_source = $this->outdata; + $this->model->save(); + + } + + //输出数据 + public function respond() + { + $rt = microtime(true) - LARAVEL_START; + + $header = ['rt' => round($rt * 1000, 2) . 'ms', 'qps' => round(1 / $rt, 1)]; + + return \Response::json($this->outdata, 200, $header); + } + } diff --git a/packages/unionpay/src/Action/UnionPayAction.php b/packages/unionpay/src/Action/UnionPayAction.php index 6f82c2a..a162c7a 100644 --- a/packages/unionpay/src/Action/UnionPayAction.php +++ b/packages/unionpay/src/Action/UnionPayAction.php @@ -9,8 +9,8 @@ use XuanChen\UnionPay\Contracts\CouponContracts; /** * Class 银联对接优惠券 - * @Author: 玄尘 - * @Date: 2020/9/28 14:02 + * @Author : 玄尘 + * @Date : 2020/9/28 14:02 * @package XuanChen\Coupon\Action */ class UnionPayAction extends Init implements CouponContracts @@ -25,9 +25,9 @@ class UnionPayAction extends Init implements CouponContracts public function grant() { return (new YsdGrant)->setActivityId($this->activityId) - ->setOutletId($this->outletId) - ->setMobile($this->mobile) - ->start(); + ->setOutletId($this->outletId) + ->setMobile($this->mobile) + ->start(); } /** @@ -39,8 +39,8 @@ class UnionPayAction extends Init implements CouponContracts public function detail() { $query_coupon = (new YsdQuery)->setOutletId($this->outletId) - ->setCode($this->redemptionCode) - ->start(); + ->setCode($this->redemptionCode) + ->start(); if (!is_string($query_coupon)) { return [ @@ -67,8 +67,8 @@ class UnionPayAction extends Init implements CouponContracts public function destroy() { return $res = (new YsdDestroy)->setCode($this->redemptionCode) - ->setOutletId($this->outletId) - ->start(); + ->setOutletId($this->outletId) + ->start(); } /** @@ -80,11 +80,11 @@ class UnionPayAction extends Init implements CouponContracts public function start() { return $res = (new YsdVerification)->setCode($this->redemptionCode) - ->setUser($this->user) - ->setOutletId($this->outletId) - ->setTotal($this->total) - ->setOrderId($this->orderid) - ->start(); + ->setUser($this->user) + ->setOutletId($this->outletId) + ->setTotal($this->total) + ->setOrderId($this->orderid) + ->start(); } } diff --git a/packages/unionpay/src/UnionPay.php b/packages/unionpay/src/UnionPay.php index 5a23b15..19eef25 100644 --- a/packages/unionpay/src/UnionPay.php +++ b/packages/unionpay/src/UnionPay.php @@ -2,8 +2,10 @@ namespace XuanChen\UnionPay; +use App\Models\UnionpayLog; use App\Models\User; use XuanChen\UnionPay\Action\Init; +use XuanChen\Coupon\Coupon; /** * 银联入口 @@ -17,26 +19,85 @@ class UnionPay extends Init $this->sign = $sign; $this->msg_txn_code = $params['msg_txn_code']; $this->msg_sender = config('unionpay.msg_sender'); + $this->outlet_id = config('unionpay.outlet_id'); } public function start() { - $out = $this->out_data(); - dump($out); + $this->updateInputData(); + $this->out_data(); } + //处理流程 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)) { + $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': + break; + default: + break; + } + + } + + } + + /** + * Notes: 返回的基础数据 + * @Author: 玄尘 + * @Date : 2020/9/30 14:48 + */ + public function getOutBaseData() { $basics = [ - "msg_type" => "00", + "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" => "0000", - "msg_rsp_desc" => "成功", + "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) { @@ -44,15 +105,21 @@ class UnionPay extends Init $basics = array_merge($basics, [ "discount" => 0, "actual_amt" => 0, - "pos_display" => "POS显示", + "pos_display" => "", // "pos_receipt" => config('unionpay.pos_receipt'), // "pos_ad" => config('unionpay.pos_ad'), "pos_mkt_ad" => config('unionpay.pos_receipt'), ]); break; case '002100': - $data = array_merge($basics, [ - 'msg_ver' => 0.1, + $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' => $this->params['orig_amt'], + 'event_no' => '',//什么是活动号 ]); break; case '002101': @@ -62,123 +129,53 @@ class UnionPay extends Init default: break; } - - return $basics; + $this->outdata = $basics; } /** - * Notes: 查询接口 + * Notes: 入库数据 * @Author: 玄尘 - * @Date : 2020/7/21 11:58 - * @param $redemptionCode + * @Date : 2020/9/30 8:46 */ - public static function Query(Request $request) - { - - $inputs = $request->all(); - $sign = $inputs['sign']; - unset($inputs['sign']); - - if (!$redemptionCode) { - return '查询失败,未获取到券码'; - } - - $model = self::getModelByCode($redemptionCode); - if (is_string($model)) { - return $model; - } - - return $model->setCode($redemptionCode) - ->setOutletId($outletId) - ->detail(); - } - - /** - * Notes: 卡券作废 - * @Author: 玄尘 - * @Date : 2020/9/2 16:54 - * @param $redemptionCode - * @param $outletId - * @return string - */ - public static function Destroy($redemptionCode, $outletId) + private function updateInputData() { try { - $model = self::getModelByCode($redemptionCode); - if (is_string($model)) { - return $model; + if (!isset($this->params['msg_sys_sn'])) { + $this->params['msg_sys_sn'] = date('ymdHis') . sprintf('%06d', rand(1, 99999999)); } - return $model->setCode($redemptionCode) - ->setOutletId($outletId) - ->destroy(); + $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) { - return $e->getMessage(); + $this->msg_rsp_code = 9999; + $this->msg_rsp_desc = $e->getMessage(); + + return false; } } /** - * Notes: 根据券码 获取class + * Notes: 校验输入的数据 * @Author: 玄尘 - * @Date : 2020/7/21 12:00 - * @param $code - * @return string + * @Date : 2020/9/30 14:46 */ - public static function getModelByCode($code) + public function checkInData() { - $rules = config('xuanchen_coupon.rules'); - if (!$rules) { - return '系统出错,未找到配置文件'; + 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 = '聚合营销码不能为空'; } - - $model = ''; - foreach ($rules as $rule) { - if (preg_match($rule['pattern'], $code, $matches)) { - $model = $rule['model']; - break; - } - } - - if (!$model) { - throw new \Exception('卡券核销失败。未查到卡券所属'); - } - - return new $model; - - } - - /** - * Notes: description - * @Author: 玄尘 - * @Date : 2020/8/21 13:33 - * @param \App\Models\User $user 渠道 - * @param string $redemptionCode 要核销的券码 - * @param float $total 订单金额 - * @param string $outletId 网点id - * @param string $orderid 订单id - * @return string - */ - public static function Redemption(User $user, string $redemptionCode, float $total, string $outletId, string $orderid = '') - { - - try { - $model = self::getModelByCode($redemptionCode); - if (is_string($model)) { - return $model; - } - - return $model->setUser($user) - ->setCode($redemptionCode) - ->setTotal($total) - ->setOutletId($outletId) - ->setOrderId($orderid) - ->start(); - - } catch (\Exception $e) { - return $e->getMessage(); - } - } }