Fix issues with retrieving supportsTestMode/supportsLiveMode for payment processors
[civicrm-core.git] / CRM / Core / Payment / Dummy.php
CommitLineData
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>
14 * $Id: Dummy.php 45429 2013-02-06 22:11:18Z lobo $
15 */
16
65f1a9a1 17use Civi\Payment\Exception\PaymentProcessorException;
18
c866eb5f
TO
19/**
20 * Dummy payment processor
6a488035
TO
21 */
22class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
7da04cde 23 const CHARSET = 'iso-8859-1';
6a488035
TO
24
25 protected $_mode = NULL;
26
be2fb01f
CW
27 protected $_params = [];
28 protected $_doDirectPaymentResult = [];
f8fe0df6 29
30 /**
eca28728
EM
31 * Set result from do Direct Payment for test purposes.
32 *
f8fe0df6 33 * @param array $doDirectPaymentResult
eca28728 34 * Result to be returned from test.
f8fe0df6 35 */
36 public function setDoDirectPaymentResult($doDirectPaymentResult) {
37 $this->_doDirectPaymentResult = $doDirectPaymentResult;
9d0f10b7 38 if (empty($this->_doDirectPaymentResult['trxn_id'])) {
be2fb01f 39 $this->_doDirectPaymentResult['trxn_id'] = [];
9d0f10b7
EM
40 }
41 else {
42 $this->_doDirectPaymentResult['trxn_id'] = (array) $doDirectPaymentResult['trxn_id'];
43 }
f8fe0df6 44 }
6a488035
TO
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
6a488035
TO
51 */
52 static private $_singleton = NULL;
53
54 /**
fe482240 55 * Constructor.
6a488035 56 *
6a0b768e
TO
57 * @param string $mode
58 * The mode of operation: live or test.
6a488035 59 *
77b97be7
EM
60 * @param $paymentProcessor
61 *
62 * @return \CRM_Core_Payment_Dummy
6a488035 63 */
00be9182 64 public function __construct($mode, &$paymentProcessor) {
6a488035
TO
65 $this->_mode = $mode;
66 $this->_paymentProcessor = $paymentProcessor;
67 $this->_processorName = ts('Dummy Processor');
68 }
69
6a488035 70 /**
fe482240 71 * Submit a payment using Advanced Integration Method.
6a488035 72 *
6a0b768e
TO
73 * @param array $params
74 * Assoc array of input parameters for this transaction.
6a488035 75 *
a6c01b45
CW
76 * @return array
77 * the result in a nice formatted array (or an error object)
65f1a9a1 78 * @throws \Civi\Payment\Exception\PaymentProcessorException
6a488035 79 */
00be9182 80 public function doDirectPayment(&$params) {
6a488035
TO
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.
3a1f9fd3 86 if (!empty($params['is_recur'])) {
87 $throwAnENoticeIfNotSetAsTheseAreRequired = $params['frequency_interval'] . $params['frequency_unit'];
88 }
6a488035
TO
89 // no translation in Dummy processor
90 $cookedParams = $params;
91 CRM_Utils_Hook::alterPaymentProcessorParams($this,
92 $params,
93 $cookedParams
94 );
7758bd2b
EM
95 // This means we can test failing transactions by setting a past year in expiry. A full expiry check would
96 // be more complete.
5a89b539 97 if (!empty($params['credit_card_exp_date']['Y']) && date('Y') >
7758bd2b 98 CRM_Core_Payment_Form::getCreditCardExpirationYear($params)) {
90530b87 99 throw new PaymentProcessorException(ts('Invalid expiry date'));
7758bd2b 100 }
f8fe0df6 101 //end of hook invocation
102 if (!empty($this->_doDirectPaymentResult)) {
9d0f10b7 103 $result = $this->_doDirectPaymentResult;
65f1a9a1 104 if (CRM_Utils_Array::value('payment_status_id', $result) === 'failed') {
105 throw new PaymentProcessorException($result['message'] ?? 'failed');
106 }
9d0f10b7
EM
107 $result['trxn_id'] = array_shift($this->_doDirectPaymentResult['trxn_id']);
108 return $result;
f8fe0df6 109 }
6a488035 110 if ($this->_mode == 'test') {
353ffa53 111 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
be2fb01f 112 $p = [];
46710ea6 113 $trxn_id = strval(CRM_Core_DAO::singleValueQuery($query, $p));
353ffa53
TO
114 $trxn_id = str_replace('test_', '', $trxn_id);
115 $trxn_id = intval($trxn_id) + 1;
24dc4331 116 $params['trxn_id'] = 'test_' . $trxn_id . '_' . uniqid();
6a488035
TO
117 }
118 else {
353ffa53 119 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'live_%'";
be2fb01f 120 $p = [];
46710ea6 121 $trxn_id = strval(CRM_Core_DAO::singleValueQuery($query, $p));
353ffa53
TO
122 $trxn_id = str_replace('live_', '', $trxn_id);
123 $trxn_id = intval($trxn_id) + 1;
24dc4331 124 $params['trxn_id'] = 'live_' . $trxn_id . '_' . uniqid();
6a488035
TO
125 }
126 $params['gross_amount'] = $params['amount'];
3a2251cf
DG
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'];
3209a807 130 $params['description'] = $this->getPaymentDescription($params);
3a2251cf 131
6a488035
TO
132 return $params;
133 }
134
6c786a9b 135 /**
6bc775cf
MD
136 * Does this payment processor support refund?
137 *
138 * @return bool
139 */
140 public function supportsRefund() {
141 return TRUE;
142 }
143
b59a905f
SL
144 /**
145 * Supports altering future start dates
146 * @return bool
147 */
148 public function supportsFutureRecurStartDate() {
149 return TRUE;
150 }
151
6bc775cf
MD
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 /**
eca28728
EM
163 * Generate error object.
164 *
165 * Throwing exceptions is preferred over this.
166 *
167 * @param string $errorCode
168 * @param string $errorMessage
6c786a9b 169 *
eca28728
EM
170 * @return CRM_Core_Error
171 * Error object.
6c786a9b 172 */
00be9182 173 public function &error($errorCode = NULL, $errorMessage = NULL) {
6a488035
TO
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 /**
fe482240 185 * This function checks to see if we have the right config values.
6a488035 186 *
a6c01b45
CW
187 * @return string
188 * the error message if any
6a488035 189 */
00be9182 190 public function checkConfig() {
6a488035
TO
191 return NULL;
192 }
96025800 193
10df056e 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() {
be2fb01f 220 return ['amount', 'next_sched_contribution_date'];
10df056e 221 }
222
f926d56f
MWMC
223 /**
224 * @param string $message
225 * @param array $params
226 *
227 * @return bool|object
228 */
be2fb01f 229 public function cancelSubscription(&$message = '', $params = []) {
f926d56f
MWMC
230 return TRUE;
231 }
232
6a488035 233}