diff --git a/components/TongLianPay.php b/components/TongLianPay.php new file mode 100644 index 0000000..dd3eebc --- /dev/null +++ b/components/TongLianPay.php @@ -0,0 +1,199 @@ +where(['user_id' => $userId])->one(); + $signing = Signing::find()->where(['order_no' => $orderNom])->one(); + + $data = [ + 'cusid' => $this->cusid, + 'appid' => $this->appid, + 'version' => '11', + 'trxamt' => 1, + 'reqsn' => $signing->order_no, + 'paytype' => 'W06', + 'randomstr' => date('dHis') . rand(1000000, 9999999), + 'signtype' => 'RSA', + 'front_url' => $frontUrl, + 'notify_url' => 'http://app.cxgj.dev.1nww.com/api/signing/signing-pay-notify', + 'acct' => $userOauth->openid, + 'sub_appid' => 'wxbcdac64cf147ee22', + ]; + + $data['sign'] = urlencode($this->sign($data)); + + $request = $this->ToUrlParams($data); + + $res = $this->request('https://vsp.allinpay.com/apiweb/unitorder/pay', $request); + + $result = json_decode($res, true); + + if ($result['retcode'] !== 'SUCCESS') { + return $this->apiReturnError('支付错误', $result); + } + return $this->apiReturnSuccess('success', $result); + } + + public function notify($data) + { + if (!$this->ValidSign($data)) { + return false; + } + + $signing = Signing::find()->where(['order_no' => $data['outtrxid'], 'status' => 0])->one(); + + if (!$signing) { + return true; + } + + $signing->status = 1; + + $signing->pay_time = time(); + + return $signing->save(); + } + + public function refund($signingId) + { + $signing = Signing::findOne($signingId); + + if ($signing && $signing->status == 1) { + + $data = [ + 'cusid' => $this->cusid, + 'appid' => $this->appid, + 'trxamt' => 1, + 'reqsn' => date('YmdH') . rand(10000, 99999), + 'oldreqsn' => $signing->order_no, + 'randomstr' => date('YmdH') . rand(10000, 99999), + 'signtype' => 'RSA', + ]; + + $data['sign'] = urlencode($this->sign($data)); + + $request = $this->ToUrlParams($data); + + $res = $this->request('https://vsp.allinpay.com/apiweb/tranx/refund', $request); + + $result = json_decode($res, true); + + if ($result['trxstatus'] == '0000') { + + $signing->status = 2; + + $signing->save(); + + return $this->apiReturnSuccess('操作成功'); + } + return $this->apiReturnError($result['errmsg']); + } + return $this->apiReturnError('订单异常'); + } + + + //RSA签名 + public function sign(array $array) + { + ksort($array); + $bufSignSrc = $this->ToUrlParams($array); + $private_key = $this->privateKey; + $private_key = chunk_split($private_key, 64, "\n"); + $key = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($private_key) . "-----END RSA PRIVATE KEY-----"; + // echo $key; + if (openssl_sign($bufSignSrc, $signature, $key)) { + // echo 'sign success'; + } else { + echo 'sign fail'; + } + $sign = base64_encode($signature);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的 + + return $sign; + + } + + + public function ToUrlParams(array $array) + { + $buff = ""; + foreach ($array as $k => $v) { + if ($v != "" && !is_array($v)) { + $buff .= $k . "=" . $v . "&"; + } + } + $buff = trim($buff, "&"); + return $buff; + } + + /** + * 校验签名 + * @param array 参数 + * @param unknown_type appkey + */ + public function ValidSign(array $array) + { + $sign = $array['sign']; + unset($array['sign']); + ksort($array); + $bufSignSrc = $this->ToUrlParams($array); + $public_key = $this->publicKey; + $public_key = chunk_split($public_key, 64, "\n"); + $key = "-----BEGIN PUBLIC KEY-----\n$public_key-----END PUBLIC KEY-----\n"; + $result = openssl_verify($bufSignSrc, base64_decode($sign), $key); + return $result; + } + + + //发送请求操作仅供参考,不为最佳实践 + public function request($url, $params) + { + $ch = curl_init(); + $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8"); + curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + + $output = curl_exec($ch); + curl_close($ch); + return $output; + } + + public function apiReturnSuccess($msg = "ok", $data = [], $code = 0) + { + return [ + 'code' => $code, + 'msg' => $msg, + 'data' => $data + ]; + } + + public function apiReturnError($msg = "failed", $data = [], $code = 1) + { + return [ + 'code' => $code, + 'msg' => $msg, + 'data' => $data + ]; + } +} \ No newline at end of file diff --git a/modules/admin/controllers/SigningController.php b/modules/admin/controllers/SigningController.php new file mode 100644 index 0000000..a927823 --- /dev/null +++ b/modules/admin/controllers/SigningController.php @@ -0,0 +1,54 @@ + [ + 'class' => LoginBehavior::className(), + ], + ]); + } + + + public function actionIndex() + { + if (\Yii::$app->request->isAjax) { + + $signingForm = new SigningForm(); + + $signingForm->attributes = \Yii::$app->request->get(); + + $data = $signingForm->getList(); + + return $this->responseHandler($data); + + } + return $this->render('index'); + } + + + public function actionRefund() + { + + if (\Yii::$app->request->isAjax) { + + $tongLianPay = new TongLianPay(); + + return $this->responseHandler($tongLianPay->refund(\Yii::$app->request->post('id'))); + + } + + + } + +} \ No newline at end of file diff --git a/modules/admin/models/Menu.php b/modules/admin/models/Menu.php index 55eb43c..cd91156 100644 --- a/modules/admin/models/Menu.php +++ b/modules/admin/models/Menu.php @@ -26,6 +26,15 @@ class Menu 'children' => [ ], ], + [ + 'name' => '签约列表', + 'is_menu' => true, + 'is_show' => true, + 'route' => "admin/signing/index", + 'icon' => 'layui-icon-form', + 'children' => [ + ], + ], // [ // 'name' => '布告管理', // 'is_menu' => true, diff --git a/modules/admin/models/SigningForm.php b/modules/admin/models/SigningForm.php new file mode 100644 index 0000000..b43ed77 --- /dev/null +++ b/modules/admin/models/SigningForm.php @@ -0,0 +1,60 @@ + 1], + [['limit'], 'default', 'value' => 20], + [['status'], 'default', 'value' => null], + ]; + } + + public function getList() + { + + $query = Signing::find()->alias('s') + ->leftJoin(['u' => User::tableName()], 's.user_id=u.id') + ->leftJoin(['g' => Goods::tableName()], 's.goods_id=g.id') + ->leftJoin(['gh' => GoodsHub::tableName()], 'g.goods_hub_id=gh.id') + ->select('u.username,gh.name as goods_name,s.*'); + + if ($this->status != null) { + $query->where(['s.status' => $this->status]); + } + if ($this->order_no) { + $query->where(['s.order_no' => $this->order_no]); + } + $pagination = new Pagination(['totalCount' => $query->count(), 'defaultPageSize' => $this->limit]); + + $list = $query->offset($pagination->offset)->orderBy(['s.create_time' => SORT_DESC])->asArray()->limit($pagination->limit)->all(); + + return [ + 'code' => 0, + 'msg' => 'ok', + 'data' => $list, + 'count' => $query->count() + ]; + } + + +} \ No newline at end of file diff --git a/modules/admin/views/signing/index.php b/modules/admin/views/signing/index.php new file mode 100644 index 0000000..2d1c610 --- /dev/null +++ b/modules/admin/views/signing/index.php @@ -0,0 +1,210 @@ +title = '签约列表'; +$this->params['breadcrumbs'][] = $this->title; +$status = \Yii::$app->request->get('status'); +?> +