Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +----------------------------------------------------------------------------+ | |
7e9e8871 | 4 | | PayflowPro Core Payment Module for CiviCRM version 4.7 | |
6a488035 TO |
5 | +----------------------------------------------------------------------------+ |
6 | | Licensed to CiviCRM under the Academic Free License version 3.0 | | |
7 | | | | |
8 | | Written & Contributed by Eileen McNaughton - 2009 | | |
9 | +---------------------------------------------------------------------------+ | |
10 | */ | |
28518c90 EM |
11 | |
12 | /** | |
0b014509 | 13 | * Class CRM_Core_Payment_PayflowPro. |
28518c90 | 14 | */ |
6a488035 TO |
15 | class CRM_Core_Payment_PayflowPro extends CRM_Core_Payment { |
16 | // (not used, implicit in the API, might need to convert?) | |
7da04cde | 17 | const |
353ffa53 | 18 | CHARSET = 'UFT-8'; |
6a488035 TO |
19 | |
20 | /** | |
21 | * We only need one instance of this object. So we use the singleton | |
22 | * pattern and cache the instance in this variable | |
23 | * | |
24 | * @var object | |
6a488035 TO |
25 | */ |
26 | static private $_singleton = NULL; | |
27 | ||
3ecad08a | 28 | /** |
6a488035 TO |
29 | * Constructor |
30 | * | |
6a0b768e TO |
31 | * @param string $mode |
32 | * The mode of operation: live or test. | |
6c786a9b EM |
33 | * @param $paymentProcessor |
34 | */ | |
00be9182 | 35 | public function __construct($mode, &$paymentProcessor) { |
6a488035 TO |
36 | // live or test |
37 | $this->_mode = $mode; | |
38 | $this->_paymentProcessor = $paymentProcessor; | |
39 | $this->_processorName = ts('Payflow Pro'); | |
40 | } | |
ceb10dc7 | 41 | |
6a488035 TO |
42 | /* |
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 | |
46 | */ | |
b5c2afd0 EM |
47 | /** |
48 | * This function collects all the information from a web/api form and invokes | |
49 | * the relevant payment processor specific functions to perform the transaction | |
50 | * | |
6a0b768e TO |
51 | * @param array $params |
52 | * Assoc array of input parameters for this transaction. | |
b5c2afd0 | 53 | * |
a6c01b45 CW |
54 | * @return array |
55 | * the result in an nice formatted array (or an error object) | |
b5c2afd0 EM |
56 | * @abstract |
57 | */ | |
00be9182 | 58 | public function doDirectPayment(&$params) { |
6a488035 TO |
59 | if (!defined('CURLOPT_SSLCERT')) { |
60 | CRM_Core_Error::fatal(ts('PayFlowPro requires curl with SSL support')); | |
61 | } | |
62 | ||
63 | /* | |
64 | * define variables for connecting with the gateway | |
65 | */ | |
ceb10dc7 | 66 | |
6a488035 TO |
67 | // Are you using the Payflow Fraud Protection Service? |
68 | // Default is YES, change to NO or blank if not. | |
69 | //This has not been investigated as part of writing this payment processor | |
70 | $fraud = 'NO'; | |
71 | //if you have not set up a separate user account the vendor name is used as the username | |
72 | if (!$this->_paymentProcessor['subject']) { | |
73 | $user = $this->_paymentProcessor['user_name']; | |
74 | } | |
75 | else { | |
76 | $user = $this->_paymentProcessor['subject']; | |
77 | } | |
78 | ||
79 | // ideally this id would be passed through into this class as | |
80 | // part of the paymentProcessor | |
81 | //object with the other variables. It seems inefficient to re-query to get it. | |
82 | //$params['processor_id'] = CRM_Core_DAO::getFieldValue( | |
83 | // 'CRM_Contribute_DAO_ContributionP | |
84 | //age',$params['contributionPageID'], 'payment_processor_id' ); | |
85 | ||
86 | /* | |
87 | *Create the array of variables to be sent to the processor from the $params array | |
88 | * passed into this function | |
89 | * | |
90 | */ | |
91 | ||
92 | $payflow_query_array = array( | |
93 | 'USER' => $user, | |
94 | 'VENDOR' => $this->_paymentProcessor['user_name'], | |
95 | 'PARTNER' => $this->_paymentProcessor['signature'], | |
96 | 'PWD' => $this->_paymentProcessor['password'], | |
97 | // C - Direct Payment using credit card | |
98 | 'TENDER' => 'C', | |
99 | // A - Authorization, S - Sale | |
100 | 'TRXTYPE' => 'S', | |
101 | 'ACCT' => urlencode($params['credit_card_number']), | |
102 | 'CVV2' => $params['cvv2'], | |
103 | 'EXPDATE' => urlencode(sprintf('%02d', (int) $params['month']) . substr($params['year'], 2, 2)), | |
104 | 'ACCTTYPE' => urlencode($params['credit_card_type']), | |
105 | 'AMT' => urlencode($params['amount']), | |
106 | 'CURRENCY' => urlencode($params['currency']), | |
107 | 'FIRSTNAME' => $params['billing_first_name'], | |
108 | //credit card name | |
109 | 'LASTNAME' => $params['billing_last_name'], | |
110 | //credit card name | |
111 | 'STREET' => $params['street_address'], | |
112 | 'CITY' => urlencode($params['city']), | |
113 | 'STATE' => urlencode($params['state_province']), | |
114 | 'ZIP' => urlencode($params['postal_code']), | |
115 | 'COUNTRY' => urlencode($params['country']), | |
116 | 'EMAIL' => $params['email'], | |
117 | 'CUSTIP' => urlencode($params['ip_address']), | |
118 | 'COMMENT1' => urlencode($params['contributionType_accounting_code']), | |
119 | 'COMMENT2' => $mode, | |
120 | 'INVNUM' => urlencode($params['invoiceID']), | |
121 | 'ORDERDESC' => urlencode($params['description']), | |
122 | 'VERBOSITY' => 'MEDIUM', | |
123 | 'BILLTOCOUNTRY' => urlencode($params['country']), | |
124 | ); | |
125 | ||
126 | if ($params['installments'] == 1) { | |
0f76c95a | 127 | $params['is_recur'] = FALSE; |
6a488035 TO |
128 | } |
129 | ||
130 | if ($params['is_recur'] == TRUE) { | |
131 | ||
132 | $payflow_query_array['TRXTYPE'] = 'R'; | |
133 | $payflow_query_array['OPTIONALTRX'] = 'S'; | |
134 | $payflow_query_array['OPTIONALTRXAMT'] = $params['amount']; | |
135 | //Amount of the initial Transaction. Required | |
136 | $payflow_query_array['ACTION'] = 'A'; | |
137 | //A for add recurring (M-modify,C-cancel,R-reactivate,I-inquiry,P-payment | |
138 | $payflow_query_array['PROFILENAME'] = urlencode('RegularContribution'); | |
139 | //A for add recurring (M-modify,C-cancel,R-reactivate,I-inquiry,P-payment | |
140 | if ($params['installments'] > 0) { | |
141 | $payflow_query_array['TERM'] = $params['installments'] - 1; | |
142 | //ie. in addition to the one happening with this transaction | |
143 | } | |
144 | // $payflow_query_array['COMPANYNAME'] | |
145 | // $payflow_query_array['DESC'] = not set yet Optional | |
146 | // description of the goods or | |
147 | //services being purchased. | |
148 | //This parameter applies only for ACH_CCD accounts. | |
149 | // The | |
150 | // $payflow_query_array['MAXFAILPAYMENTS'] = 0; | |
151 | // number of payment periods (as s | |
152 | //pecified by PAYPERIOD) for which the transaction is allowed | |
153 | //to fail before PayPal cancels a profile. the default | |
154 | // value of 0 (zero) specifies no | |
155 | //limit. Retry | |
156 | //attempts occur until the term is complete. | |
157 | // $payflow_query_array['RETRYNUMDAYS'] = (not set as can't assume business rule | |
158 | ||
159 | switch ($params['frequency_unit']) { | |
160 | case '1 week': | |
797b807e | 161 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d") + 7, |
6a488035 TO |
162 | date("Y") |
163 | ); | |
164 | $params['end_date'] = mktime(0, 0, 0, date("m"), date("d") + (7 * $payflow_query_array['TERM']), | |
165 | date("Y") | |
166 | ); | |
797b807e | 167 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']); |
6a488035 TO |
168 | $payflow_query_array['PAYPERIOD'] = "WEEK"; |
169 | $params['frequency_unit'] = "week"; | |
170 | $params['frequency_interval'] = 1; | |
171 | break; | |
172 | ||
173 | case '2 weeks': | |
797b807e | 174 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d") + 14, date("Y")); |
2aa397bc | 175 | $params['end_date'] = mktime(0, 0, 0, date("m"), date("d") + (14 * $payflow_query_array['TERM']), date("Y ") |
6a488035 | 176 | ); |
797b807e | 177 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']); |
6a488035 TO |
178 | $payflow_query_array['PAYPERIOD'] = "BIWK"; |
179 | $params['frequency_unit'] = "week"; | |
180 | $params['frequency_interval'] = 2; | |
181 | break; | |
182 | ||
183 | case '4 weeks': | |
797b807e | 184 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d") + 28, date("Y") |
6a488035 | 185 | ); |
2aa397bc | 186 | $params['end_date'] = mktime(0, 0, 0, date("m"), date("d") + (28 * $payflow_query_array['TERM']), date("Y") |
6a488035 | 187 | ); |
797b807e | 188 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']); |
6a488035 TO |
189 | $payflow_query_array['PAYPERIOD'] = "FRWK"; |
190 | $params['frequency_unit'] = "week"; | |
191 | $params['frequency_interval'] = 4; | |
192 | break; | |
193 | ||
194 | case '1 month': | |
797b807e | 195 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m") + 1, |
6a488035 TO |
196 | date("d"), date("Y") |
197 | ); | |
198 | $params['end_date'] = mktime(0, 0, 0, date("m") + | |
199 | (1 * $payflow_query_array['TERM']), | |
200 | date("d"), date("Y") | |
201 | ); | |
797b807e | 202 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']); |
6a488035 TO |
203 | $payflow_query_array['PAYPERIOD'] = "MONT"; |
204 | $params['frequency_unit'] = "month"; | |
205 | $params['frequency_interval'] = 1; | |
206 | break; | |
207 | ||
208 | case '3 months': | |
2aa397bc | 209 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m") + 3, date("d"), date("Y") |
6a488035 TO |
210 | ); |
211 | $params['end_date'] = mktime(0, 0, 0, date("m") + | |
212 | (3 * $payflow_query_array['TERM']), | |
213 | date("d"), date("Y") | |
214 | ); | |
797b807e | 215 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']); |
6a488035 TO |
216 | $payflow_query_array['PAYPERIOD'] = "QTER"; |
217 | $params['frequency_unit'] = "month"; | |
218 | $params['frequency_interval'] = 3; | |
219 | break; | |
220 | ||
221 | case '6 months': | |
797b807e | 222 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m") + 6, date("d"), |
6a488035 TO |
223 | date("Y") |
224 | ); | |
225 | $params['end_date'] = mktime(0, 0, 0, date("m") + | |
226 | (6 * $payflow_query_array['TERM']), | |
227 | date("d"), date("Y") | |
228 | ); | |
2aa397bc | 229 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date'] |
6a488035 TO |
230 | ); |
231 | $payflow_query_array['PAYPERIOD'] = "SMYR"; | |
232 | $params['frequency_unit'] = "month"; | |
233 | $params['frequency_interval'] = 6; | |
234 | break; | |
235 | ||
236 | case '1 year': | |
797b807e | 237 | $params['next_sched_contribution_date'] = mktime(0, 0, 0, date("m"), date("d"), |
6a488035 TO |
238 | date("Y") + 1 |
239 | ); | |
240 | $params['end_date'] = mktime(0, 0, 0, date("m"), date("d"), | |
241 | date("Y") + | |
242 | (1 * $payflow_query_array['TEM']) | |
243 | ); | |
797b807e | 244 | $payflow_query_array['START'] = date('mdY', $params['next_sched_contribution_date']); |
6a488035 TO |
245 | $payflow_query_array['PAYPERIOD'] = "YEAR"; |
246 | $params['frequency_unit'] = "year"; | |
247 | $params['frequency_interval'] = 1; | |
248 | break; | |
249 | } | |
250 | } | |
251 | ||
252 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $payflow_query_array); | |
253 | $payflow_query = $this->convert_to_nvp($payflow_query_array); | |
254 | ||
255 | /* | |
256 | * Check to see if we have a duplicate before we send | |
257 | */ | |
d253aeb8 | 258 | if ($this->checkDupe($params['invoiceID'], CRM_Utils_Array::value('contributionID', $params))) { |
6a488035 TO |
259 | 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.'); |
260 | } | |
261 | ||
262 | // ie. url at payment processor to submit to. | |
263 | $submiturl = $this->_paymentProcessor['url_site']; | |
264 | ||
265 | $responseData = self::submit_transaction($submiturl, $payflow_query); | |
266 | ||
267 | /* | |
ceb10dc7 | 268 | * Payment successfully sent to gateway - process the response now |
6a488035 TO |
269 | */ |
270 | $result = strstr($responseData, "RESULT"); | |
3ae790f3 JM |
271 | if (empty($result)) { |
272 | return self::errorExit(9016, "No RESULT code from PayPal."); | |
273 | } | |
274 | ||
6a488035 TO |
275 | $nvpArray = array(); |
276 | while (strlen($result)) { | |
277 | // name | |
278 | $keypos = strpos($result, '='); | |
279 | $keyval = substr($result, 0, $keypos); | |
280 | // value | |
281 | $valuepos = strpos($result, '&') ? strpos($result, '&') : strlen($result); | |
282 | $valval = substr($result, $keypos + 1, $valuepos - $keypos - 1); | |
283 | // decoding the respose | |
284 | $nvpArray[$keyval] = $valval; | |
285 | $result = substr($result, $valuepos + 1, strlen($result)); | |
286 | } | |
287 | // get the result code to validate. | |
288 | $result_code = $nvpArray['RESULT']; | |
289 | /*debug | |
e70a7fc0 TO |
290 | echo "<p>Params array</p><br>"; |
291 | print_r($params); | |
292 | echo "<p></p><br>"; | |
293 | echo "<p>Values to Payment Processor</p><br>"; | |
294 | print_r($payflow_query_array); | |
295 | echo "<p></p><br>"; | |
296 | echo "<p>Results from Payment Processor</p><br>"; | |
297 | print_r($nvpArray); | |
298 | echo "<p></p><br>"; | |
299 | */ | |
6a488035 TO |
300 | |
301 | switch ($result_code) { | |
302 | case 0: | |
303 | ||
304 | /******************************************************* | |
305 | * Success ! | |
306 | * This is a successful transaction. PayFlow Pro does return further information | |
307 | * about transactions to help you identify fraud including whether they pass | |
308 | * the cvv check, the avs check. This is stored in | |
309 | * CiviCRM as part of the transact | |
310 | * but not further processing is done. Business rules would need to be defined | |
6a488035 TO |
311 | *******************************************************/ |
312 | $params['trxn_id'] = $nvpArray['PNREF'] . $nvpArray['TRXPNREF']; | |
313 | //'trxn_id' is varchar(255) field. returned value is length 12 | |
314 | $params['trxn_result_code'] = $nvpArray['AUTHCODE'] . "-Cvv2:" . $nvpArray['CVV2MATCH'] . "-avs:" . $nvpArray['AVSADDR']; | |
315 | ||
316 | if ($params['is_recur'] == TRUE) { | |
317 | $params['recur_trxn_id'] = $nvpArray['PROFILEID']; | |
318 | //'trxn_id' is varchar(255) field. returned value is length 12 | |
319 | } | |
320 | return $params; | |
321 | ||
322 | case 1: | |
323 | 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. "); | |
324 | ||
325 | case 12: | |
326 | // Hard decline from bank. | |
327 | return self::errorExit(9009, "Your transaction was declined "); | |
328 | ||
329 | case 13: | |
330 | // Voice authorization required. | |
331 | return self::errorExit(9010, "Your Transaction is pending. Contact Customer Service to complete your order."); | |
332 | ||
333 | case 23: | |
334 | // Issue with credit card number or expiration date. | |
335 | return self::errorExit(9011, "Invalid credit card information. Please re-enter."); | |
336 | ||
337 | case 26: | |
338 | 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 "); | |
339 | ||
340 | default: | |
341 | return self::errorExit(9013, "Error - from payment processor: [" . $result_code . " " . $nvpArray['RESPMSG'] . "] "); | |
342 | } | |
343 | ||
344 | return self::errorExit(9014, "Check the code - all transactions should have been headed off before they got here. Something slipped through the net"); | |
345 | } | |
346 | ||
6a488035 TO |
347 | /* |
348 | * Produces error message and returns from class | |
349 | */ | |
6c786a9b EM |
350 | /** |
351 | * @param null $errorCode | |
352 | * @param null $errorMessage | |
353 | * | |
354 | * @return object | |
355 | */ | |
00be9182 | 356 | public function &errorExit($errorCode = NULL, $errorMessage = NULL) { |
6a488035 TO |
357 | $e = CRM_Core_Error::singleton(); |
358 | if ($errorCode) { | |
359 | $e->push($errorCode, 0, NULL, $errorMessage); | |
360 | } | |
361 | else { | |
362 | $e->push(9000, 0, NULL, 'Unknown System Error.'); | |
363 | } | |
364 | return $e; | |
365 | } | |
366 | ||
367 | ||
368 | /* | |
369 | * NOTE: 'doTransferCheckout' not implemented | |
370 | */ | |
6c786a9b | 371 | /** |
c490a46a | 372 | * @param array $params |
6c786a9b EM |
373 | * @param $component |
374 | * | |
375 | * @throws Exception | |
376 | */ | |
00be9182 | 377 | public function doTransferCheckout(&$params, $component) { |
6a488035 TO |
378 | CRM_Core_Error::fatal(ts('This function is not implemented')); |
379 | } | |
380 | ||
381 | /* | |
382 | * This public function checks to see if we have the right processor config values set | |
383 | * | |
384 | * NOTE: Called by Events and Contribute to check config params are set prior to trying | |
385 | * register any credit card details | |
386 | * | |
6a0b768e TO |
387 | * @param string $mode |
388 | * The mode we are operating in (live or test) - not used. | |
6a488035 TO |
389 | * |
390 | * returns string $errorMsg if any errors found - null if OK | |
6a488035 | 391 | */ |
ceb10dc7 | 392 | |
6a488035 TO |
393 | // function checkConfig( $mode ) // CiviCRM V1.9 Declaration |
394 | ||
b5c2afd0 | 395 | /** |
4f1f1f2a | 396 | * CiviCRM V2.0 Declaration |
b5c2afd0 EM |
397 | * This function checks to see if we have the right config values |
398 | * | |
399 | * @internal param string $mode the mode we are operating in (live or test) | |
400 | * | |
a6c01b45 CW |
401 | * @return string |
402 | * the error message if any | |
b5c2afd0 | 403 | */ |
00be9182 | 404 | public function checkConfig() { |
6a488035 TO |
405 | $errorMsg = array(); |
406 | if (empty($this->_paymentProcessor['user_name'])) { | |
407 | $errorMsg[] = ' ' . ts('ssl_merchant_id is not set for this payment processor'); | |
408 | } | |
409 | ||
410 | if (empty($this->_paymentProcessor['url_site'])) { | |
411 | $errorMsg[] = ' ' . ts('URL is not set for %1', array(1 => $this->_paymentProcessor['name'])); | |
412 | } | |
413 | ||
414 | if (!empty($errorMsg)) { | |
415 | return implode('<p>', $errorMsg); | |
416 | } | |
417 | else { | |
418 | return NULL; | |
419 | } | |
420 | } | |
421 | //end check config | |
422 | ||
423 | /* | |
424 | * convert to a name/value pair (nvp) string | |
425 | */ | |
6c786a9b EM |
426 | /** |
427 | * @param $payflow_query_array | |
428 | * | |
429 | * @return array|string | |
430 | */ | |
00be9182 | 431 | public function convert_to_nvp($payflow_query_array) { |
6a488035 TO |
432 | foreach ($payflow_query_array as $key => $value) { |
433 | $payflow_query[] = $key . '[' . strlen($value) . ']=' . $value; | |
434 | } | |
435 | $payflow_query = implode('&', $payflow_query); | |
436 | ||
437 | return $payflow_query; | |
438 | } | |
439 | ||
440 | /* | |
441 | * Submit transaction using CuRL | |
442 | * @submiturl string Url to direct HTTPS GET to | |
443 | * @payflow_query value string to be posted | |
6a488035 | 444 | */ |
6c786a9b EM |
445 | /** |
446 | * @param $submiturl | |
447 | * @param $payflow_query | |
448 | * | |
449 | * @return mixed|object | |
450 | */ | |
00be9182 | 451 | public function submit_transaction($submiturl, $payflow_query) { |
6a488035 TO |
452 | /* |
453 | * Submit transaction using CuRL | |
454 | */ | |
ceb10dc7 | 455 | |
6a488035 TO |
456 | // get data ready for API |
457 | $user_agent = $_SERVER['HTTP_USER_AGENT']; | |
458 | // Here's your custom headers; adjust appropriately for your setup: | |
459 | $headers[] = "Content-Type: text/namevalue"; | |
460 | //or text/xml if using XMLPay. | |
461 | $headers[] = "Content-Length : " . strlen($data); | |
462 | // Length of data to be passed | |
463 | // Here the server timeout value is set to 45, but notice | |
464 | // below in the cURL section, the timeout | |
465 | // for cURL is 90 seconds. You want to make sure the server | |
466 | // timeout is less, then the connection. | |
467 | $headers[] = "X-VPS-Timeout: 45"; | |
468 | //random unique number - the transaction is retried using this transaction ID | |
469 | // in this function but if that doesn't work and it is re- submitted | |
470 | // it is treated as a new attempt. PayflowPro doesn't allow | |
471 | // you to change details (e.g. card no) when you re-submit | |
472 | // you can only try the same details | |
473 | $headers[] = "X-VPS-Request-ID: " . rand(1, 1000000000); | |
474 | // optional header field | |
475 | $headers[] = "X-VPS-VIT-Integration-Product: CiviCRM"; | |
476 | // other Optional Headers. If used adjust as necessary. | |
477 | // Name of your OS | |
478 | //$headers[] = "X-VPS-VIT-OS-Name: Linux"; | |
479 | // OS Version | |
480 | //$headers[] = "X-VPS-VIT-OS-Version: RHEL 4"; | |
481 | // What you are using | |
482 | //$headers[] = "X-VPS-VIT-Client-Type: PHP/cURL"; | |
483 | // For your info | |
484 | //$headers[] = "X-VPS-VIT-Client-Version: 0.01"; | |
485 | // For your info | |
486 | //$headers[] = "X-VPS-VIT-Client-Architecture: x86"; | |
487 | // Application version | |
488 | //$headers[] = "X-VPS-VIT-Integration-Version: 0.01"; | |
489 | $ch = curl_init(); | |
490 | curl_setopt($ch, CURLOPT_URL, $submiturl); | |
491 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
492 | curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); | |
493 | curl_setopt($ch, CURLOPT_HEADER, 1); | |
494 | // tells curl to include headers in response | |
495 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
496 | // return into a variable | |
497 | curl_setopt($ch, CURLOPT_TIMEOUT, 90); | |
498 | // times out after 90 secs | |
439b9688 LS |
499 | if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') { |
500 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); | |
501 | } | |
aaffa79f | 502 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Civi::settings()->get('verifySSL')); |
6a488035 TO |
503 | // this line makes it work under https |
504 | curl_setopt($ch, CURLOPT_POSTFIELDS, $payflow_query); | |
505 | //adding POST data | |
aaffa79f | 506 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Civi::settings()->get('verifySSL') ? 2 : 0); |
6a488035 TO |
507 | //verifies ssl certificate |
508 | curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE); | |
509 | //forces closure of connection when done | |
510 | curl_setopt($ch, CURLOPT_POST, 1); | |
511 | //data sent as POST | |
512 | ||
513 | // Try to submit the transaction up to 3 times with 5 second delay. This can be used | |
514 | // in case of network issues. The idea here is since you are posting via HTTPS there | |
515 | // could be general network issues, so try a few times before you tell customer there | |
516 | // is an issue. | |
517 | ||
518 | $i = 1; | |
519 | while ($i++ <= 3) { | |
520 | $responseData = curl_exec($ch); | |
521 | $responseHeaders = curl_getinfo($ch); | |
522 | if ($responseHeaders['http_code'] != 200) { | |
523 | // Let's wait 5 seconds to see if its a temporary network issue. | |
524 | sleep(5); | |
525 | } | |
526 | elseif ($responseHeaders['http_code'] == 200) { | |
527 | // we got a good response, drop out of loop. | |
528 | break; | |
529 | } | |
530 | } | |
3ae790f3 JM |
531 | if ($responseHeaders['http_code'] != 200) { |
532 | return self::errorExit(9015, "Error connecting to the payflo API server."); | |
533 | } | |
6a488035 TO |
534 | |
535 | /* | |
536 | * Transaction submitted - | |
537 | * See if we had a curl error - if so tell 'em and bail out | |
538 | * | |
539 | * NOTE: curl_error does not return a logical value (see its documentation), but | |
540 | * a string, which is empty when there was no error. | |
541 | */ | |
542 | if ((curl_errno($ch) > 0) || (strlen(curl_error($ch)) > 0)) { | |
543 | curl_close($ch); | |
544 | $errorNum = curl_errno($ch); | |
545 | $errorDesc = curl_error($ch); | |
546 | ||
547 | //Paranoia - in the unlikley event that 'curl' errno fails | |
2aa397bc TO |
548 | if ($errorNum == 0) { |
549 | $errorNum = 9005; | |
550 | } | |
6a488035 TO |
551 | |
552 | // Paranoia - in the unlikley event that 'curl' error fails | |
2aa397bc TO |
553 | if (strlen($errorDesc) == 0) { |
554 | $errorDesc = "Connection to payment gateway failed"; | |
555 | } | |
6a488035 TO |
556 | if ($errorNum = 60) { |
557 | return self::errorExit($errorNum, "Curl error - " . $errorDesc . | |
558 | " Try this link for more information http://curl.haxx.se/d | |
559 | ocs/sslcerts.html" | |
560 | ); | |
561 | } | |
562 | ||
563 | return self::errorExit($errorNum, "Curl error - " . $errorDesc . | |
564 | " processor response = " . $processorResponse | |
565 | ); | |
566 | } | |
567 | ||
568 | /* | |
569 | * If null data returned - tell 'em and bail out | |
570 | * | |
571 | * NOTE: You will not necessarily get a string back, if the request failed for | |
572 | * any reason, the return value will be the boolean false. | |
573 | */ | |
574 | if (($responseData === FALSE) || (strlen($responseData) == 0)) { | |
575 | curl_close($ch); | |
576 | return self::errorExit(9006, "Error: Connection to payment gateway failed - no data | |
577 | returned. Gateway url set to $submiturl"); | |
578 | } | |
579 | ||
580 | /* | |
581 | * If gateway returned no data - tell 'em and bail out | |
582 | */ | |
583 | if (empty($responseData)) { | |
584 | curl_close($ch); | |
585 | return self::errorExit(9007, "Error: No data returned from payment gateway."); | |
586 | } | |
587 | ||
588 | /* | |
589 | * Success so far - close the curl and check the data | |
590 | */ | |
591 | curl_close($ch); | |
592 | return $responseData; | |
593 | } | |
594 | //end submit_transaction | |
595 | ||
6c786a9b | 596 | /** |
100fef9d CW |
597 | * @param int $recurringProfileID |
598 | * @param int $processorID | |
6c786a9b EM |
599 | * |
600 | * @throws Exception | |
601 | */ | |
00be9182 | 602 | public function getRecurringTransactionStatus($recurringProfileID, $processorID) { |
6a488035 TO |
603 | if (!defined('CURLOPT_SSLCERT')) { |
604 | CRM_Core_Error::fatal(ts('PayFlowPro requires curl with SSL support')); | |
605 | } | |
606 | ||
607 | /* | |
608 | * define variables for connecting with the gateway | |
609 | */ | |
ceb10dc7 | 610 | |
6a488035 TO |
611 | //if you have not set up a separate user account the vendor name is used as the username |
612 | if (!$this->_paymentProcessor['subject']) { | |
613 | $user = $this->_paymentProcessor['user_name']; | |
614 | } | |
615 | else { | |
616 | $user = $this->_paymentProcessor['subject']; | |
617 | } | |
618 | //$recurringProfileID = "RT0000000001"; | |
619 | // c $trythis = $this->getRecurringTransactionStatus($recurringProfileID,17); | |
620 | ||
6a488035 TO |
621 | /* |
622 | *Create the array of variables to be sent to the processor from the $params array | |
623 | * passed into this function | |
624 | * | |
625 | */ | |
626 | ||
627 | $payflow_query_array = array( | |
628 | 'USER' => $user, | |
629 | 'VENDOR' => $this->_paymentProcessor['user_name'], | |
630 | 'PARTNER' => $this->_paymentProcessor['signature'], | |
631 | 'PWD' => $this->_paymentProcessor['password'], | |
632 | // C - Direct Payment using credit card | |
633 | 'TENDER' => 'C', | |
634 | // A - Authorization, S - Sale | |
635 | 'TRXTYPE' => 'R', | |
636 | 'ACTION' => 'I', | |
637 | //A for add recurring | |
638 | //(M-modify,C-cancel,R-reactivate, | |
639 | //I-inquiry,P-payment | |
640 | 'ORIGPROFILEID' => $recurringProfileID, | |
641 | 'PAYMENTHISTORY' => 'Y', | |
642 | ); | |
643 | ||
644 | $payflow_query = $this->convert_to_nvp($payflow_query_array); | |
645 | echo $payflow_query; | |
646 | $submiturl = $this->_paymentProcessor['url_site']; | |
647 | //ie. url at payment processor to submit to. | |
648 | $responseData = self::submit_transaction($submiturl, $payflow_query); | |
649 | /* | |
ceb10dc7 | 650 | * Payment successfully sent to gateway - process the response now |
6a488035 | 651 | */ |
ceb10dc7 | 652 | |
6a488035 TO |
653 | $result = strstr($responseData, "RESULT"); |
654 | $nvpArray = array(); | |
655 | while (strlen($result)) { | |
656 | // name | |
657 | $keypos = strpos($result, '='); | |
658 | $keyval = substr($result, 0, $keypos); | |
659 | // value | |
660 | $valuepos = strpos($result, '&') ? strpos($result, '&') : strlen($result); | |
661 | $valval = substr($result, $keypos + 1, $valuepos - $keypos - 1); | |
662 | // decoding the respose | |
663 | $nvpArray[$keyval] = $valval; | |
664 | $result = substr($result, $valuepos + 1, strlen($result)); | |
665 | } | |
666 | // get the result code to validate. | |
667 | $result_code = $nvpArray['RESULT']; | |
668 | print_r($responseData); | |
669 | ||
670 | //RESPMSG=Invalid Profile ID: Invalid recurring profile ID | |
671 | //RT0000000001 | |
672 | } | |
96025800 | 673 | |
6a488035 | 674 | } |