From 801cd99a4b023766d455c7e91af7b9baf292cef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Thu, 13 Apr 2017 19:06:07 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=94=AF=E4=BB=98=E6=B5=8B=E8=AF=95=E5=8A=9F=E8=83=BD?= =?UTF-8?q?svn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/extra/mines.php | 2 +- application/extra/view/admin.content.html | 1 + application/wechat/controller/Config.php | 54 ++++++-- application/wechat/view/config.pay.html | 150 ++++++++++++---------- extend/service/FileService.php | 11 +- extend/service/PayService.php | 14 +- 6 files changed, 143 insertions(+), 89 deletions(-) diff --git a/application/extra/mines.php b/application/extra/mines.php index 6d32985f8..353505b30 100644 --- a/application/extra/mines.php +++ b/application/extra/mines.php @@ -109,7 +109,7 @@ return [ 'xl' => 'application/excel', 'eml' => 'message/rfc822', 'json' => ['application/json', 'text/json'], - 'pem' => ['application/x-x509-user-cert', 'application/x-pem-file', 'application/octet-stream'], + 'pem' => ['application/x-x509-user-cert', 'application/x-x509-ca-cert', 'application/x-pem-file', 'application/octet-stream'], 'p10' => ['application/x-pkcs10', 'application/pkcs10'], 'p12' => 'application/x-pkcs12', 'p7a' => 'application/x-pkcs7-signature', diff --git a/application/extra/view/admin.content.html b/application/extra/view/admin.content.html index 6c968e142..5363c1c15 100644 --- a/application/extra/view/admin.content.html +++ b/application/extra/view/admin.content.html @@ -1,4 +1,5 @@
+ {block name="style"}{/block} {if isset($title)}
{$title}
diff --git a/application/wechat/controller/Config.php b/application/wechat/controller/Config.php index d5f208b32..11bb6402d 100644 --- a/application/wechat/controller/Config.php +++ b/application/wechat/controller/Config.php @@ -15,6 +15,9 @@ namespace app\wechat\controller; use controller\BasicAdmin; use service\DataService; +use service\PayService; +use think\Db; +use Wechat\WechatService; /** * 微信配置管理 @@ -54,18 +57,49 @@ class Config extends BasicAdmin { */ public function pay() { if ($this->request->isGet()) { - $this->assign('title', '微信支付配置'); - return view(); + switch ($this->request->get('action')) { + // 生成测试支付二维码 + case 'payqrc': + $pay = &load_wechat('pay'); + // 生成订单号 + $order_no = session('pay-test-order-no'); + if (empty($order_no)) { + $order_no = DataService::createSequence(10, 'wechat-pay-test'); + session('pay-test-order-no',$order_no); + } + // 该订单号已经支付 + if(PayService::isPay($order_no)){ + return json(['code'=>2,'order_no'=>$order_no]); + } + // 订单号未支付,生成支付二维码URL + $url = PayService::createWechatPayQrc($pay, $order_no, 1, '扫码支付测试!'); + if ($url !== false) { + return json(['code'=>1,'url'=>$url,'order_no'=>$order_no]); + } + // 生成支付二维码URL失败 + $this->error("生成支付二维码失败,{$pay->errMsg}[{$pay->errCode}]"); + break; + // 检查订单是否支付成功 + case 'check': + $order_no = $this->request->get('order_no'); + if (PayService::isPay($order_no)) { + $this->success('已经支付成功!', ''); + } + $this->error('订单尚未支付!'); + break; + default: + $this->assign('title', '微信支付配置'); + return view(); + } } $data = $this->request->post(); - if (!empty($data['cert_zip_md5'])) { - $filename = ROOT_PATH . 'public/upload/' . join('/', str_split($data['cert_zip_md5'], 16)) . '.zip'; - if (file_exists($filename)) { - $zip = new \PclZip($filename); - $dirpath = APP_PATH . 'extra/wechat/cert'; - !file_exists($dirpath) && mkdir($dirpath, 0755, true); - $result = $zip->extract(PCLZIP_OPT_PATH, $dirpath); - dump($result); + foreach ($data as $key => $vo) { + if (in_array($key, ['wechat_cert_key_md5', 'wechat_cert_cert_md5']) && !empty($vo)) { + $filename = ROOT_PATH . 'public/upload/' . join('/', str_split($vo, 16)) . '.pem'; + !file_exists($filename) && $this->error('支付双向证书上传失败,请重新上传!'); + $keyname = str_replace('_md5', '', $key); + $data[$keyname] = $filename; + unset($data[$key]); } } foreach ($data as $key => $vo) { diff --git a/application/wechat/view/config.pay.html b/application/wechat/view/config.pay.html index c4b91b269..4cd2a20f1 100644 --- a/application/wechat/view/config.pay.html +++ b/application/wechat/view/config.pay.html @@ -1,5 +1,20 @@ {extend name="extra@admin/content"} +{block name="style"} + +{/block} + {block name="content"}
@@ -30,14 +45,26 @@
- -
- -
- - 已设置支付证书 (cert.zip) -
+ +
+ + + +

企业打款、企业红包、订单退款等操作需要使用双向证书,可在微信商户平台下载证书!

@@ -52,66 +79,57 @@
- - - +
- +{/block} + +{block name="script"} + {/block} diff --git a/extend/service/FileService.php b/extend/service/FileService.php index 120a64ddc..c647abb60 100644 --- a/extend/service/FileService.php +++ b/extend/service/FileService.php @@ -117,8 +117,9 @@ class FileService { static public function readFile($filename, $storage = null) { switch (empty($storage) ? sysconf('storage_type') : $storage) { case 'local': - if (file_exists(ROOT_PATH . 'public/upload/' . $filename)) { - return file_get_contents(ROOT_PATH . 'public/upload/' . $filename); + $filepath = ROOT_PATH . 'public/upload/' . $filename; + if (file_exists($filepath)) { + return file_get_contents($filepath); } case 'qiniu': $auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key')); @@ -133,13 +134,13 @@ class FileService { * @param string $filename * @param string $bodycontent * @param string|null $file_storage - * @return array|null + * @return array|false */ static public function save($filename, $bodycontent, $file_storage = null) { $type = empty($file_storage) ? sysconf('storage_type') : $file_storage; if (!method_exists(__CLASS__, $type)) { Log::error("保存存储失败,调用{$type}存储引擎不存在!"); - return null; + return false; } return self::$type($filename, $bodycontent); } @@ -153,7 +154,7 @@ class FileService { static public function local($filename, $bodycontent) { $filepath = ROOT_PATH . 'public/upload/' . $filename; try { - !is_dir(dirname($filepath)) && mkdir(dirname($filepath), '0755', true); + !file_exists(dirname($filepath)) && mkdir(dirname($filepath), '0755', true); if (file_put_contents($filepath, $bodycontent)) { return [ 'file' => $filepath, diff --git a/extend/service/PayService.php b/extend/service/PayService.php index 41afd9b9a..ea0093c80 100644 --- a/extend/service/PayService.php +++ b/extend/service/PayService.php @@ -46,22 +46,22 @@ class PayService { * @param int $fee 支付金额 * @param string $title 订单标题 * @param string $from 订单来源 - * @return bool + * @return false|string */ public static function createWechatPayQrc(WechatPay $pay, $order_no, $fee, $title, $from = 'wechat') { $prepayid = self::_createWechatPrepayid($pay, null, $order_no, $fee, $title, 'NATIVE', $from); if ($prepayid === false) { return false; } - $filename = 'wechat/payqrc/' . join('/', str_split(md5($prepayid), 16)) . '.png'; - if (!FileService::hasFile($filename)) { + $filename = 'wechat/qrc/' . join('/', str_split(md5($prepayid), 16)) . '.png'; + if (!FileService::hasFile($filename, 'local')) { $qrCode = new QrCode(); $qrCode->setText($prepayid); - FileService::save($filename, $qrCode->get()); + if (null === FileService::save($filename, $qrCode->get(), 'local')) { + return false; + } } - ob_clean(); - header("Content-type: image/png"); - return FileService::readFile($filename); + return FileService::getFileUrl($filename, 'local'); }