Merge branch '4.4' of https://github.com/civicrm/civicrm-core into 4.5
[civicrm-core.git] / CRM / Core / Payment / PaymentExpress.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 the mode of operation: live or test
57 *
58 * @param $paymentProcessor
59 *
60 * @return \CRM_Core_Payment_PaymentExpress
61 */
62 function __construct($mode, &$paymentProcessor) {
63
64 $this->_mode = $mode;
65 $this->_paymentProcessor = $paymentProcessor;
66 $this->_processorName = ts('DPS Payment Express');
67 }
68
69 /**
70 * singleton function used to manage this object
71 *
72 * @param string $mode the mode of operation: live or test
73 *
74 * @param object $paymentProcessor
75 * @param null $paymentForm
76 * @param bool $force
77 *
78 * @return object
79 * @static
80 */
81 static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
82 $processorName = $paymentProcessor['name'];
83 if (self::$_singleton[$processorName] === NULL) {
84 self::$_singleton[$processorName] = new CRM_Core_Payment_PaymentExpress($mode, $paymentProcessor);
85 }
86 return self::$_singleton[$processorName];
87 }
88
89 /**
90 * This function checks to see if we have the right config values
91 *
92 * @internal param string $mode the mode we are operating in (live or test)
93 *
94 * @return string the error message if any
95 * @public
96 */
97 function checkConfig() {
98 $config = CRM_Core_Config::singleton();
99
100 $error = array();
101
102 if (empty($this->_paymentProcessor['user_name'])) {
103 $error[] = ts('UserID is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
104 }
105
106 if (empty($this->_paymentProcessor['password'])) {
107 $error[] = ts('pxAccess / pxPay Key is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
108 }
109
110 if (!empty($error)) {
111 return implode('<p>', $error);
112 }
113 else {
114 return NULL;
115 }
116 }
117
118 /**
119 * @param $params
120 *
121 * @throws Exception
122 */
123 function setExpressCheckOut(&$params) {
124 CRM_Core_Error::fatal(ts('This function is not implemented'));
125 }
126
127 /**
128 * @param $token
129 *
130 * @throws Exception
131 */
132 function getExpressCheckoutDetails($token) {
133 CRM_Core_Error::fatal(ts('This function is not implemented'));
134 }
135
136 /**
137 * @param $params
138 *
139 * @throws Exception
140 */
141 function doExpressCheckout(&$params) {
142 CRM_Core_Error::fatal(ts('This function is not implemented'));
143 }
144
145 /**
146 * This function collects all the information from a web/api form and invokes
147 * the relevant payment processor specific functions to perform the transaction
148 *
149 * @param array $params assoc array of input parameters for this transaction
150 *
151 * @return array the result in an nice formatted array (or an error object)
152 * @abstract
153 */
154 function doDirectPayment(&$params) {
155 CRM_Core_Error::fatal(ts('This function is not implemented'));
156 }
157
158 /**
159 * Main transaction function
160 *
161 * @param array $params name value pair of contribution data
162 *
163 * @param $component
164 *
165 * @return void
166 * @access public
167 */
168 function doTransferCheckout(&$params, $component) {
169 $component = strtolower($component);
170 $config = CRM_Core_Config::singleton();
171 if ($component != 'contribute' && $component != 'event') {
172 CRM_Core_Error::fatal(ts('Component is invalid'));
173 }
174
175 $url = $config->userFrameworkResourceURL . "extern/pxIPN.php";
176
177 if ($component == 'event') {
178 $cancelURL = CRM_Utils_System::url('civicrm/event/register',
179 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
180 FALSE, NULL, FALSE
181 );
182 }
183 elseif ($component == 'contribute') {
184 $cancelURL = CRM_Utils_System::url('civicrm/contribute/transact',
185 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
186 FALSE, NULL, FALSE
187 );
188 }
189
190
191 /*
192 * Build the private data string to pass to DPS, which they will give back to us with the
193 *
194 * transaction result. We are building this as a comma-separated list so as to avoid long URLs.
195 *
196 * Parameters passed: a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID
197 */
198
199 $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}";
200
201 if ($component == 'event') {
202 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 27, 20), 0, 24);
203 $privateData .= ",f={$params['participantID']},g={$params['eventID']}";
204 }
205 elseif ($component == 'contribute') {
206 $membershipID = CRM_Utils_Array::value('membershipID', $params);
207 if ($membershipID) {
208 $privateData .= ",e=$membershipID";
209 }
210 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 20, 20), 0, 24);
211
212 }
213
214 $dpsParams = array(
215 'AmountInput' => str_replace(",", "", number_format($params['amount'], 2)),
216 'CurrencyInput' => $params['currencyID'],
217 'MerchantReference' => $merchantRef,
218 'TxnData1' => $params['qfKey'],
219 'TxnData2' => $privateData,
220 'TxnData3' => $component . ",".$this->_paymentProcessor['id'],
221 'TxnType' => 'Purchase',
222 // Leave this empty for now, causes an error with DPS if we populate it
223 'TxnId' => '',
224 'UrlFail' => $url,
225 'UrlSuccess' => $url,
226 );
227 // Allow further manipulation of params via custom hooks
228 CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $dpsParams);
229
230 /*
231 * determine whether method is pxaccess or pxpay by whether signature (mac key) is defined
232 */
233
234
235 if (empty($this->_paymentProcessor['signature'])) {
236 /*
237 * Processor is pxpay
238 *
239 * This contains the XML/Curl functions we'll need to generate the XML request
240 */
241
242 $dpsParams['PxPayUserId'] = $this->_paymentProcessor['user_name'];
243 $dpsParams['PxPayKey'] = $this->_paymentProcessor['password'];
244 // Build a valid XML string to pass to DPS
245 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml($dpsParams);
246
247 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml('GenerateRequest', $generateRequest);
248 // Get the special validated URL back from DPS by sending them the XML we've generated
249 $curl = CRM_Core_Payment_PaymentExpressUtils::_initCURL($generateRequest, $this->_paymentProcessor['url_site']);
250 $success = FALSE;
251
252 if ($response = curl_exec($curl)) {
253 curl_close($curl);
254 $valid = CRM_Core_Payment_PaymentExpressUtils::_xmlAttribute($response, 'valid');
255 if (1 == $valid) {
256 // the request was validated, so we'll get the URL and redirect to it
257 $uri = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'URI');
258 CRM_Utils_System::redirect($uri);
259 }
260 else {
261 // redisplay confirmation page
262 CRM_Utils_System::redirect($cancelURL);
263 }
264 }
265 else {
266 // calling DPS failed
267 CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway.'));
268 }
269 }
270 else {
271 $processortype = "pxaccess";
272 require_once ('PaymentExpress/pxaccess.inc.php');
273 // URL
274 $PxAccess_Url = $this->_paymentProcessor['url_site'];
275 // User ID
276 $PxAccess_Userid = $this->_paymentProcessor['user_name'];
277 // Your DES Key from DPS
278 $PxAccess_Key = $this->_paymentProcessor['password'];
279 // Your MAC key from DPS
280 $Mac_Key = $this->_paymentProcessor['signature'];
281
282 $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key);
283 $request = new PxPayRequest();
284 $request->setAmountInput($dpsParams['AmountInput']);
285 $request->setTxnData1($dpsParams['TxnData1']);
286 $request->setTxnData2($dpsParams['TxnData2']);
287 $request->setTxnData3($dpsParams['TxnData3']);
288 $request->setTxnType($dpsParams['TxnType']);
289 $request->setInputCurrency($dpsParams['InputCurrency']);
290 $request->setMerchantReference($dpsParams['MerchantReference']);
291 $request->setUrlFail($dpsParams['UrlFail']);
292 $request->setUrlSuccess($dpsParams['UrlSuccess']);
293 $request_string = $pxaccess->makeRequest($request);
294 CRM_Utils_System::redirect($request_string);
295 }
296 }
297 }
298