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 +--------------------------------------------------------------------+
13 * This class stores logic for managing CiviCRM extensions.
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 class CRM_Extension_Manager_Payment
extends CRM_Extension_Manager_Base
{
21 * @var CRM_Extension_Mapper
26 * @param CRM_Extension_Mapper $mapper
28 public function __construct(CRM_Extension_Mapper
$mapper) {
29 parent
::__construct(TRUE);
30 $this->mapper
= $mapper;
36 * @param CRM_Extension_Info $info
37 * @throws CRM_Core_Exception
39 public function onPreInstall(CRM_Extension_Info
$info) {
40 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
42 if (array_key_exists($info->key
, $paymentProcessorTypes)) {
43 throw new CRM_Core_Exception(ts('This payment processor type is already installed.'));
46 $ppByName = $this->_getAllPaymentProcessorTypes('name');
47 if (array_key_exists($info->name
, $ppByName)) {
48 throw new CRM_Core_Exception(ts('This payment processor type already exists.'));
51 $dao = new CRM_Financial_DAO_PaymentProcessorType();
54 $dao->class_name
= trim($info->key
);
55 $dao->title
= trim($info->name
) . ' (' . trim($info->key
) . ')';
56 $dao->name
= trim($info->name
);
57 $dao->description
= trim($info->description
);
59 $dao->user_name_label
= trim($info->typeInfo
['userNameLabel']);
60 $dao->password_label
= trim($info->typeInfo
['passwordLabel']);
61 $dao->signature_label
= trim($info->typeInfo
['signatureLabel']);
62 $dao->subject_label
= trim($info->typeInfo
['subjectLabel']);
63 $dao->url_site_default
= trim($info->typeInfo
['urlSiteDefault']);
64 $dao->url_api_default
= trim($info->typeInfo
['urlApiDefault']);
65 $dao->url_recur_default
= trim($info->typeInfo
['urlRecurDefault']);
66 $dao->url_site_test_default
= trim($info->typeInfo
['urlSiteTestDefault']);
67 $dao->url_api_test_default
= trim($info->typeInfo
['urlApiTestDefault']);
68 $dao->url_recur_test_default
= trim($info->typeInfo
['urlRecurTestDefault']);
69 $dao->url_button_default
= trim($info->typeInfo
['urlButtonDefault']);
70 $dao->url_button_test_default
= trim($info->typeInfo
['urlButtonTestDefault']);
72 switch (trim($info->typeInfo
['billingMode'])) {
74 $dao->billing_mode
= CRM_Core_Payment
::BILLING_MODE_FORM
;
78 $dao->billing_mode
= CRM_Core_Payment
::BILLING_MODE_BUTTON
;
82 $dao->billing_mode
= CRM_Core_Payment
::BILLING_MODE_NOTIFY
;
86 throw new CRM_Core_Exception(ts('Billing mode in info file has wrong value.'));
89 $dao->is_recur
= trim($info->typeInfo
['isRecur']);
90 $dao->payment_type
= trim($info->typeInfo
['paymentType']);
98 * @param CRM_Extension_Info $info
100 public function onPostInstall(CRM_Extension_Info
$info) {
101 $this->_runPaymentHook($info, 'install');
107 * @param CRM_Extension_Info $info
109 public function onPreUninstall(CRM_Extension_Info
$info) {
110 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
111 if (!array_key_exists($info->key
, $paymentProcessorTypes)) {
112 throw new CRM_Core_Exception(ts('This payment processor type is not registered.'));
115 $dao = new CRM_Financial_DAO_PaymentProcessor();
116 $dao->payment_processor_type_id
= $paymentProcessorTypes[$info->key
];
118 while ($dao->fetch()) {
119 throw new CRM_Extension_Exception_DependencyException('payment');
122 $this->_runPaymentHook($info, 'uninstall');
123 return CRM_Financial_BAO_PaymentProcessorType
::del($paymentProcessorTypes[$info->key
]);
129 * @param CRM_Extension_Info $info
131 public function onPreDisable(CRM_Extension_Info
$info) {
132 // HMM? // if ($this->type == 'payment' && $this->status != 'missing') {
133 $this->_runPaymentHook($info, 'disable');
135 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
136 CRM_Financial_BAO_PaymentProcessorType
::setIsActive($paymentProcessorTypes[$info->key
], 0);
142 * @param CRM_Extension_Info $info
144 public function onPreEnable(CRM_Extension_Info
$info) {
145 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
146 CRM_Financial_BAO_PaymentProcessorType
::setIsActive($paymentProcessorTypes[$info->key
], 1);
152 * @param CRM_Extension_Info $info
154 public function onPostEnable(CRM_Extension_Info
$info) {
155 // HMM? // if ($this->type == 'payment' && $this->status != 'missing') {
156 $this->_runPaymentHook($info, 'enable');
160 * @param string $attr
161 * The attribute used to key the array.
165 private function _getAllPaymentProcessorTypes($attr) {
167 $dao = new CRM_Financial_DAO_PaymentProcessorType();
169 while ($dao->fetch()) {
170 $ppt[$dao->$attr] = $dao->id
;
176 * Run hooks in the payment processor class.
177 * Load requested payment processor and call the method specified.
179 * @param CRM_Extension_Info $info
180 * @param string $method
181 * The method to call in the payment processor class.
183 private function _runPaymentHook(CRM_Extension_Info
$info, $method) {
184 // Not concerned about performance at this stage, as these are seldom performed tasks
185 // (payment processor enable/disable/install/uninstall). May wish to implement some
186 // kind of registry/caching system if more hooks are added.
189 $paymentClass = $this->mapper
->keyToClass($info->key
, 'payment');
190 $file = $this->mapper
->classToPath($paymentClass);
191 if (!file_exists($file)) {
192 CRM_Core_Session
::setStatus(ts('Failed to load file (%3) for payment processor (%1) while running "%2"', [
203 catch (CRM_Extension_Exception
$e) {
204 CRM_Core_Session
::setStatus(ts('Failed to determine file path for payment processor (%1) while running "%2"', [
211 $processorDAO = CRM_Core_DAO
::executeQuery(
212 " SELECT pp.id, ppt.class_name
213 FROM civicrm_extension ext
214 INNER JOIN civicrm_payment_processor_type ppt
215 ON ext.name = ppt.name
216 LEFT JOIN civicrm_payment_processor pp
217 ON ppt.id = pp.payment_processor_type_id
218 WHERE ext.type = 'payment'
219 AND ext.full_name = %1
222 1 => [$info->key
, 'String'],
226 while ($processorDAO->fetch()) {
227 $class_name = $processorDAO->class_name
;
228 $processor_id = $processorDAO->id
;
231 if (empty($class_name)) {
232 throw new CRM_Core_Exception('Unable to find payment processor in ' . __CLASS__
. '::' . __METHOD__
);
235 // In the case of uninstall, check for instances of PP first.
236 // Don't run hook if any are found.
237 if ($method == 'uninstall' && $processor_id > 0) {
246 // Instantiate PP - the getClass function allows us to do this when no payment processor instances exist.
247 $processorInstance = Civi\Payment\System
::singleton()->getByClass($class_name);
249 // Does PP implement this method, and can we call it?
250 if (method_exists($processorInstance, $method) && is_callable([
254 // If so, call it ...
255 $processorInstance->$method();
260 CRM_Core_Session
::setStatus(ts("Unrecognized payment hook (%1) in %2::%3",
261 [1 => $method, 2 => __CLASS__
, 3 => __METHOD__
]),