Merge pull request #4493 from eileenmcnaughton/CRM-15555
[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 /* NOTE:
18 * When looking up response codes in the Authorize.Net API, they
19 * begin at one, so always delete one from the "Position in Response"
20 */
21 class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
22 CONST CHARSET = 'iso-8859-1';
23
24 protected $_mode = NULL;
25
26 protected $_params = array();
27 protected $_doDirectPaymentResult = array();
28
29 /**
30 * @param array $doDirectPaymentResult
31 */
32 public function setDoDirectPaymentResult($doDirectPaymentResult) {
33 $this->_doDirectPaymentResult = $doDirectPaymentResult;
34 }
35
36 /**
37 * We only need one instance of this object. So we use the singleton
38 * pattern and cache the instance in this variable
39 *
40 * @var object
41 * @static
42 */
43 static private $_singleton = NULL;
44
45 /**
46 * Constructor
47 *
48 * @param string $mode the mode of operation: live or test
49 *
50 * @param $paymentProcessor
51 *
52 * @return \CRM_Core_Payment_Dummy
53 */
54 function __construct($mode, &$paymentProcessor) {
55 $this->_mode = $mode;
56 $this->_paymentProcessor = $paymentProcessor;
57 $this->_processorName = ts('Dummy Processor');
58 }
59
60 /**
61 * singleton function used to manage this object
62 *
63 * @param string $mode the mode of operation: live or test
64 *
65 * @param object $paymentProcessor
66 * @param null $paymentForm
67 * @param bool $force
68 *
69 * @return object
70 * @static
71 */
72 static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
73 $processorName = $paymentProcessor['name'];
74 if (CRM_Utils_Array::value($processorName, self::$_singleton) === NULL) {
75 self::$_singleton[$processorName] = new CRM_Core_Payment_Dummy($mode, $paymentProcessor);
76 }
77 return self::$_singleton[$processorName];
78 }
79
80 /**
81 * Submit a payment using Advanced Integration Method
82 *
83 * @param array $params assoc array of input parameters for this transaction
84 *
85 * @return array the result in a nice formatted array (or an error object)
86 * @public
87 */
88 function doDirectPayment(&$params) {
89 // Invoke hook_civicrm_paymentProcessor
90 // In Dummy's case, there is no translation of parameters into
91 // the back-end's canonical set of parameters. But if a processor
92 // does this, it needs to invoke this hook after it has done translation,
93 // but before it actually starts talking to its proprietary back-end.
94
95 // no translation in Dummy processor
96 $cookedParams = $params;
97 CRM_Utils_Hook::alterPaymentProcessorParams($this,
98 $params,
99 $cookedParams
100 );
101 //end of hook invocation
102 if (!empty($this->_doDirectPaymentResult)) {
103 return $this->_doDirectPaymentResult;
104 }
105 if ($this->_mode == 'test') {
106 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
107 $p = array();
108 $trxn_id = strval(CRM_Core_Dao::singleValueQuery($query, $p));
109 $trxn_id = str_replace('test_', '', $trxn_id);
110 $trxn_id = intval($trxn_id) + 1;
111 $params['trxn_id'] = sprintf('test_%08d', $trxn_id);
112 }
113 else {
114 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'live_%'";
115 $p = array();
116 $trxn_id = strval(CRM_Core_Dao::singleValueQuery($query, $p));
117 $trxn_id = str_replace('live_', '', $trxn_id);
118 $trxn_id = intval($trxn_id) + 1;
119 $params['trxn_id'] = sprintf('live_%08d', $trxn_id);
120 }
121 $params['gross_amount'] = $params['amount'];
122 return $params;
123 }
124
125 /**
126 * are back office payments supported - e.g paypal standard won't permit you to enter a credit card associated with someone else's login
127 * @return bool
128 */
129 protected function supportsLiveMode() {
130 return FALSE;
131 }
132
133 /**
134 * @param null $errorCode
135 * @param null $errorMessage
136 *
137 * @return object
138 */
139 function &error($errorCode = NULL, $errorMessage = NULL) {
140 $e = CRM_Core_Error::singleton();
141 if ($errorCode) {
142 $e->push($errorCode, 0, NULL, $errorMessage);
143 }
144 else {
145 $e->push(9001, 0, NULL, 'Unknown System Error.');
146 }
147 return $e;
148 }
149
150 /**
151 * This function checks to see if we have the right config values
152 *
153 * @return string the error message if any
154 * @public
155 */
156 function checkConfig() {
157 return NULL;
158 }
159 }
160