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