Merge pull request #3654 from yashodha/CRM-14968
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType {
36
37 /**
38 * static holder for the default payment processor
39 */
40 static $_defaultPaymentProcessorType = NULL;
41
42 /**
43 * class constructor
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Takes a bunch of params that are needed to match certain criteria and
51 * retrieves the relevant objects. Typically the valid params are only
52 * contact_id. We'll tweak this function to be more full featured over a period
53 * of time. This is the inverse function of create. It also stores all the retrieved
54 * values in the default array
55 *
56 * @param array $params (reference ) an assoc array of name/value pairs
57 * @param array $defaults (reference ) an assoc array to hold the flattened values
58 *
59 * @return object CRM_Core_BAO_LocaationType object on success, null otherwise
60 * @access public
61 * @static
62 */
63 static function retrieve(&$params, &$defaults) {
64 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
65 $paymentProcessorType->copyValues($params);
66 if ($paymentProcessorType->find(TRUE)) {
67 CRM_Core_DAO::storeValues($paymentProcessorType, $defaults);
68 return $paymentProcessorType;
69 }
70 return NULL;
71 }
72
73 /**
74 * update the is_active flag in the db
75 *
76 * @param int $id id of the database record
77 * @param boolean $is_active value we want to set the is_active field
78 *
79 * @return Object DAO object on sucess, null otherwise
80 *
81 * @access public
82 * @static
83 */
84 static function setIsActive($id, $is_active) {
85 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
86 }
87
88 /**
89 * retrieve the default payment processor
90 *
91 * @param NULL
92 *
93 * @return object The default payment processor object on success,
94 * null otherwise
95 * @static
96 * @access public
97 */
98 static function &getDefault() {
99 if (self::$_defaultPaymentProcessorType == NULL) {
100 $params = array('is_default' => 1);
101 $defaults = array();
102 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
103 }
104 return self::$_defaultPaymentProcessorType;
105 }
106
107 /**
108 * Function to add the payment-processor type in the db
109 *
110 * @param array $params (reference ) an assoc array of name/value pairs
77b97be7
EM
111 *
112 * @throws Exception
113 * @internal param array $ids the array that holds all the db ids
6a488035
TO
114 *
115 * @return object CRM_Financial_DAO_PaymentProcessorType
116 * @access public
117 * @static
6a488035
TO
118 */
119 static function create(&$params) {
120 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
121 $paymentProcessorType->copyValues($params);
122
123 /*
124 // adapted from CRM_Core_Extensions_Payment::install
125 foreach (array(
126 'class_name',
127 'title',
128 'name',
129 'description',
130 'user_name_label',
131 'password_label',
132 'signature_label',
133 'subject_label',
134 'url_site_default',
135 'url_api_default',
136 'url_recur_default',
137 'url_site_test_default',
138 'url_api_test_default',
139 'url_recur_test_default',
140 'url_button_default',
141 'url_button_test_default',
142 'billing_mode',
143 'is_recur',
144 'payment_type'
145 ) as $trimmable) {
146 if (isset($paymentProcessorType->{$trimmable})) {
147 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
148 }
149 }
150 */
151
152 if (isset($paymentProcessorType->billing_mode)) {
153 // ugh unidirectional manipulation
154 if (!is_numeric($paymentProcessorType->billing_mode)) {
5973d43c 155 $billingModes = array_flip(self::buildOptions('billing_mode'));
6a488035
TO
156 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
157 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
158 }
159 }
5973d43c 160 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
6a488035
TO
161 throw new Exception("Unrecognized billing_mode");
162 }
163 }
164
165 // FIXME handle is_default
166 if (!empty($paymentProcessorType->id)) {
167 $ppByName = self::getAllPaymentProcessorTypes('name');
168 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
169 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
170 CRM_Core_Error::fatal('This payment processor type already exists.');
171 }
172 }
173 }
174
175 return $paymentProcessorType->save();
176 }
177
178 /**
179 * Function to delete payment processor
180 *
77b97be7 181 * @param int $paymentProcessorTypeId ID of the processor to be deleted.
6a488035 182 *
77b97be7 183 * @return bool
6a488035
TO
184 * @access public
185 * @static
186 */
187 static function del($paymentProcessorTypeId) {
188 $query = "
189SELECT pp.id processor_id
190FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
191WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
192
193 $params = array(1 => array($paymentProcessorTypeId, 'Integer'));
194 $dao = CRM_Core_DAO::executeQuery($query, $params);
195
196 if ($dao->fetch()) {
197 CRM_Core_Session::setStatus(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.'), ts('Deletion Error'), 'error');
198 return;
199 }
200
201 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
202 $paymentProcessorType->id = $paymentProcessorTypeId;
203 if ($paymentProcessorType->delete()) {
204 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br>'), '', 'success');
205 return TRUE;
206 }
207 }
208
e0ef6999
EM
209 /**
210 * @param $attr
211 *
212 * @return array
213 */
6a488035
TO
214 static private function getAllPaymentProcessorTypes($attr) {
215 $ppt = array();
216 $dao = new CRM_Financial_DAO_PaymentProcessorType();
217 $dao->find();
218 while ($dao->fetch()) {
219 $ppt[$dao->$attr] = $dao->id;
220 }
221 return $ppt;
222 }
5973d43c
CW
223
224 /**
225 * Get options for a given field.
226 * @see CRM_Core_DAO::buildOptions
227 *
228 * @param String $fieldName
77b97be7
EM
229 * @param String $context : @see CRM_Core_DAO::buildOptionsContext
230 * @param Array $props : whatever is known about this dao object
231 *
232 * @return Array|bool
5973d43c
CW
233 */
234 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
235 $params = array();
236 // Special logic for fields whose options depend on context or properties
237 switch ($fieldName) {
238 // These options are not in the db
239 case 'billing_mode':
240 return array(
241 CRM_Core_Payment::BILLING_MODE_FORM => 'form',
242 CRM_Core_Payment::BILLING_MODE_BUTTON => 'button',
243 CRM_Core_Payment::BILLING_MODE_NOTIFY => 'notify',
244 );
245 }
786ad6e1 246 return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
5973d43c 247 }
6a488035
TO
248}
249