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