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