Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType {
36
37 /**
fe482240 38 * Static holder for the default payment processor.
6a488035
TO
39 */
40 static $_defaultPaymentProcessorType = NULL;
41
42 /**
fe482240 43 * Class constructor.
6a488035 44 */
00be9182 45 public function __construct() {
6a488035
TO
46 parent::__construct();
47 }
48
49 /**
fe482240 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 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 /**
fe482240 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 78 * @return Object
b44e3f84 79 * DAO object on success, null otherwise
6a488035 80 *
6a488035 81 */
00be9182 82 public static function setIsActive($id, $is_active) {
6a488035
TO
83 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
84 }
85
86 /**
fe482240 87 * Retrieve the default payment processor.
6a488035 88 *
a6c01b45
CW
89 * @return object
90 * The default payment processor object on success,
6a488035 91 * null otherwise
6a488035 92 */
00be9182 93 public static function &getDefault() {
6a488035
TO
94 if (self::$_defaultPaymentProcessorType == NULL) {
95 $params = array('is_default' => 1);
96 $defaults = array();
97 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
98 }
99 return self::$_defaultPaymentProcessorType;
100 }
101
102 /**
100fef9d 103 * Add the payment-processor type in the db
6a488035 104 *
ed5dd492
TO
105 * @param array $params
106 * (reference ) an assoc array of name/value pairs.
77b97be7
EM
107 *
108 * @throws Exception
c490a46a 109 * @return CRM_Financial_DAO_PaymentProcessorType
6a488035 110 */
00be9182 111 public static function create(&$params) {
6a488035
TO
112 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
113 $paymentProcessorType->copyValues($params);
114
006389de 115 /* @codingStandardsIgnoreStart
6a488035
TO
116 // adapted from CRM_Core_Extensions_Payment::install
117 foreach (array(
118 'class_name',
119 'title',
120 'name',
121 'description',
122 'user_name_label',
123 'password_label',
124 'signature_label',
125 'subject_label',
126 'url_site_default',
127 'url_api_default',
128 'url_recur_default',
129 'url_site_test_default',
130 'url_api_test_default',
131 'url_recur_test_default',
132 'url_button_default',
133 'url_button_test_default',
134 'billing_mode',
135 'is_recur',
136 'payment_type'
006389de 137 ) as $trimmable) {
6a488035
TO
138 if (isset($paymentProcessorType->{$trimmable})) {
139 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
140 }
141 }
006389de 142 @codingStandardsIgnoreEnd */
6a488035
TO
143
144 if (isset($paymentProcessorType->billing_mode)) {
145 // ugh unidirectional manipulation
146 if (!is_numeric($paymentProcessorType->billing_mode)) {
5973d43c 147 $billingModes = array_flip(self::buildOptions('billing_mode'));
6a488035
TO
148 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
149 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
150 }
151 }
5973d43c 152 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
6a488035
TO
153 throw new Exception("Unrecognized billing_mode");
154 }
155 }
156
157 // FIXME handle is_default
158 if (!empty($paymentProcessorType->id)) {
159 $ppByName = self::getAllPaymentProcessorTypes('name');
160 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
161 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
162 CRM_Core_Error::fatal('This payment processor type already exists.');
163 }
164 }
165 }
166
167 return $paymentProcessorType->save();
168 }
169
170 /**
fe482240 171 * Delete payment processor.
6a488035 172 *
ed5dd492
TO
173 * @param int $paymentProcessorTypeId
174 * ID of the processor to be deleted.
6a488035 175 *
ae5ffbb7 176 * @return bool|NULL
6a488035 177 */
00be9182 178 public static function del($paymentProcessorTypeId) {
6a488035
TO
179 $query = "
180SELECT pp.id processor_id
181FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
182WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
183
184 $params = array(1 => array($paymentProcessorTypeId, 'Integer'));
185 $dao = CRM_Core_DAO::executeQuery($query, $params);
186
187 if ($dao->fetch()) {
188 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');
ae5ffbb7 189 return NULL;
6a488035
TO
190 }
191
192 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
193 $paymentProcessorType->id = $paymentProcessorTypeId;
194 if ($paymentProcessorType->delete()) {
d3e86119 195 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br/>'), '', 'success');
6a488035
TO
196 return TRUE;
197 }
198 }
199
e0ef6999
EM
200 /**
201 * @param $attr
202 *
203 * @return array
204 */
6a488035
TO
205 static private function getAllPaymentProcessorTypes($attr) {
206 $ppt = array();
207 $dao = new CRM_Financial_DAO_PaymentProcessorType();
208 $dao->find();
209 while ($dao->fetch()) {
210 $ppt[$dao->$attr] = $dao->id;
211 }
212 return $ppt;
213 }
5973d43c 214
6a488035 215}