Skip to content

Commit

Permalink
Update payment processors for 5.24+ and propertybag. Support recur fo…
Browse files Browse the repository at this point in the history
…r offline basic
  • Loading branch information
mattwire committed Jul 7, 2020
1 parent bf62c95 commit ed802dc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 87 deletions.
42 changes: 7 additions & 35 deletions CRM/Core/Payment/RecurOffline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ class CRM_Core_Payment_RecurOffline extends CRM_Core_Payment {

protected $_mode = NULL;

protected $_params = array();

/**
* We only need one instance of this object. So we use the singleton
* pattern and cache the instance in this variable
*
* @var object
* @static
*/
static private $_singleton = NULL;
protected $_params = [];

/**
* Constructor
Expand All @@ -28,34 +19,15 @@ class CRM_Core_Payment_RecurOffline extends CRM_Core_Payment {
public function __construct($mode, &$paymentProcessor) {
$this->_mode = $mode;
$this->_paymentProcessor = $paymentProcessor;
$this->_processorName = ts('Recurring Offline Placeholder Processor');
}

/**
* singleton function used to manage this object
*
* @param string $mode the mode of operation: live or test
*
* @return object
* @static
*
*/
static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
$processorName = $paymentProcessor['name'];
if (CRM_Utils_Array::value($processorName, self::$_singleton) === NULL) {
self::$_singleton[$processorName] = new CRM_Core_Payment_RecurOffline($mode, $paymentProcessor);
}
return self::$_singleton[$processorName];
}

/**
* @param array $params assoc array of input parameters for this transaction
* @param array|\Civi\Payment\PropertyBag $params
* @param string $component
*
* @return array the result in a nice formatted array (or an error object)
* @public
* @return array
*/
function doDirectPayment(&$params) {

public function doPayment(&$params, $component = 'contribute') {
if ($this->_mode == 'test') {
$query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
$p = array();
Expand All @@ -72,11 +44,11 @@ function doDirectPayment(&$params) {
$trxn_id = intval($trxn_id) + 1;
$params['trxn_id'] = sprintf('live_%08d', $trxn_id);
}
$params['gross_amount'] = $params['amount'];
$params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
return $params;
}

/**
/**
* Are back office payments supported.
*
* @return bool
Expand Down
46 changes: 9 additions & 37 deletions CRM/Core/Payment/RecurOfflineACHEFT.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ class CRM_Core_Payment_RecurOfflineACHEFT extends CRM_Core_Payment {

protected $_mode = NULL;

protected $_params = array();

/**
* We only need one instance of this object. So we use the singleton
* pattern and cache the instance in this variable
*
* @var object
* @static
*/
static private $_singleton = NULL;
protected $_params = [];

/**
* Constructor
Expand All @@ -32,31 +23,12 @@ public function __construct($mode, &$paymentProcessor) {
}

/**
* singleton function used to manage this object
*
* @param string $mode the mode of operation: live or test
*
* @return object
* @static
* @param array|\Civi\Payment\PropertyBag $params
* @param string $component
*
* @return array
*/
static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
$processorName = $paymentProcessor['name'];
if (CRM_Utils_Array::value($processorName, self::$_singleton) === NULL) {
self::$_singleton[$processorName] = new CRM_Core_Payment_RecurOfflineACHEFT($mode, $paymentProcessor);
}
return self::$_singleton[$processorName];
}

/**
*
* @param array $params assoc array of input parameters for this transaction
*
* @return array the result in a nice formatted array (or an error object)
* @public
*/
function doDirectPayment(&$params) {

public function doPayment(&$params, $component = 'contribute') {
if ($this->_mode == 'test') {
$query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
$p = array();
Expand All @@ -73,16 +45,16 @@ function doDirectPayment(&$params) {
$trxn_id = intval($trxn_id) + 1;
$params['trxn_id'] = sprintf('live_%08d', $trxn_id);
}
$params['gross_amount'] = $params['amount'];
$params['payment_status_id'] = 2;

$params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
return $params;
}

protected function getDirectDebitFormFields() {
return [];
}
/**

/**
* Are back office payments supported.
*
* @return bool
Expand Down
49 changes: 37 additions & 12 deletions CRM/Core/Payment/RecurOfflineBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

class CRM_Core_Payment_RecurOfflineBasic extends CRM_Core_Payment {

use CRM_Core_Payment_MJWTrait;

protected $_mode = NULL;

/**
Expand Down Expand Up @@ -88,19 +86,25 @@ public function getPaymentFormFieldsMetadata() {
* @param array|\Civi\Payment\PropertyBag $params
* @param string $component
*
* @return array|\Civi\Payment\PropertyBag
* @return array
* @throws \CiviCRM_API3_Exception
*/
public function doPayment(&$params, $component = 'contribute') {
$propertyBag = \Civi\Payment\PropertyBag::cast($params);
if (!$propertyBag->has('isRecur')) {
// This defaults to false from 5.27 (https://github.com/civicrm/civicrm-core/pull/17452) so we can remove this when minVer = 5.27
$propertyBag->setIsRecur(FALSE);
}

// Set default contribution status
$params['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
$returnParams['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');

if (!empty($params['is_recur']) && !empty($this->getRecurringContributionId($params))) {
$reference = CRM_Utils_Array::value('reference_id', $params);
if ($propertyBag->getIsRecur() && $propertyBag->getContributionRecurID()) {
$reference = $propertyBag->getCustomProperty('reference_id');
if ($reference) {
// Save the external reference
$recurParams = [
'id' => $this->getRecurringContributionId($params),
'id' => $propertyBag->getContributionRecurID(),
'trxn_id' => $reference,
'processor_id' => $reference,
];
Expand All @@ -109,11 +113,7 @@ public function doPayment(&$params, $component = 'contribute') {
}

// We always complete the first contribution as we are "adding" it.
$params['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
// We need to set this to ensure that contributions are set to the correct status
if (!empty($params['contribution_status_id'])) {
$params['payment_status_id'] = $params['contribution_status_id'];
}
$returnParams['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
return $params;
}

Expand All @@ -136,5 +136,30 @@ function checkConfig() {
return NULL;
}

/**
* Does this processor support cancelling recurring contributions through code.
*
* If the processor returns true it must be possible to take action from within CiviCRM
* that will result in no further payments being processed.
*
* @return bool
*/
protected function supportsCancelRecurring() {
return TRUE;
}

/**
* Does the processor support the user having a choice as to whether to cancel the recurring with the processor?
*
* If this returns TRUE then there will be an option to send a cancellation request in the cancellation form.
*
* This would normally be false for processors where CiviCRM maintains the schedule.
*
* @return bool
*/
protected function supportsCancelRecurringNotifyOptional() {
return FALSE;
}

}

6 changes: 3 additions & 3 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
<url desc="Support">https://mjw.pt/support/contributionrecur</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2020-04-08</releaseDate>
<version>1.4</version>
<releaseDate>2020-07-07</releaseDate>
<version>1.5</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.19</ver>
<ver>5.24</ver>
</compatibility>
<requires>
<ext>mjwshared</ext>
Expand Down

0 comments on commit ed802dc

Please sign in to comment.