4 * Licensed to CiviCRM under the Academic Free License version 3.0.
6 * Written and contributed by Ideal Solution, LLC (http://www.idealso.com)
13 * @author Marshal Newrock <marshal@idealso.com>
14 * $Id: Dummy.php 45429 2013-02-06 22:11:18Z lobo $
17 use Civi\Payment\Exception\PaymentProcessorException
;
20 * Dummy payment processor
22 class CRM_Core_Payment_Dummy
extends CRM_Core_Payment
{
23 const CHARSET
= 'iso-8859-1';
25 protected $_mode = NULL;
27 protected $_params = [];
28 protected $_doDirectPaymentResult = [];
31 * Set result from do Direct Payment for test purposes.
33 * @param array $doDirectPaymentResult
34 * Result to be returned from test.
36 public function setDoDirectPaymentResult($doDirectPaymentResult) {
37 $this->_doDirectPaymentResult
= $doDirectPaymentResult;
38 if (empty($this->_doDirectPaymentResult
['trxn_id'])) {
39 $this->_doDirectPaymentResult
['trxn_id'] = [];
42 $this->_doDirectPaymentResult
['trxn_id'] = (array) $doDirectPaymentResult['trxn_id'];
47 * We only need one instance of this object. So we use the singleton
48 * pattern and cache the instance in this variable
52 static private $_singleton = NULL;
58 * The mode of operation: live or test.
60 * @param $paymentProcessor
62 * @return \CRM_Core_Payment_Dummy
64 public function __construct($mode, &$paymentProcessor) {
66 $this->_paymentProcessor
= $paymentProcessor;
67 $this->_processorName
= ts('Dummy Processor');
71 * Submit a payment using Advanced Integration Method.
73 * @param array $params
74 * Assoc array of input parameters for this transaction.
77 * the result in a nice formatted array (or an error object)
78 * @throws \Civi\Payment\Exception\PaymentProcessorException
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'];
89 // no translation in Dummy processor
90 $cookedParams = $params;
91 CRM_Utils_Hook
::alterPaymentProcessorParams($this,
95 // This means we can test failing transactions by setting a past year in expiry. A full expiry check would
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'));
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');
107 $result['trxn_id'] = array_shift($this->_doDirectPaymentResult
['trxn_id']);
110 if ($this->_mode
== 'test') {
111 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
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();
119 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'live_%'";
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();
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);
136 * Does this payment processor support refund?
140 public function supportsRefund() {
145 * Supports altering future start dates
148 public function supportsFutureRecurStartDate() {
153 * Submit a refund payment
155 * @throws \Civi\Payment\Exception\PaymentProcessorException
157 * @param array $params
158 * Assoc array of input parameters for this transaction.
160 public function doRefund(&$params) {}
163 * Generate error object.
165 * Throwing exceptions is preferred over this.
167 * @param string $errorCode
168 * @param string $errorMessage
170 * @return CRM_Core_Error
173 public function &error($errorCode = NULL, $errorMessage = NULL) {
174 $e = CRM_Core_Error
::singleton();
176 $e->push($errorCode, 0, NULL, $errorMessage);
179 $e->push(9001, 0, NULL, 'Unknown System Error.');
185 * This function checks to see if we have the right config values.
188 * the error message if any
190 public function checkConfig() {
195 * Get an array of the fields that can be edited on the recurring contribution.
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.
201 * The fields are likely to be a subset of these
204 * - 'frequency_interval',
205 * - 'frequency_unit',
207 * - 'next_sched_contribution_date',
209 * - 'failure_retry_day',
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.
214 * Open question - would it make sense to return membership_id in this - which is sometimes editable and is on that
215 * form (UpdateSubscription).
219 public function getEditableRecurringScheduleFields() {
220 return ['amount', 'next_sched_contribution_date'];
224 * @param string $message
225 * @param array $params
227 * @return bool|object
229 public function cancelSubscription(&$message = '', $params = []) {