From 8af4a94909af7c9ee8cc5da03e0b683d38055b50 Mon Sep 17 00:00:00 2001 From: weigang Date: Thu, 13 Aug 2020 13:05:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=94=AF=E4=BB=98=EF=BC=9A?= =?UTF-8?q?=E7=BA=BF=E4=B8=8A=E3=80=81=E7=BA=BF=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/PaymentController.php | 81 ++++++++++++++++++---------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/app/Controller/PaymentController.php b/app/Controller/PaymentController.php index a411185..f8d36fd 100644 --- a/app/Controller/PaymentController.php +++ b/app/Controller/PaymentController.php @@ -39,6 +39,7 @@ class PaymentController extends BaseController 'openid' => $data['openid'], ]); + // 返回支付参数给前端 $parameters = [ 'appId' => $result['appid'], 'timeStamp' => '' . time() . '', @@ -47,36 +48,11 @@ class PaymentController extends BaseController 'signType' => 'MD5' ]; - //签名步骤一:按字典序排序参数 - ksort($parameters); - $string = $this->formatBizQueryParaMap($parameters, false); - //签名步骤二:在string后加入KEY - $string = $string . "&key=" . $config['key']; - //签名步骤三:MD5加密 - $string = md5($string); - //签名步骤四:所有字符转为大写 - $parameters['paySign'] = strtoupper($string); + $parameters['paySign'] = $this->signture($parameters, $config['key']); return $this->success($parameters); } - ///作用:格式化参数,签名过程需要使用 - private function formatBizQueryParaMap($paraMap, $urlencode) { - $buff = ""; - ksort($paraMap); - foreach ($paraMap as $k => $v) { - if ($urlencode) { - $v = urlencode($v); - } - $buff .= $k . "=" . $v . "&"; - } - $reqPar = null; - if (strlen($buff) > 0) { - $reqPar = substr($buff, 0, strlen($buff) - 1); - } - return $reqPar; - } - public function wxminiPayOffline(WxminiPayRequest $request){ $data = $request->validated(); @@ -104,10 +80,57 @@ class PaymentController extends BaseController 'openid' => $data['openid'], ]); - $result['sign_type'] = 'MD5'; - $result['timestamp'] = time(); + // 返回支付参数给前端 + $parameters = [ + 'appId' => $result['appid'], + 'timeStamp' => '' . time() . '', + 'nonceStr' => uniqid(), + 'package' => 'prepay_id=' . $result['prepay_id'], + 'signType' => 'MD5' + ]; + + $parameters['paySign'] = $this->signture($parameters, $config['key']); + + return $this->success($parameters); + } + + /** + * 支付参数加签 + * @param $parameters + * @param $key + * @return string + */ + private function signture($parameters, $key) + { + // 按字典序排序参数 + ksort($parameters); + + // http_query + $queryParams = $this->http_query($parameters); + + // 加入KEY + $queryParams = $queryParams . "&key=" . $key; + + // MD5加密 + $queryParams = md5($queryParams); + + // 字符转为大写 + return strtoupper($queryParams); + } + + /** + * 参数转为http query字串 + * @param $parameters + * @return string + */ + private function http_query($parameters) { + + $http_query = []; + foreach ($parameters as $key => $value) { + $http_query[] = $key.'='.$value; + } - return $this->success($result); + return implode('&', $http_query); } } \ No newline at end of file