Merge pull request #4899 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 /**
100fef9d 38 * Static holder for the default payment processor
6a488035
TO
39 */
40 static $_defaultPaymentProcessorType = NULL;
41
42 /**
100fef9d 43 * Class constructor
6a488035 44 */
00be9182 45 public function __construct() {
6a488035
TO
46 parent::__construct();
47 }
48
49 /**
c490a46a 50 * Fetch object based on array of properties
6a488035 51 *
ed5dd492
TO
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
6a488035 56 *
c490a46a 57 * @return CRM_Core_BAO_LocaationType object on success, null otherwise
6a488035
TO
58 * @static
59 */
00be9182 60 public static function retrieve(&$params, &$defaults) {
6a488035
TO
61 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
62 $paymentProcessorType->copyValues($params);
63 if ($paymentProcessorType->find(TRUE)) {
64 CRM_Core_DAO::storeValues($paymentProcessorType, $defaults);
65 return $paymentProcessorType;
66 }
67 return NULL;
68 }
69
70 /**
100fef9d 71 * Update the is_active flag in the db
6a488035 72 *
ed5dd492
TO
73 * @param int $id
74 * Id of the database record.
75 * @param bool $is_active
76 * Value we want to set the is_active field.
6a488035 77 *
a6c01b45
CW
78 * @return Object
79 * DAO object on sucess, null otherwise
6a488035 80 *
6a488035
TO
81 * @static
82 */
00be9182 83 public static function setIsActive($id, $is_active) {
6a488035
TO
84 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
85 }
86
87 /**
100fef9d 88 * Retrieve the default payment processor
6a488035
TO
89 *
90 * @param NULL
91 *
a6c01b45
CW
92 * @return object
93 * The default payment processor object on success,
6a488035
TO
94 * null otherwise
95 * @static
6a488035 96 */
00be9182 97 public static function &getDefault() {
6a488035
TO
98 if (self::$_defaultPaymentProcessorType == NULL) {
99 $params = array('is_default' => 1);
100 $defaults = array();
101 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
102 }
103 return self::$_defaultPaymentProcessorType;
104 }
105
106 /**
100fef9d 107 * Add the payment-processor type in the db
6a488035 108 *
ed5dd492
TO
109 * @param array $params
110 * (reference ) an assoc array of name/value pairs.
77b97be7
EM
111 *
112 * @throws Exception
c490a46a 113 * @return CRM_Financial_DAO_PaymentProcessorType
6a488035 114 * @static
6a488035 115 */
00be9182 116 public static function create(&$params) {
6a488035
TO
117 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
118 $paymentProcessorType->copyValues($params);
119
120 /*
121 // adapted from CRM_Core_Extensions_Payment::install
122 foreach (array(
123 'class_name',
124 'title',
125 'name',
126 'description',
127 'user_name_label',
128 'password_label',
129 'signature_label',
130 'subject_label',
131 'url_site_default',
132 'url_api_default',
133 'url_recur_default',
134 'url_site_test_default',
135 'url_api_test_default',
136 'url_recur_test_default',
137 'url_button_default',
138 'url_button_test_default',
139 'billing_mode',
140 'is_recur',
141 'payment_type'
142 ) as $trimmable) {
143 if (isset($paymentProcessorType->{$trimmable})) {
144 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
145 }
146 }
147 */
148
149 if (isset($paymentProcessorType->billing_mode)) {
150 // ugh unidirectional manipulation
151 if (!is_numeric($paymentProcessorType->billing_mode)) {
5973d43c 152 $billingModes = array_flip(self::buildOptions('billing_mode'));
6a488035
TO
153 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
154 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
155 }
156 }
5973d43c 157 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
6a488035
TO
158 throw new Exception("Unrecognized billing_mode");
159 }
160 }
161
162 // FIXME handle is_default
163 if (!empty($paymentProcessorType->id)) {
164 $ppByName = self::getAllPaymentProcessorTypes('name');
165 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
166 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
167 CRM_Core_Error::fatal('This payment processor type already exists.');
168 }
169 }
170 }
171
172 return $paymentProcessorType->save();
173 }
174
175 /**
100fef9d 176 * Delete payment processor
6a488035 177 *
ed5dd492
TO
178 * @param int $paymentProcessorTypeId
179 * ID of the processor to be deleted.
6a488035 180 *
77b97be7 181 * @return bool
6a488035
TO
182 * @static
183 */
00be9182 184 public static function del($paymentProcessorTypeId) {
6a488035
TO
185 $query = "
186SELECT pp.id processor_id
187FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
188WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
189
190 $params = array(1 => array($paymentProcessorTypeId, 'Integer'));
191 $dao = CRM_Core_DAO::executeQuery($query, $params);
192
193 if ($dao->fetch()) {
194 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');
195 return;
196 }
197
198 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
199 $paymentProcessorType->id = $paymentProcessorTypeId;
200 if ($paymentProcessorType->delete()) {
d3e86119 201 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br/>'), '', 'success');
6a488035
TO
202 return TRUE;
203 }
204 }
205
e0ef6999
EM
206 /**
207 * @param $attr
208 *
209 * @return array
210 */
6a488035
TO
211 static private function getAllPaymentProcessorTypes($attr) {
212 $ppt = array();
213 $dao = new CRM_Financial_DAO_PaymentProcessorType();
214 $dao->find();
215 while ($dao->fetch()) {
216 $ppt[$dao->$attr] = $dao->id;
217 }
218 return $ppt;
219 }
5973d43c 220
6a488035 221}