3 +--------------------------------------------------------------------+
4 | FirstData Core Payment Module for CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Licensed to CiviCRM under the Academic Free License version 3.0 |
8 | Written & Contributed by Eileen McNaughton - Nov March 2008 |
9 +--------------------------------------------------------------------+
10 | This processor is based heavily on the Eway processor by Peter |
14 +--------------------------------------------------------------------+
18 * Note that in order to use FirstData / LinkPoint you need a certificate (.pem) file issued by them
19 * and a store number. You can configure the path to the certificate and the store number
20 * through the front end of civiCRM. The path is as seen by the server not the url
21 * -----------------------------------------------------------------------------------------------
22 * The basic functionality of this processor is that variables from the $params object are transformed
23 * into xml using a function provided by the processor. The xml is submitted to the processor's https site
24 * using curl and the response is translated back into an array using the processor's function.
26 * If an array ($params) is returned to the calling function it is treated as a success and the values from
27 * the array are merged into the calling functions array.
29 * If an result of class error is returned it is treated as a failure
31 * -----------------------------------------------------------------------------------------------
35 * From Payment processor documentation
36 * For testing purposes, you can use any of the card numbers listed below. The test card numbers
37 * will not result in any charges to the card. Use these card numbers with any expiration date in the
39 * Visa Level 2 - 4275330012345675 (replies with a referral message)
40 * JCB - 3566007770003510
41 * Discover - 6011000993010978
42 * MasterCard - 5424180279791765
43 * Visa - 4005550000000019 or 4111111111111111
44 * MasterCard Level 2 - 5404980000008386
45 * Diners - 36555565010005
46 * Amex - 372700997251009
48 * **************************
49 * Lines starting with CRM_Core_Error::debug_log_message output messages to files/upload/civicrm.log - you may with to comment them out once it is working satisfactorily
51 * For live testing uncomment the result field below and set the value to the response you wish to get from the payment processor
52 * **************************
54 class CRM_Core_Payment_FirstData
extends CRM_Core_Payment
{
55 // (not used, implicit in the API, might need to convert?)
56 const CHARSET
= 'UFT-8';
62 * The mode of operation: live or test.
64 * @param $paymentProcessor
66 * @return \CRM_Core_Payment_FirstData *******************************************************
68 public function __construct($mode, &$paymentProcessor) {
71 $this->_paymentProcessor
= $paymentProcessor;
75 * Map fields from params array.
77 * This function is set up and put here to make the mapping of fields
78 * as visually clear as possible for easy editing
80 * Comment out irrelevant fields
82 * @param array $params
86 public function mapProcessorFieldstoParams($params) {
87 /*concatenate full customer name first - code from EWAY gateway
90 $credit_card_name = $params['first_name'] . " ";
91 if (strlen($params['middle_name']) > 0) {
92 $credit_card_name .= $params['middle_name'] . " ";
94 $credit_card_name .= $params['last_name'];
98 /**********************************************************
99 * Payment Processor field name **fields from $params array ***
100 *******************************************************************/
102 $requestFields['cardnumber'] = $params['credit_card_number'];
103 $requestFields['chargetotal'] = $params['amount'];
104 $requestFields['cardexpmonth'] = sprintf('%02d', (int) $params['month']);
105 $requestFields['cardexpyear'] = substr($params['year'], 2, 2);
106 $requestFields['cvmvalue'] = $params['cvv2'];
107 $requestFields['cvmindicator'] = "provided";
108 $requestFields['name'] = $credit_card_name;
109 $requestFields['address1'] = $params['street_address'];
110 $requestFields['city'] = $params['city'];
111 $requestFields['state'] = $params['state_province'];
112 $requestFields['zip'] = $params['postal_code'];
113 $requestFields['country'] = $params['country'];
114 $requestFields['email'] = $params['email'];
115 $requestFields['ip'] = $params['ip_address'];
116 $requestFields['transactionorigin'] = "Eci";
117 // 32 character string
118 $requestFields['invoice_number'] = $params['invoiceID'];
119 $requestFields['ordertype'] = 'Sale';
120 $requestFields['comments'] = $params['description'];
121 //**********************set 'result' for live testing **************************
122 // $requestFields[ 'result' ] = ""; #set to "Good", "Decline" or "Duplicate"
123 // $requestFields[ '' ] = $params[ 'qfKey' ];
124 // $requestFields[ '' ] = $params[ 'amount_other' ];
125 // $requestFields[ '' ] = $params[ 'billing_first_name' ];
126 // $requestFields[ '' ] = $params[ 'billing_middle_name' ];
127 // $requestFields[ '' ] = $params[ 'billing_last_name' ];
129 // $requestFields[ '' ] = $params[ 'contributionType_name' ];
130 // $requestFields[ '' ] = $params[ 'contributionPageID' ];
131 // $requestFields[ '' ] = $params[ 'contributionType_accounting_code' ];
132 // $requestFields[ '' ] = $params['amount_level' ];
133 // $requestFields[ '' ] = $params['credit_card_type' ];
134 // $requestFields[ 'addrnum' ] = numeric portion of street address - not yet implemented
135 // $requestFields[ 'taxexempt' ] taxexempt status (Y or N) - not implemented
137 return $requestFields;
141 * This function sends request and receives response from
144 * @param array $params
146 * @return array|object
149 public function doDirectPayment(&$params) {
150 if ($params['is_recur'] == TRUE) {
151 throw new CRM_Core_Exception(ts('First Data - recurring payments not implemented'));
154 if (!defined('CURLOPT_SSLCERT')) {
155 throw new CRM_Core_Exception(ts('%1 - Gateway requires curl with SSL support', [1 => $paymentProcessor]));
158 /**********************************************************
159 * Create the array of variables to be sent to the processor from the $params array
160 * passed into this function
161 **********************************************************/
162 $requestFields = self
::mapProcessorFieldstoParams($params);
164 /**********************************************************
165 * create FirstData request object
166 **********************************************************/
167 require_once 'FirstData/lphp.php';
170 /**********************************************************
171 * define variables for connecting with the gateway
172 **********************************************************/
174 // Name and location of certificate file
175 $key = $this->_paymentProcessor
['password'];
177 $requestFields["configfile"] = $this->_paymentProcessor
['user_name'];
179 $host = $this->_paymentProcessor
['url_site'] . ":" . $port . "/LSGSXML";
181 //----------------------------------------------------------------------------------------------------
182 // Check to see if we have a duplicate before we send
183 //----------------------------------------------------------------------------------------------------
184 if ($this->checkDupe($params['invoiceID'], CRM_Utils_Array
::value('contributionID', $params))) {
185 return self
::errorExit(9003, 'It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt from eWAY. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator.');
187 //----------------------------------------------------------------------------------------------------
188 // Convert to XML using function provided by payment processor
189 //----------------------------------------------------------------------------------------------------
190 $requestxml = lphp
::buildXML($requestFields);
192 /*----------------------------------------------------------------------------------------------------
193 // Send to the payment information using cURL
194 /----------------------------------------------------------------------------------------------------
197 $ch = curl_init($host);
199 return self
::errorExit(9004, 'Could not initiate connection to payment gateway');
202 curl_setopt($ch, CURLOPT_POST
, 1);
203 curl_setopt($ch, CURLOPT_POSTFIELDS
, $requestxml);
204 curl_setopt($ch, CURLOPT_SSLCERT
, $key);
205 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, Civi
::settings()->get('verifySSL') ?
2 : 0);
206 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, Civi
::settings()->get('verifySSL'));
207 // return the result on success, FALSE on failure
208 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
209 curl_setopt($ch, CURLOPT_TIMEOUT
, 36000);
210 // ensures any Location headers are followed
211 if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
212 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, 1);
215 // Send the data out over the wire
216 //--------------------------------
217 $responseData = curl_exec($ch);
219 //----------------------------------------------------------------------------------------------------
220 // See if we had a curl error - if so tell 'em and bail out
222 // NOTE: curl_error does not return a logical value (see its documentation), but
223 // a string, which is empty when there was no error.
224 //----------------------------------------------------------------------------------------------------
225 if ((curl_errno($ch) > 0) ||
(strlen(curl_error($ch)) > 0)) {
226 $errorNum = curl_errno($ch);
227 $errorDesc = curl_error($ch);
229 // Paranoia - in the unlikley event that 'curl' errno fails
230 if ($errorNum == 0) {
234 // Paranoia - in the unlikley event that 'curl' error fails
235 if (strlen($errorDesc) == 0) {
236 $errorDesc = "Connection to payment gateway failed";
238 if ($errorNum == 60) {
239 return self
::errorExit($errorNum, "Curl error - " . $errorDesc . " Try this link for more information http://curl.haxx.se/docs/sslcerts.html");
242 return self
::errorExit($errorNum, "Curl error - " . $errorDesc . " your key is located at " . $key . " the url is " . $host . " xml is " . $requestxml . " processor response = " . $processorResponse);
245 //----------------------------------------------------------------------------------------------------
246 // If null data returned - tell 'em and bail out
248 // NOTE: You will not necessarily get a string back, if the request failed for
249 // any reason, the return value will be the boolean false.
250 //----------------------------------------------------------------------------------------------------
251 if (($responseData === FALSE) ||
(strlen($responseData) == 0)) {
252 return self
::errorExit(9006, "Error: Connection to payment gateway failed - no data returned.");
255 //----------------------------------------------------------------------------------------------------
256 // If gateway returned no data - tell 'em and bail out
257 //----------------------------------------------------------------------------------------------------
258 if (empty($responseData)) {
259 return self
::errorExit(9007, "Error: No data returned from payment gateway.");
262 //----------------------------------------------------------------------------------------------------
263 // Success so far - close the curl and check the data
264 //----------------------------------------------------------------------------------------------------
267 //----------------------------------------------------------------------------------------------------
268 // Payment successfully sent to gateway - process the response now
269 //----------------------------------------------------------------------------------------------------
271 $processorResponse = lphp
::decodeXML($responseData);
273 // transaction failed, print the reason
274 if ($processorResponse["r_approved"] != "APPROVED") {
275 return self
::errorExit(9009, "Error: [" . $processorResponse['r_error'] . "] - from payment processor");
279 //-----------------------------------------------------------------------------------------------------
280 // Cross-Check - the unique 'TrxnReference' we sent out should match the just received 'TrxnReference'
282 // this section not used as the processor doesn't appear to pass back our invoice no. Code in eWay model if
284 //-----------------------------------------------------------------------------------------------------
289 $params['trxn_result_code'] = $processorResponse['r_message'];
290 $params['trxn_id'] = $processorResponse['r_ref'];
291 CRM_Core_Error
::debug_log_message("r_authresponse " . $processorResponse['r_authresponse']);
292 CRM_Core_Error
::debug_log_message("r_code " . $processorResponse['r_code']);
293 CRM_Core_Error
::debug_log_message("r_tdate " . $processorResponse['r_tdate']);
294 CRM_Core_Error
::debug_log_message("r_avs " . $processorResponse['r_avs']);
295 CRM_Core_Error
::debug_log_message("r_ordernum " . $processorResponse['r_ordernum']);
296 CRM_Core_Error
::debug_log_message("r_error " . $processorResponse['r_error']);
297 CRM_Core_Error
::debug_log_message("csp " . $processorResponse['r_csp']);
298 CRM_Core_Error
::debug_log_message("r_message " . $processorResponse['r_message']);
299 CRM_Core_Error
::debug_log_message("r_ref " . $processorResponse['r_ref']);
300 CRM_Core_Error
::debug_log_message("r_time " . $processorResponse['r_time']);
305 // end function doDirectPayment
308 * Produces error message and returns from class.
310 * @param int $errorCode
311 * @param string $errorMessage
315 public function &errorExit($errorCode = NULL, $errorMessage = NULL) {
316 $e = CRM_Core_Error
::singleton();
319 $e->push($errorCode, 0, NULL, $errorMessage);
322 $e->push(9000, 0, NULL, 'Unknown System Error.');
328 * This public function checks to see if we have the right processor config values set.
330 * NOTE: Called by Events and Contribute to check config params are set prior to trying
331 * register any credit card details
333 * @return null|string
334 * @internal param string $mode the mode we are operating in (live or test) - not used
336 * returns string $errorMsg if any errors found - null if OK
338 * function checkConfig( $mode ) CiviCRM V1.9 Declaration
339 * CiviCRM V2.0 Declaration
341 public function checkConfig() {
344 if (empty($this->_paymentProcessor
['user_name'])) {
345 $errorMsg[] = ts(' Store Name is not set for this payment processor');
348 if (empty($this->_paymentProcessor
['url_site'])) {
349 $errorMsg[] = ts(' URL is not set for this payment processor');
352 if (!empty($errorMsg)) {
353 return implode('<p>', $errorMsg);
361 // end class CRM_Core_Payment_FirstData