Exemple #1
0
 /**
  * This method is handles the response that will be invoked by the
  * notification or request sent by the payment processor.
  *hex string from paymentexpress is passed to this function as hex string. Code based on googleIPN
  * mac_key is only passed if the processor is pxaccess as it is used for decryption
  * $dps_method is either pxaccess or pxpay
  */
 static function main($dps_method, $rawPostData, $dps_url, $dps_user, $dps_key, $mac_key)
 {
     $config =& CRM_Core_Config::singleton();
     define('RESPONSE_HANDLER_LOG_FILE', $config->uploadDir . 'CiviCRM.PaymentExpress.log');
     //Setup the log file
     if (!($message_log = fopen(RESPONSE_HANDLER_LOG_FILE, "a"))) {
         error_func("Cannot open " . RESPONSE_HANDLER_LOG_FILE . " file.\n", 0);
         exit(1);
     }
     if ($dps_method == "pxpay") {
         require_once 'CRM/Core/Payment/PaymentExpressUtils.php';
         $processResponse = _valueXml(array('PxPayUserId' => $dps_user, 'PxPayKey' => $dps_key, 'Response' => $_GET['result']));
         $processResponse = _valueXml('ProcessResponse', $processResponse);
         fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"), $processResponse));
         // Send the XML-formatted validation request to DPS so that we can receive a decrypted XML response which contains the transaction results
         $curl = _initCURL($processResponse, $dps_url);
         fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"), $curl));
         $success = false;
         if ($response = curl_exec($curl)) {
             fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"), $response));
             curl_close($curl);
             // Assign the returned XML values to variables
             $valid = _xmlAttribute($response, 'valid');
             $success = _xmlElement($response, 'Success');
             $txnId = _xmlElement($response, 'TxnId');
             $responseText = _xmlElement($response, 'ResponseText');
             $authCode = _xmlElement($response, 'AuthCode');
             $DPStxnRef = _xmlElement($response, 'DpsTxnRef');
             $qfKey = _xmlElement($response, "TxnData1");
             $privateData = _xmlElement($response, "TxnData2");
             $component = _xmlElement($response, "TxnData3");
             $amount = _xmlElement($response, "AmountSettlement");
             $merchantReference = _xmlElement($response, "MerchantReference");
         } else {
             // calling DPS failed
             CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway to verify transaction response.'));
             exit;
         }
     } elseif ($dps_method == "pxaccess") {
         require_once 'PaymentExpress/pxaccess.php';
         global $pxaccess;
         $pxaccess = new PxAccess($dps_url, $dps_user, $dps_key, $mac_key);
         #getResponse method in PxAccess object returns PxPayResponse object
         #which encapsulates all the response data
         $rsp = $pxaccess->getResponse($rawPostData);
         $qfKey = $rsp->getTxnData1();
         $privateData = $rsp->getTxnData2();
         $component = $rsp->getTxnData3();
         $success = $rsp->getSuccess();
         $authCode = $rsp->getAuthCode();
         $DPStxnRef = $rsp->getDpsTxnRef();
         $amount = $rsp->getAmountSettlement();
         $MerchantReference = $rsp->getMerchantReference();
     }
     $privateData = $privateData ? self::stringToArray($privateData) : '';
     // Private Data consists of : a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID
     $privateData['contactID'] = $privateData['a'];
     $privateData['contributionID'] = $privateData['b'];
     $privateData['contributionTypeID'] = $privateData['c'];
     $privateData['invoiceID'] = $privateData['d'];
     if ($component == "event") {
         $privateData['participantID'] = $privateData['f'];
         $privateData['eventID'] = $privateData['g'];
     } else {
         if ($component == "contribute") {
             if (count($privateData) == 5) {
                 $privateData["membershipID"] = $privateData['e'];
             }
         }
     }
     $transactionReference = $authCode . "-" . $DPStxnRef;
     list($mode, $component, $paymentProcessorID, $duplicateTransaction) = self::getContext($privateData, $transactionReference);
     $mode = $mode ? 'test' : 'live';
     require_once 'CRM/Core/BAO/PaymentProcessor.php';
     $paymentProcessor = CRM_Core_BAO_PaymentProcessor::getPayment($paymentProcessorID, $mode);
     $ipn =& self::singleton($mode, $component, $paymentProcessor);
     //Check status and take appropriate action
     if ($success == 1) {
         if ($duplicateTransaction == 0) {
             $ipn->newOrderNotify($success, $privateData, $component, $amount, $transactionReference);
         }
         if ($component == "event") {
             $finalURL = CRM_Utils_System::url('civicrm/event/register', "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", false, null, false);
         } elseif ($component == "contribute") {
             $finalURL = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", false, null, false);
         }
         CRM_Utils_System::redirect($finalURL);
     } else {
         if ($component == "event") {
             $finalURL = CRM_Utils_System::url('civicrm/event/register', "_qf_Register_display=1&cancel=1&qfKey={$params['qfKey']}", false, null, false);
         } elseif ($component == "contribute") {
             $finalURL = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=1&cancel=1&qfKey={$params['qfKey']}", false, null, false);
         }
         CRM_Utils_System::redirect($finalURL);
     }
 }
 /**  
  * Main transaction function
  *  
  * @param array $params  name value pair of contribution data
  *  
  * @return void  
  * @access public 
  *  
  */
 function doTransferCheckout(&$params, $component)
 {
     $component = strtolower($component);
     $config = CRM_Core_Config::singleton();
     if ($component != 'contribute' && $component != 'event') {
         CRM_Core_Error::fatal(ts('Component is invalid'));
     }
     $url = $config->userFrameworkResourceURL . "extern/pxIPN.php";
     if ($component == 'event') {
         $cancelURL = CRM_Utils_System::url('civicrm/event/register', "_qf_Confirm_display=true&qfKey={$params['qfKey']}", false, null, false);
     } else {
         if ($component == 'contribute') {
             $cancelURL = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Confirm_display=true&qfKey={$params['qfKey']}", false, null, false);
         }
     }
     /*  
      * Build the private data string to pass to DPS, which they will give back to us with the
      *
      * transaction result.  We are building this as a comma-separated list so as to avoid long URLs.
      *
      * Parameters passed: a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID
      */
     $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}";
     if ($component == 'event') {
         $privateData .= ",f={$params['participantID']},g={$params['eventID']}";
         $merchantRef = "event registration";
     } elseif ($component == 'contribute') {
         $merchantRef = "Charitable Contribution";
         $membershipID = CRM_Utils_Array::value('membershipID', $params);
         if ($membershipID) {
             $privateData .= ",e={$membershipID}";
         }
     }
     // Allow further manipulation of params via custom hooks
     CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $privateData);
     /*  
      *  determine whether method is pxaccess or pxpay by whether signature (mac key) is defined
      */
     if (empty($this->_paymentProcessor['signature'])) {
         /*
          * Processor is pxpay 
          *
          * This contains the XML/Curl functions we'll need to generate the XML request
          */
         require_once 'CRM/Core/Payment/PaymentExpressUtils.php';
         // Build a valid XML string to pass to DPS
         $generateRequest = _valueXml(array('PxPayUserId' => $this->_paymentProcessor['user_name'], 'PxPayKey' => $this->_paymentProcessor['password'], 'AmountInput' => str_replace(",", "", number_format($params['amount'], 2)), 'CurrencyInput' => $params['currencyID'], 'MerchantReference' => $merchantRef, 'TxnData1' => $params['qfKey'], 'TxnData2' => $privateData, 'TxnData3' => $component, 'TxnType' => 'Purchase', 'TxnId' => '', 'UrlFail' => $url, 'UrlSuccess' => $url));
         $generateRequest = _valueXml('GenerateRequest', $generateRequest);
         // Get the special validated URL back from DPS by sending them the XML we've generated
         $curl = _initCURL($generateRequest, $this->_paymentProcessor['url_site']);
         $success = false;
         if ($response = curl_exec($curl)) {
             curl_close($curl);
             $valid = _xmlAttribute($response, 'valid');
             if (1 == $valid) {
                 // the request was validated, so we'll get the URL and redirect to it
                 $uri = _xmlElement($response, 'URI');
                 CRM_Utils_System::redirect($uri);
             } else {
                 // redisplay confirmation page
                 CRM_Utils_System::redirect($cancelURL);
             }
         } else {
             // calling DPS failed
             CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway.'));
         }
     } else {
         $processortype = "pxaccess";
         require_once 'PaymentExpress/pxaccess.inc.php';
         $PxAccess_Url = $this->_paymentProcessor['url_site'];
         // URL
         $PxAccess_Userid = $this->_paymentProcessor['user_name'];
         // User ID
         $PxAccess_Key = $this->_paymentProcessor['password'];
         // Your DES Key from DPS
         $Mac_Key = $this->_paymentProcessor['signature'];
         // Your MAC key from DPS
         $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key);
         $request = new PxPayRequest();
         $request->setAmountInput(number_format($params['amount'], 2));
         $request->setTxnData1($params['qfKey']);
         $request->setTxnData2($privateData);
         $request->setTxnData3($component);
         $request->setTxnType("Purchase");
         $request->setInputCurrency($params['currencyID']);
         $request->setMerchantReference($merchantRef);
         $request->setUrlFail($url);
         $request->setUrlSuccess($url);
         $request_string = $pxaccess->makeRequest($request);
         CRM_Utils_System::redirect($request_string);
     }
 }