Completed
Push — master ( 8787ba...721c87 )
by wannanbigpig
05:01 queued 11s
created

Client::orderSettle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 19
ccs 0
cts 15
cp 0
crap 6
rs 9.7998
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace WannanBigPig\Alipay\Payment\Base;
12
13
use WannanBigPig\Alipay\Payment\Kernel\BaseClient;
14
15
/**
16
 * Class Client
17
 *
18
 * @author   liuml  <[email protected]>
19
 * @DateTime 2019-07-24  14:24
20
 */
21
class Client extends BaseClient
22
{
23
    /**
24
     * alipay.trade.pay(统一收单交易支付接口).
25
     *
26
     * @param array $params
27
     *
28
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
29
     *
30
     * @throws \GuzzleHttp\Exception\GuzzleException
31
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
32
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
33
     */
34
    public function pay(array $params)
35
    {
36
        $method = 'alipay.trade.pay';
37
        $params['scene'] = $params['scene'] ?? 'bar_code';
38
        $this->app->setEndpointConfig($method, [
39
            'notify_url' => $this->app['config']->get('notify_url'),
40
        ]);
41
42
        return $this->request($method, [
43
            'biz_content' => $params,
44
        ]);
45
    }
46
47
    /**
48
     * alipay.trade.create(统一收单交易创建接口).
49
     *
50
     * @param array $params
51
     *
52
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
53
     *
54
     * @throws \GuzzleHttp\Exception\GuzzleException
55
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
56
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
57
     */
58
    public function create(array $params)
59
    {
60
        $method = 'alipay.trade.create';
61
        $this->app->setEndpointConfig($method, [
62
            'notify_url' => $this->app['config']->get('notify_url'),
63
        ]);
64
65
        return $this->request($method, [
66
            'biz_content' => $params,
67
        ]);
68
    }
69
70
    /**
71
     * alipay.trade.precreate(统一收单线下交易预创建).
72
     *
73
     * @param array $params
74
     *
75
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
76
     *
77
     * @throws \GuzzleHttp\Exception\GuzzleException
78
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
79
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
80
     */
81
    public function preCreate(array $params)
82
    {
83
        $method = 'alipay.trade.precreate';
84
        $this->app->setEndpointConfig($method, [
85
            'notify_url' => $this->app['config']->get('notify_url'),
86
        ]);
87
88
        return $this->request($method, [
89
            'biz_content' => $params,
90
        ]);
91
    }
92
93
94
    /**
95
     * alipay.trade.close(统一收单交易关闭接口).
96
     *
97
     * @param string      $tradeNo
98
     * @param string|null $outTradeNo
99
     * @param string|null $operatorId
100
     *
101
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
102
     *
103
     * @throws \GuzzleHttp\Exception\GuzzleException
104
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
105
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
106
     */
107
    public function close(string $tradeNo, string $outTradeNo = null, string $operatorId = null)
108
    {
109
        $method = 'alipay.trade.close';
110
        $params = array_filter([
111
            'trade_no' => $tradeNo,
112
            'out_trade_no' => $outTradeNo,
113
            'operator_id' => $operatorId,
114
        ], function ($value) {
115
            return !($this->checkEmpty($value));
116
        });
117
        $this->app->setEndpointConfig($method, [
118
            'notify_url' => $this->app['config']->get('notify_url'),
119
        ]);
120
121
        return $this->request($method, [
122
            'biz_content' => $params,
123
        ]);
124
    }
125
126
    /**
127
     * alipay.trade.refund(统一收单交易退款接口).
128
     *
129
     * @param string           $tradeNo
130
     * @param float|int|string $amount
131
     * @param string|null      $outTradeNo
132
     * @param array            $params
133
     *
134
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
135
     *
136
     * @throws \GuzzleHttp\Exception\GuzzleException
137
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
138
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
139
     */
140
    public function refund(string $tradeNo, $amount, string $outTradeNo = null, array $params = [])
141
    {
142
        $params = array_merge(array_filter([
143
            'trade_no' => $tradeNo,
144
            'out_trade_no' => $outTradeNo,
145
            'refund_amount' => $amount,
146
        ], function ($value) {
147
            return !($this->checkEmpty($value));
148
        }), $params);
149
150
        return $this->request('alipay.trade.refund', [
151
            'biz_content' => $params,
152
        ]);
153
    }
154
155
    /**
156
     * alipay.trade.query(统一收单线下交易查询).
157
     *
158
     * @param string      $tradeNo
159
     * @param string|null $outTradeNo
160
     * @param string|null $orgPid
161
     *
162
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
163
     *
164
     * @throws \GuzzleHttp\Exception\GuzzleException
165
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
166
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
167
     */
168
    public function query(string $tradeNo, string $outTradeNo = null, string $orgPid = null)
169
    {
170
        $params = array_filter([
171
            'trade_no' => $tradeNo,
172
            'out_trade_no' => $outTradeNo,
173
            'org_pid' => $orgPid,
174
        ], function ($value) {
175
            return !($this->checkEmpty($value));
176
        });
177
178
        return $this->request('alipay.trade.query', [
179
            'biz_content' => $params,
180
        ]);
181
    }
182
183
    /**
184
     * alipay.trade.cancel(统一收单交易撤销接口).
185
     *
186
     * @param string      $tradeNo
187
     * @param string|null $outTradeNo
188
     *
189
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
190
     *
191
     * @throws \GuzzleHttp\Exception\GuzzleException
192
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
193
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
194
     */
195
    public function cancel(string $tradeNo, string $outTradeNo = null)
196
    {
197
        $params = array_filter([
198
            'trade_no' => $tradeNo,
199
            'out_trade_no' => $outTradeNo,
200
        ], function ($value) {
201
            return !($this->checkEmpty($value));
202
        });
203
204
        return $this->request('alipay.trade.cancel', [
205
            'biz_content' => $params,
206
        ]);
207
    }
208
209
    /**
210
     * alipay.trade.order.settle(统一收单交易结算接口).
211
     *
212
     * @param string      $outRequestNo
213
     * @param string      $tradeNo
214
     * @param array       $royaltyParameters
215
     * @param string|null $operatorId
216
     *
217
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
218
     *
219
     * @throws \GuzzleHttp\Exception\GuzzleException
220
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
221
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
222
     */
223
    public function orderSettle(string $outRequestNo, string $tradeNo, array $royaltyParameters, string $operatorId = null)
224
    {
225
        $royalty = [];
226
        if (count($royaltyParameters) === count($royaltyParameters, COUNT_RECURSIVE)) {
227
            $royalty[0] = $royaltyParameters;
228
        } else {
229
            $royalty = $royaltyParameters;
230
        }
231
        $params = array_filter([
232
            'out_request_no' => $outRequestNo,
233
            'trade_no' => $tradeNo,
234
            'royalty_parameters' => $royalty,
235
            'operator_id' => $operatorId,
236
        ], function ($value) {
237
            return !($this->checkEmpty($value));
238
        });
239
240
        return $this->request('alipay.trade.order.settle', [
241
            'biz_content' => $params,
242
        ]);
243
    }
244
245
    /**
246
     * alipay.trade.orderinfo.sync(支付宝订单信息同步接口).
247
     *
248
     * @param string      $tradeNo
249
     * @param string      $outRequestNo
250
     * @param string      $bizType
251
     * @param string|null $origRequestNo
252
     * @param string|null $orderBizInfo
253
     *
254
     * @return array|object|\Psr\Http\Message\ResponseInterface|\WannanBigPig\Supports\Collection|\WannanBigPig\Supports\Http\Response
255
     *
256
     * @throws \GuzzleHttp\Exception\GuzzleException
257
     * @throws \WannanBigPig\Alipay\Kernel\Exceptions\InvalidSignException
258
     * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException
259
     */
260
    public function orderInfoSync(string $tradeNo, string $outRequestNo, string $bizType, string $origRequestNo = null, string $orderBizInfo = null)
261
    {
262
        $params = array_filter([
263
            'trade_no' => $tradeNo,
264
            'out_request_no' => $outRequestNo,
265
            'orig_request_no' => $origRequestNo,
266
            'biz_type' => $bizType,
267
        ], function ($value) {
268
            return !($this->checkEmpty($value));
269
        });
270
271
        if (!($this->checkEmpty($orderBizInfo))) {
272
            $params['order_biz_info'] = json_encode(['status' => $orderBizInfo]);
273
        }
274
275
        return $this->request('alipay.trade.orderinfo.sync', [
276
            'biz_content' => $params,
277
        ]);
278
    }
279
}
280