|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EntWeChat\Payment\CashCoupon; |
|
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/send_coupon'; |
|
25
|
|
|
const API_QUERY_STOCK = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/query_coupon_stock'; |
|
26
|
|
|
const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/querycouponsinfo'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* API constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \EntWeChat\Payment\Merchant $merchant |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(Merchant $merchant) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->merchant = $merchant; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* send a cash coupon. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $params |
|
42
|
|
|
* |
|
43
|
|
|
* @return \EntWeChat\Support\Collection |
|
44
|
|
|
*/ |
|
45
|
|
|
public function send(array $params) |
|
46
|
|
|
{ |
|
47
|
|
|
$params['openid_count'] = 1; |
|
48
|
|
|
|
|
49
|
|
|
return $this->request(self::API_SEND, $params); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* query a coupon stock. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $params |
|
56
|
|
|
* |
|
57
|
|
|
* @return \EntWeChat\Support\Collection |
|
58
|
|
|
*/ |
|
59
|
|
|
public function queryStock(array $params) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->request(self::API_QUERY_STOCK, $params); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* query a info of coupon. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $params |
|
68
|
|
|
* |
|
69
|
|
|
* @return \EntWeChat\Support\Collection |
|
70
|
|
|
*/ |
|
71
|
|
|
public function query(array $params) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->request(self::API_QUERY, $params); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Merchant setter. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Merchant $merchant |
|
80
|
|
|
* |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setMerchant(Merchant $merchant) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->merchant = $merchant; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Merchant getter. |
|
90
|
|
|
* |
|
91
|
|
|
* @return Merchant |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getMerchant() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->merchant; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Make a API request. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $api |
|
102
|
|
|
* @param array $params |
|
103
|
|
|
* @param string $method |
|
104
|
|
|
* |
|
105
|
|
|
* @return \EntWeChat\Support\Collection |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function request($api, array $params, $method = 'post') |
|
108
|
|
|
{ |
|
109
|
|
|
$params = array_filter($params); |
|
110
|
|
|
$params['mch_id'] = $this->merchant->merchant_id; |
|
111
|
|
|
$params['appid'] = $this->merchant->app_id; |
|
112
|
|
|
$params['nonce_str'] = uniqid(); |
|
113
|
|
|
$params['sign'] = \EntWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5'); |
|
114
|
|
|
|
|
115
|
|
|
$options = [ |
|
116
|
|
|
'body' => XML::build($params), |
|
117
|
|
|
'cert' => $this->merchant->get('cert_path'), |
|
118
|
|
|
'ssl_key' => $this->merchant->get('key_path'), |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
return $this->parseResponse($this->getHttp()->request($api, $method, $options)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Parse Response XML to array. |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Psr\Http\Message\ResponseInterface|string $response |
|
128
|
|
|
* |
|
129
|
|
|
* @return \EntWeChat\Support\Collection |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function parseResponse($response) |
|
132
|
|
|
{ |
|
133
|
|
|
if ($response instanceof ResponseInterface) { |
|
134
|
|
|
$response = $response->getBody(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return new Collection((array) XML::parse($response)); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|