Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
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 | 39 | class 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 | /** | |
fe482240 | 53 | * Constructor. |
6a488035 | 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 | 69 | /** |
fe482240 | 70 | * This function checks to see if we have the right config values. |
b5c2afd0 EM |
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 | ||
be2fb01f | 80 | $error = []; |
6a488035 TO |
81 | |
82 | if (empty($this->_paymentProcessor['user_name'])) { | |
2e122327 | 83 | $error[] = ts('UserID is not set in the Administer » System Settings » 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 » System Settings » Payment Processors'); |
6a488035 TO |
88 | } |
89 | ||
90 | if (!empty($error)) { | |
91 | return implode('<p>', $error); | |
92 | } | |
93 | else { | |
94 | return NULL; | |
95 | } | |
96 | } | |
97 | ||
b5c2afd0 EM |
98 | /** |
99 | * This function collects all the information from a web/api form and invokes | |
100 | * the relevant payment processor specific functions to perform the transaction | |
101 | * | |
6a0b768e TO |
102 | * @param array $params |
103 | * Assoc array of input parameters for this transaction. | |
b5c2afd0 | 104 | */ |
00be9182 | 105 | public function doDirectPayment(&$params) { |
6a488035 TO |
106 | CRM_Core_Error::fatal(ts('This function is not implemented')); |
107 | } | |
108 | ||
109 | /** | |
fe482240 | 110 | * Main transaction function. |
6a488035 | 111 | * |
6a0b768e TO |
112 | * @param array $params |
113 | * Name value pair of contribution data. | |
6c8f6e67 EM |
114 | * |
115 | * @param $component | |
6a488035 | 116 | */ |
00be9182 | 117 | public function doTransferCheckout(&$params, $component) { |
6a488035 TO |
118 | $component = strtolower($component); |
119 | $config = CRM_Core_Config::singleton(); | |
120 | if ($component != 'contribute' && $component != 'event') { | |
121 | CRM_Core_Error::fatal(ts('Component is invalid')); | |
122 | } | |
123 | ||
9df3628e | 124 | $url = CRM_Utils_System::externUrl('extern/pxIPN'); |
6a488035 TO |
125 | |
126 | if ($component == 'event') { | |
127 | $cancelURL = CRM_Utils_System::url('civicrm/event/register', | |
128 | "_qf_Confirm_display=true&qfKey={$params['qfKey']}", | |
129 | FALSE, NULL, FALSE | |
130 | ); | |
131 | } | |
132 | elseif ($component == 'contribute') { | |
133 | $cancelURL = CRM_Utils_System::url('civicrm/contribute/transact', | |
134 | "_qf_Confirm_display=true&qfKey={$params['qfKey']}", | |
135 | FALSE, NULL, FALSE | |
136 | ); | |
137 | } | |
138 | ||
6a488035 | 139 | /* |
d424ffde CW |
140 | * Build the private data string to pass to DPS, which they will give back to us with the |
141 | * | |
142 | * transaction result. We are building this as a comma-separated list so as to avoid long URLs. | |
143 | * | |
144 | * Parameters passed: a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID | |
145 | */ | |
6a488035 TO |
146 | |
147 | $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}"; | |
148 | ||
149 | if ($component == 'event') { | |
2aa397bc | 150 | $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 27, 20), 0, 24); |
6a488035 TO |
151 | $privateData .= ",f={$params['participantID']},g={$params['eventID']}"; |
152 | } | |
153 | elseif ($component == 'contribute') { | |
9c1bc317 | 154 | $membershipID = $params['membershipID'] ?? NULL; |
6a488035 TO |
155 | if ($membershipID) { |
156 | $privateData .= ",e=$membershipID"; | |
157 | } | |
158 | $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 20, 20), 0, 24); | |
159 | ||
160 | } | |
161 | ||
be2fb01f | 162 | $dpsParams = [ |
6a488035 TO |
163 | 'AmountInput' => str_replace(",", "", number_format($params['amount'], 2)), |
164 | 'CurrencyInput' => $params['currencyID'], | |
165 | 'MerchantReference' => $merchantRef, | |
166 | 'TxnData1' => $params['qfKey'], | |
167 | 'TxnData2' => $privateData, | |
92fcb95f | 168 | 'TxnData3' => $component . "," . $this->_paymentProcessor['id'], |
6a488035 TO |
169 | 'TxnType' => 'Purchase', |
170 | // Leave this empty for now, causes an error with DPS if we populate it | |
171 | 'TxnId' => '', | |
172 | 'UrlFail' => $url, | |
173 | 'UrlSuccess' => $url, | |
be2fb01f | 174 | ]; |
6a488035 TO |
175 | // Allow further manipulation of params via custom hooks |
176 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $dpsParams); | |
177 | ||
178 | /* | |
d424ffde CW |
179 | * determine whether method is pxaccess or pxpay by whether signature (mac key) is defined |
180 | */ | |
6a488035 | 181 | |
6a488035 TO |
182 | if (empty($this->_paymentProcessor['signature'])) { |
183 | /* | |
184 | * Processor is pxpay | |
185 | * | |
186 | * This contains the XML/Curl functions we'll need to generate the XML request | |
187 | */ | |
188 | ||
189 | $dpsParams['PxPayUserId'] = $this->_paymentProcessor['user_name']; | |
190 | $dpsParams['PxPayKey'] = $this->_paymentProcessor['password']; | |
191 | // Build a valid XML string to pass to DPS | |
192 | $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml($dpsParams); | |
193 | ||
194 | $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml('GenerateRequest', $generateRequest); | |
195 | // Get the special validated URL back from DPS by sending them the XML we've generated | |
196 | $curl = CRM_Core_Payment_PaymentExpressUtils::_initCURL($generateRequest, $this->_paymentProcessor['url_site']); | |
197 | $success = FALSE; | |
198 | ||
199 | if ($response = curl_exec($curl)) { | |
200 | curl_close($curl); | |
201 | $valid = CRM_Core_Payment_PaymentExpressUtils::_xmlAttribute($response, 'valid'); | |
202 | if (1 == $valid) { | |
203 | // the request was validated, so we'll get the URL and redirect to it | |
204 | $uri = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'URI'); | |
205 | CRM_Utils_System::redirect($uri); | |
206 | } | |
207 | else { | |
208 | // redisplay confirmation page | |
209 | CRM_Utils_System::redirect($cancelURL); | |
210 | } | |
211 | } | |
212 | else { | |
213 | // calling DPS failed | |
214 | CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway.')); | |
215 | } | |
216 | } | |
217 | else { | |
218 | $processortype = "pxaccess"; | |
2aa397bc | 219 | require_once 'PaymentExpress/pxaccess.inc.php'; |
6a488035 TO |
220 | // URL |
221 | $PxAccess_Url = $this->_paymentProcessor['url_site']; | |
222 | // User ID | |
223 | $PxAccess_Userid = $this->_paymentProcessor['user_name']; | |
224 | // Your DES Key from DPS | |
225 | $PxAccess_Key = $this->_paymentProcessor['password']; | |
226 | // Your MAC key from DPS | |
227 | $Mac_Key = $this->_paymentProcessor['signature']; | |
228 | ||
229 | $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key); | |
230 | $request = new PxPayRequest(); | |
231 | $request->setAmountInput($dpsParams['AmountInput']); | |
232 | $request->setTxnData1($dpsParams['TxnData1']); | |
233 | $request->setTxnData2($dpsParams['TxnData2']); | |
234 | $request->setTxnData3($dpsParams['TxnData3']); | |
235 | $request->setTxnType($dpsParams['TxnType']); | |
236 | $request->setInputCurrency($dpsParams['InputCurrency']); | |
237 | $request->setMerchantReference($dpsParams['MerchantReference']); | |
238 | $request->setUrlFail($dpsParams['UrlFail']); | |
239 | $request->setUrlSuccess($dpsParams['UrlSuccess']); | |
240 | $request_string = $pxaccess->makeRequest($request); | |
241 | CRM_Utils_System::redirect($request_string); | |
242 | } | |
243 | } | |
96025800 | 244 | |
6a488035 | 245 | } |