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 |
||
14 | abstract class Alipay implements GatewayInterface |
||
15 | { |
||
16 | use HasHttpRequest; |
||
17 | |||
18 | /** |
||
19 | * 支付宝支付网关. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $gateway = 'https://openapi.alipay.com/gateway.do'; |
||
24 | |||
25 | /** |
||
26 | * 支付宝公共参数. |
||
27 | * |
||
28 | * @var [type] |
||
29 | */ |
||
30 | protected $config; |
||
31 | |||
32 | /** |
||
33 | * 用户的传参 |
||
34 | * |
||
35 | * @var [type] |
||
36 | */ |
||
37 | protected $user_config; |
||
38 | |||
39 | /** |
||
40 | * [__construct description]. |
||
41 | * |
||
42 | * @author yansongda <[email protected]> |
||
43 | * |
||
44 | * @version 2017-08-14 |
||
45 | * |
||
46 | * @param array $config [description] |
||
47 | */ |
||
48 | public function __construct(array $config) |
||
70 | |||
71 | |||
72 | /** |
||
73 | * 对外接口 - 支付. |
||
74 | * |
||
75 | * @author yansongda <[email protected]> |
||
76 | * |
||
77 | * @version 2017-08-15 |
||
78 | * |
||
79 | * @param array $config_biz [description] |
||
80 | * |
||
81 | * @return [type] [description] |
||
|
|||
82 | */ |
||
83 | View Code Duplication | public function pay(array $config_biz = []) |
|
1 ignored issue
–
show
|
|||
84 | { |
||
85 | $config_biz['product_code'] = $this->getPayProductCode(); |
||
86 | |||
87 | $this->config['method'] = $this->getPayMethod(); |
||
88 | $this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE); |
||
89 | $this->config['sign'] = $this->getSign(); |
||
90 | |||
91 | return $this->buildPayHtml(); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * 对外接口 - 退款. |
||
96 | * |
||
97 | * @author yansongda <[email protected]> |
||
98 | * |
||
99 | * @version 2017-08-15 |
||
100 | * |
||
101 | * @param array $config_biz [description] |
||
102 | * |
||
103 | * @return [type] [description] |
||
104 | */ |
||
105 | View Code Duplication | public function refund(array $config_biz = []) |
|
106 | { |
||
107 | $this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE); |
||
108 | $this->config['method'] = 'alipay.trade.refund'; |
||
109 | |||
110 | return $this->getResult('alipay_trade_refund_response'); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * 对外接口 - 关闭. |
||
115 | * |
||
116 | * @author yansongda <[email protected]> |
||
117 | * |
||
118 | * @version 2017-08-15 |
||
119 | * |
||
120 | * @param array $config_biz [description] |
||
121 | * |
||
122 | * @return [type] [description] |
||
123 | */ |
||
124 | View Code Duplication | public function close(array $config_biz = []) |
|
125 | { |
||
126 | $this->config['biz_content'] = json_encode($config_biz, JSON_UNESCAPED_UNICODE); |
||
127 | $this->config['method'] = 'alipay.trade.close'; |
||
128 | |||
129 | return $this->getResult('alipay_trade_close_response'); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * 对外接口 - 验证. |
||
134 | * |
||
135 | * @author yansongda <[email protected]> |
||
136 | * |
||
137 | * @version 2017-08-11 |
||
138 | * |
||
139 | * @param array $data 待签名数组 |
||
140 | * @param string $sign 签名字符串-支付宝服务器发送过来的原始串 |
||
141 | * @param bool $sync 是否同步验证 |
||
142 | * |
||
143 | * @return [type] [description] |
||
144 | */ |
||
145 | public function verify($data, $sign = null, $sync = false) |
||
146 | { |
||
147 | if (is_null($this->user_config->get('ali_public_key'))) { |
||
148 | throw new InvalidArgumentException("Missing Config -- [ali_public_key]"); |
||
149 | } |
||
150 | |||
151 | $sign = is_null($sign) ? $data['sign'] : $sign; |
||
152 | |||
153 | $res = "-----BEGIN PUBLIC KEY-----\n" . |
||
154 | wordwrap($this->user_config->get('ali_public_key'), 64, "\n", true) . |
||
155 | "\n-----END PUBLIC KEY-----"; |
||
156 | |||
157 | $data = $sync ? json_encode($data, JSON_UNESCAPED_UNICODE) : $this->getSignContent($data, true); |
||
158 | |||
159 | if (openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256) === 1) { |
||
160 | return true; |
||
161 | } |
||
162 | |||
163 | return false; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * [getMethod description]. |
||
168 | * |
||
169 | * @author yansongda <[email protected]> |
||
170 | * |
||
171 | * @version 2017-08-10 |
||
172 | * |
||
173 | * @return [type] [description] |
||
174 | */ |
||
175 | abstract protected function getPayMethod(); |
||
176 | |||
177 | /** |
||
178 | * [getProductCode description]. |
||
179 | * |
||
180 | * @author yansongda <[email protected]> |
||
181 | * |
||
182 | * @version 2017-08-10 |
||
183 | * |
||
184 | * @return [type] [description] |
||
185 | */ |
||
186 | abstract protected function getPayProductCode(); |
||
187 | |||
188 | /** |
||
189 | * [buildHtmlPay description] |
||
190 | * |
||
191 | * @author yansongda <[email protected]> |
||
192 | * |
||
193 | * @version 2017-08-11 |
||
194 | * |
||
195 | * @return [type] [description] |
||
196 | */ |
||
197 | protected function buildPayHtml() |
||
198 | { |
||
199 | $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->gateway."?charset=utf-8' method='POST'>"; |
||
200 | while (list ($key, $val) = each ($this->config)) { |
||
201 | $val = str_replace("'","'",$val); |
||
202 | $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; |
||
203 | } |
||
204 | $sHtml = $sHtml."<input type='submit' value='ok' style='display:none;''></form>"; |
||
205 | $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>"; |
||
206 | |||
207 | return $sHtml; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * get alipay api result. |
||
212 | * |
||
213 | * @author yansongda <[email protected]> |
||
214 | * |
||
215 | * @version 2017-08-12 |
||
216 | * |
||
217 | * @param [type] $method [description] |
||
218 | * |
||
219 | * @return [type] [description] |
||
220 | */ |
||
221 | protected function getResult($method) |
||
222 | { |
||
223 | $this->config['sign'] = $this->getSign(); |
||
224 | |||
225 | $data = json_decode($this->post($this->gateway, $this->config), true); |
||
226 | |||
227 | if (! isset($data[$method]['code']) || $data[$method]['code'] !== '10000') { |
||
228 | throw new GatewayException( |
||
229 | 'get result error:' . $data[$method]['msg'] . ' - ' . $data[$method]['sub_msg'], |
||
230 | $data[$method]['code'], |
||
231 | $data); |
||
232 | } |
||
233 | |||
234 | return $this->verify($data[$method], $data['sign'], true); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * 签名. |
||
239 | * |
||
240 | * @author yansongda <[email protected]> |
||
241 | * |
||
242 | * @version 2017-08-10 |
||
243 | * |
||
244 | * @return [type] [description] |
||
245 | */ |
||
246 | protected function getSign() |
||
260 | |||
261 | /** |
||
262 | * 待签名数组. |
||
263 | * |
||
264 | * @author yansongda <[email protected]> |
||
265 | * |
||
266 | * @version 2017-08-11 |
||
267 | * |
||
268 | * @param array $toBeSigned [description] |
||
269 | * @param boolean $verify 是否异步同时验证签名 |
||
270 | * |
||
271 | * @return [type] [description] |
||
272 | */ |
||
273 | protected function getSignContent(array $toBeSigned, $verify = false) |
||
291 | |||
292 | } |
||
293 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.