/**
  * Process payment by sending payment info to Paytrail with REST API.
  *
  * @param array $params name value pair of Payment processor data
  * @param string $component name of CiviCRM component that is using this Payment Processor (contribute or event)
  * @param string $merchantId Paytrail merchant id
  * @param string $merchantSecret Paytrail merchant secret
  * @return Verkkomaksut_Module_Rest_Result REST call result with redirect URL and token
  */
 public function processPayment(&$params, $component, $merchantId, $merchantSecret)
 {
     $this->paytrailConfig->setPaymentProcessorParams($params);
     if ($component != 'contribute' && $component != 'event') {
         CRM_Core_Error::fatal(ts('Component is invalid'));
     }
     // Create payment. Default Paytrail API mode is E1.
     $payment =& $this->createPaymentObject($params, $component);
     $payment->setLocale($this->paytrailConfig->get('locale'));
     // Send request to https://payment.verkkomaksut.fi with Merchant ID and Merchant secret
     $module = new Verkkomaksut_Module_Rest($merchantId, $merchantSecret);
     try {
         $result = $module->processPayment($payment);
     } catch (Verkkomaksut_Exception $e) {
         CRM_Core_Error::fatal("Error contacting Paytrail: " . $e->getMessage() . ". Order number: " . $params['invoiceID']);
         exit;
     }
     return $result;
 }
コード例 #2
0
 /**
  * This method is handles the response that will be invoked (from PaytrailNotify.php) every time
  * a notification or request is sent by the Paytrail Server.
  */
 static function main()
 {
     require_once 'CRM/Utils/Request.php';
     $config = CRM_Core_Config::singleton();
     //Get payment info from civicrm_paytrail_payment_processor_invoice_data table. These are saved to table before payment.
     $invoiceID = $_GET["ORDER_NUMBER"];
     $invoiceData = self::readInvoiceData($invoiceID);
     $contributionPageCmsUrl = $invoiceData['contributionPageCmsUrl'] ?: '';
     $privateData['invoiceID'] = $invoiceData['invoice_id'];
     $privateData['contactID'] = $invoiceData['contact_id'];
     $privateData['contributionID'] = $invoiceData['contribution_id'];
     $privateData['contributionTypeID'] = $invoiceData['contribution_financial_type_id'];
     $privateData['eventID'] = $invoiceData['event_id'];
     $privateData['participantID'] = $invoiceData['participant_id'];
     $privateData['membershipID'] = $invoiceData['membership_id'];
     $privateData['amount'] = $invoiceData['amount'];
     $privateData['qfKey'] = $invoiceData['qfKey'];
     list($mode, $component, $paymentProcessorID, $duplicateTransaction) = self::getContext($privateData);
     $mode = $mode ? 'test' : 'live';
     $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $mode);
     //Validate Paytrail result
     require_once "Verkkomaksut_Module_Rest.php";
     $merchantId = $paymentProcessor['user_name'];
     $merchantSecret = $paymentProcessor['password'];
     $module = new Verkkomaksut_Module_Rest($merchantId, $merchantSecret);
     if ($module->confirmPayment($_GET["ORDER_NUMBER"], $_GET["TIMESTAMP"], $_GET["PAID"], $_GET["METHOD"], $_GET["RETURN_AUTHCODE"])) {
         $success = true;
     } else {
         CRM_Core_Error::debug_log_message("Failure: Paytrail notify is incorrect");
         $success = false;
     }
     $finalUrlAction = $component == 'event' ? 'civicrm/event/register' : 'civicrm/contribute/transact';
     $finalUrlParams = array('qfKey' => $privateData['qfKey']);
     // Redirect our users to the correct url.
     if ($success) {
         // Success. Process the transaction.
         if ($duplicateTransaction == 0) {
             $ipn = self::singleton($mode, $component, $paymentProcessor);
             $amount = (double) $privateData['amount'];
             $transactionId = $_GET["ORDER_NUMBER"];
             $ipn->newOrderNotify($success, $privateData, $component, $amount, $transactionId);
         }
         $finalUrlParams['_qf_ThankYou_display'] = 1;
     } else {
         $finalUrlParams[$component == 'event' ? '_qf_Register_display' : '_qf_Main_display'] = 1;
         $finalUrlParams['cancel'] = 1;
     }
     if ($contributionPageCmsUrl) {
         $queryParams = array_merge(array('page' => 'CiviCRM', 'q' => $finalUrlAction), $finalUrlParams);
         $glue = strpos($contributionPageCmsUrl, '?') === FALSE ? '?' : '&';
         $finalUrl = $contributionPageCmsUrl . $glue . http_build_query($queryParams, null, '&');
         CRM_Utils_System::redirect($finalUrl);
     } else {
         CRM_Utils_System::redirect(CRM_Utils_System::url($finalUrlAction, $finalUrlParams, true, null, false, true));
     }
 }