Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
32 | class API extends AbstractAPI |
||
33 | { |
||
34 | /** |
||
35 | * Merchant instance. |
||
36 | * |
||
37 | * @var Merchant |
||
38 | */ |
||
39 | protected $merchant; |
||
40 | |||
41 | // api |
||
42 | const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; |
||
43 | const API_SEND_GROUP = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; |
||
44 | const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; |
||
45 | const API_PREPARE = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder'; |
||
46 | |||
47 | // LuckyMoney type |
||
48 | const TYPE_NORMAL = 'NORMAL'; |
||
49 | const TYPE_GROUP = 'GROUP'; |
||
50 | |||
51 | // Risk control type. |
||
52 | const RISK_NORMAL = 'NORMAL'; |
||
53 | const RISK_IGN_FREQ_LMT = 'IGN_FREQ_LMT'; |
||
54 | const RISK_IGN_DAY_LMT = 'IGN_DAY_LMT'; |
||
55 | const RISK_IGN_FREQ_DAY_LMT = 'IGN_FREQ_DAY_LMT'; |
||
56 | |||
57 | /** |
||
58 | * API constructor. |
||
59 | * |
||
60 | * @param \EasyWeChat\Payment\Merchant $merchant |
||
61 | */ |
||
62 | 7 | public function __construct(Merchant $merchant) |
|
66 | |||
67 | /** |
||
68 | * Prepare luckymoney. |
||
69 | * |
||
70 | * @param array $params |
||
71 | * |
||
72 | * @return \EasyWeChat\Support\Collection |
||
73 | */ |
||
74 | 1 | public function prepare(array $params) |
|
75 | { |
||
76 | 1 | $params['wxappid'] = $this->merchant->app_id; |
|
77 | |||
78 | // XXX: PLEASE DON'T CHANGE THE FOLLOWING LINES. |
||
79 | 1 | $params['auth_mchid'] = '1000052601'; |
|
80 | 1 | $params['auth_appid'] = 'wxbf42bd79c4391863'; |
|
81 | |||
82 | 1 | $params['amt_type'] = 'ALL_RAND'; |
|
83 | |||
84 | 1 | return $this->request(self::API_PREPARE, $params); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Query luckymoney. |
||
89 | * |
||
90 | * @param string $mchBillNo |
||
91 | * |
||
92 | * @return \EasyWeChat\Support\Collection |
||
93 | */ |
||
94 | 1 | public function query($mchBillNo) |
|
95 | { |
||
96 | $params = [ |
||
97 | 1 | 'appid' => $this->merchant->app_id, |
|
98 | 1 | 'mch_billno' => $mchBillNo, |
|
99 | 1 | 'bill_type' => 'MCHT', |
|
100 | 1 | ]; |
|
101 | |||
102 | 1 | return $this->request(self::API_QUERY, $params); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Send LuckyMoney. |
||
107 | * |
||
108 | * @param array $params |
||
109 | * @param string $type |
||
110 | * |
||
111 | * @return \EasyWeChat\Support\Collection |
||
112 | */ |
||
113 | 3 | public function send(array $params, $type = self::TYPE_NORMAL) |
|
114 | { |
||
115 | 3 | $api = ($type === self::TYPE_NORMAL) ? self::API_SEND : self::API_SEND_GROUP; |
|
116 | |||
117 | 3 | $params['wxappid'] = $this->merchant->app_id; |
|
118 | |||
119 | 3 | return $this->request($api, $params); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Send normal LuckyMoney. |
||
124 | * |
||
125 | * @param array $params |
||
126 | * |
||
127 | * @return \EasyWeChat\Support\Collection |
||
128 | */ |
||
129 | 1 | public function sendNormal($params) |
|
130 | { |
||
131 | 1 | $params['total_num'] = 1; |
|
132 | 1 | $params['client_ip'] = !empty($params['client_ip']) ? $params['client_ip'] : getenv('SERVER_ADDR'); |
|
133 | |||
134 | 1 | return $this->send($params, self::TYPE_NORMAL); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Send group luckymoney. |
||
139 | * |
||
140 | * @param array $params |
||
141 | * |
||
142 | * @return \EasyWeChat\Support\Collection |
||
143 | */ |
||
144 | 1 | public function sendGroup($params) |
|
145 | { |
||
146 | 1 | $params['amt_type'] = 'ALL_RAND'; |
|
147 | |||
148 | 1 | return $this->send($params, self::TYPE_GROUP); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Merchant setter. |
||
153 | * |
||
154 | * @param Merchant $merchant |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | 1 | public function setMerchant(Merchant $merchant) |
|
162 | |||
163 | /** |
||
164 | * Merchant getter. |
||
165 | * |
||
166 | * @return Merchant |
||
167 | */ |
||
168 | 1 | public function getMerchant() |
|
172 | |||
173 | /** |
||
174 | * Make a API request. |
||
175 | * |
||
176 | * @param string $api |
||
177 | * @param array $params |
||
178 | * @param string $method |
||
179 | * |
||
180 | * @return \EasyWeChat\Support\Collection |
||
181 | */ |
||
182 | 5 | View Code Duplication | protected function request($api, array $params, $method = 'post') |
183 | { |
||
184 | 5 | $params = array_filter($params); |
|
185 | 5 | $params['mch_id'] = $this->merchant->merchant_id; |
|
186 | 5 | $params['nonce_str'] = uniqid(); |
|
187 | 5 | $params['sign'] = \EasyWeChat\Payment\generate_sign($params, $this->merchant->key, 'md5'); |
|
188 | |||
189 | $options = [ |
||
190 | 5 | 'body' => XML::build($params), |
|
191 | 5 | 'cert' => $this->merchant->get('cert_path'), |
|
192 | 5 | 'ssl_key' => $this->merchant->get('key_path'), |
|
193 | 5 | ]; |
|
194 | |||
195 | 5 | return $this->parseResponse($this->getHttp()->request($api, $method, $options)); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Parse Response XML to array. |
||
200 | * |
||
201 | * @param \Psr\Http\Message\ResponseInterface|string $response |
||
202 | * |
||
203 | * @return \EasyWeChat\Support\Collection |
||
204 | */ |
||
205 | 5 | protected function parseResponse($response) |
|
213 | } |
||
214 |