3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Financial_BAO_PaymentProcessorType
extends CRM_Financial_DAO_PaymentProcessorType
implements \Civi\Core\HookInterface
{
20 * Static holder for the default payment processor.
23 public static $_defaultPaymentProcessorType = NULL;
26 * Retrieve DB object and copy to defaults array.
28 * @param array $params
29 * Array of criteria values.
30 * @param array $defaults
31 * Array to be populated with found values.
34 * The DAO object, if found.
38 public static function retrieve($params, &$defaults) {
39 return self
::commonRetrieve(self
::class, $params, $defaults);
43 * Update the is_active flag in the db.
46 * Id of the database record.
47 * @param bool $is_active
48 * Value we want to set the is_active field.
51 * true if we found and updated the object, else false
53 public static function setIsActive($id, $is_active) {
54 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
58 * Retrieve the default payment processor.
61 * The default payment processor object on success,
64 public static function &getDefault() {
65 if (self
::$_defaultPaymentProcessorType == NULL) {
66 $params = ['is_default' => 1];
68 self
::$_defaultPaymentProcessorType = self
::retrieve($params, $defaults);
70 return self
::$_defaultPaymentProcessorType;
74 * Add the payment-processor type in the db
76 * @param array $params
77 * (reference ) an assoc array of name/value pairs.
80 * @return CRM_Financial_DAO_PaymentProcessorType
82 public static function create(&$params) {
83 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
84 $paymentProcessorType->copyValues($params);
86 /* @codingStandardsIgnoreStart
87 // adapted from CRM_Core_Extensions_Payment::install
100 'url_site_test_default',
101 'url_api_test_default',
102 'url_recur_test_default',
103 'url_button_default',
104 'url_button_test_default',
109 if (isset($paymentProcessorType->{$trimmable})) {
110 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
113 @codingStandardsIgnoreEnd */
115 if (isset($paymentProcessorType->billing_mode
)) {
116 // ugh unidirectional manipulation
117 if (!is_numeric($paymentProcessorType->billing_mode
)) {
118 $billingModes = array_flip(self
::buildOptions('billing_mode'));
119 if (array_key_exists($paymentProcessorType->billing_mode
, $billingModes)) {
120 $paymentProcessorType->billing_mode
= $billingModes[$paymentProcessorType->billing_mode
];
123 if (!array_key_exists($paymentProcessorType->billing_mode
, self
::buildOptions('billing_mode'))) {
124 throw new Exception("Unrecognized billing_mode");
128 // FIXME handle is_default
129 if (!empty($paymentProcessorType->id
)) {
130 $ppByName = self
::getAllPaymentProcessorTypes('name');
131 if (array_key_exists($paymentProcessorType->name
, $ppByName)) {
132 if ($ppByName[$paymentProcessorType->name
] != $paymentProcessorType->id
) {
133 throw new CRM_Core_Exception('This payment processor type already exists.');
138 return $paymentProcessorType->save();
142 * Delete payment processor.
144 * @param int $paymentProcessorTypeId
148 public static function del($paymentProcessorTypeId) {
150 static::deleteRecord(['id' => $paymentProcessorTypeId]);
151 // This message is bad on so many levels
152 CRM_Core_Session
::setStatus(ts('Selected Payment Processor type has been deleted.<br/>'), '', 'success');
155 catch (CRM_Core_Exception
$e) {
156 CRM_Core_Session
::setStatus($e->getMessage(), ts('Deletion Error'), 'error');
162 * Callback for hook_civicrm_pre().
163 * @param \Civi\Core\Event\PreEvent $event
164 * @throws CRM_Core_Exception
166 public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent
$event) {
167 if ($event->action
=== 'delete') {
169 SELECT pp.id processor_id
170 FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
171 WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
173 $params = [1 => [$event->id
, 'Integer']];
174 $dao = CRM_Core_DAO
::executeQuery($query, $params);
177 throw new CRM_Core_Exception(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.'));
183 * @param string $attr
187 private static function getAllPaymentProcessorTypes($attr) {
189 $dao = new CRM_Financial_DAO_PaymentProcessorType();
191 while ($dao->fetch()) {
192 $ppt[$dao->$attr] = $dao->id
;