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>
16 use Civi\Payment\Exception\PaymentProcessorException
;
17 use Civi\Payment\PropertyBag
;
20 * Dummy payment processor
22 class CRM_Core_Payment_Dummy
extends CRM_Core_Payment
{
25 protected $_params = [];
26 protected $_doDirectPaymentResult = [];
29 * Set result from do Direct Payment for test purposes.
31 * @param array $doDirectPaymentResult
32 * Result to be returned from test.
34 public function setDoDirectPaymentResult($doDirectPaymentResult) {
35 $this->_doDirectPaymentResult
= $doDirectPaymentResult;
36 if (empty($this->_doDirectPaymentResult
['trxn_id'])) {
37 $this->_doDirectPaymentResult
['trxn_id'] = [];
40 $this->_doDirectPaymentResult
['trxn_id'] = (array) $doDirectPaymentResult['trxn_id'];
48 * The mode of operation: live or test.
50 * @param array $paymentProcessor
52 public function __construct($mode, &$paymentProcessor) {
54 $this->_paymentProcessor
= $paymentProcessor;
58 * Submit a payment using Advanced Integration Method.
60 * @param array $params
61 * Assoc array of input parameters for this transaction.
64 * the result in a nice formatted array (or an error object)
65 * @throws \Civi\Payment\Exception\PaymentProcessorException
67 public function doDirectPayment(&$params) {
68 $propertyBag = PropertyBag
::cast($params);
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.
74 if ($propertyBag->getIsRecur()) {
75 $throwAnENoticeIfNotSetAsTheseAreRequired = $propertyBag->getRecurFrequencyInterval() . $propertyBag->getRecurFrequencyUnit();
77 // no translation in Dummy processor
78 CRM_Utils_Hook
::alterPaymentProcessorParams($this,
82 // This means we can test failing transactions by setting a past year in expiry. A full expiry check would
84 if (!empty($params['credit_card_exp_date']['Y']) && date('Y') >
85 CRM_Core_Payment_Form
::getCreditCardExpirationYear($params)) {
86 throw new PaymentProcessorException(ts('Invalid expiry date'));
88 //end of hook invocation
89 if (!empty($this->_doDirectPaymentResult
)) {
90 $result = $this->_doDirectPaymentResult
;
91 if (CRM_Utils_Array
::value('payment_status_id', $result) === 'failed') {
92 throw new PaymentProcessorException($result['message'] ??
'failed');
94 $result['trxn_id'] = array_shift($this->_doDirectPaymentResult
['trxn_id']);
98 $params['trxn_id'] = $this->getTrxnID();;
100 $params['gross_amount'] = $propertyBag->getAmount();
101 // Add a fee_amount so we can make sure fees are handled properly in underlying classes.
102 $params['fee_amount'] = 1.50;
103 $params['description'] = $this->getPaymentDescription($params);
109 * Does this payment processor support refund?
113 public function supportsRefund() {
118 * Supports altering future start dates.
122 public function supportsFutureRecurStartDate() {
127 * Submit a refund payment
129 * @throws \Civi\Payment\Exception\PaymentProcessorException
131 * @param array $params
132 * Assoc array of input parameters for this transaction.
134 public function doRefund(&$params) {}
137 * This function checks to see if we have the right config values.
140 * the error message if any
142 public function checkConfig() {
147 * Get an array of the fields that can be edited on the recurring contribution.
149 * Some payment processors support editing the amount and other scheduling details of recurring payments, especially
150 * those which use tokens. Others are fixed. This function allows the processor to return an array of the fields that
151 * can be updated from the contribution recur edit screen.
153 * The fields are likely to be a subset of these
156 * - 'frequency_interval',
157 * - 'frequency_unit',
159 * - 'next_sched_contribution_date',
161 * - 'failure_retry_day',
163 * The form does not restrict which fields from the contribution_recur table can be added (although if the html_type
164 * metadata is not defined in the xml for the field it will cause an error.
166 * Open question - would it make sense to return membership_id in this - which is sometimes editable and is on that
167 * form (UpdateSubscription).
171 public function getEditableRecurringScheduleFields() {
172 return ['amount', 'next_sched_contribution_date'];
176 * Does this processor support cancelling recurring contributions through code.
178 * If the processor returns true it must be possible to take action from within CiviCRM
179 * that will result in no further payments being processed. In the case of token processors (e.g
180 * IATS, eWay) updating the contribution_recur table is probably sufficient.
184 protected function supportsCancelRecurring() {
189 * Cancel a recurring subscription.
191 * Payment processor classes should override this rather than implementing cancelSubscription.
193 * A PaymentProcessorException should be thrown if the update of the contribution_recur
194 * record should not proceed (in many cases this function does nothing
195 * as the payment processor does not need to take any action & this should silently
196 * proceed. Note the form layer will only call this after calling
197 * $processor->supports('cancelRecurring');
199 * @param \Civi\Payment\PropertyBag $propertyBag
203 * @throws \Civi\Payment\Exception\PaymentProcessorException
205 public function doCancelRecurring(PropertyBag
$propertyBag) {
206 return ['message' => ts('Recurring contribution cancelled')];
210 * Get a value for the transaction ID.
212 * Value is made up of the max existing value + a random string.
214 * Note the random string is likely a historical workaround.
218 protected function getTrxnID() {
219 $string = $this->_mode
;
220 $trxn_id = CRM_Core_DAO
::singleValueQuery("SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE '{$string}_%'");
221 $trxn_id = str_replace($string, '', $trxn_id);
222 $trxn_id = (int) $trxn_id +
1;
223 return $string . '_' . $trxn_id . '_' . uniqid();