Merge pull request #16671 from eileenmcnaughton/acl
[civicrm-core.git] / CRM / Core / Payment / Dummy.php
1 <?php
2 /*
3 * Copyright (C) 2007
4 * Licensed to CiviCRM under the Academic Free License version 3.0.
5 *
6 * Written and contributed by Ideal Solution, LLC (http://www.idealso.com)
7 *
8 */
9
10 /**
11 *
12 * @package CRM
13 * @author Marshal Newrock <marshal@idealso.com>
14 * $Id: Dummy.php 45429 2013-02-06 22:11:18Z lobo $
15 */
16
17 use Civi\Payment\Exception\PaymentProcessorException;
18
19 /**
20 * Dummy payment processor
21 */
22 class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
23 const CHARSET = 'iso-8859-1';
24
25 protected $_mode = NULL;
26
27 protected $_params = [];
28 protected $_doDirectPaymentResult = [];
29
30 /**
31 * Set result from do Direct Payment for test purposes.
32 *
33 * @param array $doDirectPaymentResult
34 * Result to be returned from test.
35 */
36 public function setDoDirectPaymentResult($doDirectPaymentResult) {
37 $this->_doDirectPaymentResult = $doDirectPaymentResult;
38 if (empty($this->_doDirectPaymentResult['trxn_id'])) {
39 $this->_doDirectPaymentResult['trxn_id'] = [];
40 }
41 else {
42 $this->_doDirectPaymentResult['trxn_id'] = (array) $doDirectPaymentResult['trxn_id'];
43 }
44 }
45
46 /**
47 * We only need one instance of this object. So we use the singleton
48 * pattern and cache the instance in this variable
49 *
50 * @var object
51 */
52 static private $_singleton = NULL;
53
54 /**
55 * Constructor.
56 *
57 * @param string $mode
58 * The mode of operation: live or test.
59 *
60 * @param $paymentProcessor
61 *
62 * @return \CRM_Core_Payment_Dummy
63 */
64 public function __construct($mode, &$paymentProcessor) {
65 $this->_mode = $mode;
66 $this->_paymentProcessor = $paymentProcessor;
67 $this->_processorName = ts('Dummy Processor');
68 }
69
70 /**
71 * Submit a payment using Advanced Integration Method.
72 *
73 * @param array $params
74 * Assoc array of input parameters for this transaction.
75 *
76 * @return array
77 * the result in a nice formatted array (or an error object)
78 * @throws \Civi\Payment\Exception\PaymentProcessorException
79 */
80 public function doDirectPayment(&$params) {
81 // Invoke hook_civicrm_paymentProcessor
82 // In Dummy's case, there is no translation of parameters into
83 // the back-end's canonical set of parameters. But if a processor
84 // does this, it needs to invoke this hook after it has done translation,
85 // but before it actually starts talking to its proprietary back-end.
86 if (!empty($params['is_recur'])) {
87 $throwAnENoticeIfNotSetAsTheseAreRequired = $params['frequency_interval'] . $params['frequency_unit'];
88 }
89 // no translation in Dummy processor
90 $cookedParams = $params;
91 CRM_Utils_Hook::alterPaymentProcessorParams($this,
92 $params,
93 $cookedParams
94 );
95 // This means we can test failing transactions by setting a past year in expiry. A full expiry check would
96 // be more complete.
97 if (!empty($params['credit_card_exp_date']['Y']) && date('Y') >
98 CRM_Core_Payment_Form::getCreditCardExpirationYear($params)) {
99 throw new PaymentProcessorException(ts('Invalid expiry date'));
100 }
101 //end of hook invocation
102 if (!empty($this->_doDirectPaymentResult)) {
103 $result = $this->_doDirectPaymentResult;
104 if (CRM_Utils_Array::value('payment_status_id', $result) === 'failed') {
105 throw new PaymentProcessorException($result['message'] ?? 'failed');
106 }
107 $result['trxn_id'] = array_shift($this->_doDirectPaymentResult['trxn_id']);
108 return $result;
109 }
110 if ($this->_mode == 'test') {
111 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
112 $p = [];
113 $trxn_id = strval(CRM_Core_DAO::singleValueQuery($query, $p));
114 $trxn_id = str_replace('test_', '', $trxn_id);
115 $trxn_id = intval($trxn_id) + 1;
116 $params['trxn_id'] = 'test_' . $trxn_id . '_' . uniqid();
117 }
118 else {
119 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'live_%'";
120 $p = [];
121 $trxn_id = strval(CRM_Core_DAO::singleValueQuery($query, $p));
122 $trxn_id = str_replace('live_', '', $trxn_id);
123 $trxn_id = intval($trxn_id) + 1;
124 $params['trxn_id'] = 'live_' . $trxn_id . '_' . uniqid();
125 }
126 $params['gross_amount'] = $params['amount'];
127 // Add a fee_amount so we can make sure fees are handled properly in underlying classes.
128 $params['fee_amount'] = 1.50;
129 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
130 $params['description'] = $this->getPaymentDescription($params);
131
132 return $params;
133 }
134
135 /**
136 * Does this payment processor support refund?
137 *
138 * @return bool
139 */
140 public function supportsRefund() {
141 return TRUE;
142 }
143
144 /**
145 * Supports altering future start dates
146 * @return bool
147 */
148 public function supportsFutureRecurStartDate() {
149 return TRUE;
150 }
151
152 /**
153 * Submit a refund payment
154 *
155 * @throws \Civi\Payment\Exception\PaymentProcessorException
156 *
157 * @param array $params
158 * Assoc array of input parameters for this transaction.
159 */
160 public function doRefund(&$params) {}
161
162 /**
163 * Generate error object.
164 *
165 * Throwing exceptions is preferred over this.
166 *
167 * @param string $errorCode
168 * @param string $errorMessage
169 *
170 * @return CRM_Core_Error
171 * Error object.
172 */
173 public function &error($errorCode = NULL, $errorMessage = NULL) {
174 $e = CRM_Core_Error::singleton();
175 if ($errorCode) {
176 $e->push($errorCode, 0, NULL, $errorMessage);
177 }
178 else {
179 $e->push(9001, 0, NULL, 'Unknown System Error.');
180 }
181 return $e;
182 }
183
184 /**
185 * This function checks to see if we have the right config values.
186 *
187 * @return string
188 * the error message if any
189 */
190 public function checkConfig() {
191 return NULL;
192 }
193
194 /**
195 * Get an array of the fields that can be edited on the recurring contribution.
196 *
197 * Some payment processors support editing the amount and other scheduling details of recurring payments, especially
198 * those which use tokens. Others are fixed. This function allows the processor to return an array of the fields that
199 * can be updated from the contribution recur edit screen.
200 *
201 * The fields are likely to be a subset of these
202 * - 'amount',
203 * - 'installments',
204 * - 'frequency_interval',
205 * - 'frequency_unit',
206 * - 'cycle_day',
207 * - 'next_sched_contribution_date',
208 * - 'end_date',
209 * - 'failure_retry_day',
210 *
211 * The form does not restrict which fields from the contribution_recur table can be added (although if the html_type
212 * metadata is not defined in the xml for the field it will cause an error.
213 *
214 * Open question - would it make sense to return membership_id in this - which is sometimes editable and is on that
215 * form (UpdateSubscription).
216 *
217 * @return array
218 */
219 public function getEditableRecurringScheduleFields() {
220 return ['amount', 'next_sched_contribution_date'];
221 }
222
223 /**
224 * @param string $message
225 * @param array $params
226 *
227 * @return bool|object
228 */
229 public function cancelSubscription(&$message = '', $params = []) {
230 return TRUE;
231 }
232
233 }