Commit | Line | Data |
---|---|---|
6a488035 TO |
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> | |
6a488035 TO |
14 | */ |
15 | ||
65f1a9a1 | 16 | use Civi\Payment\Exception\PaymentProcessorException; |
3bfefe56 | 17 | use Civi\Payment\PropertyBag; |
65f1a9a1 | 18 | |
c866eb5f TO |
19 | /** |
20 | * Dummy payment processor | |
6a488035 TO |
21 | */ |
22 | class CRM_Core_Payment_Dummy extends CRM_Core_Payment { | |
3bfefe56 | 23 | protected $_mode; |
6a488035 | 24 | |
be2fb01f CW |
25 | protected $_params = []; |
26 | protected $_doDirectPaymentResult = []; | |
f8fe0df6 | 27 | |
28 | /** | |
eca28728 EM |
29 | * Set result from do Direct Payment for test purposes. |
30 | * | |
f8fe0df6 | 31 | * @param array $doDirectPaymentResult |
eca28728 | 32 | * Result to be returned from test. |
f8fe0df6 | 33 | */ |
34 | public function setDoDirectPaymentResult($doDirectPaymentResult) { | |
35 | $this->_doDirectPaymentResult = $doDirectPaymentResult; | |
9d0f10b7 | 36 | if (empty($this->_doDirectPaymentResult['trxn_id'])) { |
be2fb01f | 37 | $this->_doDirectPaymentResult['trxn_id'] = []; |
9d0f10b7 EM |
38 | } |
39 | else { | |
40 | $this->_doDirectPaymentResult['trxn_id'] = (array) $doDirectPaymentResult['trxn_id']; | |
41 | } | |
f8fe0df6 | 42 | } |
6a488035 | 43 | |
6a488035 | 44 | /** |
fe482240 | 45 | * Constructor. |
6a488035 | 46 | * |
6a0b768e TO |
47 | * @param string $mode |
48 | * The mode of operation: live or test. | |
6a488035 | 49 | * |
3bfefe56 | 50 | * @param array $paymentProcessor |
6a488035 | 51 | */ |
00be9182 | 52 | public function __construct($mode, &$paymentProcessor) { |
6a488035 TO |
53 | $this->_mode = $mode; |
54 | $this->_paymentProcessor = $paymentProcessor; | |
6a488035 TO |
55 | } |
56 | ||
6a488035 | 57 | /** |
fe482240 | 58 | * Submit a payment using Advanced Integration Method. |
6a488035 | 59 | * |
6a0b768e TO |
60 | * @param array $params |
61 | * Assoc array of input parameters for this transaction. | |
6a488035 | 62 | * |
a6c01b45 CW |
63 | * @return array |
64 | * the result in a nice formatted array (or an error object) | |
65f1a9a1 | 65 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6a488035 | 66 | */ |
00be9182 | 67 | public function doDirectPayment(&$params) { |
3bfefe56 | 68 | $propertyBag = PropertyBag::cast($params); |
6a488035 TO |
69 | // Invoke hook_civicrm_paymentProcessor |
70 | // In Dummy's case, there is no translation of parameters into | |
71 | // the back-end's canonical set of parameters. But if a processor | |
72 | // does this, it needs to invoke this hook after it has done translation, | |
73 | // but before it actually starts talking to its proprietary back-end. | |
3bfefe56 | 74 | if ($propertyBag->getIsRecur()) { |
75 | $throwAnENoticeIfNotSetAsTheseAreRequired = $propertyBag->getRecurFrequencyInterval() . $propertyBag->getRecurFrequencyUnit(); | |
3a1f9fd3 | 76 | } |
6a488035 | 77 | // no translation in Dummy processor |
6a488035 TO |
78 | CRM_Utils_Hook::alterPaymentProcessorParams($this, |
79 | $params, | |
3bfefe56 | 80 | $propertyBag |
6a488035 | 81 | ); |
7758bd2b EM |
82 | // This means we can test failing transactions by setting a past year in expiry. A full expiry check would |
83 | // be more complete. | |
5a89b539 | 84 | if (!empty($params['credit_card_exp_date']['Y']) && date('Y') > |
7758bd2b | 85 | CRM_Core_Payment_Form::getCreditCardExpirationYear($params)) { |
90530b87 | 86 | throw new PaymentProcessorException(ts('Invalid expiry date')); |
7758bd2b | 87 | } |
f8fe0df6 | 88 | //end of hook invocation |
89 | if (!empty($this->_doDirectPaymentResult)) { | |
9d0f10b7 | 90 | $result = $this->_doDirectPaymentResult; |
65f1a9a1 | 91 | if (CRM_Utils_Array::value('payment_status_id', $result) === 'failed') { |
92 | throw new PaymentProcessorException($result['message'] ?? 'failed'); | |
93 | } | |
9d0f10b7 EM |
94 | $result['trxn_id'] = array_shift($this->_doDirectPaymentResult['trxn_id']); |
95 | return $result; | |
f8fe0df6 | 96 | } |
5562e2e3 | 97 | |
98 | $params['trxn_id'] = $this->getTrxnID();; | |
99 | ||
3a2251cf DG |
100 | // Add a fee_amount so we can make sure fees are handled properly in underlying classes. |
101 | $params['fee_amount'] = 1.50; | |
3209a807 | 102 | $params['description'] = $this->getPaymentDescription($params); |
3a2251cf | 103 | |
6a488035 TO |
104 | return $params; |
105 | } | |
106 | ||
6c786a9b | 107 | /** |
6bc775cf MD |
108 | * Does this payment processor support refund? |
109 | * | |
110 | * @return bool | |
111 | */ | |
112 | public function supportsRefund() { | |
113 | return TRUE; | |
114 | } | |
115 | ||
b59a905f | 116 | /** |
3bfefe56 | 117 | * Supports altering future start dates. |
118 | * | |
b59a905f SL |
119 | * @return bool |
120 | */ | |
121 | public function supportsFutureRecurStartDate() { | |
122 | return TRUE; | |
123 | } | |
124 | ||
6bc775cf MD |
125 | /** |
126 | * Submit a refund payment | |
127 | * | |
128 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
129 | * | |
130 | * @param array $params | |
131 | * Assoc array of input parameters for this transaction. | |
132 | */ | |
133 | public function doRefund(&$params) {} | |
134 | ||
6a488035 | 135 | /** |
fe482240 | 136 | * This function checks to see if we have the right config values. |
6a488035 | 137 | * |
a6c01b45 CW |
138 | * @return string |
139 | * the error message if any | |
6a488035 | 140 | */ |
00be9182 | 141 | public function checkConfig() { |
6a488035 TO |
142 | return NULL; |
143 | } | |
96025800 | 144 | |
10df056e | 145 | /** |
146 | * Get an array of the fields that can be edited on the recurring contribution. | |
147 | * | |
148 | * Some payment processors support editing the amount and other scheduling details of recurring payments, especially | |
149 | * those which use tokens. Others are fixed. This function allows the processor to return an array of the fields that | |
150 | * can be updated from the contribution recur edit screen. | |
151 | * | |
152 | * The fields are likely to be a subset of these | |
153 | * - 'amount', | |
154 | * - 'installments', | |
155 | * - 'frequency_interval', | |
156 | * - 'frequency_unit', | |
157 | * - 'cycle_day', | |
158 | * - 'next_sched_contribution_date', | |
159 | * - 'end_date', | |
160 | * - 'failure_retry_day', | |
161 | * | |
162 | * The form does not restrict which fields from the contribution_recur table can be added (although if the html_type | |
163 | * metadata is not defined in the xml for the field it will cause an error. | |
164 | * | |
165 | * Open question - would it make sense to return membership_id in this - which is sometimes editable and is on that | |
166 | * form (UpdateSubscription). | |
167 | * | |
168 | * @return array | |
169 | */ | |
170 | public function getEditableRecurringScheduleFields() { | |
be2fb01f | 171 | return ['amount', 'next_sched_contribution_date']; |
10df056e | 172 | } |
173 | ||
f926d56f | 174 | /** |
3bfefe56 | 175 | * Does this processor support cancelling recurring contributions through code. |
f926d56f | 176 | * |
3bfefe56 | 177 | * If the processor returns true it must be possible to take action from within CiviCRM |
178 | * that will result in no further payments being processed. In the case of token processors (e.g | |
179 | * IATS, eWay) updating the contribution_recur table is probably sufficient. | |
180 | * | |
181 | * @return bool | |
f926d56f | 182 | */ |
3bfefe56 | 183 | protected function supportsCancelRecurring() { |
f926d56f MWMC |
184 | return TRUE; |
185 | } | |
186 | ||
3bfefe56 | 187 | /** |
188 | * Cancel a recurring subscription. | |
189 | * | |
190 | * Payment processor classes should override this rather than implementing cancelSubscription. | |
191 | * | |
192 | * A PaymentProcessorException should be thrown if the update of the contribution_recur | |
193 | * record should not proceed (in many cases this function does nothing | |
194 | * as the payment processor does not need to take any action & this should silently | |
195 | * proceed. Note the form layer will only call this after calling | |
196 | * $processor->supports('cancelRecurring'); | |
197 | * | |
198 | * @param \Civi\Payment\PropertyBag $propertyBag | |
199 | * | |
200 | * @return array | |
201 | * | |
202 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
203 | */ | |
204 | public function doCancelRecurring(PropertyBag $propertyBag) { | |
205 | return ['message' => ts('Recurring contribution cancelled')]; | |
206 | } | |
207 | ||
5562e2e3 | 208 | /** |
209 | * Get a value for the transaction ID. | |
210 | * | |
211 | * Value is made up of the max existing value + a random string. | |
212 | * | |
213 | * Note the random string is likely a historical workaround. | |
214 | * | |
215 | * @return string | |
216 | */ | |
217 | protected function getTrxnID() { | |
218 | $string = $this->_mode; | |
219 | $trxn_id = CRM_Core_DAO::singleValueQuery("SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE '{$string}_%'"); | |
220 | $trxn_id = str_replace($string, '', $trxn_id); | |
221 | $trxn_id = (int) $trxn_id + 1; | |
222 | return $string . '_' . $trxn_id . '_' . uniqid(); | |
223 | } | |
224 | ||
6a488035 | 225 | } |