From e4555ff1269c3e8034375cb9877daca298001164 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sun, 5 Jun 2016 11:56:47 -0600 Subject: [PATCH] CRM-18751 - Add method for setting payment processor return URL (#8488) * CRM-18751 - Add method for setting payment processor return URL * CRM-18751 - Restore protected visibility of functions --- CRM/Core/Payment.php | 60 +++++++++++++++++++++++--- tests/phpunit/CRM/Core/PaymentTest.php | 18 ++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index dcec384ee3..b58740242c 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -83,14 +83,26 @@ abstract class CRM_Core_Payment { protected $_paymentProcessor; /** - * Base url of the calling form. - * - * This is used for processors that need to return the browser back to the CiviCRM site. + * Base url of the calling form (offsite processors). * * @var string */ protected $baseReturnUrl; + /** + * Return url upon success (offsite processors). + * + * @var string + */ + protected $successUrl; + + /** + * Return url upon failure (offsite processors). + * + * @var string + */ + protected $cancelUrl; + /** * The profile configured to show on the billing form. * @@ -108,15 +120,41 @@ abstract class CRM_Core_Payment { protected $billingProfile; /** - * Set Base return URL. + * Set base return path (offsite processors). + * + * This is only useful with an internal civicrm form. * * @param string $url - * Url of site to return browser to. + * Internal civicrm path. */ public function setBaseReturnUrl($url) { $this->baseReturnUrl = $url; } + /** + * Set success return URL (offsite processors). + * + * This overrides $baseReturnUrl + * + * @param string $url + * Full url of site to return browser to upon success. + */ + public function setSuccessUrl($url) { + $this->successUrl = $url; + } + + /** + * Set cancel return URL (offsite processors). + * + * This overrides $baseReturnUrl + * + * @param string $url + * Full url of site to return browser to upon failure. + */ + public function setCancelUrl($url) { + $this->cancelUrl = $url; + } + /** * Set the configured payment profile. * @@ -827,6 +865,10 @@ abstract class CRM_Core_Payment { * @return string cancel url */ public function getCancelUrl($qfKey, $participantID) { + if (isset($this->cancelUrl)) { + return $this->cancelUrl; + } + if ($this->_component == 'event') { return CRM_Utils_System::url($this->getBaseReturnUrl(), array( 'reset' => 1, @@ -854,6 +896,10 @@ abstract class CRM_Core_Payment { * @return string */ protected function getReturnSuccessUrl($qfKey) { + if (isset($this->successUrl)) { + return $this->successUrl; + } + return CRM_Utils_System::url($this->getBaseReturnUrl(), array( '_qf_ThankYou_display' => 1, 'qfKey' => $qfKey, @@ -873,6 +919,10 @@ abstract class CRM_Core_Payment { * URL for a failing transactor to be redirected to. */ protected function getReturnFailUrl($key, $participantID = NULL, $eventID = NULL) { + if (isset($this->cancelUrl)) { + return $this->cancelUrl; + } + $test = $this->_is_test ? '&action=preview' : ''; if ($this->_component == "event") { return CRM_Utils_System::url('civicrm/event/register', diff --git a/tests/phpunit/CRM/Core/PaymentTest.php b/tests/phpunit/CRM/Core/PaymentTest.php index 6896c285df..03a74c8e93 100644 --- a/tests/phpunit/CRM/Core/PaymentTest.php +++ b/tests/phpunit/CRM/Core/PaymentTest.php @@ -46,4 +46,22 @@ class CRM_Core_PaymentTest extends CiviUnitTestCase { $this->assertEquals('payment_notification processor_name=Paypal', $log['values'][$log['id']]['message']); } + public function testSettingUrl() { + /** @var CRM_Core_Payment_Dummy $processor */ + $processor = \Civi\Payment\System::singleton()->getById($this->processorCreate()); + $success = 'http://success.com'; + $cancel = 'http://cancel.com'; + $processor->setCancelUrl($cancel); + $processor->setSuccessUrl($success); + + // Using ReflectionUtils to access protected methods + $successGetter = new ReflectionMethod($processor, 'getReturnSuccessUrl'); + $successGetter->setAccessible(TRUE); + $this->assertEquals($success, $successGetter->invoke($processor, NULL)); + + $cancelGetter = new ReflectionMethod($processor, 'getReturnFailUrl'); + $cancelGetter->setAccessible(TRUE); + $this->assertEquals($cancel, $cancelGetter->invoke($processor, NULL)); + } + } -- 2.25.1