Browse Source

[更新]增加微信支付测试功能svn

邹景立 8 years ago
parent
commit
801cd99a4b

+ 1 - 1
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',

+ 1 - 0
application/extra/view/admin.content.html

@@ -1,4 +1,5 @@
 <div class="ibox">
+    {block name="style"}{/block}
     {if isset($title)}
     <div class="ibox-title">
         <h5>{$title}</h5>

+ 44 - 10
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) {

+ 79 - 61
application/wechat/view/config.pay.html

@@ -1,5 +1,20 @@
 {extend name="extra@admin/content"}
 
+{block name="style"}
+<style>
+    .pay-qrc-test {
+        height: 248px;
+        width: 248px;
+        background: url('__STATIC__/plugs/layui/css/modules/layer/default/loading-2.gif') no-repeat center center
+    }
+
+    .refund-qrc-test {
+        text-align: center;
+        padding: 8px
+    }
+</style>
+{/block}
+
 {block name="content"}
 <form onsubmit="return false;" action="{:request()->url(true)}" data-auto="true" method="post"
       class='form-horizontal' style='padding-top:20px'>
@@ -30,14 +45,26 @@
     <div class="hr-line-dashed"></div>
 
     <div class="form-group">
-        <label class="col-sm-2 control-label">Cert(支付证书)</label>
-        <div class="col-sm-7">
-            <input type="hidden" name="cert_zip_md5" value="" onchange="this.value=$(this).attr('data-md5')"/>
-            <div data-file="one" data-type="zip" data-field="cert_zip_md5" data-uptype="local"
-                 class="well" style="text-align:center;cursor:pointer">
-                <span class="fa fa-check-circle-o" style="color:#00B83F;font-size:16px"></span>
-                已设置支付证书 (cert.zip)
-            </div>
+        <label class="col-sm-2 control-label">Cert(双向证书)</label>
+        <div class="col-sm-8">
+            <input title="application_key.pem" type="hidden" name="wechat_cert_key_md5">
+            <input title="application_cert.pem" type="hidden" name="wechat_cert_cert_md5">
+            <button data-file="one" data-type="pem" data-field="wechat_cert_key_md5"
+                    type="button" class="layui-btn layui-btn-primary">
+                {if file_exists(sysconf('wechat_cert_key'))}
+                已上传 application_key.pem 文件
+                {else/}
+                请上传 application_key.pem 文件
+                {/if}
+            </button>
+            <button data-file="one" data-type="pem" data-field="wechat_cert_cert_md5"
+                    type="button" class="layui-btn layui-btn-primary">
+                {if file_exists(sysconf('wechat_cert_cert'))}
+                已上传 application_cert.pem 文件
+                {else/}
+                请上传 application_cert.pem 文件
+                {/if}
+            </button>
             <p class="help-block">
                 企业打款、企业红包、订单退款等操作需要使用双向证书,可在<a href="https://pay.weixin.qq.com" target="_blank">微信商户平台</a>下载证书!
             </p>
@@ -52,66 +79,57 @@
     <div class="form-group">
         <div class="col-sm-6 col-sm-offset-2 text-center">
             <button type="submit" class="layui-btn">保存配置</button>
-            <button type="button" onerror="alert(this.src)" data-pay-test class="layui-btn layui-btn-primary">支付测试
-            </button>
-            <style>
-                .pay-qrc-test {
-                    height: 248px;
-                    width: 248px;
-                    background: url('http://basic.data.cuci.cc/static/plugs/layui/layer/skin/default/loading-2.gif') no-repeat center center
-                }
+            <button type="button" data-pay-test class="layui-btn layui-btn-primary">支付测试</button>
+        </div>
+    </div>
 
-                .refund-qrc-test {
-                    text-align: center;
-                    padding: 8px
-                }
-            </style>
-            <script>
-                $(function () {
-                    /* global layer */
-                    $('[data-pay-test]').on('click', function () {
-                        var timer = null;
+</form>
+{/block}
+
+{block name="script"}
+<script>
+    $(function () {
+
+        /*! 证书上传动作 */
+        $('[name="wechat_cert_key_md5"],[name="wechat_cert_cert_md5"]').on('change', function () {
+            this.value = $(this).attr('data-md5');
+            $('[data-field="' + this.name + '"]').html('已选择 ' + this.title + ' 文件');
+        });
+
+        var isShow = false;
+        var timer = null;
+
+        /*! 支付测试动作 */
+        function payTest() {
+            $.form.load('{:url("pay")}?action=payqrc', {}, 'get', function (ret) {
+                if (ret.code === 1 || ret.code === 2) {
+                    if(ret.code===2){
+                        ret.url = '__STATIC__/theme/default/img/wechat/qrc_payed.jpg';
+                    }
+                    if(isShow){
+                        $('.pay-qrc-test').attr('src',ret.url)
+                    }else{
                         layer.open({
                             type: 1, title: false, closeBtn: 1, shadeClose: true,
-                            content: function () {
-                                return '<img class="pay-qrc-test" src="{src}"/>'.replace('{src}', 'http://basic.data.cuci.cc/index.php/wechat/conf/pay.html?action=payqrc')
-                                    + '<p style="text-align:center">请用微信扫码测试支付!</p>'
-                                    + '<p class="refund-qrc-test"><a class="btn btn-xs btn-warning">退款测试</a></p>';
-                            }(),
+                            content: '<img class="pay-qrc-test" onerror="this.src=__STATIC__/theme/default/img/wechat/qrc_payed.jpg" src="' + ret.url + '"/>'
+                            + '<p style="text-align:center">请用微信扫码测试支付!</p>'
+                            + '<p class="refund-qrc-test"><a class="btn btn-xs btn-warning">退款测试</a></p>',
                             end: function () {
-                                (timer !== null) && window.clearTimeout(timer), timer = null;
+                                isShow = false;
+                                timer && window.clearInterval(timer);
                             },
                             success: function (layero, index) {
-                                $(layero).find('.pay-qrc-test').on('error', function () {
-                                    $.form.load(this.src), layer.close(index);
-                                }).on('load', function () {
-                                    var img = this;
-                                    (timer !== null) && window.clearTimeout(timer), timer = null;
-                                    timer = setTimeout(function () {
-                                        img.src = img.src.replace(/\&t=\d*/, '') + '&t=' + (new Date().getTime());
-                                    }, 3000);
-                                });
-                                $(layero).on('click', '.refund-qrc-test a', function () {
-                                    $.msg.loading();
-                                    $.get('http://basic.data.cuci.cc/index.php/wechat/conf/pay.html?action=refund', function (ret) {
-                                        if (ret.code === "SUCCESS") {
-                                            $.msg.tips(ret.info, 2, function () {
-                                                (timer !== null) && window.clearTimeout(timer), timer = null;
-                                                layer.close(index);
-                                            });
-                                        } else {
-                                            $.msg.tips(ret.info, 2);
-                                        }
-                                    });
-                                });
+                                isShow = true;
+                                timer = setInterval(payTest,1000);
                             }
                         });
-                    });
-                });
-            </script>
-        </div>
-    </div>
-
-</form>
+                    }
+                }
+                return false;
+            });
+        }
+        $('[data-pay-test]').on('click', payTest);
+    });
 
+</script>
 {/block}

+ 6 - 5
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,

+ 7 - 7
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');
     }