Completed
Push — master ( 891443...e38f5c )
by Carlos
03:00
created

API::queryStock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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
ccs 2
cts 2
cp 1
crap 1
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
/**
13
 * API.php.
14
 *
15
 * @author    tianyong90 <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Payment\CashCoupon;
23
24
use EasyWeChat\Core\AbstractAPI;
25
use EasyWeChat\Payment\Merchant;
26
use EasyWeChat\Support\Collection;
27
use EasyWeChat\Support\XML;
28
use Psr\Http\Message\ResponseInterface;
29
30
/**
31
 * Class API.
32
 */
33
class API extends AbstractAPI
34
{
35
    /**
36
     * Merchant instance.
37
     *
38
     * @var Merchant
39
     */
40
    protected $merchant;
41
42
    // api
43
    const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon';
44
    const API_QUERY_STOCK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/query_coupon_stock';
45
    const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/querycouponsinfo';
46
47
    /**
48
     * API constructor.
49
     *
50
     * @param \EasyWeChat\Payment\Merchant $merchant
51
     */
52 3
    public function __construct(Merchant $merchant)
53
    {
54 3
        $this->merchant = $merchant;
55 3
    }
56
57
    /**
58
     * send a cash coupon.
59
     *
60
     * @param array $params
61
     *
62
     * @return \EasyWeChat\Support\Collection
63
     */
64 1
    public function send(array $params)
65
    {
66 1
        $params['openid_count'] = 1;
67
68 1
        return $this->request(self::API_SEND, $params);
69
    }
70
71
    /**
72
     * query a coupon stock.
73
     *
74
     * @param array $params
75
     *
76
     * @return \EasyWeChat\Support\Collection
77
     */
78 1
    public function queryStock(array $params)
79
    {
80 1
        return $this->request(self::API_QUERY_STOCK, $params);
81
    }
82
83
    /**
84
     * query a info of coupon.
85
     *
86
     * @param array $params
87
     *
88
     * @return \EasyWeChat\Support\Collection
89
     */
90 1
    public function query(array $params)
91
    {
92 1
        return $this->request(self::API_QUERY, $params);
93
    }
94
95
    /**
96
     * Merchant setter.
97
     *
98
     * @param Merchant $merchant
99
     *
100
     * @return $this
101
     */
102
    public function setMerchant(Merchant $merchant)
103
    {
104
        $this->merchant = $merchant;
105
    }
106
107
    /**
108
     * Merchant getter.
109
     *
110
     * @return Merchant
111
     */
112
    public function getMerchant()
113
    {
114
        return $this->merchant;
115
    }
116
117
    /**
118
     * Make a API request.
119
     *
120
     * @param string $api
121
     * @param array  $params
122
     * @param string $method
123
     *
124
     * @return \EasyWeChat\Support\Collection
125
     */
126 3 View Code Duplication
    protected function request($api, array $params, $method = 'post')
127
    {
128 3
        $params = array_filter($params);
129 3
        $params['mch_id'] = $this->merchant->merchant_id;
130 3
        $params['appid'] = $this->merchant->app_id;
131 3
        $params['nonce_str'] = uniqid();
132 3
        $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5');
133
134
        $options = [
135 3
            'body' => XML::build($params),
136 3
            'cert' => $this->merchant->get('cert_path'),
137 3
            'ssl_key' => $this->merchant->get('key_path'),
138 3
        ];
139
140 3
        return $this->parseResponse($this->getHttp()->request($api, $method, $options));
141
    }
142
143
    /**
144
     * Parse Response XML to array.
145
     *
146
     * @param \Psr\Http\Message\ResponseInterface|string $response
147
     *
148
     * @return \EasyWeChat\Support\Collection
149
     */
150 3
    protected function parseResponse($response)
151
    {
152 3
        if ($response instanceof ResponseInterface) {
153
            $response = $response->getBody();
154
        }
155
156 3
        return new Collection((array) XML::parse($response));
157
    }
158
}
159