INFRA-132 - CRM/Core - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Core / Payment / PaymentExpress.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | This file is a part of CiviCRM. |
7 | |
8 | CiviCRM is free software; you can copy, modify, and distribute it |
9 | under the terms of the GNU Affero General Public License |
10 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
11 | |
12 | CiviCRM is distributed in the hope that it will be useful, but |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15 | See the GNU Affero General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Affero General Public |
18 | License and the CiviCRM Licensing Exception along |
19 | with this program; if not, contact CiviCRM LLC |
20 | at info[AT]civicrm[DOT]org. If you have questions about the |
21 | GNU Affero General Public License or the licensing of CiviCRM, |
22 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
23 +--------------------------------------------------------------------+
24 */
25
26
27 /*
28 * PxPay Functionality Copyright (C) 2008 Lucas Baker, Logistic Information Systems Limited (Logis)
29 * PxAccess Functionality Copyright (C) 2008 Eileen McNaughton
30 * Licensed to CiviCRM under the Academic Free License version 3.0.
31 *
32 * Grateful acknowledgements go to Donald Lobo for invaluable assistance
33 * in creating this payment processor module
34 */
35
36 /**
37 * Class CRM_Core_Payment_PaymentExpress
38 */
39 class CRM_Core_Payment_PaymentExpress extends CRM_Core_Payment {
40 const CHARSET = 'iso-8859-1';
41
42 protected $_mode = NULL;
43
44 /**
45 * We only need one instance of this object. So we use the singleton
46 * pattern and cache the instance in this variable
47 *
48 * @var object
49 * @static
50 */
51 static private $_singleton = NULL;
52
53 /**
54 * Constructor
55 *
56 * @param string $mode
57 * The mode of operation: live or test.
58 *
59 * @param $paymentProcessor
60 *
61 * @return \CRM_Core_Payment_PaymentExpress
62 */
63 public function __construct($mode, &$paymentProcessor) {
64
65 $this->_mode = $mode;
66 $this->_paymentProcessor = $paymentProcessor;
67 $this->_processorName = ts('DPS Payment Express');
68 }
69
70 /**
71 * Singleton function used to manage this object
72 *
73 * @param string $mode
74 * The mode of operation: live or test.
75 *
76 * @param object $paymentProcessor
77 * @param null $paymentForm
78 * @param bool $force
79 *
80 * @return object
81 * @static
82 */
83 public static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
84 if (!empty($paymentProcessor['id'])) {
85 $cacheKey = $paymentProcessor['id'];
86 }
87 else {
88 //@todo eliminated instances of this in favour of id-specific instances.
89 $cacheKey = $mode . '_' . $paymentProcessor['name'];
90 }
91 if (self::$_singleton[$cacheKey] === NULL) {
92 self::$_singleton[$cacheKey] = new CRM_Core_Payment_PaymentExpress($mode, $paymentProcessor);
93 }
94 return self::$_singleton[$cacheKey];
95 }
96
97 /**
98 * This function checks to see if we have the right config values
99 *
100 * @internal param string $mode the mode we are operating in (live or test)
101 *
102 * @return string the error message if any
103 */
104 public function checkConfig() {
105 $config = CRM_Core_Config::singleton();
106
107 $error = array();
108
109 if (empty($this->_paymentProcessor['user_name'])) {
110 $error[] = ts('UserID is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
111 }
112
113 if (empty($this->_paymentProcessor['password'])) {
114 $error[] = ts('pxAccess / pxPay Key is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
115 }
116
117 if (!empty($error)) {
118 return implode('<p>', $error);
119 }
120 else {
121 return NULL;
122 }
123 }
124
125 /**
126 * @param array $params
127 *
128 * @throws Exception
129 */
130 public function setExpressCheckOut(&$params) {
131 CRM_Core_Error::fatal(ts('This function is not implemented'));
132 }
133
134 /**
135 * @param $token
136 *
137 * @throws Exception
138 */
139 public function getExpressCheckoutDetails($token) {
140 CRM_Core_Error::fatal(ts('This function is not implemented'));
141 }
142
143 /**
144 * @param array $params
145 *
146 * @throws Exception
147 */
148 public function doExpressCheckout(&$params) {
149 CRM_Core_Error::fatal(ts('This function is not implemented'));
150 }
151
152 /**
153 * This function collects all the information from a web/api form and invokes
154 * the relevant payment processor specific functions to perform the transaction
155 *
156 * @param array $params
157 * Assoc array of input parameters for this transaction.
158 *
159 * @return array the result in an nice formatted array (or an error object)
160 * @abstract
161 */
162 public function doDirectPayment(&$params) {
163 CRM_Core_Error::fatal(ts('This function is not implemented'));
164 }
165
166 /**
167 * Main transaction function
168 *
169 * @param array $params
170 * Name value pair of contribution data.
171 *
172 * @param $component
173 *
174 * @return void
175 */
176 public function doTransferCheckout(&$params, $component) {
177 $component = strtolower($component);
178 $config = CRM_Core_Config::singleton();
179 if ($component != 'contribute' && $component != 'event') {
180 CRM_Core_Error::fatal(ts('Component is invalid'));
181 }
182
183 $url = $config->userFrameworkResourceURL . "extern/pxIPN.php";
184
185 if ($component == 'event') {
186 $cancelURL = CRM_Utils_System::url('civicrm/event/register',
187 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
188 FALSE, NULL, FALSE
189 );
190 }
191 elseif ($component == 'contribute') {
192 $cancelURL = CRM_Utils_System::url('civicrm/contribute/transact',
193 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
194 FALSE, NULL, FALSE
195 );
196 }
197
198
199 /*
200 * Build the private data string to pass to DPS, which they will give back to us with the
201 *
202 * transaction result. We are building this as a comma-separated list so as to avoid long URLs.
203 *
204 * Parameters passed: a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID
205 */
206
207 $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}";
208
209 if ($component == 'event') {
210 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 27, 20), 0, 24);
211 $privateData .= ",f={$params['participantID']},g={$params['eventID']}";
212 }
213 elseif ($component == 'contribute') {
214 $membershipID = CRM_Utils_Array::value('membershipID', $params);
215 if ($membershipID) {
216 $privateData .= ",e=$membershipID";
217 }
218 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 20, 20), 0, 24);
219
220 }
221
222 $dpsParams = array(
223 'AmountInput' => str_replace(",", "", number_format($params['amount'], 2)),
224 'CurrencyInput' => $params['currencyID'],
225 'MerchantReference' => $merchantRef,
226 'TxnData1' => $params['qfKey'],
227 'TxnData2' => $privateData,
228 'TxnData3' => $component . ",".$this->_paymentProcessor['id'],
229 'TxnType' => 'Purchase',
230 // Leave this empty for now, causes an error with DPS if we populate it
231 'TxnId' => '',
232 'UrlFail' => $url,
233 'UrlSuccess' => $url,
234 );
235 // Allow further manipulation of params via custom hooks
236 CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $dpsParams);
237
238 /*
239 * determine whether method is pxaccess or pxpay by whether signature (mac key) is defined
240 */
241
242
243 if (empty($this->_paymentProcessor['signature'])) {
244 /*
245 * Processor is pxpay
246 *
247 * This contains the XML/Curl functions we'll need to generate the XML request
248 */
249
250 $dpsParams['PxPayUserId'] = $this->_paymentProcessor['user_name'];
251 $dpsParams['PxPayKey'] = $this->_paymentProcessor['password'];
252 // Build a valid XML string to pass to DPS
253 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml($dpsParams);
254
255 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml('GenerateRequest', $generateRequest);
256 // Get the special validated URL back from DPS by sending them the XML we've generated
257 $curl = CRM_Core_Payment_PaymentExpressUtils::_initCURL($generateRequest, $this->_paymentProcessor['url_site']);
258 $success = FALSE;
259
260 if ($response = curl_exec($curl)) {
261 curl_close($curl);
262 $valid = CRM_Core_Payment_PaymentExpressUtils::_xmlAttribute($response, 'valid');
263 if (1 == $valid) {
264 // the request was validated, so we'll get the URL and redirect to it
265 $uri = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'URI');
266 CRM_Utils_System::redirect($uri);
267 }
268 else {
269 // redisplay confirmation page
270 CRM_Utils_System::redirect($cancelURL);
271 }
272 }
273 else {
274 // calling DPS failed
275 CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway.'));
276 }
277 }
278 else {
279 $processortype = "pxaccess";
280 require_once ('PaymentExpress/pxaccess.inc.php');
281 // URL
282 $PxAccess_Url = $this->_paymentProcessor['url_site'];
283 // User ID
284 $PxAccess_Userid = $this->_paymentProcessor['user_name'];
285 // Your DES Key from DPS
286 $PxAccess_Key = $this->_paymentProcessor['password'];
287 // Your MAC key from DPS
288 $Mac_Key = $this->_paymentProcessor['signature'];
289
290 $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key);
291 $request = new PxPayRequest();
292 $request->setAmountInput($dpsParams['AmountInput']);
293 $request->setTxnData1($dpsParams['TxnData1']);
294 $request->setTxnData2($dpsParams['TxnData2']);
295 $request->setTxnData3($dpsParams['TxnData3']);
296 $request->setTxnType($dpsParams['TxnType']);
297 $request->setInputCurrency($dpsParams['InputCurrency']);
298 $request->setMerchantReference($dpsParams['MerchantReference']);
299 $request->setUrlFail($dpsParams['UrlFail']);
300 $request->setUrlSuccess($dpsParams['UrlSuccess']);
301 $request_string = $pxaccess->makeRequest($request);
302 CRM_Utils_System::redirect($request_string);
303 }
304 }
305 }