Total Complexity | 66 |
Total Lines | 302 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like paypal_payment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use paypal_payment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
149 | class paypal_payment |
||
150 | { |
||
151 | /** |
||
152 | * @var string $return_data The data to return |
||
153 | */ |
||
154 | private $return_data; |
||
155 | |||
156 | /** |
||
157 | * This function returns true/false for whether this gateway thinks the data is intended for it. |
||
158 | * |
||
159 | * @return boolean Whether this gateway things the data is valid |
||
160 | */ |
||
161 | public function isValid() |
||
162 | { |
||
163 | global $modSettings; |
||
164 | |||
165 | // Has the user set up an email address? |
||
166 | if ((empty($modSettings['paidsubs_test']) && empty($modSettings['paypal_email'])) || (!empty($modSettings['paidsubs_test']) && empty($modSettings['paypal_sandbox_email']))) |
||
167 | return false; |
||
168 | // Check the correct transaction types are even here. |
||
169 | if ((!isset($_POST['txn_type']) && !isset($_POST['payment_status'])) || (!isset($_POST['business']) && !isset($_POST['receiver_email']))) |
||
170 | return false; |
||
171 | // Correct email address? |
||
172 | if (!isset($_POST['business'])) |
||
173 | $_POST['business'] = $_POST['receiver_email']; |
||
174 | |||
175 | // Are we testing? |
||
176 | if (!empty($modSettings['paidsubs_test']) && strtolower($modSettings['paypal_sandbox_email']) != strtolower($_POST['business']) && (empty($modSettings['paypal_additional_emails']) || !in_array(strtolower($_POST['business']), explode(',', strtolower($modSettings['paypal_additional_emails']))))) |
||
177 | return false; |
||
178 | elseif (strtolower($modSettings['paypal_email']) != strtolower($_POST['business']) && (empty($modSettings['paypal_additional_emails']) || !in_array(strtolower($_POST['business']), explode(',', $modSettings['paypal_additional_emails'])))) |
||
179 | return false; |
||
180 | return true; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Post the IPN data received back to paypal for validation |
||
185 | * Sends the complete unaltered message back to PayPal. The message must contain the same fields |
||
186 | * in the same order and be encoded in the same way as the original message |
||
187 | * PayPal will respond back with a single word, which is either VERIFIED if the message originated with PayPal or INVALID |
||
188 | * |
||
189 | * If valid returns the subscription and member IDs we are going to process if it passes |
||
190 | * |
||
191 | * @return string A string containing the subscription ID and member ID, separated by a + |
||
192 | */ |
||
193 | public function precheck() |
||
194 | { |
||
195 | global $modSettings, $txt; |
||
196 | |||
197 | // Put this to some default value. |
||
198 | if (!isset($_POST['txn_type'])) |
||
199 | $_POST['txn_type'] = ''; |
||
200 | |||
201 | // Build the request string - starting with the minimum requirement. |
||
202 | $requestString = 'cmd=_notify-validate'; |
||
203 | |||
204 | // Now my dear, add all the posted bits in the order we got them |
||
205 | foreach ($_POST as $k => $v) |
||
206 | $requestString .= '&' . $k . '=' . urlencode($v); |
||
207 | |||
208 | // Can we use curl? |
||
209 | if (function_exists('curl_init') && $curl = curl_init((!empty($modSettings['paidsubs_test']) ? 'https://www.sandbox.' : 'https://www.') . 'paypal.com/cgi-bin/webscr')) |
||
210 | { |
||
211 | // Set the post data. |
||
212 | curl_setopt($curl, CURLOPT_POST, true); |
||
213 | curl_setopt($curl, CURLOPT_POSTFIELDSIZE, 0); |
||
|
|||
214 | curl_setopt($curl, CURLOPT_POSTFIELDS, $requestString); |
||
215 | |||
216 | // Set up the headers so paypal will accept the post |
||
217 | curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
||
218 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1); |
||
219 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); |
||
220 | curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); |
||
221 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
||
222 | 'Host: www.' . (!empty($modSettings['paidsubs_test']) ? 'sandbox.' : '') . 'paypal.com', |
||
223 | 'Connection: close' |
||
224 | )); |
||
225 | |||
226 | // Fetch the data returned as a string. |
||
227 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
228 | |||
229 | // Fetch the data. |
||
230 | $this->return_data = curl_exec($curl); |
||
231 | |||
232 | // Close the session. |
||
233 | curl_close($curl); |
||
234 | } |
||
235 | // Otherwise good old HTTP. |
||
236 | else |
||
237 | { |
||
238 | // Setup the headers. |
||
239 | $header = 'POST /cgi-bin/webscr HTTP/1.1' . "\r\n"; |
||
240 | $header .= 'content-type: application/x-www-form-urlencoded' . "\r\n"; |
||
241 | $header .= 'Host: www.' . (!empty($modSettings['paidsubs_test']) ? 'sandbox.' : '') . 'paypal.com' . "\r\n"; |
||
242 | $header .= 'content-length: ' . strlen($requestString) . "\r\n"; |
||
243 | $header .= 'connection: close' . "\r\n\r\n"; |
||
244 | |||
245 | // Open the connection. |
||
246 | if (!empty($modSettings['paidsubs_test'])) |
||
247 | $fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); |
||
248 | else |
||
249 | $fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30); |
||
250 | |||
251 | // Did it work? |
||
252 | if (!$fp) |
||
253 | generateSubscriptionError($txt['paypal_could_not_connect']); |
||
254 | |||
255 | // Put the data to the port. |
||
256 | fputs($fp, $header . $requestString); |
||
257 | |||
258 | // Get the data back... |
||
259 | while (!feof($fp)) |
||
260 | { |
||
261 | $this->return_data = fgets($fp, 1024); |
||
262 | if (strcmp(trim($this->return_data), 'VERIFIED') === 0) |
||
263 | break; |
||
264 | } |
||
265 | |||
266 | // Clean up. |
||
267 | fclose($fp); |
||
268 | } |
||
269 | |||
270 | // If this isn't verified then give up... |
||
271 | if (strcmp(trim($this->return_data), 'VERIFIED') !== 0) |
||
272 | exit; |
||
273 | |||
274 | // Check that this is intended for us. |
||
275 | if (strtolower($modSettings['paypal_email']) != strtolower($_POST['business']) && (empty($modSettings['paypal_additional_emails']) || !in_array(strtolower($_POST['business']), explode(',', strtolower($modSettings['paypal_additional_emails']))))) |
||
276 | exit; |
||
277 | |||
278 | // Is this a subscription - and if so is it a secondary payment that we need to process? |
||
279 | // If so, make sure we get it in the expected format. Seems PayPal sometimes sends it without urlencoding. |
||
280 | if (!empty($_POST['item_number']) && strpos($_POST['item_number'], ' ') !== false) |
||
281 | $_POST['item_number'] = str_replace(' ', '+', $_POST['item_number']); |
||
282 | if ($this->isSubscription() && (empty($_POST['item_number']) || strpos($_POST['item_number'], '+') === false)) |
||
283 | // Calculate the subscription it relates to! |
||
284 | $this->_findSubscription(); |
||
285 | |||
286 | // Verify the currency! |
||
287 | if (strtolower($_POST['mc_currency']) !== strtolower($modSettings['paid_currency_code'])) |
||
288 | exit; |
||
289 | |||
290 | // Can't exist if it doesn't contain anything. |
||
291 | if (empty($_POST['item_number'])) |
||
292 | exit; |
||
293 | |||
294 | // Return the id_sub and id_member |
||
295 | return explode('+', $_POST['item_number']); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Is this a refund? |
||
300 | * |
||
301 | * @return boolean Whether this is a refund |
||
302 | */ |
||
303 | public function isRefund() |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Is this a subscription? |
||
313 | * |
||
314 | * @return boolean Whether this is a subscription |
||
315 | */ |
||
316 | public function isSubscription() |
||
317 | { |
||
318 | if (substr($_POST['txn_type'], 0, 14) === 'subscr_payment' && $_POST['payment_status'] === 'Completed') |
||
319 | return true; |
||
320 | else |
||
321 | return false; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Is this a normal payment? |
||
326 | * |
||
327 | * @return boolean Whether this is a normal payment |
||
328 | */ |
||
329 | public function isPayment() |
||
330 | { |
||
331 | if ($_POST['payment_status'] === 'Completed' && $_POST['txn_type'] === 'web_accept') |
||
332 | return true; |
||
333 | else |
||
334 | return false; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Is this a cancellation? |
||
339 | * |
||
340 | * @return boolean Whether this is a cancellation |
||
341 | */ |
||
342 | public function isCancellation() |
||
343 | { |
||
344 | // subscr_cancel is sent when the user cancels, subscr_eot is sent when the subscription reaches final payment |
||
345 | // Neither require us to *do* anything as per performCancel(). |
||
346 | // subscr_eot, if sent, indicates an end of payments term. |
||
347 | if (substr($_POST['txn_type'], 0, 13) === 'subscr_cancel' || substr($_POST['txn_type'], 0, 10) === 'subscr_eot') |
||
348 | return true; |
||
349 | else |
||
350 | return false; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Things to do in the event of a cancellation |
||
355 | * |
||
356 | * @param string $subscription_id |
||
357 | * @param int $member_id |
||
358 | * @param array $subscription_info |
||
359 | */ |
||
360 | public function performCancel($subscription_id, $member_id, $subscription_info) |
||
362 | // PayPal doesn't require SMF to notify it every time the subscription is up for renewal. |
||
363 | // A cancellation should not cause the user to be immediately dropped from their subscription, but |
||
364 | // let it expire normally. Some systems require taking action in the database to deal with this, but |
||
365 | // PayPal does not, so we actually just do nothing. But this is a nice prototype/example just in case. |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * How much was paid? |
||
370 | * |
||
371 | * @return float The amount paid |
||
372 | */ |
||
373 | public function getCost() |
||
374 | { |
||
375 | return (isset($_POST['tax']) ? $_POST['tax'] : 0) + $_POST['mc_gross']; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Record the transaction reference to finish up. |
||
380 | * |
||
381 | */ |
||
382 | public function close() |
||
383 | { |
||
384 | global $smcFunc, $subscription_id; |
||
385 | |||
386 | // If it's a subscription record the reference. |
||
387 | if ($_POST['txn_type'] == 'subscr_payment' && !empty($_POST['subscr_id'])) |
||
388 | { |
||
389 | $smcFunc['db_query']('', ' |
||
390 | UPDATE {db_prefix}log_subscribed |
||
391 | SET vendor_ref = {string:vendor_ref} |
||
392 | WHERE id_sublog = {int:current_subscription}', |
||
393 | array( |
||
394 | 'current_subscription' => $subscription_id, |
||
395 | 'vendor_ref' => $_POST['subscr_id'], |
||
396 | ) |
||
397 | ); |
||
398 | } |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * A private function to find out the subscription details. |
||
403 | * |
||
404 | * @access private |
||
405 | * @return boolean|void False on failure, otherwise just sets $_POST['item_number'] |
||
406 | */ |
||
407 | private function _findSubscription() |
||
451 | } |
||
452 | } |
||
453 | |||
454 | ?> |