plugins/paymethod/paypal/PayPalPlugin.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00017 import('classes.plugins.PaymethodPlugin');
00018 
00019 class PayPalPlugin extends PaymethodPlugin {
00020 
00021    function getName() {
00022       return 'Paypal';
00023    }
00024 
00025    function getDisplayName() {
00026       return __('plugins.paymethod.paypal.displayName');
00027    }
00028 
00029    function getDescription() {
00030       return __('plugins.paymethod.paypal.description');
00031    }
00032 
00033    function register($category, $path) {
00034       if (parent::register($category, $path)) {
00035          $this->addLocaleData();
00036          $this->import('PayPalDAO');
00037          $payPalDao = new PayPalDAO();
00038          DAORegistry::registerDAO('PayPalDAO', $payPalDao);
00039          return true;
00040       }
00041       return false;
00042    }
00043 
00044    function getSettingsFormFieldNames() {
00045       return array('paypalurl', 'selleraccount');
00046    }
00047 
00048    function isCurlInstalled() {
00049       return (function_exists('curl_init'));
00050    }
00051 
00052    function isConfigured() {
00053       $schedConf =& Request::getSchedConf();
00054       if (!$schedConf) return false;
00055 
00056       // Make sure CURL support is included.
00057       if (!$this->isCurlInstalled()) return false;
00058 
00059       // Make sure that all settings form fields have been filled in
00060       foreach ($this->getSettingsFormFieldNames() as $settingName) {
00061          $setting = $this->getSetting($schedConf->getConferenceId(), $schedConf->getId(), $settingName);
00062          if (empty($setting)) return false;
00063       }
00064       return true;
00065    }
00066 
00067    function displayPaymentSettingsForm(&$params, &$smarty) {
00068       $smarty->assign('isCurlInstalled', $this->isCurlInstalled());
00069       return parent::displayPaymentSettingsForm($params, $smarty);
00070    }
00071 
00072    function displayPaymentForm($queuedPaymentId, &$queuedPayment) {
00073       if (!$this->isConfigured()) return false;
00074       $schedConf =& Request::getSchedConf();
00075       $user =& Request::getUser();
00076 
00077       $params = array(
00078          'charset' => Config::getVar('i18n', 'client_charset'),
00079          'business' => $this->getSetting($schedConf->getConferenceId(), $schedConf->getId(), 'selleraccount'),
00080          'item_name' => $queuedPayment->getDescription(),
00081          'amount' => $queuedPayment->getAmount(),
00082          'quantity' => 1,
00083          'no_note' => 1,
00084          'no_shipping' => 1,
00085          'currency_code' => $queuedPayment->getCurrencyCode(),
00086          'lc' => String::substr(AppLocale::getLocale(), 3),
00087          'custom' => $queuedPaymentId,
00088          'notify_url' => Request::url(null, null, 'payment', 'plugin', array($this->getName(), 'ipn')),
00089          'return' => $queuedPayment->getRequestUrl(),
00090          'cancel_return' => Request::url(null, null, 'payment', 'plugin', array($this->getName(), 'cancel')),
00091          'first_name' => ($user)?$user->getFirstName():'',
00092          'last_name' => ($user)?$user->getLastname():'',
00093          'item_number' => 1,
00094          'cmd' => '_xclick'
00095       );
00096 
00097       $templateMgr =& TemplateManager::getManager();
00098       $templateMgr->assign('params', $params);
00099       $templateMgr->assign('paypalFormUrl', $this->getSetting($schedConf->getConferenceId(), $schedConf->getId(), 'paypalurl'));
00100       $templateMgr->display($this->getTemplatePath() . 'paymentForm.tpl');
00101    }
00102 
00106    function handle($args) {
00107       $templateMgr =& TemplateManager::getManager();
00108       $schedConf =& Request::getSchedConf();
00109       if (!$schedConf) return parent::handle($args);
00110 
00111       // Just in case we need to contact someone
00112       import('mail.MailTemplate');
00113       $contactName = $schedConf->getSetting('contactName');
00114       $contactEmail = $schedConf->getSetting('contactEmail');
00115       $mail = new MailTemplate('PAYPAL_INVESTIGATE_PAYMENT');
00116       $mail->setFrom($contactEmail, $contactName);
00117       $mail->addRecipient($contactEmail, $contactName);
00118 
00119       $paymentStatus = Request::getUserVar('payment_status');
00120 
00121       switch (array_shift($args)) {
00122          case 'ipn':
00123             // Build a confirmation transaction.
00124             $req = 'cmd=_notify-validate';
00125             if (get_magic_quotes_gpc()) {
00126                foreach ($_POST as $key => $value) $req .= '&' . urlencode(stripslashes($key)) . '=' . urlencode(stripslashes($value));
00127             } else {
00128                foreach ($_POST as $key => $value) $req .= '&' . urlencode($key) . '=' . urlencode($value);
00129             }
00130 
00131             // Create POST response
00132             $ch = curl_init();
00133             curl_setopt($ch, CURLOPT_URL, $this->getSetting($schedConf->getConferenceId(), $schedConf->getId(), 'paypalurl'));
00134             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00135             curl_setopt($ch, CURLOPT_POST, 1);
00136             curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($req)));
00137             curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
00138             $ret = curl_exec ($ch);
00139             $curlError = curl_error($ch);
00140             curl_close ($ch);
00141 
00142             // Check the confirmation response and handle as necessary.
00143             if (strcmp($ret, 'VERIFIED') == 0) switch ($paymentStatus) {
00144                case 'Completed':
00145                   $payPalDao =& DAORegistry::getDAO('PayPalDAO');
00146                   $transactionId = Request::getUserVar('txn_id');
00147                   if ($payPalDao->transactionExists($transactionId)) {
00148                      // A duplicate transaction was received; notify someone.
00149                      $mail->assignParams(array(
00150                         'schedConfName' => $schedConf->getFullTitle(),
00151                         'postInfo' => print_r($_POST, true),
00152                         'additionalInfo' => "Duplicate transaction ID: $transactionId",
00153                         'serverVars' => print_r($_SERVER, true)
00154                      ));
00155                      $mail->send();
00156                      exit();
00157                   } else {
00158                      // New transaction succeeded. Record it.
00159                      $payPalDao->insertTransaction(
00160                         $transactionId,
00161                         Request::getUserVar('txn_type'),
00162                         Request::getUserVar('payer_email'),
00163                         Request::getUserVar('receiver_email'),
00164                         Request::getUserVar('item_number'),
00165                         Request::getUserVar('payment_date'),
00166                         Request::getUserVar('payer_id'),
00167                         Request::getUserVar('receiver_id')
00168                      );
00169                      $queuedPaymentId = Request::getUserVar('custom');
00170 
00171                      import('payment.ocs.OCSPaymentManager');
00172                      $ocsPaymentManager =& OCSPaymentManager::getManager();
00173 
00174                      // Verify the cost and user details as per PayPal spec.
00175                      $queuedPayment =& $ocsPaymentManager->getQueuedPayment($queuedPaymentId);
00176                      if (!$queuedPayment) {
00177                         // The queued payment entry is missing. Complain.
00178                         $mail->assignParams(array(
00179                            'schedConfName' => $schedConf->getFullTitle(),
00180                            'postInfo' => print_r($_POST, true),
00181                            'additionalInfo' => "Missing queued payment ID: $queuedPaymentId",
00182                            'serverVars' => print_r($_SERVER, true)
00183                         ));
00184                         $mail->send();
00185                         exit();
00186                      }
00187 
00188                      if (
00189                         ($queuedAmount = $queuedPayment->getAmount()) != ($grantedAmount = Request::getUserVar('mc_gross')) ||
00190                         ($queuedCurrency = $queuedPayment->getCurrencyCode()) != ($grantedCurrency = Request::getUserVar('mc_currency')) ||
00191                         ($grantedEmail = Request::getUserVar('receiver_email')) != ($queuedEmail = $this->getSetting($schedConf->getConferenceId(), $schedConf->getId(), 'selleraccount'))
00192                      ) {
00193                         // The integrity checks for the transaction failed. Complain.
00194                         $mail->assignParams(array(
00195                            'schedConfName' => $schedConf->getFullTitle(),
00196                            'postInfo' => print_r($_POST, true),
00197                            'additionalInfo' =>
00198                               "Granted amount: $grantedAmount\n" .
00199                               "Queued amount: $queuedAmount\n" .
00200                               "Granted currency: $grantedCurrency\n" .
00201                               "Queued currency: $queuedCurrency\n" .
00202                               "Granted to PayPal account: $grantedEmail\n" .
00203                               "Configured PayPal account: $queuedEmail",
00204                            'serverVars' => print_r($_SERVER, true)
00205                         ));
00206                         $mail->send();
00207                         exit();
00208                      }
00209 
00210                      // Fulfill the queued payment.
00211                      if ($ocsPaymentManager->fulfillQueuedPayment($queuedPaymentId, $queuedPayment)) {
00212                         // Send the registrant a notification that their payment was received
00213                         $schedConfSettingsDao =& DAORegistry::getDAO('SchedConfSettingsDAO');
00214 
00215                         // Get registrant name and email
00216                         $userDao =& DAORegistry::getDAO('UserDAO');
00217                         $user =& $userDao->getUser($queuedPayment->getuserId());
00218                         $registrantName = $user->getFullName();
00219                         $registrantEmail = $user->getEmail();
00220 
00221                         // Get conference contact details
00222                         $schedConfId = $schedConf->getId();
00223                         $registrationName = $schedConfSettingsDao->getSetting($schedConfId, 'registrationName');
00224                         $registrationEmail = $schedConfSettingsDao->getSetting($schedConfId, 'registrationEmail');
00225                         $registrationPhone = $schedConfSettingsDao->getSetting($schedConfId, 'registrationPhone');
00226                         $registrationFax = $schedConfSettingsDao->getSetting($schedConfId, 'registrationFax');
00227                         $registrationMailingAddress = $schedConfSettingsDao->getSetting($schedConfId, 'registrationMailingAddress');
00228                         $registrationContactSignature = $registrationName;
00229 
00230                         if ($registrationMailingAddress != '') $registrationContactSignature .= "\n" . $registrationMailingAddress;
00231                         if ($registrationPhone != '') $registrationContactSignature .= "\n" . AppLocale::Translate('user.phone') . ': ' . $registrationPhone;
00232                         if ($registrationFax != '')   $registrationContactSignature .= "\n" . AppLocale::Translate('user.fax') . ': ' . $registrationFax;
00233 
00234                         $registrationContactSignature .= "\n" . AppLocale::Translate('user.email') . ': ' . $registrationEmail;
00235 
00236                         $paramArray = array(
00237                            'registrantName' => $registrantName,
00238                            'conferenceName' => $schedConf->getFullTitle(),
00239                            'registrationContactSignature' => $registrationContactSignature
00240                         );
00241 
00242                         import('mail.MailTemplate');
00243                         $mail = new MailTemplate('MANUAL_PAYMENT_RECEIVED');
00244                         $mail->setFrom($registrationEmail, $registrationName);
00245                         $mail->assignParams($paramArray);
00246                         $mail->addRecipient($registrantEmail, $registrantName);
00247                         $mail->send();
00248 
00249                         exit();
00250                      }
00251 
00252                      // If we're still here, it means the payment couldn't be fulfilled.
00253                      $mail->assignParams(array(
00254                         'schedConfName' => $schedConf->getFullTitle(),
00255                         'postInfo' => print_r($_POST, true),
00256                         'additionalInfo' => "Queued payment ID $queuedPaymentId could not be fulfilled.",
00257                         'serverVars' => print_r($_SERVER, true)
00258                      ));
00259                      $mail->send();
00260                   }
00261                   exit();
00262                case 'Pending':
00263                   // Ignore.
00264                   exit();
00265                default:
00266                   // An unhandled payment status was received; notify someone.
00267                   $mail->assignParams(array(
00268                      'schedConfName' => $schedConf->getFullTitle(),
00269                      'postInfo' => print_r($_POST, true),
00270                      'additionalInfo' => "Payment status: $paymentStatus",
00271                      'serverVars' => print_r($_SERVER, true)
00272                   ));
00273                   $mail->send();
00274                   exit();
00275             } else {
00276                // An unknown confirmation response was received; notify someone.
00277                $mail->assignParams(array(
00278                   'schedConfName' => $schedConf->getFullTitle(),
00279                   'postInfo' => print_r($_POST, true),
00280                   'additionalInfo' => "Confirmation return: $ret\nCURL error: $curlError",
00281                   'serverVars' => print_r($_SERVER, true)
00282                ));
00283                $mail->send();
00284                exit();
00285             }
00286 
00287             break;
00288          case 'cancel':
00289             Handler::setupTemplate();
00290             $templateMgr->assign(array(
00291                'currentUrl' => Request::url(null, null, 'index'),
00292                'pageTitle' => 'plugins.paymethod.paypal.purchase.cancelled.title',
00293                'message' => 'plugins.paymethod.paypal.purchase.cancelled'
00294             ));
00295             $templateMgr->display('common/message.tpl');
00296             exit();
00297             break;
00298       }
00299       parent::handle($args); // Don't know what to do with it
00300    }
00301 
00302    function getInstallSchemaFile() {
00303       return ($this->getPluginPath() . DIRECTORY_SEPARATOR . 'schema.xml');
00304    }
00305 
00306    function getInstallEmailTemplatesFile() {
00307       return ($this->getPluginPath() . DIRECTORY_SEPARATOR . 'emailTemplates.xml');
00308    }
00309 
00310    function getInstallEmailTemplateDataFile() {
00311       return ($this->getPluginPath() . '/locale/{$installedLocale}/emailTemplates.xml');
00312    }
00313 }
00314 
00315 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7