Pos::request()   A
last analyzed

Complexity

Conditions 5
Paths 32

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 33
rs 9.2408
cc 5
nc 32
nop 1
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/kXOZy68D.html
13
 */
14
class Pos extends Base
15
{
16
    protected $gateway_type = 'wh_open';
17
    
18
    /**
19
     * @var string 接口地址
20
     */
21
    protected $api = '/v6/poslink/transaction/pay';
22
    
23
    /**
24
     * @var array $body 请求参数
25
     */
26
    protected $body = [];
27
    
28
    /**
29
     * 必传的值
30
     * @var array
31
     */
32
    protected $require = ['merchantCode', 'terminalCode', 'merchantOrderId', 'totalAmount'];
33
34
    /**
35
     * 重写 request 方法,复用父类逻辑,仅调整数据加载部分以适配银联商务参数
36
     * @return false|mixed|string
37
     */
38
    public function request($params = [])
39
    {
40
        if(!empty($params)){
41
            $this->params = $params;
42
        }
43
44
        try {
45
            // 使用银联商务的参数格式加载数据
46
            $data = $this->loadChinaumsData();
47
            $sign = $this->generateSign($data);
48
            $gateway  = $this->gateway . $this->api;
49
50
            if ('cli' == php_sapi_name()) {
51
                echo 'api:' . $gateway . PHP_EOL;
52
                echo 'request:' . $data . PHP_EOL;
53
            }
54
            $headers = [
55
                'Content-Type: application/json',
56
                'Content-Length: ' . strlen($data),
57
                'Authorization: ' . $sign
58
            ];
59
            $options = [
60
                CURLOPT_HTTPHEADER => $headers,
61
                CURLOPT_TIMEOUT => 60,
62
                CURLOPT_CONNECTTIMEOUT => 30
63
            ];
64
            $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

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