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