CRM-16555 move paypal code into payment subsystem as part of getting
authorEileen McNaughton <eileen@fuzion.co.nz>
Wed, 10 Jun 2015 00:10:13 +0000 (12:10 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Wed, 10 Jun 2015 00:11:25 +0000 (12:11 +1200)
js working

16 files changed:
CRM/Contribute/Form/AdditionalPayment.php
CRM/Contribute/Form/Contribution.php
CRM/Contribute/Form/Contribution/Confirm.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Core/Payment.php
CRM/Core/Payment/FirstData.php
CRM/Core/Payment/Form.php
CRM/Core/Payment/Moneris.php
CRM/Core/Payment/PayPalImpl.php
CRM/Core/Region.php
CRM/Event/Cart/Form/Checkout/Payment.php
CRM/Event/Form/Participant.php
CRM/Event/Form/Registration/Confirm.php
CRM/Event/Form/Registration/Register.php
CRM/Member/Form/Membership.php
CRM/Member/Form/MembershipRenewal.php

index 1ae7a5d275c1323483cb4f1ee188bc4f683eb39b..3b6d0f0420d0fe0f12e0fcc28e068d1fb73f2553 100644 (file)
@@ -491,7 +491,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       $this->_params,
       $config->defaultCurrency
     );
-    $this->_params['payment_action'] = 'Sale';
+
     if (!empty($this->_params['trxn_date'])) {
       $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['trxn_date'], $this->_params['trxn_date_time']);
     }
index bc688f57c78c017a905d36ff2a6bfb3d5c949146..b80604ff1c71160db586479bac36d691bcab7e96 100644 (file)
@@ -1143,7 +1143,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       $this->_params,
       CRM_Core_Config::singleton()->defaultCurrency
     );
-    $this->_params['payment_action'] = 'Sale';
+
     if (!empty($this->_params['receive_date'])) {
       $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']);
     }
index 272f7e13729817828d4e34edb09a3614a5396d1c..5e38c9fde013d3c18d3796d8b31bcee8d0b6f178 100644 (file)
@@ -246,8 +246,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
         // we use this here to incorporate any changes made by folks in hooks
         $this->_params['currencyID'] = $config->defaultCurrency;
 
-        $this->_params['payment_action'] = 'Sale';
-
         // also merge all the other values from the profile fields
         $values = $this->controller->exportValues('Main');
         $skipFields = array(
@@ -296,7 +294,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
         $this->setFormAmountFields($this->_params['priceSetId']);
       }
       $this->_params['currencyID'] = $config->defaultCurrency;
-      $this->_params['payment_action'] = 'Sale';
     }
 
     $this->_params['is_pay_later'] = $this->get('is_pay_later');
index 08f9e23388382434bea1aac4e7f7f4074bf56728..b45551ce20139045826ad98ebec8f6a1de52f795 100644 (file)
@@ -231,18 +231,20 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       }
     }
 
-    //         // hack to simplify credit card entry for testing
-    //         $this->_defaults['credit_card_type']     = 'Visa';
-    //         $this->_defaults['amount']               = 168;
-    //         $this->_defaults['credit_card_number']   = '4111111111111111';
-    //         $this->_defaults['cvv2']                 = '000';
-    //         $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
-
-    //         // hack to simplify direct debit entry for testing
-    //         $this->_defaults['account_holder'] = 'Max Müller';
-    //         $this->_defaults['bank_account_number'] = '12345678';
-    //         $this->_defaults['bank_identification_number'] = '12030000';
-    //         $this->_defaults['bank_name'] = 'Bankname';
+    /*
+     * hack to simplify credit card entry for testing
+     *
+     * $this->_defaults['credit_card_type']     = 'Visa';
+     *         $this->_defaults['amount']               = 168;
+     *         $this->_defaults['credit_card_number']   = '4111111111111111';
+     *         $this->_defaults['cvv2']                 = '000';
+     *         $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
+     *         // hack to simplify direct debit entry for testing
+     *         $this->_defaults['account_holder'] = 'Max Müller';
+     *         $this->_defaults['bank_account_number'] = '12345678';
+     *         $this->_defaults['bank_identification_number'] = '12030000';
+     *         $this->_defaults['bank_name'] = 'Bankname';
+     */
 
     //build set default for pledge overdue payment.
     if (!empty($this->_values['pledge_id'])) {
@@ -722,7 +724,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
    */
   public static function formRule($fields, $files, $self) {
     $errors = array();
-    $amount = self::computeAmount($fields, $self);
+    $amount = self::computeAmount($fields, $self->_values);
 
     if ((!empty($fields['selectMembership']) &&
         $fields['selectMembership'] != 'no_thanks'
@@ -1078,15 +1080,16 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
   }
 
   /**
+   * Compute amount to be paid.
+   *
    * @param array $params
-   * @param CRM_Core_Form $form
+   * @param array $formValues
    *
    * @return int|mixed|null|string
    */
-  public static function computeAmount(&$params, &$form) {
-    $amount = NULL;
+  public static function computeAmount($params, $formValues) {
 
-    // first clean up the other amount field if present
+    // First clean up the other amount field if present.
     if (isset($params['amount_other'])) {
       $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']);
     }
@@ -1101,12 +1104,12 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       }
     }
     else {
-      if (!empty($form->_values['amount'])) {
+      if (!empty($formValues['amount'])) {
         $amountID = CRM_Utils_Array::value('amount', $params);
 
         if ($amountID) {
-          $params['amount_level'] = CRM_Utils_Array::value('label', $form->_values[$amountID]);
-          $amount = CRM_Utils_Array::value('value', $form->_values[$amountID]);
+          $params['amount_level'] = CRM_Utils_Array::value('label', $formValues[$amountID]);
+          $amount = CRM_Utils_Array::value('value', $formValues[$amountID]);
         }
       }
     }
@@ -1195,7 +1198,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     }
 
     // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
-    $params['amount'] = self::computeAmount($params, $this);
+    $params['amount'] = self::computeAmount($params, $this->_values);
     $params['separate_amount'] = $params['amount'];
     $memFee = NULL;
     if (!empty($params['selectMembership'])) {
@@ -1252,7 +1255,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       if ($is_quick_config) {
         foreach ($this->_values['fee'] as $key => & $val) {
           if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && !empty($params['price_' . $key])) {
-            $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]); //Clean out any currency symbols
+            // Clean out any currency symbols.
+            $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]);
             if ($params['price_' . $key] != 0) {
               foreach ($val['options'] as $optionKey => & $options) {
                 $options['amount'] = CRM_Utils_Array::value('price_' . $key, $params);
@@ -1281,7 +1285,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
           $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount'];
         }
         if (!$this->_membershipBlock['is_separate_payment']) {
-          $params['amount'] = $proceFieldAmount; //require when separate membership not used
+          //require when separate membership not used
+          $params['amount'] = $proceFieldAmount;
         }
       }
       $this->set('lineItem', $lineItem);
@@ -1316,28 +1321,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
             array($this->_expressButtonName, $this->_expressButtonName . '_x', $this->_expressButtonName . '_y')
           ) && empty($params['is_pay_later'])
         ) {
-          $this->set('contributeMode', 'express');
-
-          $donateURL = CRM_Utils_System::url('civicrm/contribute', '_qf_Contribute_display=1');
-          $params['cancelURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
-          $params['returnURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
-          $params['invoiceID'] = $invoiceID;
-          $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']);
-
-          //default action is Sale
-          $params['payment_action'] = 'Sale';
-
-          $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
-          $token = $payment->setExpressCheckout($params);
-          if (is_a($token, 'CRM_Core_Error')) {
-            CRM_Core_Error::displaySessionError($token);
-            CRM_Utils_System::redirect($params['cancelURL']);
-          }
-
-          $this->set('token', $token);
-
-          $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token";
-          CRM_Utils_System::redirect($paymentURL);
+          $this->handlePaypalExpress($invoiceID, $params);
         }
       }
       elseif ($this->_paymentProcessor &&
@@ -1411,4 +1395,28 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     $form->assign('paymentProcessorID', $form->_paymentProcessorID);
   }
 
+  /**
+   * @param $invoiceID
+   * @param $params
+   *
+   * @return mixed
+   */
+  protected function handlePaypalExpress($invoiceID, $params) {
+    $this->set('contributeMode', 'express');
+
+    $params['invoiceID'] = $invoiceID;
+    $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']);
+
+    $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
+    $token = $payment->setExpressCheckout($params);
+    if (is_a($token, 'CRM_Core_Error')) {
+      CRM_Core_Error::displaySessionError($token);
+      CRM_Utils_System::redirect($params['cancelURL']);
+    }
+
+    $this->set('token', $token);
+    $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token";
+    CRM_Utils_System::redirect($paymentURL);
+  }
+
 }
index 7d70cd484ccd130f6dea6fe869b2b7d96938c342..d57b98460f5d11aff21d024a57bcac15af65cb8d 100644 (file)
@@ -490,6 +490,65 @@ abstract class CRM_Core_Payment {
     );
   }
 
+  /**
+   * Get base url dependent on component.
+   *
+   * @return string|void
+   */
+  protected function getBaseReturnUrl() {
+    if ($this->_component == 'event') {
+      $baseURL = 'civicrm/event/register';
+    }
+    else {
+      $baseURL = 'civicrm/contribute/transact';
+    }
+    return $baseURL;
+  }
+
+  /**
+   * Get url to return to after cancelled or failed transaction
+   *
+   * @param $qfKey
+   * @param $participantID
+   *
+   * @return string cancel url
+   */
+  protected function getCancelUrl($qfKey, $participantID) {
+    if ($this->_component == 'event') {
+      return CRM_Utils_System::url($this->getBaseReturnUrl(), array(
+        'reset' => 1,
+        'cc' => 'fail',
+        'participantId' => $participantID,
+      ),
+        TRUE, NULL, FALSE
+      );
+    }
+
+    return CRM_Utils_System::url($this->getBaseReturnUrl(), array(
+      '_qf_Main_display' => 1,
+      'qfKey' => $qfKey,
+      'cancel' => 1,
+    ),
+      TRUE, NULL, FALSE
+    );
+  }
+
+  /**
+   * Get URL to return the browser to on success
+   *
+   * @param $qfKey
+   *
+   * @return string
+   */
+  protected function getReturnSuccessUrl($qfKey) {
+    return CRM_Utils_System::url($this->getBaseReturnUrl(), array(
+      '_qf_ThankYou_display' => 1,
+      'qfKey' => $qfKey
+    ),
+      TRUE, NULL, FALSE
+    );
+  }
+
   /**
    * Calling this from outside the payment subsystem is deprecated - use doPayment.
    *
index 792d23db156ff5f05095a66c9eff6933f8366e02..9055fcb50903c86d28596b74ba0e18b23412ccfb 100644 (file)
@@ -118,7 +118,7 @@ class CRM_Core_Payment_FirstData extends CRM_Core_Payment {
     $requestFields['transactionorigin'] = "Eci";
     #32 character string
     $requestFields['invoice_number'] = $params['invoiceID'];
-    $requestFields['ordertype'] = $params['payment_action'];
+    $requestFields['ordertype'] = 'Sale';
     $requestFields['comments'] = $params['description'];
     //**********************set 'result' for live testing **************************
     //  $requestFields[       'result'  ]      =    "";  #set to "Good", "Decline" or "Duplicate"
index 2251c4608dc7b221ef6b7382dc3223b4486226b4..19599e6dfb1e0f8de6b873b259af42c05c0ae13b 100644 (file)
@@ -260,12 +260,8 @@ class CRM_Core_Payment_Form {
       $form->assign('profileAddressFields', $profileAddressFields);
     }
 
-    // $processor->buildForm appears to be an undocumented (possibly unused) option for payment processors
-    // which was previously available only in some form flows
-    if (!empty($form->_paymentProcessor) && !empty($form->_paymentProcessor['object']) && $form->_paymentProcessor['object']->isSupported('buildForm')) {
-      if ($form->_paymentProcessor['object']->buildForm($form)) {
-        return NULL;
-      }
+    if ($processor['object']->buildForm($form)) {
+      return NULL;
     }
 
     self::setPaymentFieldsByProcessor($form, $processor, empty($isBillingDataOptional));
index e6a1cde54f67e3f442ba4bc21459fc39f0c52e37..e132f4e2404f78b91bcd07ecf01060a4fcce26a4 100644 (file)
@@ -103,7 +103,6 @@ class CRM_Core_Payment_Moneris extends CRM_Core_Payment {
     /* unused params: cvv not yet implemented, payment action ingored (should test for 'Sale' value?)
     [cvv2] => 000
     [ip_address] => 192.168.0.103
-    [payment_action] => Sale
     [contact_type] => Individual
     [geo_coord_id] => 1 */
 
index db61e765fb5a775a1ff415e26b75f3f4a6bfb83c..7f257c6aa3efbea7b38d20252c503fab4892ed5e 100644 (file)
@@ -99,6 +99,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
       if ($this->_processorName == 'PayPal Express') {
         CRM_Core_Region::instance('billing-block-post')->add(array(
           'template' => 'CRM/Financial/Form/PaypalExpress.tpl',
+          'name' => 'paypal_express',
         ));
       }
       if ($this->_processorName == 'PayPal Pro') {
@@ -143,13 +144,13 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
 
     $this->initialize($args, 'SetExpressCheckout');
 
-    $args['paymentAction'] = $params['payment_action'];
+    $args['paymentAction'] = 'Sale';
     $args['amt'] = $params['amount'];
     $args['currencyCode'] = $params['currencyID'];
     $args['desc'] = CRM_Utils_Array::value('description', $params);
     $args['invnum'] = $params['invoiceID'];
-    $args['returnURL'] = $params['returnURL'];
-    $args['cancelURL'] = $params['cancelURL'];
+    $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']);
+    $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL);
     $args['version'] = '56.0';
 
     //LCD if recurring, collect additional data and set some values
@@ -230,15 +231,14 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
     $args = array();
 
     $this->initialize($args, 'DoExpressCheckoutPayment');
-
     $args['token'] = $params['token'];
-    $args['paymentAction'] = $params['payment_action'];
+    $args['paymentAction'] = 'Sale';
     $args['amt'] = $params['amount'];
     $args['currencyCode'] = $params['currencyID'];
     $args['payerID'] = $params['payer_id'];
     $args['invnum'] = $params['invoiceID'];
-    $args['returnURL'] = CRM_Utils_Array::value('returnURL', $params);
-    $args['cancelURL'] = CRM_Utils_Array::value('cancelURL', $params);
+    $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']);
+    $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL);
     $args['desc'] = $params['description'];
 
     // add CiviCRM BN code
@@ -280,7 +280,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
     $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
 
     $args['token'] = $params['token'];
-    $args['paymentAction'] = $params['payment_action'];
+    $args['paymentAction'] = 'Sale';
     $args['amt'] = $params['amount'];
     $args['currencyCode'] = $params['currencyID'];
     $args['payerID'] = $params['payer_id'];
@@ -354,7 +354,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
 
     $this->initialize($args, 'DoDirectPayment');
 
-    $args['paymentAction'] = $params['payment_action'];
+    $args['paymentAction'] = 'Sale';
     $args['amt'] = $params['amount'];
     $args['currencyCode'] = $params['currencyID'];
     $args['invnum'] = $params['invoiceID'];
index 7868cee23813384dcd91f33f40ccb39a2d88c6d8..77b3e6065b2a65083aba5e715914d7f7ad606062 100644 (file)
@@ -122,6 +122,7 @@ class CRM_Core_Region {
     if (!isset($snippet['name'])) {
       $snippet['name'] = count($this->_snippets);
     }
+
     $this->_snippets[$snippet['name']] = $snippet;
     $this->_isSorted = FALSE;
     return $snippet;
@@ -137,12 +138,14 @@ class CRM_Core_Region {
   }
 
   /**
+   * Get snippet.
+   *
    * @param string $name
    *
    * @return mixed
    */
-  public function &get($name) {
-    return @$this->_snippets[$name];
+  public function get($name) {
+    return !empty($this->_snippets[$name]) ? $this->_snippets[$name] : NULL;
   }
 
   /**
index b646e1290946cc57e69828c4ff64326448324630..c16c47c601c6637da07a24822d98483d396a34c8 100644 (file)
@@ -575,7 +575,6 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
     }
     $params['ip_address'] = CRM_Utils_System::ipAddress();
     $params['currencyID'] = $config->defaultCurrency;
-    $params['payment_action'] = 'Sale';
 
     $payment = &CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
     CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE);
index 1bd751a84818dfd247aa6915cdf6f0c07e64aa10..cd4a5497f6e924af1aae8662d73475c274460905 100644 (file)
@@ -1185,7 +1185,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment
       $this->_params['amount'] = $params['fee_amount'];
       $this->_params['amount_level'] = $params['amount_level'];
       $this->_params['currencyID'] = $config->defaultCurrency;
-      $this->_params['payment_action'] = 'Sale';
       $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
 
       // at this point we've created a contact and stored its address etc
index 72b90c6502aa48a5aed241128ff4bbc20e78a66c..11dcbcf4bba10fcf18af515fbcace2ab78fbd373 100644 (file)
@@ -126,7 +126,6 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
         }
         $params['amount_level'] = $this->_params[0]['amount_level'];
         $params['currencyID'] = $this->_params[0]['currencyID'];
-        $params['payment_action'] = 'Sale';
 
         // also merge all the other values from the profile fields
         $values = $this->controller->exportValues('Register');
@@ -172,7 +171,6 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
       if ($this->_values['event']['is_monetary']) {
         $registerParams['ip_address'] = CRM_Utils_System::ipAddress();
         $registerParams['currencyID'] = $this->_params[0]['currencyID'];
-        $registerParams['payment_action'] = 'Sale';
       }
       //assign back primary participant params.
       $this->_params[0] = $registerParams;
index 8aa260e2e80b2c930b45889042ab3df38de5d501..56b234221136024b8f0fbdaf0527ec2b406026c0 100644 (file)
@@ -1137,7 +1137,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       if ($this->_values['event']['is_monetary']) {
         $params['ip_address'] = CRM_Utils_System::ipAddress();
         $params['currencyID'] = $config->defaultCurrency;
-        $params['payment_action'] = 'Sale';
         $params['invoiceID'] = $invoiceID;
       }
       $this->_params = $this->get('params');
@@ -1187,9 +1186,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
           );
           $params['invoiceID'] = $invoiceID;
 
-          //default action is Sale
-          $params['payment_action'] = 'Sale';
-
           $token = $payment->setExpressCheckout($params);
           if (is_a($token, 'CRM_Core_Error')) {
             CRM_Core_Error::displaySessionError($token);
index 88d22638e562f645b669123569d4edbe68c55223..8d51eb4342f3d556fbb75074c2fdb85af896b2b7 100644 (file)
@@ -1483,7 +1483,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
       $this->_params['amount'] = $params['total_amount'];
       $this->_params['currencyID'] = $config->defaultCurrency;
       $this->_params['description'] = ts('Office Credit Card Membership Signup Contribution');
-      $this->_params['payment_action'] = 'Sale';
       $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
       $this->_params['financial_type_id'] = $params['financial_type_id'];
 
index 77af3103d9c60f733fd7cb5d1c9df1442a528531..0314ba27e420015b91f298e346ff04ed15ebb94f 100644 (file)
@@ -534,7 +534,6 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
       $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
       $this->_params['amount'] = $formValues['total_amount'];
       $this->_params['currencyID'] = $config->defaultCurrency;
-      $this->_params['payment_action'] = 'Sale';
       $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE));
 
       // at this point we've created a contact and stored its address etc