Follow up fix on paypal redirect
authoreileen <emcnaughton@wikimedia.org>
Sun, 20 Sep 2020 23:13:50 +0000 (11:13 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 20 Sep 2020 23:20:07 +0000 (11:20 +1200)
Turns out we did some function renaming & missed a spot before merge.

As an aside I fixed Paypal to call CRM_Core_Config::singleton()->userSystem->prePostRedirect()
directly rather than via system. I'm tempted to take the System function back out
again - I don't think it offers a better one-liner

CRM/Core/Exception/PrematureExitException.php
CRM/Core/Payment/PayPalImpl.php
CRM/Utils/System.php
tests/phpunit/CRM/Core/Payment/PaypalStdTest.php [new file with mode: 0644]

index e0eee738b9260b510cc945a8b0ac16df475874a3..f46bbfff8e55b5b40464c7b7fc697b9b73d2f123 100644 (file)
  */
 class CRM_Core_Exception_PrematureExitException extends RuntimeException {
 
+  /**
+   * Contextual data.
+   *
+   * @var array
+   */
+  public $errorData;
+
   /**
    * Construct the exception. Note: The message is NOT binary safe.
    *
index a1002fe925e73d3c3c4a5947424574cfcde7dbb5..16526443bd0196af4a07b51a7fb726a1d784302d 100644 (file)
@@ -972,7 +972,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment {
     $paypalURL = "{$url}{$sub}?$uri";
 
     // Allow each CMS to do a pre-flight check before redirecting to PayPal.
-    CRM_Utils_System::prePostRedirect();
+    CRM_Core_Config::singleton()->userSystem->prePostRedirect();
 
     CRM_Utils_System::redirect($paypalURL);
   }
index 6af92e60caff079b02c6a7b6a92b5dac6c161fb8..51ba3572d43fe9c56e08badfbd49ee41aff2d310 100644 (file)
@@ -511,7 +511,7 @@ class CRM_Utils_System {
     }
 
     self::setHttpHeader('Location', $url);
-    self::civiExit();
+    self::civiExit(0, ['url' => $url, 'context' => 'redirect']);
   }
 
   /**
@@ -1919,8 +1919,7 @@ class CRM_Utils_System {
    * Perform any necessary actions prior to redirecting via POST.
    */
   public static function prePostRedirect() {
-    $config = CRM_Core_Config::singleton();
-    $config->userSystem->paypalBeforeRedirect();
+    CRM_Core_Config::singleton()->userSystem->prePostRedirect();
   }
 
 }
diff --git a/tests/phpunit/CRM/Core/Payment/PaypalStdTest.php b/tests/phpunit/CRM/Core/Payment/PaypalStdTest.php
new file mode 100644 (file)
index 0000000..92a28eb
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Core_Payment_PaypalPro
+ * @group headless
+ */
+class CRM_Core_Payment_PaypalStdTest extends CiviUnitTestCase {
+
+  /**
+   * @var \CRM_Core_Payment_PayPalImpl
+   */
+  protected $processor;
+
+  /**
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function setUp() {
+    parent::setUp();
+    $processorID = $this->processorCreate([
+      'payment_processor_type_id' => 'PayPal_Standard',
+      'is_recur' => TRUE,
+      'billing_mode' => 4,
+      'url_site' => 'https://www.paypal.com/',
+      'url_recur' => 'https://www.paypal.com/',
+      'class_name' => 'Payment_PayPalImpl',
+    ]);
+
+    $this->processor = Civi\Payment\System::singleton()->getById($processorID);
+  }
+
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function tearDown() {
+    $this->quickCleanUpFinancialEntities();
+    parent::tearDown();
+  }
+
+  /**
+   * Test doing a one-off payment.
+   *
+   * @throws \Civi\Payment\Exception\PaymentProcessorException
+   */
+  public function testSinglePayment() {
+    $params = [];
+    $params['amount'] = 5.24;
+    $params['currency'] = 'USD';
+    $params['invoiceID'] = 'xyz';
+    $params['ip_address'] = '127.0.0.1';
+    $params['qfKey'] = 'w';
+    $params['currencyID'] = 'USD';
+    try {
+      $this->processor->doPayment($params);
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $redirectValues = parse_url($e->errorData['url']);
+      $this->assertEquals('https', $redirectValues['scheme']);
+      $this->assertEquals('www.paypal.com', $redirectValues['host']);
+      $this->assertEquals('/cgi-bin/webscr', $redirectValues['path']);
+      $query = [];
+      parse_str($redirectValues['query'], $query);
+      $this->assertEquals(5.24, $query['amount']);
+      $this->assertEquals('CiviCRM_SP', $query['bn']);
+    }
+  }
+
+}