PosVoid   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 42
c 1
b 0
f 0
dl 0
loc 90
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 33 5
B loadChinaumsData() 0 24 8
1
<?php
2
3
namespace tinymeng\Chinaums\Service\Chinaums;
4
5
use Exception;
6
use tinymeng\Chinaums\Exception\TException;
7
use tinymeng\Chinaums\Service\Common\Base;
8
use tinymeng\Chinaums\Tools\Http;
9
10
/**
11
 * B扫C支付(被扫)- 支付撤销
12
 * 接口文档:https://open.chinaums.com/saas-doc/openplate/poslink/poslink-v6/transaction-v6/wXP7gr8n.html
13
 */
14
class PosVoid extends Base
15
{
16
    protected $gateway_type = 'wh_open';
17
    
18
    /**
19
     * @var string 接口地址
20
     */
21
    protected $api = '/v6/poslink/transaction/voidpayment';
22
    
23
    /**
24
     * @var array $body 请求参数
25
     */
26
    protected $body = [];
27
    
28
    /**
29
     * 必传的值
30
     * 根据文档:merchantCode, terminalCode, merchantOrderId 或 originalOrderId 至少一个
31
     * @var array
32
     */
33
    protected $require = ['merchantCode', 'terminalCode'];
34
35
    /**
36
     * 重写 request 方法,复用父类逻辑,仅调整数据加载部分以适配银联商务参数
37
     * @return false|mixed|string
38
     */
39
    public function request($params = [])
40
    {
41
        if(!empty($params)){
42
            $this->params = $params;
43
        }
44
45
        try {
46
            // 使用银联商务的参数格式加载数据
47
            $data = $this->loadChinaumsData();
48
            $sign = $this->generateSign($data);
49
            $gateway  = $this->gateway . $this->api;
50
51
            if ('cli' == php_sapi_name()) {
52
                echo 'api:' . $gateway . PHP_EOL;
53
                echo 'request:' . $data . PHP_EOL;
54
            }
55
            $headers = [
56
                'Content-Type: application/json',
57
                'Content-Length: ' . strlen($data),
58
                'Authorization: ' . $sign
59
            ];
60
            $options = [
61
                CURLOPT_HTTPHEADER => $headers,
62
                CURLOPT_TIMEOUT => 60,
63
                CURLOPT_CONNECTTIMEOUT => 30
64
            ];
65
            $response = Http::post($gateway, $data, $options);
0 ignored issues
show
Bug introduced by
$data of type string is incompatible with the type array expected by parameter $params of tinymeng\Chinaums\Tools\Http::post(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            $response = Http::post($gateway, /** @scrutinizer ignore-type */ $data, $options);
Loading history...
66
            return $response;
67
        } catch (Exception $e) {
68
            if ('cli' == php_sapi_name()) {
69
                echo $e->getMessage() . PHP_EOL;
70
            }
71
            throw new TException("Chinaums request error:".$e->getMessage());
72
        }
73
    }
74
75
    /**
76
     * 加载银联商务 API 数据(使用 merchantCode 和 terminalCode)
77
     * @return false|string
78
     * @throws Exception
79
     */
80
    private function loadChinaumsData()
81
    {
82
        $data = $this->params;
83
        // 银联商务使用 merchantCode 和 terminalCode,而不是 mid 和 tid
84
        if (!isset($data['merchantCode']) && isset($this->config['mid'])) {
85
            $data['merchantCode'] = $this->config['mid'];
86
        }
87
        if (!isset($data['terminalCode']) && isset($this->config['tid'])) {
88
            $data['terminalCode'] = $this->config['tid'];
89
        }
90
        if ($data) {
91
            $this->body = array_merge($this->body, $data);
92
        }
93
        $this->validate();
94
        
95
        // 验证 merchantOrderId 或 originalOrderId 至少存在一个
96
        $key = array_keys($this->body);
97
        if (!in_array('merchantOrderId', $key) && !in_array('originalOrderId', $key)) {
98
            throw new Exception('merchantOrderId 或 originalOrderId 至少需要提供一个');
99
        }
100
        
101
        $data = $this->body;
102
        $data = json_encode($data);
103
        return $data;
104
    }
105
}
106
107