PaymentService.php 32.4 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
5
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
7
// | Licensed ( https://opensource.org/licenses/mit-license.php )
D
v1.2.0  
devil_gong 已提交
8 9 10 11 12
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

D
Devil 已提交
13
use think\facade\Db;
14
use app\service\ResourcesService;
D
Devil 已提交
15
use app\service\StoreService;
D
v1.2.0  
devil_gong 已提交
16 17 18 19 20 21 22 23 24 25 26

/**
 * 支付方式服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class PaymentService
{
    // 插件目录
27
    public static $payment_dir;
D
v1.2.0  
devil_gong 已提交
28 29

    // 支付业务类型
30
    public static $payment_business_type_all;
D
v1.2.0  
devil_gong 已提交
31 32

    // 不删除的支付方式
33
    public static $cannot_deleted_list;
D
v1.2.0  
devil_gong 已提交
34 35

    // 入口文件位置
36
    public static $dir_root_path;
D
v1.2.0  
devil_gong 已提交
37 38 39 40 41 42 43 44 45 46

    /**
     * 初始化
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-24
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
47
    private static function Init($params = [])
D
v1.2.0  
devil_gong 已提交
48 49
    {
        // 插件目录
50
        self::$payment_dir = ROOT.'extend'.DS.'payment'.DS;
D
v1.2.0  
devil_gong 已提交
51 52

        // 支付业务类型
D
Devil 已提交
53
        self::$payment_business_type_all = MyConfig('shopxo.payment_business_type_all');
D
v1.2.0  
devil_gong 已提交
54 55

        // 不删除的支付方式
56
        self::$cannot_deleted_list = ['DeliveryPayment', 'CashPayment'];
D
v1.2.0  
devil_gong 已提交
57 58

        // 入口文件位置
59
        self::$dir_root_path = defined('IS_ROOT_ACCESS') ? ROOT : ROOT.'public'.DS;
D
v1.2.0  
devil_gong 已提交
60 61 62 63 64 65 66 67 68 69
    }

    /**
     * 获取支付插件列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     */
70
    public static function PlugPaymentList()
D
v1.2.0  
devil_gong 已提交
71 72
    {
        // 初始化
73
        self::Init();
D
v1.2.0  
devil_gong 已提交
74 75 76

        // 开始处理
        $data = [];
77
        if(is_dir(self::$payment_dir))
D
v1.2.0  
devil_gong 已提交
78
        {
79
            if($dh = opendir(self::$payment_dir))
D
v1.2.0  
devil_gong 已提交
80
            {
D
devil 已提交
81
                $common_platform_type = lang('common_platform_type');
D
v1.2.0  
devil_gong 已提交
82 83 84 85 86 87
                while(($temp_file = readdir($dh)) !== false)
                {
                    if(substr($temp_file, 0, 1) != '.')
                    {
                        // 获取模块配置信息
                        $payment = htmlentities(str_replace('.php', '', $temp_file));
88
                        $config = self::GetPaymentConfig($payment);
D
v1.2.0  
devil_gong 已提交
89 90 91
                        if($config !== false)
                        {
                            // 数据组装
92
                            $temp = self::DataAnalysis($config);
D
v1.2.0  
devil_gong 已提交
93 94 95 96
                            $temp['id'] = date('YmdHis').GetNumberCode(8);
                            $temp['payment'] = $payment;

                            // 获取数据库配置信息
97
                            $db_config = self::PaymentList(['where'=>['payment'=>$payment]]);
D
v1.2.0  
devil_gong 已提交
98 99 100 101 102 103 104 105 106
                            if(!empty($db_config[0]))
                            {
                                $temp['is_install'] = 1;
                                $temp['id'] = $db_config[0]['id'];
                                $temp['name'] = $db_config[0]['name'];
                                $temp['logo'] = $db_config[0]['logo'];
                                $temp['config'] = $db_config[0]['config'];
                                $temp['is_enable'] = $db_config[0]['is_enable'];
                                $temp['is_open_user'] = $db_config[0]['is_open_user'];
D
devil 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

                                // 支付平台类型
                                $apply_terminal_names = [];
                                if(!empty($db_config[0]['apply_terminal']) && is_array($db_config[0]['apply_terminal']))
                                {
                                    foreach($common_platform_type as $platform_type)
                                    {
                                        if(in_array($platform_type['value'], $db_config[0]['apply_terminal']))
                                        {
                                            $apply_terminal_names[] = $platform_type['name'];
                                        }
                                    }
                                }
                                $temp['apply_terminal_names'] = $apply_terminal_names;
                                $temp['apply_terminal'] = $db_config[0]['apply_terminal'];
D
v1.2.0  
devil_gong 已提交
122 123 124 125 126 127 128 129
                            }
                            $data[] = $temp;
                        }
                    }
                }
                closedir($dh);
            }
        }
D
devil 已提交
130
        return DataReturn('success', 0, $data);
D
v1.2.0  
devil_gong 已提交
131 132 133 134 135 136 137 138 139 140 141
    }

    /**
     * 获取支付模块配置信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [string]          $payment [模块名称]
     */
D
Devil 已提交
142
    public static function GetPaymentConfig($payment)
D
v1.2.0  
devil_gong 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    {
        $payment = '\payment\\'.$payment;
        if(class_exists($payment))
        {
            $obj = new $payment();
            if(method_exists($obj, 'Config') && method_exists($obj, 'Pay') && method_exists($obj, 'Respond'))
            {
                return $obj->Config();
            }
        }
        return false;
    }

    /**
     * 数据解析
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [array]          $data [插件配置信息]
     */
165
    private static function DataAnalysis($data)
D
v1.2.0  
devil_gong 已提交
166 167
    {
        return [
D
devil 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180
            'name'                  => isset($data['base']['name']) ? htmlentities($data['base']['name']) : '',
            'version'               => isset($data['base']['version']) ? htmlentities($data['base']['version']) : '',
            'apply_version'         => isset($data['base']['apply_version']) ? htmlentities($data['base']['apply_version']) : '',
            'desc'                  => isset($data['base']['desc']) ? $data['base']['desc'] : '',
            'author'                => isset($data['base']['author']) ? htmlentities($data['base']['author']) : '',
            'author_url'            => isset($data['base']['author_url']) ? htmlentities($data['base']['author_url']) : '',
            'element'               => isset($data['element']) ? $data['element'] : [],
            'logo'                  => '',
            'is_enable'             => 0,
            'is_open_user'          => 0,
            'is_install'            => 0,
            'apply_terminal'        => empty($data['base']['apply_terminal']) ? array_column(lang('common_platform_type'), 'value') : $data['base']['apply_terminal'],
            'config'                => '',
D
v1.2.0  
devil_gong 已提交
181 182 183 184 185 186 187 188 189 190 191 192
        ];
    }

    /**
     * 支付方式列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-19
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
193
    public static function PaymentList($params = [])
D
v1.2.0  
devil_gong 已提交
194 195 196 197 198 199 200 201 202 203 204
    {
        $where = empty($params['where']) ? [] : $params['where'];
        if(isset($params['is_enable']))
        {
            $where['is_enable'] = intval($params['is_enable']);
        }
        if(isset($params['is_open_user']))
        {
            $where['is_open_user'] = intval($params['is_open_user']);
        }

D
Devil 已提交
205
        $data = Db::name('Payment')->where($where)->field('id,logo,name,sort,payment,config,apply_terminal,apply_terminal_old,element,is_enable,is_open_user')->order('sort asc')->select()->toArray();
D
v1.2.0  
devil_gong 已提交
206 207 208 209 210
        if(!empty($data) && is_array($data))
        {
            foreach($data as &$v)
            {
                $v['logo_old'] = $v['logo'];
211
                $v['logo'] = ResourcesService::AttachmentPathViewHandle($v['logo']);
D
v1.2.0  
devil_gong 已提交
212 213 214
                $v['element'] = empty($v['element']) ? '' : json_decode($v['element'], true);
                $v['config'] = empty($v['config']) ? '' : json_decode($v['config'], true);
                $v['apply_terminal'] = empty($v['apply_terminal']) ? '' : json_decode($v['apply_terminal'], true);
D
devil 已提交
215
                $v['apply_terminal_old'] = empty($v['apply_terminal_old']) ? '' : json_decode($v['apply_terminal_old'], true);
D
v1.2.0  
devil_gong 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229
            }
        }
        return $data;
    }

    /**
     * 获取支付方式列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-19
     * @desc    下订单根据终端自动筛选支付方式
     * @param   [array]          $params [输入参数]
     */
230
    public static function BuyPaymentList($params = [])
D
v1.2.0  
devil_gong 已提交
231
    {
232
        $data = self::PaymentList($params);
D
v1.2.0  
devil_gong 已提交
233 234 235 236 237 238 239 240
        $result = [];
        if(!empty($data))
        {
            foreach($data as $v)
            {
                // 根据终端类型筛选
                if(in_array(APPLICATION_CLIENT_TYPE, $v['apply_terminal']))
                {
D
devil 已提交
241
                    unset($v['config'], $v['element'], $v['apply_terminal'], $v['author'], $v['author_url'], $v['is_open_user'], $v['is_enable']);
D
v1.2.0  
devil_gong 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255
                    $result[] = $v;
                }
            }
        }
        return $result;
    }

    /**
     * 获取订单支付名称
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-19
     * @desc    description
D
Devil 已提交
256
     * @param   [int|array]      $business_ids       [业务订单id]
D
v1.2.0  
devil_gong 已提交
257
     */
D
Devil 已提交
258
    public static function OrderPaymentName($business_ids = 0)
D
v1.2.0  
devil_gong 已提交
259
    {
D
Devil 已提交
260
        if(empty($business_ids))
D
devil 已提交
261
        {
D
Devil 已提交
262 263 264 265 266 267 268 269 270 271
            return null;
        }

        // 参数处理查询数据
        if(is_array($business_ids))
        {
            $business_ids = array_filter(array_unique($business_ids));
        }
        if(!empty($business_ids))
        {
D
Devil 已提交
272
            $res = Db::name('PayLog')->alias('pl')->join('pay_log_value plv', 'pl.id=plv.pay_log_id')->where(['plv.business_id'=>$business_ids])->order('pl.id desc')->field('plv.business_id,pl.payment_name')->select()->toArray();
D
Devil 已提交
273 274
            $data = [];
            if(!empty($res) && is_array($res))
D
devil 已提交
275
            {
D
Devil 已提交
276 277 278 279 280 281 282
                foreach($res as $v)
                {
                    if(!array_key_exists($v['business_id'], $data))
                    {
                        $data[$v['business_id']] = $v['payment_name'];
                    }
                }
D
devil 已提交
283 284
            }
        }
D
Devil 已提交
285 286 287 288 289 290 291

        // id数组则直接返回
        if(is_array($business_ids))
        {
            return empty($data) ? [] : $data;
        }
        return (!empty($data) && is_array($data) && array_key_exists($business_ids, $data)) ? $data[$business_ids] : null;
D
v1.2.0  
devil_gong 已提交
292 293 294 295 296 297 298 299 300 301 302
    }

    /**
     * 数据更新
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-19
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
303
    public static function PaymentUpdate($params = [])
D
v1.2.0  
devil_gong 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    {
        // 请求类型
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'name',
                'checked_data'      => '2,60',
                'error_msg'         => '名称长度 2~60 个字符',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'apply_terminal',
                'error_msg'         => '至少选择一个适用终端',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'sort',
                'checked_data'      => '3',
                'error_msg'         => '顺序 0~255 之间的数值',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 附件
        $data_fields = ['logo'];
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);

        // 数据
        $data = [
            'name'              => $params['name'],
            'apply_terminal'    => empty($params['apply_terminal']) ? '' : json_encode(explode(',', $params['apply_terminal'])),
            'logo'              => $attachment['data']['logo'],
345
            'config'            => json_encode(self::GetPlugConfig($params)),
D
v1.2.0  
devil_gong 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            'sort'              => intval($params['sort']),
            'is_enable'         => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
            'is_open_user'      => isset($params['is_open_user']) ? intval($params['is_open_user']) : 0,
        ];

        $data['upd_time'] = time();
        if(Db::name('Payment')->where(['id'=>intval($params['id'])])->update($data))
        {
            return DataReturn('编辑成功', 0);
        }
        return DataReturn('编辑失败', -100); 
    }

    /**
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-18
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
367
    private static function GetPlugConfig($params = [])
D
v1.2.0  
devil_gong 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    {
        $data = [];
        foreach($params as $k=>$v)
        {
            if(substr($k, 0, 8) == 'plugins_')
            {
                $data[substr($k, 8)] = $v;
            }
        }
        return $data;
    }

    /**
     * 状态更新
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
388
    public static function PaymentStatusUpdate($params = [])
D
v1.2.0  
devil_gong 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'field',
                'error_msg'         => '未指定操作字段',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'state',
                'checked_data'      => [0,1],
                'error_msg'         => '状态有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 数据更新
        if(Db::name('Payment')->where(['payment'=>$params['id']])->update([$params['field']=>intval($params['state']), 'upd_time'=>time()]))
        {
            return DataReturn('操作成功');
        }
        return DataReturn('操作失败', -100);
    }

    /**
     * 权限校验
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-29T00:01:49+0800
     */
430
    private static function PowerCheck()
D
v1.2.0  
devil_gong 已提交
431 432
    {
        // 入口文件目录
433
        if(!is_writable(self::$dir_root_path))
D
v1.2.0  
devil_gong 已提交
434
        {
435
            return DataReturn('目录没有操作权限'.'['.self::$dir_root_path.']', -3);
D
v1.2.0  
devil_gong 已提交
436 437 438
        }

        // 插件权限
439
        if(!is_writable(self::$payment_dir))
D
v1.2.0  
devil_gong 已提交
440
        {
D
devil_gong 已提交
441
            return DataReturn('目录没有操作权限'.'['.self::$payment_dir.']', -4);
D
v1.2.0  
devil_gong 已提交
442
        }
D
devil 已提交
443 444

        return DataReturn('验证成功', 0);
D
v1.2.0  
devil_gong 已提交
445 446 447 448 449 450 451 452 453 454 455
    }

    /**
     * 上传插件
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
456
    public static function Upload($params = [])
D
v1.2.0  
devil_gong 已提交
457 458 459 460 461 462 463 464 465
    {
        // 文件上传校验
        $error = FileUploadError('file');
        if($error !== true)
        {
            return DataReturn($error, -1);
        }

        // 文件格式化校验
D
Devil 已提交
466
        $type = ResourcesService::ZipExtTypeList();
D
v1.2.0  
devil_gong 已提交
467 468
        if(!in_array($_FILES['file']['type'], $type))
        {
G
gongfuxiang 已提交
469
            return DataReturn('文件格式有误,请上传zip压缩包', -2);
D
v1.2.0  
devil_gong 已提交
470 471
        }

D
Devil 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
        // 上传处理
        return self::UploadHandle($_FILES['file']['tmp_name'], $params);
    }

    /**
     * 上传插件处理
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [string]         $package_file [软件包地址]
     * @param   [array]          $params       [输入参数]
     */
    public static function UploadHandle($package_file, $params = [])
    {
        // 初始化
        self::Init();

        // 权限
        $ret = self::PowerCheck();
        if($ret['code'] != 0)
        {
            return $ret;
        }

G
gongfuxiang 已提交
498
        // 开始解压文件
D
Devil 已提交
499
        $resource = zip_open($package_file);
G
gongfuxiang 已提交
500
        if(!is_resource($resource))
D
v1.2.0  
devil_gong 已提交
501
        {
G
gongfuxiang 已提交
502
            return DataReturn('压缩包打开失败['.$resource.']', -10);
D
v1.2.0  
devil_gong 已提交
503 504
        }

G
gongfuxiang 已提交
505 506 507
        $success = 0;
        $error = 0;
        while(($temp_resource = zip_read($resource)) !== false)
D
v1.2.0  
devil_gong 已提交
508
        {
G
gongfuxiang 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
            if(zip_entry_open($resource, $temp_resource))
            {
                // 当前压缩包中项目名称
                $file = zip_entry_name($temp_resource);

                // 排除临时文件和临时目录
                if(strpos($file, '/.') === false && strpos($file, '__') === false)
                {
                    // 忽略非php文件
                    if(substr($file, -4) != '.php')
                    {
                        $error++;
                        continue;
                    }

                    // 文件名称
                    $payment = str_replace(array('.', '/', '\\', ':'), '', substr($file, 0, -4));

                    // 是否已有存在插件
                    if(file_exists(self::$payment_dir.$payment))
                    {
                        $error++;
                        continue;
                    } else {
                        $file = self::$payment_dir.$payment.'.php';
                    }

                    // 如果不是目录则写入文件
                    if(!is_dir($file))
                    {
                        // 读取这个文件
                        $file_size = zip_entry_filesize($temp_resource);
                        $file_content = zip_entry_read($temp_resource, $file_size);
                        if(@file_put_contents($file, $file_content) !== false)
                        {
                            // 文件校验
                            $config = self::GetPaymentConfig($payment);
                            if($config === false)
                            {
                                $error++;
                                @unlink($file);
                            } else {
                                $success++;
                            }
                        }
                    }
                }
            }
D
v1.2.0  
devil_gong 已提交
557 558
        }

G
gongfuxiang 已提交
559
        if($success > 0)
D
v1.2.0  
devil_gong 已提交
560
        {
561
            return DataReturn('上传成功[成功'.$success.'个支付插件, 失败'.$error.'个无效文件]', 0);
D
v1.2.0  
devil_gong 已提交
562
        }
563
        return DataReturn('上传失败,'.$error.'个无效文件、如功能插件请到[ 应用中心->应用管理 ]模块里面去上传安装', -10);
D
v1.2.0  
devil_gong 已提交
564 565 566 567 568 569 570 571 572 573 574
    }

    /**
     * 安装
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
575
    public static function Install($params = [])
D
v1.2.0  
devil_gong 已提交
576 577
    {
        // 初始化
578
        self::Init();
D
v1.2.0  
devil_gong 已提交
579 580 581 582 583 584 585 586 587

        // 参数
        if(empty($params['id']))
        {
            return DataReturn('参数错误', -1);
        }

        // 数据处理
        $payment = $params['id'];
588
        $config = self::GetPaymentConfig($payment);
D
v1.2.0  
devil_gong 已提交
589 590
        if($config !== false)
        {
591
            $data = self::DataAnalysis($config);
D
devil 已提交
592
            $apply_terminal = empty($data['apply_terminal']) ? '' : json_encode($data['apply_terminal']);
D
v1.2.0  
devil_gong 已提交
593 594
            $data['payment'] = $payment;
            $data['element'] = empty($data['element']) ? '' : json_encode($data['element']);
D
devil 已提交
595 596
            $data['apply_terminal_old'] = $apply_terminal;
            $data['apply_terminal'] = $apply_terminal;
D
v1.2.0  
devil_gong 已提交
597 598 599 600 601 602 603 604 605 606
            $data['sort'] = 0;
            $data['add_time'] = time();

            // 移除多余的字段
            unset($data['is_install']);

            // 开启事务
            Db::startTrans();
            if(Db::name('Payment')->insertGetId($data) > 0)
            {
D
devil_gong 已提交
607 608 609 610
                // 提交事务
                Db::commit();

               return DataReturn('安装成功'); 
D
v1.2.0  
devil_gong 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
            } else {
                // 事务回滚
                Db::rollback();
                return DataReturn('安装失败', -100);
            }
        } else {
            return DataReturn('插件配置有误', -10);
        }
    }

    /**
     * 删除插件
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
630
    public static function Delete($params = [])
D
v1.2.0  
devil_gong 已提交
631 632
    {
        // 初始化
633
        self::Init();
D
v1.2.0  
devil_gong 已提交
634 635

        // 权限
636
        $ret = self::PowerCheck();
D
v1.2.0  
devil_gong 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 参数
        if(empty($params['id']))
        {
            return DataReturn('参数错误', -1);
        }

        // 是否禁止删除
        $payment = $params['id'];
650
        if(in_array($payment, self::$cannot_deleted_list))
D
v1.2.0  
devil_gong 已提交
651 652 653 654 655
        {
            return DataReturn('该支付方式禁止删除', -10);
        }

        // 是否存在
656
        $file = self::$payment_dir.$payment.'.php';
D
v1.2.0  
devil_gong 已提交
657 658 659 660 661 662 663 664
        if(!file_exists($file))
        {
            return DataReturn('资源不存在或已被删除', -2);
        }

        // 权限
        if(!is_writable($file))
        {
D
devil_gong 已提交
665
            return DataReturn('没操作权限['.$file.']', -3);
D
v1.2.0  
devil_gong 已提交
666 667 668 669 670 671 672 673 674
        }

        // 删除
        if(!@unlink($file))
        {
            return DataReturn('删除失败', -100);
        }

        // 删除入口文件
D
devil_gong 已提交
675
        self::PaymentEntranceDelete(['payment' => $payment]);
D
v1.2.0  
devil_gong 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688

        return DataReturn('删除成功');
    }

    /**
     * 卸载
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-17
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
689
    public static function Uninstall($params = [])
D
v1.2.0  
devil_gong 已提交
690 691 692 693 694 695 696 697
    {
        // 参数
        if(empty($params['id']))
        {
            return DataReturn('参数错误', -1);
        }

        // 初始化
698
        self::Init();
D
v1.2.0  
devil_gong 已提交
699 700 701

        // 开始卸载
        $payment = $params['id'];
D
Devil 已提交
702
        if(Db::name('Payment')->where(['payment'=>$payment])->delete())
D
v1.2.0  
devil_gong 已提交
703 704
        {
            // 删除入口文件
D
devil_gong 已提交
705
            self::PaymentEntranceDelete(['payment' => $payment]);
D
v1.2.0  
devil_gong 已提交
706 707 708 709 710 711 712

            return DataReturn('卸载成功', 0);
        }
        return DataReturn('卸载失败', -100);
    }

    /**
D
devil_gong 已提交
713
     * 入口文件创建
D
v1.2.0  
devil_gong 已提交
714 715 716 717
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-28T23:38:52+0800
D
devil_gong 已提交
718 719 720 721 722 723
     * @param    [array]            $params                 [输入参数]
     * @param    [string]           $params['payment']      [支付唯一标记]
     * @param    [array]            $params['business']     [处理业务, 默认配置文件读取]
     * @param    [array]            $params['not_notify']   [不生成异步入口]
     * @param    [string]           $params['respond']      [同步参数值]
     * @param    [string]           $params['notify']       [异步参数值]
D
v1.2.0  
devil_gong 已提交
724
     */
D
devil_gong 已提交
725
    public static function PaymentEntranceCreated($params = [])
D
v1.2.0  
devil_gong 已提交
726
    {
D
devil_gong 已提交
727 728 729
        // 初始化
        self::Init();

D
devil_gong 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
        // 参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'payment',
                'error_msg'         => '支付唯一标记不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'respond',
                'error_msg'         => '支付同步地址参数不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'notify',
                'error_msg'         => '支付异步地址参数不能为空',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

D
v1.2.0  
devil_gong 已提交
754
        // 权限
755
        $ret = self::PowerCheck();
D
v1.2.0  
devil_gong 已提交
756 757 758 759
        if($ret['code'] != 0)
        {
            return $ret;
        }
D
devil_gong 已提交
760

D
devil_gong 已提交
761 762 763 764 765
        if(empty($params['payment']))
        {
            return '支付唯一标记不能为空';
        }

D
Devil 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
        // 获取地址所属模块名称、地址标记去除模块名称
        $module_notify = 'index';
        $module_respond = 'index';
        if(substr($params['notify'], 0, 5) == '/api/')
        {
            $module_notify = 'api';
            $params['notify'] = substr($params['notify'], 5);
        } else {
            $params['notify'] = substr($params['notify'], 7);
        }
        if(substr($params['respond'], 0, 5) == '/api/')
        {
            $module_respond = 'api';
            $params['respond'] = substr($params['respond'], 5);
        } else {
            $params['respond'] = substr($params['respond'], 7);
        }

D
devil_gong 已提交
784
        // 不生成异步入口
D
Devil 已提交
785
        $not_notify = empty($params['not_notify']) ? MyConfig('shopxo.under_line_list') : $params['not_notify'];
D
devil_gong 已提交
786 787 788 789

        // 处理业务
        $business_all = empty($params['business']) ? self::$payment_business_type_all : $params['business'];

D
v1.2.0  
devil_gong 已提交
790
        // 批量创建
D
devil_gong 已提交
791
        foreach($business_all as $v)
D
v1.2.0  
devil_gong 已提交
792
        {
D
devil_gong 已提交
793
            $business_name = strtolower($v['name']);
D
v1.2.0  
devil_gong 已提交
794 795 796 797 798 799 800 801 802
            if(defined('IS_ROOT_ACCESS'))
            {
// 异步
$notify=<<<php
<?php

/**
 * {$v['desc']}支付异步入口
 */
D
Devil 已提交
803
namespace think;
D
v1.2.0  
devil_gong 已提交
804 805

// 默认绑定模块
D
devil_gong 已提交
806
\$_GET['s'] = '{$params["notify"]}';
D
v1.2.0  
devil_gong 已提交
807 808

// 支付模块标记
D
devil_gong 已提交
809
define('PAYMENT_TYPE', '{$params["payment"]}');
D
v1.2.0  
devil_gong 已提交
810

D
Devil 已提交
811 812 813
// 引入公共入口文件
require __DIR__.'/public/core.php';

D
Devil 已提交
814 815 816
// 加载基础文件
require __DIR__ . '/vendor/autoload.php';

D
v1.2.0  
devil_gong 已提交
817 818 819
// 根目录入口
define('IS_ROOT_ACCESS', true);

D
Devil 已提交
820 821 822 823 824
// 执行HTTP应用并响应
\$http = (new App())->http;
\$response = \$http->name('{$module_notify}')->run();
\$response->send();
\$http->end(\$response);
D
v1.2.0  
devil_gong 已提交
825 826 827 828 829 830 831 832 833 834
?>
php;

// 同步
$respond=<<<php
<?php

/**
 * {$v['desc']}支付同步入口
 */
D
Devil 已提交
835
namespace think;
D
v1.2.0  
devil_gong 已提交
836 837

// 默认绑定模块
D
devil_gong 已提交
838
\$_GET['s'] = '{$params["respond"]}';
D
v1.2.0  
devil_gong 已提交
839 840

// 支付模块标记
D
devil_gong 已提交
841
define('PAYMENT_TYPE', '{$params["payment"]}');
D
v1.2.0  
devil_gong 已提交
842

D
Devil 已提交
843 844 845
// 引入公共入口文件
require __DIR__.'/public/core.php';

D
Devil 已提交
846 847 848
// 加载基础文件
require __DIR__ . '/vendor/autoload.php';

D
v1.2.0  
devil_gong 已提交
849 850 851
// 根目录入口
define('IS_ROOT_ACCESS', true);

D
Devil 已提交
852 853 854 855 856
// 执行HTTP应用并响应
\$http = (new App())->http;
\$response = \$http->name('{$module_respond}')->run();
\$response->send();
\$http->end(\$response);
D
v1.2.0  
devil_gong 已提交
857 858 859 860 861 862 863 864 865 866 867 868
?>
php;

            } else {

// 异步
$notify=<<<php
<?php

/**
 * {$v['desc']}支付异步入口
 */
D
Devil 已提交
869
namespace think;
D
v1.2.0  
devil_gong 已提交
870 871

// 默认绑定模块
D
devil_gong 已提交
872
\$_GET['s'] = '{$params["notify"]}';
D
v1.2.0  
devil_gong 已提交
873 874

// 支付模块标记
D
devil_gong 已提交
875
define('PAYMENT_TYPE', '{$params["payment"]}');
D
v1.2.0  
devil_gong 已提交
876

D
Devil 已提交
877 878 879
// 引入公共入口文件
require __DIR__.'/core.php';

D
Devil 已提交
880 881 882
// 加载基础文件
require __DIR__ . '/../vendor/autoload.php';

D
Devil 已提交
883 884 885 886 887
// 执行HTTP应用并响应
\$http = (new App())->http;
\$response = \$http->name('{$module_notify}')->run();
\$response->send();
\$http->end(\$response);
D
v1.2.0  
devil_gong 已提交
888 889 890 891 892 893 894 895 896 897
?>
php;

// 同步
$respond=<<<php
<?php

/**
 * {$v['desc']}支付同步入口
 */
D
Devil 已提交
898
namespace think;
D
v1.2.0  
devil_gong 已提交
899 900

// 默认绑定模块
D
devil_gong 已提交
901
\$_GET['s'] = '{$params["respond"]}';
D
v1.2.0  
devil_gong 已提交
902 903

// 支付模块标记
D
devil_gong 已提交
904
define('PAYMENT_TYPE', '{$params["payment"]}');
D
v1.2.0  
devil_gong 已提交
905

D
Devil 已提交
906 907 908
// 引入公共入口文件
require __DIR__.'/core.php';

D
Devil 已提交
909 910 911
// 加载基础文件
require __DIR__ . '/../vendor/autoload.php';

D
Devil 已提交
912 913 914 915 916
// 执行HTTP应用并响应
\$http = (new App())->http;
\$response = \$http->name('{$module_respond}')->run();
\$response->send();
\$http->end(\$response);
D
v1.2.0  
devil_gong 已提交
917 918 919 920
?>
php;
            }

D
devil_gong 已提交
921
            @file_put_contents(self::$dir_root_path.'payment_'.$business_name.'_'.strtolower($params['payment']).'_respond.php', $respond);
D
v1.2.0  
devil_gong 已提交
922 923

            // 线下支付不生成异步入口文件
D
devil_gong 已提交
924
            if(!in_array($params['payment'], $not_notify))
D
v1.2.0  
devil_gong 已提交
925
            {
D
devil_gong 已提交
926
                @file_put_contents(self::$dir_root_path.'payment_'.$business_name.'_'.strtolower($params['payment']).'_notify.php', $notify);
D
v1.2.0  
devil_gong 已提交
927 928 929 930 931 932 933 934 935 936 937 938
            }
        }

        return DataReturn('操作成功', 0);
    }

    /**
     * [PaymentEntranceDelete 入口文件删除]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-28T23:38:52+0800
D
devil_gong 已提交
939 940 941
     * @param    [array]            $params                 [输入参数]
     * @param    [string]           $params['payment']      [支付唯一标记]
     * @param    [array]            $params['business']     [处理业务, 默认配置文件读取]
D
v1.2.0  
devil_gong 已提交
942
     */
D
devil_gong 已提交
943
    public static function PaymentEntranceDelete($params = [])
D
v1.2.0  
devil_gong 已提交
944
    {
D
devil_gong 已提交
945 946 947
        // 初始化
        self::Init();

D
v1.2.0  
devil_gong 已提交
948
        // 权限
949
        $ret = self::PowerCheck();
D
v1.2.0  
devil_gong 已提交
950 951 952 953
        if($ret['code'] != 0)
        {
            return $ret;
        }
D
devil_gong 已提交
954 955 956 957 958 959 960
        if(empty($params['payment']))
        {
            return '支付唯一标记不能为空';
        }

        // 处理业务
        $business_all = empty($params['business']) ? self::$payment_business_type_all : $params['business'];
D
v1.2.0  
devil_gong 已提交
961

D
devil_gong 已提交
962 963
        $payment = strtolower($params['payment']);
        foreach($business_all as $v)
D
v1.2.0  
devil_gong 已提交
964
        {
D
devil_gong 已提交
965 966
            $business_name = strtolower($v['name']);
            if(file_exists(self::$dir_root_path.'payment_'.$business_name.'_'.$payment.'_notify.php'))
D
v1.2.0  
devil_gong 已提交
967
            {
D
devil_gong 已提交
968
                @unlink(self::$dir_root_path.'payment_'.$business_name.'_'.$payment.'_notify.php');
D
v1.2.0  
devil_gong 已提交
969
            }
D
devil_gong 已提交
970
            if(file_exists(self::$dir_root_path.'payment_'.$business_name.'_'.$payment.'_respond.php'))
D
v1.2.0  
devil_gong 已提交
971
            {
D
devil_gong 已提交
972
                @unlink(self::$dir_root_path.'payment_'.$business_name.'_'.$payment.'_respond.php');
D
v1.2.0  
devil_gong 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
            }
        }

        return DataReturn('操作成功', 0);
    }

    /**
     * 入库文件检查
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-26
     * @desc    description
     * @param   [string]          $payment [支付标记]
     * @param   [string]          $name    [支付业务方式名称]
     */
989
    public static function EntranceFileChecked($payment, $name)
D
v1.2.0  
devil_gong 已提交
990 991
    {
        // 同步返回文件
992
        if(!file_exists(self::$dir_root_path.'payment_'.strtolower($name).'_'.strtolower($payment).'_respond.php'))
D
v1.2.0  
devil_gong 已提交
993 994 995 996 997
        {
            return DataReturn('支付返回入口文件不存在,请联系管理员处理', -10);
        }

        // 线下支付不生成异步入口文件
D
Devil 已提交
998
        if(!in_array($payment, MyConfig('shopxo.under_line_list')))
D
v1.2.0  
devil_gong 已提交
999
        {
1000
            if(!file_exists(self::$dir_root_path.'payment_'.strtolower($name).'_'.strtolower($payment).'_notify.php'))
D
v1.2.0  
devil_gong 已提交
1001 1002 1003 1004 1005 1006
            {
                return DataReturn('支付通知入口文件不存在,请联系管理员处理', -10);
            }
        }
        return DataReturn('校验成功', 0);
    }
D
Devil 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

    /**
     * 支付插件更新信息
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-04-22
     * @desc    description
     * @param   [array]           $params [输入参数]
     */
    public static function PaymentUpgradeInfo($params = [])
    {
        if(!empty($params))
        {
            // 数据处理
            $data = [];
            foreach($params as $v)
            {
                if(!empty($v['name']) && !empty($v['version']) && !empty($v['payment']) && !empty($v['author']))
                {
                    $data[] = [
                        'plugins'   => $v['payment'],
                        'name'      => $v['name'],
                        'ver'       => $v['version'],
                        'author'    => $v['author'],
                    ];
                }
            }
            if(!empty($data))
            {
                // 获取更新信息
                $request_params = [
                    'plugins_type'  => 'payment',
                    'plugins_data'  => $data,
                ];
                $res = StoreService::PluginsUpgradeInfo($request_params);
                if(!empty($res['data']))
                {
                    $res['data'] = array_column($res['data'], null, 'plugins');
                }
                return $res;
            }
        }

        return DataReturn('无插件数据', 0);
    }
D
v1.2.0  
devil_gong 已提交
1053 1054
}
?>