include_once 'classes/DataAccess.class.php'; include_once 'classes/PayPalMsgHandler.class.php'; include_once 'classes/Logger.class.php'; include_once 'classes/Constants.class.php'; $dataAccess = new DataAccess(); $loggerDataAccess = new DataAccess(); $logger = new Logger($loggerDataAccess); $payPalMsgHandler = new PayPalMsgHandler(); $ipnData = $_POST; $status = 500; $url = Constants::$payPalProdButtonFormUrl; if ($payPalMsgHandler->GetResponseValByKey($ipnData, 'test_ipn') == "1") { $url = Constants::$payPalTestButtonFormUrl; } // Debugging: log what we received //$logger->LogInfo("IPN Received. Post Data: " . $payPalMsgHandler->ConvertIPNPostDataToRawString($ipnData)); // For all instant notifications, must acknowledge as per PayPal requirement by sending a POST request containing the same data back to PayPal, // but only need to take further action for subscription creation, subscription renewal, and subscription cancellation. $response = $payPalMsgHandler->SendIPNPostRequest($ipnData, $url, $status); if ($status == 200 && $response == "VERIFIED") { // If this notification really came from PayPal, proceed to handle it and execute any necessary actions $rawIpnMessage = $payPalMsgHandler->ConvertIPNPostDataToRawString($ipnData); $notificationType = "IPN"; $payPalMsgHandler->HandlePayPalTxnResponse($ipnData, $rawIpnMessage, $dataAccess, $logger, $notificationType); } else { $txnId = ""; if (isset($ipnData["txn_id"])) { $txnId = $ipnData["txn_id"]; } $logger->LogError(sprintf("Received bad PayPal IPN message. Status=[%d]; Response=[%s]; txn ID=[%s]", $status, $response, $txnId)); }
if (isset($_GET['tx'])) { $tx = filter_var($_GET['tx'], FILTER_SANITIZE_STRING); if (strlen($tx) > 0) { $replyMsg = PayPalTxnMsg::ConstructDefaultMsg(); $url = Constants::$isPayPalTest ? Constants::$payPalTestButtonFormUrl : Constants::$payPalProdButtonFormUrl; $identityToken = Constants::$isPayPalTest ? Constants::$payPalTestPostIdentityToken : Constants::$payPalProdPostIdentityToken; $status = 500; $response = $payPalMsgHandler->SendPDTPostRequest($tx, $url, $status, $identityToken); if ($status != 200 || strpos($response, 'SUCCESS') !== 0) { $logger->LogError(sprintf("Received bad PayPal PDT message (status %d): %s", $status, $response)); throw new Exception("Unable to retrieve PDT transaction details."); } else { $txnDetailsAssociativeArray = $payPalMsgHandler->FormatPDTResponseInAssociativeArray($response); $rawPdtMessage = urldecode($response); $notificationType = "PDT"; $replyMsg = $payPalMsgHandler->HandlePayPalTxnResponse($txnDetailsAssociativeArray, $rawPdtMessage, $dataAccess, $logger, $notificationType); } // If IPN for this payment has already been received, will not have all txn info in this replyMsg. // Must look up PayPalUsers entry for this payer ID, to determine if user is now premium. if (!$replyMsg->IsTxnLogged) { $payPalUser = $payPalMsgHandler->LookUpPayPalUser($replyMsg->PayerId, $replyMsg->SubscriptionID, $dataAccess, $logger, $notificationType); if ($payPalUser->IsActive) { $replyMsg->UserUpgradedPremium = true; $replyMsg->UserMessage = "Subscription successfully created!"; } } // Redirect to AccountManagement page to report results to user $sessionDataAccess = new DataAccess(); $sessionHandler = new DBSessionHandler($sessionDataAccess); session_set_save_handler($sessionHandler, true); session_start();