API::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Payment\MerchantPay;
4
5
use EntWeChat\Core\AbstractAPI;
6
use EntWeChat\Payment\Merchant;
7
use EntWeChat\Support\Collection;
8
use EntWeChat\Support\XML;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class API.
13
 */
14
class API extends AbstractAPI
15
{
16
    /**
17
     * Merchant instance.
18
     *
19
     * @var Merchant
20
     */
21
    protected $merchant;
22
23
    // api
24
    const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
25
    const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo';
26
27
    /**
28
     * API constructor.
29
     *
30
     * @param \EntWeChat\Payment\Merchant $merchant
31
     */
32
    public function __construct(Merchant $merchant)
33
    {
34
        $this->merchant = $merchant;
35
    }
36
37
    /**
38
     * Query MerchantPay.
39
     *
40
     * @param string $mchBillNo
41
     *
42
     * @return \EntWeChat\Support\Collection
43
     *
44
     * @notice mch_id when query, but mchid when send
45
     */
46
    public function query($mchBillNo)
47
    {
48
        $params = [
49
            'appid'            => $this->merchant->app_id,
50
            'mch_id'           => $this->merchant->merchant_id,
51
            'partner_trade_no' => $mchBillNo,
52
        ];
53
54
        return $this->request(self::API_QUERY, $params);
55
    }
56
57
    /**
58
     * Send MerchantPay.
59
     *
60
     * @param array $params
61
     *
62
     * @return \EntWeChat\Support\Collection
63
     */
64
    public function send(array $params)
65
    {
66
        $params['mchid'] = $this->merchant->merchant_id;
67
        $params['mch_appid'] = $this->merchant->app_id;
68
69
        return $this->request(self::API_SEND, $params);
70
    }
71
72
    /**
73
     * Merchant setter.
74
     *
75
     * @param Merchant $merchant
76
     *
77
     * @return $this
78
     */
79
    public function setMerchant(Merchant $merchant)
80
    {
81
        $this->merchant = $merchant;
82
    }
83
84
    /**
85
     * Merchant getter.
86
     *
87
     * @return Merchant
88
     */
89
    public function getMerchant()
90
    {
91
        return $this->merchant;
92
    }
93
94
    /**
95
     * Make a API request.
96
     *
97
     * @param string $api
98
     * @param array  $params
99
     * @param string $method
100
     *
101
     * @return \EntWeChat\Support\Collection
102
     */
103 View Code Duplication
    protected function request($api, array $params, $method = 'post')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $params = array_filter($params);
106
        $params['nonce_str'] = uniqid();
107
        $params['sign'] = \EntWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5');
108
109
        $options = [
110
            'body'    => XML::build($params),
111
            'cert'    => $this->merchant->get('cert_path'),
112
            'ssl_key' => $this->merchant->get('key_path'),
113
        ];
114
115
        return $this->parseResponse($this->getHttp()->request($api, $method, $options));
116
    }
117
118
    /**
119
     * Parse Response XML to array.
120
     *
121
     * @param ResponseInterface $response
122
     *
123
     * @return \EntWeChat\Support\Collection
124
     */
125
    protected function parseResponse($response)
126
    {
127
        if ($response instanceof ResponseInterface) {
128
            $response = $response->getBody();
129
        }
130
131
        return new Collection((array) XML::parse($response));
132
    }
133
}
134