From 648147cd4d77630f4b02ca54004cbc1824329b01 Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Tue, 29 Sep 2020 15:26:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Api/Controllers/UnionPayController.php | 24 ++----- config/unionpay.php | 9 +++ packages/unionpay/src/Action/Init.php | 20 ++++-- packages/unionpay/src/UnionPay.php | 83 +++++++++++++++++----- 4 files changed, 94 insertions(+), 42 deletions(-) diff --git a/app/Api/Controllers/UnionPayController.php b/app/Api/Controllers/UnionPayController.php index e328336..364c36d 100644 --- a/app/Api/Controllers/UnionPayController.php +++ b/app/Api/Controllers/UnionPayController.php @@ -10,6 +10,7 @@ class UnionPayController extends Controller public function index() { + $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); @@ -32,25 +33,12 @@ class UnionPayController extends Controller public function query(Request $request) { $inputs = $request->all(); + $sign = $inputs['sign']; + unset($inputs['sign']); - if (!isset($inputs['msg_txn_code'])) { - - } - - $validator = \Validator::make($inputs, [ - 'activityId' => 'required', - 'outletId' => 'required', - 'mobile' => 'required', - ], [ - 'activityId.required' => '缺少活动编码', - 'outletId.required' => '缺少网点id', - 'mobile.required' => '缺少手机号', - ]); - - if ($validator->fails()) { - return $this->error($validator->errors()->first()); - } - + $action = new UnionPay($inputs, $sign); + $res = $action->start(); + dd($res); } } diff --git a/config/unionpay.php b/config/unionpay.php index 09ff420..442d856 100644 --- a/config/unionpay.php +++ b/config/unionpay.php @@ -1,6 +1,15 @@ 01, + //打印在小票上,由活动标题、优惠金额、原始金额组合而成 + 'pos_receipt' => '', + //广告,用于打印在小票上 + 'pos_ad' => '', + //营销联盟广告,用于打印在小票上 + 'pos_mkt_ad' => '', + 'check' => [ 'self' => [ 'private' => storage_path('cert/unionpay/self/private_rsa.pem'), diff --git a/packages/unionpay/src/Action/Init.php b/packages/unionpay/src/Action/Init.php index 13a5e2b..bd8386c 100644 --- a/packages/unionpay/src/Action/Init.php +++ b/packages/unionpay/src/Action/Init.php @@ -2,26 +2,31 @@ namespace XuanChen\UnionPay\Action; - use Illuminate\Http\Request; class Init { + public $params; + public $sign; + public $msg_txn_code; + + public $msg_sender; + /** * RSA验签 - * @param $params 待签名数据 + * @param $params 待签名数据 * @param $public_key 公钥字符串 - * @param $sign 要校对的的签名结果 - * return 验证结果 + * @param $sign 要校对的的签名结果 + * return 验证结果 */ public function checkSign($self = false) { $sign = $this->sign; $sign = base64_decode($sign); - + $public_key = $this->getPublic($self); $pub_key_id = openssl_get_publickey($public_key); @@ -41,12 +46,12 @@ class Init /** * Notes: 签名 * @Author: 玄尘 - * @Date: 2020/9/29 9:56 + * @Date : 2020/9/29 9:56 * @return string */ public function getSign() { - $signStr = $this->getSignString(); + $signStr = $this->getSignString(); $private_key = $this->getPrivate(); $privKeyId = openssl_pkey_get_private($private_key); @@ -70,6 +75,7 @@ class Init $params = array_filter($this->params); ksort($params); $signStr = http_build_query($params); + return $signStr; } diff --git a/packages/unionpay/src/UnionPay.php b/packages/unionpay/src/UnionPay.php index 8048f9f..a3c1d9a 100644 --- a/packages/unionpay/src/UnionPay.php +++ b/packages/unionpay/src/UnionPay.php @@ -13,8 +13,57 @@ class UnionPay extends Init public function __construct($params, $sign = '') { - $this->params = $params; - $this->sign = $sign; + $this->params = collect($params); + $this->sign = $sign; + $this->msg_txn_code = $params['msg_txn_code']; + $this->msg_sender = config('unionpay.msg_sender'); + } + + public function start() + { + $out = $this->out(); + + } + + public function out_data() + { + $basics = [ + "msg_type" => "00", + "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" => "成功", + ]; + + switch ($this->msg_txn_code) { + case '002025': + $basics = array_merge($basics, [ + "discount" => 0, + "actual_amt" => 0, + "pos_display" => "POS显示", + // "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, + ]); + break; + case '002101': + break; + case '002102': + break; + default: + break; + } + + return $basics; } /** @@ -27,7 +76,7 @@ class UnionPay extends Init { $inputs = $request->all(); - $sign = $inputs['sign']; + $sign = $inputs['sign']; unset($inputs['sign']); if (!$redemptionCode) { @@ -40,8 +89,8 @@ class UnionPay extends Init } return $model->setCode($redemptionCode) - ->setOutletId($outletId) - ->detail(); + ->setOutletId($outletId) + ->detail(); } /** @@ -61,8 +110,8 @@ class UnionPay extends Init } return $model->setCode($redemptionCode) - ->setOutletId($outletId) - ->destroy(); + ->setOutletId($outletId) + ->destroy(); } catch (\Exception $e) { return $e->getMessage(); } @@ -103,11 +152,11 @@ class UnionPay extends Init * 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 + * @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 = '') @@ -120,11 +169,11 @@ class UnionPay extends Init } return $model->setUser($user) - ->setCode($redemptionCode) - ->setTotal($total) - ->setOutletId($outletId) - ->setOrderId($orderid) - ->start(); + ->setCode($redemptionCode) + ->setTotal($total) + ->setOutletId($outletId) + ->setOrderId($orderid) + ->start(); } catch (\Exception $e) { return $e->getMessage();