INFRA-132 - tests/- Fix misc oddball syntax
[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
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 */
21class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
7da04cde 22 const CHARSET = 'iso-8859-1';
6a488035
TO
23
24 protected $_mode = NULL;
25
26 protected $_params = array();
f8fe0df6 27 protected $_doDirectPaymentResult = array();
28
29 /**
30 * @param array $doDirectPaymentResult
31 */
32 public function setDoDirectPaymentResult($doDirectPaymentResult) {
33 $this->_doDirectPaymentResult = $doDirectPaymentResult;
34 }
6a488035
TO
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 *
6a0b768e
TO
48 * @param string $mode
49 * The mode of operation: live or test.
6a488035 50 *
77b97be7
EM
51 * @param $paymentProcessor
52 *
53 * @return \CRM_Core_Payment_Dummy
6a488035 54 */
00be9182 55 public function __construct($mode, &$paymentProcessor) {
6a488035
TO
56 $this->_mode = $mode;
57 $this->_paymentProcessor = $paymentProcessor;
58 $this->_processorName = ts('Dummy Processor');
59 }
60
61 /**
100fef9d 62 * Singleton function used to manage this object
6a488035 63 *
6a0b768e
TO
64 * @param string $mode
65 * The mode of operation: live or test.
6a488035 66 *
dd244018
EM
67 * @param object $paymentProcessor
68 * @param null $paymentForm
69 * @param bool $force
70 *
6a488035
TO
71 * @return object
72 * @static
6a488035 73 */
00be9182 74 public static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
52767de0
EM
75 if (!empty($paymentProcessor['id'])) {
76 $cacheKey = $paymentProcessor['id'];
6a488035 77 }
52767de0
EM
78 else {
79 //@todo eliminated instances of this in favour of id-specific instances.
80 $cacheKey = $mode . '_' . $paymentProcessor['name'];
81 }
82 if (CRM_Utils_Array::value($cacheKey, self::$_singleton) === NULL) {
83 self::$_singleton[$cacheKey] = new CRM_Core_Payment_Dummy($mode, $paymentProcessor);
84 }
85 return self::$_singleton[$cacheKey];
6a488035
TO
86 }
87
88 /**
89 * Submit a payment using Advanced Integration Method
90 *
6a0b768e
TO
91 * @param array $params
92 * Assoc array of input parameters for this transaction.
6a488035
TO
93 *
94 * @return array the result in a nice formatted array (or an error object)
6a488035 95 */
00be9182 96 public function doDirectPayment(&$params) {
6a488035
TO
97 // Invoke hook_civicrm_paymentProcessor
98 // In Dummy's case, there is no translation of parameters into
99 // the back-end's canonical set of parameters. But if a processor
100 // does this, it needs to invoke this hook after it has done translation,
101 // but before it actually starts talking to its proprietary back-end.
102
103 // no translation in Dummy processor
104 $cookedParams = $params;
105 CRM_Utils_Hook::alterPaymentProcessorParams($this,
106 $params,
107 $cookedParams
108 );
f8fe0df6 109 //end of hook invocation
110 if (!empty($this->_doDirectPaymentResult)) {
111 return $this->_doDirectPaymentResult;
112 }
6a488035
TO
113 if ($this->_mode == 'test') {
114 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
115 $p = array();
116 $trxn_id = strval(CRM_Core_Dao::singleValueQuery($query, $p));
117 $trxn_id = str_replace('test_', '', $trxn_id);
118 $trxn_id = intval($trxn_id) + 1;
119 $params['trxn_id'] = sprintf('test_%08d', $trxn_id);
120 }
121 else {
122 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'live_%'";
123 $p = array();
124 $trxn_id = strval(CRM_Core_Dao::singleValueQuery($query, $p));
125 $trxn_id = str_replace('live_', '', $trxn_id);
126 $trxn_id = intval($trxn_id) + 1;
127 $params['trxn_id'] = sprintf('live_%08d', $trxn_id);
128 }
129 $params['gross_amount'] = $params['amount'];
3a2251cf
DG
130 // Add a fee_amount so we can make sure fees are handled properly in underlying classes.
131 $params['fee_amount'] = 1.50;
132 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
133
6a488035
TO
134 return $params;
135 }
136
fbcb6fba 137 /**
100fef9d 138 * Are back office payments supported - e.g paypal standard won't permit you to enter a credit card associated with someone else's login
fbcb6fba
EM
139 * @return bool
140 */
d8ce0d68 141 protected function supportsLiveMode() {
fbcb6fba
EM
142 return FALSE;
143 }
144
6c786a9b
EM
145 /**
146 * @param null $errorCode
147 * @param null $errorMessage
148 *
149 * @return object
150 */
00be9182 151 public function &error($errorCode = NULL, $errorMessage = NULL) {
6a488035
TO
152 $e = CRM_Core_Error::singleton();
153 if ($errorCode) {
154 $e->push($errorCode, 0, NULL, $errorMessage);
155 }
156 else {
157 $e->push(9001, 0, NULL, 'Unknown System Error.');
158 }
159 return $e;
160 }
161
162 /**
163 * This function checks to see if we have the right config values
164 *
165 * @return string the error message if any
6a488035 166 */
00be9182 167 public function checkConfig() {
6a488035
TO
168 return NULL;
169 }
170}