3 +----------------------------------------------------------------------------+
4 | Payflow Pro 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 - 2009 |
9 +---------------------------------------------------------------------------+
13 * Class CRM_Core_Payment_PayflowPro.
15 class CRM_Core_Payment_PayflowPro
extends CRM_Core_Payment
{
16 // (not used, implicit in the API, might need to convert?)
21 * We only need one instance of this object. So we use the singleton
22 * pattern and cache the instance in this variable
26 static private $_singleton = NULL;
32 * The mode of operation: live or test.
33 * @param $paymentProcessor
35 public function __construct($mode, &$paymentProcessor) {
38 $this->_paymentProcessor
= $paymentProcessor;
39 $this->_processorName
= ts('Payflow Pro');
43 * This function sends request and receives response from
44 * the processor. It is the main function for processing on-server
45 * credit card transactions
49 * This function collects all the information from a web/api form and invokes
50 * the relevant payment processor specific functions to perform the transaction
52 * @param array $params
53 * Assoc array of input parameters for this transaction.
56 * the result in an nice formatted array (or an error object)
59 public function doDirectPayment(&$params) {
60 if (!defined('CURLOPT_SSLCERT')) {
61 CRM_Core_Error
::fatal(ts('Payflow Pro requires curl with SSL support'));
65 * define variables for connecting with the gateway
68 // Are you using the Payflow Fraud Protection Service?
69 // Default is YES, change to NO or blank if not.
70 //This has not been investigated as part of writing this payment processor
72 //if you have not set up a separate user account the vendor name is used as the username
73 if (!$this->_paymentProcessor
['subject']) {
74 $user = $this->_paymentProcessor
['user_name'];
77 $user = $this->_paymentProcessor
['subject'];
80 // ideally this id would be passed through into this class as
81 // part of the paymentProcessor
82 //object with the other variables. It seems inefficient to re-query to get it.
83 //$params['processor_id'] = CRM_Core_DAO::getFieldValue(
84 // 'CRM_Contribute_DAO_ContributionP
85 //age',$params['contributionPageID'], 'payment_processor_id' );
88 *Create the array of variables to be sent to the processor from the $params array
89 * passed into this function
93 $payflow_query_array = [
95 'VENDOR' => $this->_paymentProcessor
['user_name'],
96 'PARTNER' => $this->_paymentProcessor
['signature'],
97 'PWD' => $this->_paymentProcessor
['password'],
98 // C - Direct Payment using credit card
100 // A - Authorization, S - Sale
102 'ACCT' => urlencode($params['credit_card_number']),
103 'CVV2' => $params['cvv2'],
104 'EXPDATE' => urlencode(sprintf('%02d', (int) $params['month']) . substr($params['year'], 2, 2)),
105 'ACCTTYPE' => urlencode($params['credit_card_type']),
106 'AMT' => urlencode($this->getAmount($params)),
107 'CURRENCY' => urlencode($params['currency']),
108 'FIRSTNAME' => $params['billing_first_name'],
110 'LASTNAME' => $params['billing_last_name'],
112 'STREET' => $params['street_address'],
113 'CITY' => urlencode($params['city']),
114 'STATE' => urlencode($params['state_province']),
115 'ZIP' => urlencode($params['postal_code']),
116 'COUNTRY' => urlencode($params['country']),
117 'EMAIL' => $params['email'],
118 'CUSTIP' => urlencode($params['ip_address']),
119 'COMMENT1' => urlencode($params['contributionType_accounting_code']),
121 'INVNUM' => urlencode($params['invoiceID']),
122 'ORDERDESC' => urlencode($params['description']),
123 'VERBOSITY' => 'MEDIUM',
124 'BILLTOCOUNTRY' => urlencode($params['country']),
127 if ($params['installments'] == 1) {
128 $params['is_recur'] = FALSE;
131 if ($params['is_recur'] == TRUE) {
133 $payflow_query_array['TRXTYPE'] = 'R';
134 $payflow_query_array['OPTIONALTRX'] = 'S';
135 $payflow_query_array['OPTIONALTRXAMT'] = $this->getAmount($params);
136 //Amount of the initial Transaction. Required
137 $payflow_query_array['ACTION'] = 'A';
138 //A for add recurring (M-modify,C-cancel,R-reactivate,I-inquiry,P-payment
139 $payflow_query_array['PROFILENAME'] = urlencode('RegularContribution');
140 //A for add recurring (M-modify,C-cancel,R-reactivate,I-inquiry,P-payment
141 if ($params['installments'] > 0) {
142 $payflow_query_array['TERM'] = $params['installments'] - 1;
143 //ie. in addition to the one happening with this transaction
145 // $payflow_query_array['COMPANYNAME']
146 // $payflow_query_array['DESC'] = not set yet Optional
147 // description of the goods or
148 //services being purchased.
149 //This parameter applies only for ACH_CCD accounts.
151 // $payflow_query_array['MAXFAILPAYMENTS'] = 0;
152 // number of payment periods (as s
153 //pecified by PAYPERIOD) for which the transaction is allowed
154 //to fail before PayPal cancels a profile. the default
155 // value of 0 (zero) specifies no
157 //attempts occur until the term is complete.
158 // $payflow_query_array['RETRYNUMDAYS'] = (not set as can't assume business rule
160 switch ($params['frequency_unit']) {
162 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d") +
7,
165 $params['end_date'] = mktime(0, 0, 0, date("m"), date("d") +
(7 * $payflow_query_array['TERM']),
168 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']);
169 $payflow_query_array['PAYPERIOD'] = "WEEK";
170 $params['frequency_unit'] = "week";
171 $params['frequency_interval'] = 1;
175 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d") +
14, date("Y"));
176 $params['end_date'] = mktime(0, 0, 0, date("m"), date("d") +
(14 * $payflow_query_array['TERM']), date("Y ")
178 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']);
179 $payflow_query_array['PAYPERIOD'] = "BIWK";
180 $params['frequency_unit'] = "week";
181 $params['frequency_interval'] = 2;
185 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d") +
28, date("Y")
187 $params['end_date'] = mktime(0, 0, 0, date("m"), date("d") +
(28 * $payflow_query_array['TERM']), date("Y")
189 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']);
190 $payflow_query_array['PAYPERIOD'] = "FRWK";
191 $params['frequency_unit'] = "week";
192 $params['frequency_interval'] = 4;
196 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m") +
1,
199 $params['end_date'] = mktime(0, 0, 0, date("m") +
200 (1 * $payflow_query_array['TERM']),
203 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']);
204 $payflow_query_array['PAYPERIOD'] = "MONT";
205 $params['frequency_unit'] = "month";
206 $params['frequency_interval'] = 1;
210 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m") +
3, date("d"), date("Y")
212 $params['end_date'] = mktime(0, 0, 0, date("m") +
213 (3 * $payflow_query_array['TERM']),
216 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']);
217 $payflow_query_array['PAYPERIOD'] = "QTER";
218 $params['frequency_unit'] = "month";
219 $params['frequency_interval'] = 3;
223 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m") +
6, date("d"),
226 $params['end_date'] = mktime(0, 0, 0, date("m") +
227 (6 * $payflow_query_array['TERM']),
230 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']
232 $payflow_query_array['PAYPERIOD'] = "SMYR";
233 $params['frequency_unit'] = "month";
234 $params['frequency_interval'] = 6;
238 $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d"),
241 $params['end_date'] = mktime(0, 0, 0, date("m"), date("d"),
243 (1 * $payflow_query_array['TEM'])
245 $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']);
246 $payflow_query_array['PAYPERIOD'] = "YEAR";
247 $params['frequency_unit'] = "year";
248 $params['frequency_interval'] = 1;
253 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $payflow_query_array);
254 $payflow_query = $this->convert_to_nvp($payflow_query_array);
257 * Check to see if we have a duplicate before we send
259 if ($this->checkDupe($params['invoiceID'], CRM_Utils_Array
::value('contributionID', $params))) {
260 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. 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.');
263 // ie. url at payment processor to submit to.
264 $submiturl = $this->_paymentProcessor
['url_site'];
266 $responseData = self
::submit_transaction($submiturl, $payflow_query);
269 * Payment successfully sent to gateway - process the response now
271 $result = strstr($responseData, "RESULT");
272 if (empty($result)) {
273 return self
::errorExit(9016, "No RESULT code from PayPal.");
277 while (strlen($result)) {
279 $keypos = strpos($result, '=');
280 $keyval = substr($result, 0, $keypos);
282 $valuepos = strpos($result, '&') ?
strpos($result, '&') : strlen($result);
283 $valval = substr($result, $keypos +
1, $valuepos - $keypos - 1);
284 // decoding the respose
285 $nvpArray[$keyval] = $valval;
286 $result = substr($result, $valuepos +
1, strlen($result));
288 // get the result code to validate.
289 $result_code = $nvpArray['RESULT'];
291 echo "<p>Params array</p><br>";
294 echo "<p>Values to Payment Processor</p><br>";
295 print_r($payflow_query_array);
297 echo "<p>Results from Payment Processor</p><br>";
302 switch ($result_code) {
305 /*******************************************************
307 * This is a successful transaction. Payflow Pro does return further information
308 * about transactions to help you identify fraud including whether they pass
309 * the cvv check, the avs check. This is stored in
310 * CiviCRM as part of the transact
311 * but not further processing is done. Business rules would need to be defined
312 *******************************************************/
313 $params['trxn_id'] = $nvpArray['PNREF'] . $nvpArray['TRXPNREF'];
314 //'trxn_id' is varchar(255) field. returned value is length 12
315 $params['trxn_result_code'] = $nvpArray['AUTHCODE'] . "-Cvv2:" . $nvpArray['CVV2MATCH'] . "-avs:" . $nvpArray['AVSADDR'];
317 if ($params['is_recur'] == TRUE) {
318 $params['recur_trxn_id'] = $nvpArray['PROFILEID'];
319 //'trxn_id' is varchar(255) field. returned value is length 12
324 return self
::errorExit(9008, "There is a payment processor configuration problem. This is usually due to invalid account information or ip restrictions on the account. You can verify ip restriction by logging // into Manager. See Service Settings >> Allowed IP Addresses. ");
327 // Hard decline from bank.
328 return self
::errorExit(9009, "Your transaction was declined ");
331 // Voice authorization required.
332 return self
::errorExit(9010, "Your Transaction is pending. Contact Customer Service to complete your order.");
335 // Issue with credit card number or expiration date.
336 return self
::errorExit(9011, "Invalid credit card information. Please re-enter.");
339 return self
::errorExit(9012, "You have not configured your payment processor with the correct credentials. Make sure you have provided both the <vendor> and the <user> variables ");
342 return self
::errorExit(9013, "Error - from payment processor: [" . $result_code . " " . $nvpArray['RESPMSG'] . "] ");
345 return self
::errorExit(9014, "Check the code - all transactions should have been headed off before they got here. Something slipped through the net");
349 * Produces error message and returns from class
351 * @param null $errorCode
352 * @param null $errorMessage
356 public function &errorExit($errorCode = NULL, $errorMessage = NULL) {
357 $e = CRM_Core_Error
::singleton();
359 $e->push($errorCode, 0, NULL, $errorMessage);
362 $e->push(9000, 0, NULL, 'Unknown System Error.');
368 * NOTE: 'doTransferCheckout' not implemented
370 * @param array $params
375 public function doTransferCheckout(&$params, $component) {
376 CRM_Core_Error
::fatal(ts('This function is not implemented'));
380 * This public function checks to see if we have the right processor config values set
382 * NOTE: Called by Events and Contribute to check config params are set prior to trying
383 * register any credit card details
385 * @return string|null
386 * the error message if any, null if OK
388 public function checkConfig() {
390 if (empty($this->_paymentProcessor
['user_name'])) {
391 $errorMsg[] = ' ' . ts('ssl_merchant_id is not set for this payment processor');
394 if (empty($this->_paymentProcessor
['url_site'])) {
395 $errorMsg[] = ' ' . ts('URL is not set for %1', [1 => $this->_paymentProcessor
['name']]);
398 if (!empty($errorMsg)) {
399 return implode('<p>', $errorMsg);
407 * convert to a name/value pair (nvp) string
409 * @param $payflow_query_array
411 * @return array|string
413 public function convert_to_nvp($payflow_query_array) {
414 foreach ($payflow_query_array as $key => $value) {
415 $payflow_query[] = $key . '[' . strlen($value) . ']=' . $value;
417 $payflow_query = implode('&', $payflow_query);
419 return $payflow_query;
423 * Submit transaction using cURL
425 * @param string $submiturl Url to direct HTTPS GET to
426 * @param $payflow_query value string to be posted
428 * @return mixed|object
430 public function submit_transaction($submiturl, $payflow_query) {
431 // get data ready for API
432 $user_agent = $_SERVER['HTTP_USER_AGENT'];
433 // Here's your custom headers; adjust appropriately for your setup:
434 $headers[] = "Content-Type: text/namevalue";
435 //or text/xml if using XMLPay.
436 $headers[] = "Content-Length : " . strlen($data);
437 // Length of data to be passed
438 // Here the server timeout value is set to 45, but notice
439 // below in the cURL section, the timeout
440 // for cURL is 90 seconds. You want to make sure the server
441 // timeout is less, then the connection.
442 $headers[] = "X-VPS-Timeout: 45";
443 //random unique number - the transaction is retried using this transaction ID
444 // in this function but if that doesn't work and it is re- submitted
445 // it is treated as a new attempt. Payflow Pro doesn't allow
446 // you to change details (e.g. card no) when you re-submit
447 // you can only try the same details
448 $headers[] = "X-VPS-Request-ID: " . rand(1, 1000000000);
449 // optional header field
450 $headers[] = "X-VPS-VIT-Integration-Product: CiviCRM";
451 // other Optional Headers. If used adjust as necessary.
453 //$headers[] = "X-VPS-VIT-OS-Name: Linux";
455 //$headers[] = "X-VPS-VIT-OS-Version: RHEL 4";
456 // What you are using
457 //$headers[] = "X-VPS-VIT-Client-Type: PHP/cURL";
459 //$headers[] = "X-VPS-VIT-Client-Version: 0.01";
461 //$headers[] = "X-VPS-VIT-Client-Architecture: x86";
462 // Application version
463 //$headers[] = "X-VPS-VIT-Integration-Version: 0.01";
465 curl_setopt($ch, CURLOPT_URL
, $submiturl);
466 curl_setopt($ch, CURLOPT_HTTPHEADER
, $headers);
467 curl_setopt($ch, CURLOPT_USERAGENT
, $user_agent);
468 curl_setopt($ch, CURLOPT_HEADER
, 1);
469 // tells curl to include headers in response
470 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
471 // return into a variable
472 curl_setopt($ch, CURLOPT_TIMEOUT
, 90);
473 // times out after 90 secs
474 if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
475 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, 0);
477 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, Civi
::settings()->get('verifySSL'));
478 // this line makes it work under https
479 curl_setopt($ch, CURLOPT_POSTFIELDS
, $payflow_query);
481 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, Civi
::settings()->get('verifySSL') ?
2 : 0);
482 //verifies ssl certificate
483 curl_setopt($ch, CURLOPT_FORBID_REUSE
, TRUE);
484 //forces closure of connection when done
485 curl_setopt($ch, CURLOPT_POST
, 1);
488 // Try to submit the transaction up to 3 times with 5 second delay. This can be used
489 // in case of network issues. The idea here is since you are posting via HTTPS there
490 // could be general network issues, so try a few times before you tell customer there
495 $responseData = curl_exec($ch);
496 $responseHeaders = curl_getinfo($ch);
497 if ($responseHeaders['http_code'] != 200) {
498 // Let's wait 5 seconds to see if its a temporary network issue.
501 elseif ($responseHeaders['http_code'] == 200) {
502 // we got a good response, drop out of loop.
506 if ($responseHeaders['http_code'] != 200) {
507 return self
::errorExit(9015, "Error connecting to the Payflow Pro API server.");
511 * Transaction submitted -
512 * See if we had a curl error - if so tell 'em and bail out
514 * NOTE: curl_error does not return a logical value (see its documentation), but
515 * a string, which is empty when there was no error.
517 if ((curl_errno($ch) > 0) ||
(strlen(curl_error($ch)) > 0)) {
519 $errorNum = curl_errno($ch);
520 $errorDesc = curl_error($ch);
522 //Paranoia - in the unlikley event that 'curl' errno fails
523 if ($errorNum == 0) {
527 // Paranoia - in the unlikley event that 'curl' error fails
528 if (strlen($errorDesc) == 0) {
529 $errorDesc = "Connection to payment gateway failed";
531 if ($errorNum = 60) {
532 return self
::errorExit($errorNum, "Curl error - " . $errorDesc .
533 " Try this link for more information http://curl.haxx.se/d
538 return self
::errorExit($errorNum, "Curl error - " . $errorDesc .
539 " processor response = " . $processorResponse
544 * If null data returned - tell 'em and bail out
546 * NOTE: You will not necessarily get a string back, if the request failed for
547 * any reason, the return value will be the boolean false.
549 if (($responseData === FALSE) ||
(strlen($responseData) == 0)) {
551 return self
::errorExit(9006, "Error: Connection to payment gateway failed - no data
552 returned. Gateway url set to $submiturl");
556 * If gateway returned no data - tell 'em and bail out
558 if (empty($responseData)) {
560 return self
::errorExit(9007, "Error: No data returned from payment gateway.");
564 * Success so far - close the curl and check the data
567 return $responseData;