AgreementSign::request()   A
last analyzed

Complexity

Conditions 3
Paths 18

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
c 1
b 0
f 1
dl 0
loc 21
rs 9.7
cc 3
nc 18
nop 1
1
<?php
2
3
namespace tinymeng\Chinaums\Service\Contract;
4
5
use tinymeng\Chinaums\Tools\DES;
6
use Exception;
7
8
/**
9
 * 前台签约接口
10
 */
11
class AgreementSign extends Base
12
{
13
    /**
14
     * @var string 接口地址
15
     */
16
    protected $api = '/self-contract-nmrs/interface/autoReg';
17
    /**
18
     * @var array $body 请求参数
19
     */
20
    protected $body = [
21
        'service' => 'agreement_sign',
22
        'sign_type' => 'SHA-256',
23
    ];
24
    /**
25
     * 必传的值
26
     * @var array
27
     */
28
    protected $require = ['service', 'accesser_id', 'sign_type', 'request_date', 'request_seq', 'ums_reg_id'];
29
30
    public function request($data = [])
31
    {
32
        $data['accesser_id'] = $this->config['accesser_id'];
33
        $key = $this->config['private_key'];
34
        if ($data) {
35
            $this->body = array_merge($this->body, $data);
36
        }
37
        try {
38
            $this->validate();
39
            $data = $this->body;
40
            $gateway  = $this->config['gateway'] . $this->api;
41
            $data = json_encode($data);
42
            $sign = hash('sha256', $data);
43
            $method = $this->method;
44
            $des = new DES($key, $method, DES::OUTPUT_HEX);
45
            // 加密
46
            $str = $des->encrypt($data);
47
            $url = $gateway . '?sign_data=' . $sign . '&json_data=' . $str . '&accesser_id=' . $this->config['accesser_id'];
48
            return json_encode(['res_code' => '0000', 'res_msg' => 'success', 'url' => $url], JSON_UNESCAPED_SLASHES);
49
        } catch (Exception $e) {
50
            return json_encode(['res_code' => -1, 'res_msg' => $e->getMessage(), 'request_seq' => null]);
51
        }
52
    }
53
}
54