quanwei
2025-12-25 a47b138c7455dee981af9b4fac431a16c0eee675
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
 
namespace app\supplier\model\app;
 
use app\common\model\app\AppPay as AppPayModel;
use think\facade\Cache;
use app\common\library\fbpay\FbPay; //by yj
 
/**
 * 应用模型
 */
class AppPay extends AppPayModel
{
    /**
     * 保存支付设置
     */
    public function editPay($data){
 
        $this->startTrans();
        try {
            $data['app_id'] = self::$app_id;
            $data['pay_type'] = empty($data['pay_type']) ? '' : json_encode($data['pay_type']);
            if (!empty($data['cert_pem']) && !empty($data['key_pem'])) {
                // 写入微信支付证书文件
                $this->writeCertPemFiles($data['cert_pem'], $data['key_pem'],$data['shop_supplier_id']);
            }
            $this->save($data);
 
            //如果是付呗接口,检查参数配置 by yj
            if($data['apitype']) {
                $app = FbPay::getFbPayApp(self::$app_id, $data['shop_supplier_id']);
                $FbPay = new FbPay($app);
                $res = $FbPay->wxconfig();
                if($res != true) {
                    $this->error = $res;
                    $this->rollback();
                    return false;
                }
            }
 
            // 更新缓存 by yj 2022.12.23
            Cache::tag('cache')->set('app_pay' . $data['shop_supplier_id'], $data);
 
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 写入cert证书文件
     */
    private function writeCertPemFiles($cert_pem = '', $key_pem = '',$shop_supplier_id)
    {
        // 证书目录
        $filePath = root_path() . 'runtime/cert/app/' . self::$app_id . '/'. $shop_supplier_id . '/';
        // 目录不存在则自动创建
        if (!is_dir($filePath)) {
            mkdir($filePath, 0755, true);
        }
        // 写入cert.pem文件
        if (!empty($cert_pem)) {
            file_put_contents($filePath . 'cert.pem', $cert_pem);
        }
        // 写入key.pem文件
        if (!empty($key_pem)) {
            file_put_contents($filePath . 'key.pem', $key_pem);
        }
        return true;
    }
 
}