INFRA-132 - phpcbf Drupal.WhiteSpace.ScopeIndent.IncorrectExact
[civicrm-core.git] / CRM / Core / Payment / PaymentExpress.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035
TO
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 +--------------------------------------------------------------------+
e70a7fc0 24 */
6a488035
TO
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 */
4c6ce474
EM
35
36/**
37 * Class CRM_Core_Payment_PaymentExpress
38 */
6a488035 39class CRM_Core_Payment_PaymentExpress extends CRM_Core_Payment {
7da04cde 40 const CHARSET = 'iso-8859-1';
6a488035
TO
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
6a488035
TO
49 */
50 static private $_singleton = NULL;
51
52 /**
53 * Constructor
54 *
6a0b768e
TO
55 * @param string $mode
56 * The mode of operation: live or test.
6a488035 57 *
77b97be7
EM
58 * @param $paymentProcessor
59 *
60 * @return \CRM_Core_Payment_PaymentExpress
6a488035 61 */
00be9182 62 public function __construct($mode, &$paymentProcessor) {
6a488035
TO
63
64 $this->_mode = $mode;
65 $this->_paymentProcessor = $paymentProcessor;
66 $this->_processorName = ts('DPS Payment Express');
67 }
68
b5c2afd0
EM
69 /**
70 * This function checks to see if we have the right config values
71 *
72 * @internal param string $mode the mode we are operating in (live or test)
73 *
a6c01b45
CW
74 * @return string
75 * the error message if any
b5c2afd0 76 */
00be9182 77 public function checkConfig() {
6a488035
TO
78 $config = CRM_Core_Config::singleton();
79
80 $error = array();
81
82 if (empty($this->_paymentProcessor['user_name'])) {
2e122327 83 $error[] = ts('UserID is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
6a488035
TO
84 }
85
86 if (empty($this->_paymentProcessor['password'])) {
2e122327 87 $error[] = ts('pxAccess / pxPay Key is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
6a488035
TO
88 }
89
90 if (!empty($error)) {
91 return implode('<p>', $error);
92 }
93 else {
94 return NULL;
95 }
96 }
97
6c786a9b 98 /**
c490a46a 99 * @param array $params
6c786a9b
EM
100 *
101 * @throws Exception
102 */
00be9182 103 public function setExpressCheckOut(&$params) {
6a488035
TO
104 CRM_Core_Error::fatal(ts('This function is not implemented'));
105 }
106
6c786a9b
EM
107 /**
108 * @param $token
109 *
110 * @throws Exception
111 */
00be9182 112 public function getExpressCheckoutDetails($token) {
6a488035
TO
113 CRM_Core_Error::fatal(ts('This function is not implemented'));
114 }
115
6c786a9b 116 /**
c490a46a 117 * @param array $params
6c786a9b
EM
118 *
119 * @throws Exception
120 */
00be9182 121 public function doExpressCheckout(&$params) {
6a488035
TO
122 CRM_Core_Error::fatal(ts('This function is not implemented'));
123 }
124
b5c2afd0
EM
125 /**
126 * This function collects all the information from a web/api form and invokes
127 * the relevant payment processor specific functions to perform the transaction
128 *
6a0b768e
TO
129 * @param array $params
130 * Assoc array of input parameters for this transaction.
b5c2afd0 131 *
a6c01b45
CW
132 * @return array
133 * the result in an nice formatted array (or an error object)
b5c2afd0
EM
134 * @abstract
135 */
00be9182 136 public function doDirectPayment(&$params) {
6a488035
TO
137 CRM_Core_Error::fatal(ts('This function is not implemented'));
138 }
139
140 /**
141 * Main transaction function
142 *
6a0b768e
TO
143 * @param array $params
144 * Name value pair of contribution data.
6c8f6e67
EM
145 *
146 * @param $component
6a488035
TO
147 *
148 * @return void
6a488035 149 */
00be9182 150 public function doTransferCheckout(&$params, $component) {
6a488035
TO
151 $component = strtolower($component);
152 $config = CRM_Core_Config::singleton();
153 if ($component != 'contribute' && $component != 'event') {
154 CRM_Core_Error::fatal(ts('Component is invalid'));
155 }
156
157 $url = $config->userFrameworkResourceURL . "extern/pxIPN.php";
158
159 if ($component == 'event') {
160 $cancelURL = CRM_Utils_System::url('civicrm/event/register',
161 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
162 FALSE, NULL, FALSE
163 );
164 }
165 elseif ($component == 'contribute') {
166 $cancelURL = CRM_Utils_System::url('civicrm/contribute/transact',
167 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
168 FALSE, NULL, FALSE
169 );
170 }
171
6a488035 172 /*
d424ffde
CW
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 */
6a488035
TO
179
180 $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}";
181
182 if ($component == 'event') {
2aa397bc 183 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 27, 20), 0, 24);
6a488035
TO
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,
92fcb95f 201 'TxnData3' => $component . "," . $this->_paymentProcessor['id'],
6a488035
TO
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 /*
d424ffde
CW
212 * determine whether method is pxaccess or pxpay by whether signature (mac key) is defined
213 */
6a488035 214
6a488035
TO
215 if (empty($this->_paymentProcessor['signature'])) {
216 /*
217 * Processor is pxpay
218 *
219 * This contains the XML/Curl functions we'll need to generate the XML request
220 */
221
222 $dpsParams['PxPayUserId'] = $this->_paymentProcessor['user_name'];
223 $dpsParams['PxPayKey'] = $this->_paymentProcessor['password'];
224 // Build a valid XML string to pass to DPS
225 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml($dpsParams);
226
227 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml('GenerateRequest', $generateRequest);
228 // Get the special validated URL back from DPS by sending them the XML we've generated
229 $curl = CRM_Core_Payment_PaymentExpressUtils::_initCURL($generateRequest, $this->_paymentProcessor['url_site']);
230 $success = FALSE;
231
232 if ($response = curl_exec($curl)) {
233 curl_close($curl);
234 $valid = CRM_Core_Payment_PaymentExpressUtils::_xmlAttribute($response, 'valid');
235 if (1 == $valid) {
236 // the request was validated, so we'll get the URL and redirect to it
237 $uri = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'URI');
238 CRM_Utils_System::redirect($uri);
239 }
240 else {
241 // redisplay confirmation page
242 CRM_Utils_System::redirect($cancelURL);
243 }
244 }
245 else {
246 // calling DPS failed
247 CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway.'));
248 }
249 }
250 else {
251 $processortype = "pxaccess";
2aa397bc 252 require_once 'PaymentExpress/pxaccess.inc.php';
6a488035
TO
253 // URL
254 $PxAccess_Url = $this->_paymentProcessor['url_site'];
255 // User ID
256 $PxAccess_Userid = $this->_paymentProcessor['user_name'];
257 // Your DES Key from DPS
258 $PxAccess_Key = $this->_paymentProcessor['password'];
259 // Your MAC key from DPS
260 $Mac_Key = $this->_paymentProcessor['signature'];
261
262 $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key);
263 $request = new PxPayRequest();
264 $request->setAmountInput($dpsParams['AmountInput']);
265 $request->setTxnData1($dpsParams['TxnData1']);
266 $request->setTxnData2($dpsParams['TxnData2']);
267 $request->setTxnData3($dpsParams['TxnData3']);
268 $request->setTxnType($dpsParams['TxnType']);
269 $request->setInputCurrency($dpsParams['InputCurrency']);
270 $request->setMerchantReference($dpsParams['MerchantReference']);
271 $request->setUrlFail($dpsParams['UrlFail']);
272 $request->setUrlSuccess($dpsParams['UrlSuccess']);
273 $request_string = $pxaccess->makeRequest($request);
274 CRM_Utils_System::redirect($request_string);
275 }
276 }
96025800 277
6a488035 278}