MerAlter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 29
c 1
b 0
f 1
dl 0
loc 57
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setPlatform() 0 4 1
A request() 0 24 4
1
<?php
2
3
namespace tinymeng\Chinaums\Service\Contract;
4
5
use tinymeng\Chinaums\Service\Contract\Base;
6
use tinymeng\Chinaums\Tools\DES;
7
use Exception;
8
9
/**
10
 * 平台类全前台商户变更接入接口
11
 */
12
class MerAlter extends Base
13
{
14
    /**
15
     * 平台 PC H5
16
     *
17
     * @var string
18
     */
19
    protected $platform = 'PC';
20
    /**
21
     * @var string h5接口地址
22
     */
23
    protected $api = '/self-sign-mobile/#/chooseChangeType';
24
    /**
25
     * @var string pc接口地址
26
     */
27
    protected $PCapi = '/self-sign-web/#/alterHome';
28
    /**
29
     * @var array $body 请求参数
30
     */
31
    protected $body = [
32
        'service' => 'merAlter',
33
        'sign_type' => 'SHA-256',
34
    ];
35
    /**
36
     * 必传的值
37
     * @var array
38
     */
39
    protected $require = ['service', 'accesser_id', 'sign_type', 'request_date', 'request_seq', 'mchnNo'];
40
    public function setPlatform($value)
41
    {
42
        $this->platform = $value;
43
        return $this;
44
    }
45
    public function request($data = [])
46
    {
47
        $data['accesser_id'] = $this->config['accesser_id'];
48
        $key = $this->config['private_key'];
49
        if ($data) {
50
            $this->body = array_merge($this->body, $data);
51
        }
52
        try {
53
            $this->validate();
54
            $data = $this->body;
55
            $gateway  = $this->config['gateway'] . $this->api;
56
            if (strtoupper($this->platform) == 'PC') {
57
                $gateway  = $this->config['gateway'] . $this->PCapi;
58
            }
59
            $data = json_encode($data);
60
            $sign = hash('sha256', $data);
61
            $method = $this->method;
62
            $des = new DES($key, $method, DES::OUTPUT_HEX);
63
            // 加密
64
            $str = $des->encrypt($data);
65
            $url = $gateway . '?sign_data=' . $sign . '&json_data=' . $str . '&accesser_id=' . $this->config['accesser_id'];
66
            return json_encode(['res_code' => '0000', 'res_msg' => 'success', 'url' => $url], JSON_UNESCAPED_SLASHES);
67
        } catch (Exception $e) {
68
            return json_encode(['res_code' => -1, 'res_msg' => $e->getMessage(), 'request_seq' => null]);
69
        }
70
    }
71
}
72