Passed
Push — master ( e50567...183de1 )
by Carlos
02:58
created

Client::unify()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 2
b 0
f 0
nc 6
nop 2
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.9
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Payment\Order;
13
14
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
15
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
16
use EasyWeChat\Kernel\Support;
17
use EasyWeChat\Kernel\Support\Collection;
18
use EasyWeChat\Payment\Kernel\BaseClient;
19
use Psr\Http\Message\ResponseInterface;
20
21
class Client extends BaseClient
22
{
23
    /**
24
     * Unify order.
25
     *
26
     * @param array $params
27
     * @param bool  $isContract
28
     *
29
     * @return ResponseInterface|Collection|array|object|string
30
     *
31
     * @throws InvalidArgumentException
32
     * @throws InvalidConfigException
33
     */
34 2
    public function unify(array $params, $isContract = false)
35
    {
36 2
        if (empty($params['spbill_create_ip'])) {
37 2
            $params['spbill_create_ip'] = ('NATIVE' === $params['trade_type']) ? Support\get_server_ip() : Support\get_client_ip();
0 ignored issues
show
Bug introduced by
The function get_server_ip was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

37
            $params['spbill_create_ip'] = ('NATIVE' === $params['trade_type']) ? /** @scrutinizer ignore-call */ Support\get_server_ip() : Support\get_client_ip();
Loading history...
Bug introduced by
The function get_client_ip was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

37
            $params['spbill_create_ip'] = ('NATIVE' === $params['trade_type']) ? Support\get_server_ip() : /** @scrutinizer ignore-call */ Support\get_client_ip();
Loading history...
38
        }
39
40 2
        $params['appid'] = $this->app['config']->app_id;
41 2
        $params['notify_url'] = $params['notify_url'] ?? $this->app['config']['notify_url'];
42
43 2
        if ($isContract) {
44 1
            $params['contract_appid'] = $this->app['config']['app_id'];
45 1
            $params['contract_mchid'] = $this->app['config']['mch_id'];
46 1
            $params['request_serial'] = $params['request_serial'] ?? time();
47 1
            $params['contract_notify_url'] = $params['contract_notify_url'] ?? $this->app['config']['contract_notify_url'];
48
49 1
            return $this->request($this->wrap('pay/contractorder'), $params);
50
        }
51
52 1
        return $this->request($this->wrap('pay/unifiedorder'), $params);
53
    }
54
55
    /**
56
     * Query order by out trade number.
57
     *
58
     * @param string $number
59
     *
60
     * @return ResponseInterface|Collection|array|object|string
61
     *
62
     * @throws InvalidArgumentException
63
     * @throws InvalidConfigException
64
     */
65 1
    public function queryByOutTradeNumber(string $number)
66
    {
67 1
        return $this->query([
68 1
            'out_trade_no' => $number,
69
        ]);
70
    }
71
72
    /**
73
     * Query order by transaction id.
74
     *
75
     * @param string $transactionId
76
     *
77
     * @return ResponseInterface|Collection|array|object|string
78
     *
79
     * @throws InvalidArgumentException
80
     * @throws InvalidConfigException
81
     */
82 1
    public function queryByTransactionId(string $transactionId)
83
    {
84 1
        return $this->query([
85 1
            'transaction_id' => $transactionId,
86
        ]);
87
    }
88
89
    /**
90
     * @param array $params
91
     *
92
     * @return ResponseInterface|Collection|array|object|string
93
     *
94
     * @throws InvalidArgumentException
95
     * @throws InvalidConfigException
96
     */
97 2
    protected function query(array $params)
98
    {
99 2
        $params['appid'] = $this->app['config']->app_id;
100
101 2
        return $this->request($this->wrap('pay/orderquery'), $params);
102
    }
103
104
    /**
105
     * Close order by out_trade_no.
106
     *
107
     * @param string $tradeNo
108
     *
109
     * @return ResponseInterface|Collection|array|object|string
110
     *
111
     * @throws InvalidArgumentException
112
     * @throws InvalidConfigException
113
     */
114 1
    public function close(string $tradeNo)
115
    {
116
        $params = [
117 1
            'appid' => $this->app['config']->app_id,
118 1
            'out_trade_no' => $tradeNo,
119
        ];
120
121 1
        return $this->request($this->wrap('pay/closeorder'), $params);
122
    }
123
}
124