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