Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { | |
36 | CONST CHARSET = 'iso-8859-1'; | |
37 | ||
38 | protected $_mode = NULL; | |
39 | ||
40 | /** | |
41 | * We only need one instance of this object. So we use the singleton | |
42 | * pattern and cache the instance in this variable | |
43 | * | |
44 | * @var object | |
45 | * @static | |
46 | */ | |
47 | static private $_singleton = NULL; | |
48 | ||
49 | /** | |
50 | * Constructor | |
51 | * | |
52 | * @param string $mode the mode of operation: live or test | |
53 | * | |
54 | * @return void | |
55 | */ | |
56 | function __construct($mode, &$paymentProcessor) { | |
57 | $this->_mode = $mode; | |
58 | $this->_paymentProcessor = $paymentProcessor; | |
59 | $this->_processorName = ts('PayPal Pro'); | |
60 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name'); | |
61 | ||
62 | if ($this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType)) { | |
63 | $this->_processorName = ts('PayPal Standard'); | |
64 | return; | |
65 | } | |
66 | elseif ($this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Express', $paymentProcessorType)) { | |
67 | $this->_processorName = ts('PayPal Express'); | |
68 | } | |
69 | ||
70 | if (!$this->_paymentProcessor['user_name']) { | |
71 | CRM_Core_Error::fatal(ts('Could not find user name for payment processor')); | |
72 | } | |
73 | } | |
74 | ||
75 | /** | |
76 | * singleton function used to manage this object | |
77 | * | |
78 | * @param string $mode the mode of operation: live or test | |
79 | * | |
80 | * @return object | |
81 | * @static | |
82 | * | |
83 | */ | |
84 | static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) { | |
85 | $processorName = $paymentProcessor['name']; | |
86 | if (!isset(self::$_singleton[$processorName]) || self::$_singleton[$processorName] === NULL) { | |
87 | self::$_singleton[$processorName] = new CRM_Core_Payment_PaypalImpl($mode, $paymentProcessor); | |
88 | } | |
89 | return self::$_singleton[$processorName]; | |
90 | } | |
91 | ||
92 | /** | |
93 | * express checkout code. Check PayPal documentation for more information | |
94 | * | |
95 | * @param array $params assoc array of input parameters for this transaction | |
96 | * | |
97 | * @return array the result in an nice formatted array (or an error object) | |
98 | * @public | |
99 | */ | |
100 | function setExpressCheckOut(&$params) { | |
101 | $args = array(); | |
102 | ||
103 | $this->initialize($args, 'SetExpressCheckout'); | |
104 | ||
105 | $args['paymentAction'] = $params['payment_action']; | |
106 | $args['amt'] = $params['amount']; | |
107 | $args['currencyCode'] = $params['currencyID']; | |
108 | $args['invnum'] = $params['invoiceID']; | |
109 | $args['returnURL'] = $params['returnURL']; | |
110 | $args['cancelURL'] = $params['cancelURL']; | |
111 | $args['version'] = '56.0'; | |
112 | ||
113 | //LCD if recurring, collect additional data and set some values | |
114 | if (CRM_Utils_Array::value('is_recur',$params)) { | |
115 | $args['L_BILLINGTYPE0'] = 'RecurringPayments'; | |
116 | //$args['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Recurring Contribution'; | |
117 | $args['L_BILLINGAGREEMENTDESCRIPTION0'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit']; | |
118 | $args['L_PAYMENTTYPE0'] = 'Any'; | |
119 | } | |
120 | ||
121 | // Allow further manipulation of the arguments via custom hooks .. | |
122 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $args); | |
123 | ||
124 | $result = $this->invokeAPI($args); | |
125 | ||
126 | if (is_a($result, 'CRM_Core_Error')) { | |
127 | return $result; | |
128 | } | |
129 | ||
130 | /* Success */ | |
131 | ||
132 | return $result['token']; | |
133 | } | |
134 | ||
135 | /** | |
136 | * get details from paypal. Check PayPal documentation for more information | |
137 | * | |
138 | * @param string $token the key associated with this transaction | |
139 | * | |
140 | * @return array the result in an nice formatted array (or an error object) | |
141 | * @public | |
142 | */ | |
143 | function getExpressCheckoutDetails($token) { | |
144 | $args = array(); | |
145 | ||
146 | $this->initialize($args, 'GetExpressCheckoutDetails'); | |
147 | $args['token'] = $token; | |
148 | // LCD | |
149 | $args['method'] = 'GetExpressCheckoutDetails'; | |
150 | ||
151 | $result = $this->invokeAPI($args); | |
152 | ||
153 | if (is_a($result, 'CRM_Core_Error')) { | |
154 | return $result; | |
155 | } | |
156 | ||
157 | /* Success */ | |
158 | ||
159 | $params = array(); | |
160 | $params['token'] = $result['token']; | |
161 | $params['payer_id'] = $result['payerid']; | |
162 | $params['payer_status'] = $result['payerstatus']; | |
163 | $params['first_name'] = $result['firstname']; | |
164 | $params['middle_name'] = $result['middlename']; | |
165 | $params['last_name'] = $result['lastname']; | |
166 | $params['street_address'] = $result['shiptostreet']; | |
167 | $params['supplemental_address_1'] = $result['shiptostreet2']; | |
168 | $params['city'] = $result['shiptocity']; | |
169 | $params['state_province'] = $result['shiptostate']; | |
170 | $params['postal_code'] = $result['shiptozip']; | |
171 | $params['country'] = $result['shiptocountrycode']; | |
172 | ||
173 | return $params; | |
174 | } | |
175 | ||
176 | /** | |
177 | * do the express checkout at paypal. Check PayPal documentation for more information | |
178 | * | |
179 | * @param string $token the key associated with this transaction | |
180 | * | |
181 | * @return array the result in an nice formatted array (or an error object) | |
182 | * @public | |
183 | */ | |
184 | function doExpressCheckout(&$params) { | |
185 | $args = array(); | |
186 | ||
187 | $this->initialize($args, 'DoExpressCheckoutPayment'); | |
188 | ||
189 | $args['token'] = $params['token']; | |
190 | $args['paymentAction'] = $params['payment_action']; | |
191 | $args['amt'] = $params['amount']; | |
192 | $args['currencyCode'] = $params['currencyID']; | |
193 | $args['payerID'] = $params['payer_id']; | |
194 | $args['invnum'] = $params['invoiceID']; | |
195 | $args['returnURL'] = $params['returnURL']; | |
196 | $args['cancelURL'] = $params['cancelURL']; | |
197 | ||
198 | $result = $this->invokeAPI($args); | |
199 | ||
200 | if (is_a($result, 'CRM_Core_Error')) { | |
201 | return $result; | |
202 | } | |
203 | ||
204 | /* Success */ | |
205 | ||
206 | $params['trxn_id'] = $result['transactionid']; | |
207 | $params['gross_amount'] = $result['amt']; | |
208 | $params['fee_amount'] = $result['feeamt']; | |
209 | $params['net_amount'] = $result['settleamt']; | |
210 | if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) { | |
211 | $params['net_amount'] = $params['gross_amount'] - $params['fee_amount']; | |
212 | } | |
213 | $params['payment_status'] = $result['paymentstatus']; | |
214 | $params['pending_reason'] = $result['pendingreason']; | |
215 | ||
216 | return $params; | |
217 | } | |
218 | ||
219 | //LCD add new function for handling recurring payments for PayPal Express | |
220 | function createRecurringPayments(&$params) { | |
221 | $args = array(); | |
222 | ||
223 | $this->initialize($args, 'CreateRecurringPaymentsProfile'); | |
224 | ||
225 | $start_time = strtotime(date('m/d/Y')); | |
226 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
227 | ||
228 | $args['token'] = $params['token']; | |
229 | $args['paymentAction'] = $params['payment_action']; | |
230 | $args['amt'] = $params['amount']; | |
231 | $args['currencyCode'] = $params['currencyID']; | |
232 | $args['payerID'] = $params['payer_id']; | |
233 | $args['invnum'] = $params['invoiceID']; | |
234 | $args['returnURL'] = $params['returnURL']; | |
235 | $args['cancelURL'] = $params['cancelURL']; | |
236 | $args['profilestartdate'] = $start_date; | |
237 | $args['method'] = 'CreateRecurringPaymentsProfile'; | |
238 | $args['billingfrequency'] = $params['frequency_interval']; | |
239 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
240 | $args['desc'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit']; | |
241 | //$args['desc'] = 'Recurring Contribution'; | |
242 | $args['totalbillingcycles'] = $params['installments']; | |
243 | $args['version'] = '56.0'; | |
1ac462d0 DL |
244 | $args['profilereference'] = |
245 | "i={$params['invoiceID']}" . | |
246 | "&m=$component" . | |
247 | "&c={$params['contactID']}" . | |
248 | "&r={$params['contributionRecurID']}" . | |
249 | "&b={$params['contributionID']}" . | |
250 | "&p={$params['contributionPageID']}"; | |
6a488035 TO |
251 | |
252 | $result = $this->invokeAPI($args); | |
253 | ||
254 | if (is_a($result, 'CRM_Core_Error')) { | |
255 | return $result; | |
256 | } | |
257 | ||
258 | /* Success */ | |
259 | ||
260 | $params['trxn_id'] = $result['transactionid']; | |
261 | $params['gross_amount'] = $result['amt']; | |
262 | $params['fee_amount'] = $result['feeamt']; | |
263 | $params['net_amount'] = $result['settleamt']; | |
264 | if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) { | |
265 | $params['net_amount'] = $params['gross_amount'] - $params['fee_amount']; | |
266 | } | |
267 | $params['payment_status'] = $result['paymentstatus']; | |
268 | $params['pending_reason'] = $result['pendingreason']; | |
269 | ||
270 | return $params; | |
271 | } | |
272 | //LCD end | |
273 | function initialize(&$args, $method) { | |
274 | $args['user'] = $this->_paymentProcessor['user_name']; | |
275 | $args['pwd'] = $this->_paymentProcessor['password']; | |
276 | $args['version'] = 3.0; | |
277 | $args['signature'] = $this->_paymentProcessor['signature']; | |
278 | $args['subject'] = $this->_paymentProcessor['subject']; | |
279 | $args['method'] = $method; | |
280 | } | |
281 | ||
282 | /** | |
283 | * This function collects all the information from a web/api form and invokes | |
284 | * the relevant payment processor specific functions to perform the transaction | |
285 | * | |
286 | * @param array $params assoc array of input parameters for this transaction | |
287 | * | |
288 | * @return array the result in an nice formatted array (or an error object) | |
289 | * @public | |
290 | */ | |
291 | function doDirectPayment(&$params, $component = 'contribute') { | |
292 | $args = array(); | |
293 | ||
294 | $this->initialize($args, 'DoDirectPayment'); | |
295 | ||
296 | $args['paymentAction'] = $params['payment_action']; | |
297 | $args['amt'] = $params['amount']; | |
298 | $args['currencyCode'] = $params['currencyID']; | |
299 | $args['invnum'] = $params['invoiceID']; | |
300 | $args['ipaddress'] = $params['ip_address']; | |
301 | $args['creditCardType'] = $params['credit_card_type']; | |
302 | $args['acct'] = $params['credit_card_number']; | |
303 | $args['expDate'] = sprintf('%02d', $params['month']) . $params['year']; | |
304 | $args['cvv2'] = $params['cvv2']; | |
305 | $args['firstName'] = $params['first_name']; | |
306 | $args['lastName'] = $params['last_name']; | |
307 | $args['email'] = CRM_Utils_Array::value('email', $params); | |
308 | $args['street'] = $params['street_address']; | |
309 | $args['city'] = $params['city']; | |
310 | $args['state'] = $params['state_province']; | |
311 | $args['countryCode'] = $params['country']; | |
312 | $args['zip'] = $params['postal_code']; | |
313 | $args['desc'] = CRM_Utils_Array::value('description', $params); | |
314 | $args['custom'] = CRM_Utils_Array::value('accountingCode', $params); | |
315 | ||
316 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
317 | $start_time = strtotime(date('m/d/Y')); | |
318 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
319 | ||
320 | $args['PaymentAction'] = 'Sale'; | |
321 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
322 | $args['billingfrequency'] = $params['frequency_interval']; | |
323 | $args['method'] = "CreateRecurringPaymentsProfile"; | |
324 | $args['profilestartdate'] = $start_date; | |
1ac462d0 DL |
325 | $args['desc'] = |
326 | $params['description'] . ": " . | |
327 | $params['amount'] . " Per " . | |
328 | $params['frequency_interval'] . " " . | |
329 | $params['frequency_unit']; | |
6a488035 TO |
330 | $args['amt'] = $params['amount']; |
331 | $args['totalbillingcycles'] = $params['installments']; | |
332 | $args['version'] = 56.0; | |
1ac462d0 DL |
333 | $args['PROFILEREFERENCE'] = |
334 | "i=" . $params['invoiceID'] . "&m=" . $component . | |
335 | "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] . | |
336 | "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID']; | |
6a488035 TO |
337 | } |
338 | ||
339 | // Allow further manipulation of the arguments via custom hooks .. | |
340 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $args); | |
341 | ||
342 | $result = $this->invokeAPI($args); | |
343 | ||
344 | //WAG | |
345 | if (is_a($result, 'CRM_Core_Error')) { | |
346 | return $result; | |
347 | } | |
348 | ||
349 | $params['recurr_profile_id'] = NULL; | |
350 | ||
351 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
352 | $params['recurr_profile_id'] = $result['profileid']; | |
353 | } | |
354 | ||
355 | /* Success */ | |
356 | ||
357 | $params['trxn_id'] = CRM_Utils_Array::value('transactionid', $result); | |
358 | $params['gross_amount'] = CRM_Utils_Array::value('amt', $result); | |
359 | return $params; | |
360 | } | |
361 | ||
362 | /** | |
363 | * This function checks to see if we have the right config values | |
364 | * | |
365 | * @return string the error message if any | |
366 | * @public | |
367 | */ | |
368 | function checkConfig() { | |
369 | $error = array(); | |
370 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name'); | |
1ac462d0 DL |
371 | if ( |
372 | $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType) || | |
6a488035 TO |
373 | $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal', $paymentProcessorType) |
374 | ) { | |
375 | if (empty($this->_paymentProcessor['user_name'])) { | |
0501a7d6 | 376 | $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
377 | } |
378 | } | |
379 | ||
380 | if ($this->_paymentProcessor['payment_processor_type_id'] != CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType)) { | |
381 | if (empty($this->_paymentProcessor['signature'])) { | |
0501a7d6 | 382 | $error[] = ts('Signature is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
383 | } |
384 | ||
385 | if (empty($this->_paymentProcessor['password'])) { | |
0501a7d6 | 386 | $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
387 | } |
388 | } | |
389 | ||
390 | if (!empty($error)) { | |
391 | return implode('<p>', $error); | |
392 | } | |
393 | else { | |
394 | return NULL; | |
395 | } | |
396 | } | |
397 | ||
398 | function cancelSubscriptionURL() { | |
399 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal_Standard') { | |
400 | return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor['user_name']); | |
401 | } | |
402 | else { | |
403 | return NULL; | |
404 | } | |
405 | } | |
406 | ||
407 | function isSupported($method = 'cancelSubscription') { | |
408 | if ($this->_paymentProcessor['payment_processor_type'] != 'PayPal') { | |
409 | // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported | |
410 | // by standard or express. | |
411 | return FALSE; | |
412 | } | |
413 | return parent::isSupported($method); | |
414 | } | |
415 | ||
416 | function cancelSubscription(&$message = '', $params = array( | |
417 | )) { | |
418 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { | |
419 | $args = array(); | |
420 | $this->initialize($args, 'ManageRecurringPaymentsProfileStatus'); | |
421 | ||
422 | $args['PROFILEID'] = CRM_Utils_Array::value('subscriptionId', $params); | |
423 | $args['ACTION'] = 'Cancel'; | |
424 | $args['NOTE'] = CRM_Utils_Array::value('reason', $params); | |
425 | ||
426 | $result = $this->invokeAPI($args); | |
427 | if (is_a($result, 'CRM_Core_Error')) { | |
428 | return $result; | |
429 | } | |
430 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
431 | return TRUE; | |
432 | } | |
433 | return FALSE; | |
434 | } | |
435 | ||
436 | function updateSubscriptionBillingInfo(&$message = '', $params = array( | |
437 | )) { | |
438 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { | |
439 | $config = CRM_Core_Config::singleton(); | |
440 | $args = array(); | |
441 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
442 | ||
443 | $args['PROFILEID'] = $params['subscriptionId']; | |
444 | $args['AMT'] = $params['amount']; | |
445 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
446 | $args['CREDITCARDTYPE'] = $params['credit_card_type']; | |
447 | $args['ACCT'] = $params['credit_card_number']; | |
448 | $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year']; | |
449 | $args['CVV2'] = $params['cvv2']; | |
450 | ||
451 | $args['FIRSTNAME'] = $params['first_name']; | |
452 | $args['LASTNAME'] = $params['last_name']; | |
453 | $args['STREET'] = $params['street_address']; | |
454 | $args['CITY'] = $params['city']; | |
455 | $args['STATE'] = $params['state_province']; | |
456 | $args['COUNTRYCODE'] = $params['postal_code']; | |
457 | $args['ZIP'] = $params['country']; | |
458 | ||
459 | $result = $this->invokeAPI($args); | |
460 | if (is_a($result, 'CRM_Core_Error')) { | |
461 | return $result; | |
462 | } | |
463 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
464 | return TRUE; | |
465 | } | |
466 | return FALSE; | |
467 | } | |
468 | ||
14965416 | 469 | function changeSubscriptionAmount(&$message = '', $params = array()) { |
6a488035 TO |
470 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
471 | $config = CRM_Core_Config::singleton(); | |
472 | $args = array(); | |
473 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
474 | ||
475 | $args['PROFILEID'] = $params['subscriptionId']; | |
476 | $args['AMT'] = $params['amount']; | |
477 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
478 | $args['BILLINGFREQUENCY'] = $params['installments']; | |
479 | ||
480 | $result = $this->invokeAPI($args); | |
481 | CRM_Core_Error::debug_var('$result', $result); | |
482 | if (is_a($result, 'CRM_Core_Error')) { | |
483 | return $result; | |
484 | } | |
485 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
486 | return TRUE; | |
487 | } | |
488 | return FALSE; | |
489 | } | |
490 | ||
491 | function doTransferCheckout(&$params, $component = 'contribute') { | |
492 | $config = CRM_Core_Config::singleton(); | |
493 | ||
494 | if ($component != 'contribute' && $component != 'event') { | |
495 | CRM_Core_Error::fatal(ts('Component is invalid')); | |
496 | } | |
497 | ||
498 | $notifyURL = $config->userFrameworkResourceURL . "extern/ipn.php?reset=1&contactID={$params['contactID']}" . "&contributionID={$params['contributionID']}" . "&module={$component}"; | |
499 | ||
500 | if ($component == 'event') { | |
501 | $notifyURL .= "&eventID={$params['eventID']}&participantID={$params['participantID']}"; | |
502 | } | |
503 | else { | |
504 | $membershipID = CRM_Utils_Array::value('membershipID', $params); | |
505 | if ($membershipID) { | |
506 | $notifyURL .= "&membershipID=$membershipID"; | |
507 | } | |
508 | $relatedContactID = CRM_Utils_Array::value('related_contact', $params); | |
509 | if ($relatedContactID) { | |
510 | $notifyURL .= "&relatedContactID=$relatedContactID"; | |
511 | ||
512 | $onBehalfDupeAlert = CRM_Utils_Array::value('onbehalf_dupe_alert', $params); | |
513 | if ($onBehalfDupeAlert) { | |
514 | $notifyURL .= "&onBehalfDupeAlert=$onBehalfDupeAlert"; | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
519 | $url = ($component == 'event') ? 'civicrm/event/register' : 'civicrm/contribute/transact'; | |
520 | $cancel = ($component == 'event') ? '_qf_Register_display' : '_qf_Main_display'; | |
521 | $returnURL = CRM_Utils_System::url($url, | |
522 | "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", | |
523 | TRUE, NULL, FALSE | |
524 | ); | |
525 | ||
526 | $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}"; | |
527 | if (CRM_Utils_Array::value('is_recur', $params)) { | |
1ac462d0 | 528 | $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}"; |
6a488035 TO |
529 | } |
530 | ||
1ac462d0 DL |
531 | $cancelURL = CRM_Utils_System::url( |
532 | $url, | |
6a488035 TO |
533 | $cancelUrlString, |
534 | TRUE, NULL, FALSE | |
535 | ); | |
536 | ||
537 | // ensure that the returnURL is absolute. | |
538 | if (substr($returnURL, 0, 4) != 'http') { | |
539 | $fixUrl = CRM_Utils_System::url("civicrm/admin/setting/url", '&reset=1'); | |
0501a7d6 | 540 | CRM_Core_Error::fatal(ts('Sending a relative URL to PayPalIPN is erroneous. Please make your resource URL (in <a href="%1">Administer » System Settings » Resource URLs</a> ) complete.', array(1 => $fixUrl))); |
6a488035 TO |
541 | } |
542 | ||
543 | $paypalParams = array( | |
544 | 'business' => $this->_paymentProcessor['user_name'], | |
545 | 'notify_url' => $notifyURL, | |
546 | 'item_name' => $params['item_name'], | |
547 | 'quantity' => 1, | |
548 | 'undefined_quantity' => 0, | |
549 | 'cancel_return' => $cancelURL, | |
550 | 'no_note' => 1, | |
551 | 'no_shipping' => 1, | |
552 | 'return' => $returnURL, | |
553 | 'rm' => 2, | |
554 | 'currency_code' => $params['currencyID'], | |
555 | 'invoice' => $params['invoiceID'], | |
556 | 'lc' => substr($config->lcMessages, -2), | |
557 | 'charset' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'UTF-8', | |
558 | 'custom' => CRM_Utils_Array::value('accountingCode', | |
559 | $params | |
560 | ), | |
561 | ); | |
562 | ||
563 | // add name and address if available, CRM-3130 | |
564 | $otherVars = array( | |
565 | 'first_name' => 'first_name', | |
566 | 'last_name' => 'last_name', | |
567 | 'street_address' => 'address1', | |
568 | 'country' => 'country', | |
569 | 'preferred_language' => 'lc', | |
570 | 'city' => 'city', | |
571 | 'state_province' => 'state', | |
572 | 'postal_code' => 'zip', | |
573 | 'email' => 'email', | |
574 | ); | |
575 | ||
576 | foreach (array_keys($params) as $p) { | |
577 | // get the base name without the location type suffixed to it | |
578 | $parts = explode('-', $p); | |
579 | $name = count($parts) > 1 ? $parts[0] : $p; | |
580 | if (isset($otherVars[$name])) { | |
581 | $value = $params[$p]; | |
582 | if ($value) { | |
583 | if ($name == 'state_province') { | |
584 | $stateName = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
585 | $value = $stateName; | |
586 | } | |
587 | if ($name == 'country') { | |
588 | $countryName = CRM_Core_PseudoConstant::countryIsoCode($value); | |
589 | $value = $countryName; | |
590 | } | |
591 | // ensure value is not an array | |
592 | // CRM-4174 | |
593 | if (!is_array($value)) { | |
594 | $paypalParams[$otherVars[$name]] = $value; | |
595 | } | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | // if recurring donations, add a few more items | |
601 | if (!empty($params['is_recur'])) { | |
602 | if ($params['contributionRecurID']) { | |
603 | $notifyURL .= "&contributionRecurID={$params['contributionRecurID']}&contributionPageID={$params['contributionPageID']}"; | |
604 | $paypalParams['notify_url'] = $notifyURL; | |
605 | } | |
606 | else { | |
607 | CRM_Core_Error::fatal(ts('Recurring contribution, but no database id')); | |
608 | } | |
609 | ||
610 | $paypalParams += array( | |
611 | 'cmd' => '_xclick-subscriptions', | |
14965416 DL |
612 | 'a3' => $params['amount'], |
613 | 'p3' => $params['frequency_interval'], | |
614 | 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)), | |
6a488035 TO |
615 | 'src' => 1, |
616 | 'sra' => 1, | |
14965416 | 617 | 'srt' => CRM_Utils_Array::value('installments', $params), |
6a488035 TO |
618 | 'no_note' => 1, |
619 | 'modify' => 0, | |
620 | ); | |
621 | } | |
622 | else { | |
623 | $paypalParams += array( | |
624 | 'cmd' => '_xclick', | |
625 | 'amount' => $params['amount'], | |
626 | ); | |
627 | } | |
628 | ||
629 | // Allow further manipulation of the arguments via custom hooks .. | |
630 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $paypalParams); | |
631 | ||
632 | $uri = ''; | |
633 | foreach ($paypalParams as $key => $value) { | |
634 | if ($value === NULL) { | |
635 | continue; | |
636 | } | |
637 | ||
638 | $value = urlencode($value); | |
639 | if ($key == 'return' || | |
640 | $key == 'cancel_return' || | |
641 | $key == 'notify_url' | |
642 | ) { | |
643 | $value = str_replace('%2F', '/', $value); | |
644 | } | |
645 | $uri .= "&{$key}={$value}"; | |
646 | } | |
647 | ||
648 | $uri = substr($uri, 1); | |
649 | $url = $this->_paymentProcessor['url_site']; | |
650 | $sub = empty($params['is_recur']) ? 'cgi-bin/webscr' : 'subscriptions'; | |
651 | $paypalURL = "{$url}{$sub}?$uri"; | |
652 | ||
653 | CRM_Utils_System::redirect($paypalURL); | |
654 | } | |
655 | ||
656 | /** | |
657 | * hash_call: Function to perform the API call to PayPal using API signature | |
658 | * @methodName is name of API method. | |
659 | * @nvpStr is nvp string. | |
660 | * returns an associtive array containing the response from the server. | |
661 | */ | |
662 | function invokeAPI($args, $url = NULL) { | |
663 | ||
664 | if ($url === NULL) { | |
665 | if (empty($this->_paymentProcessor['url_api'])) { | |
666 | CRM_Core_Error::fatal(ts('Please set the API URL. Please refer to the documentation for more details')); | |
667 | } | |
668 | ||
669 | $url = $this->_paymentProcessor['url_api'] . 'nvp'; | |
670 | } | |
671 | ||
672 | if (!function_exists('curl_init')) { | |
673 | CRM_Core_Error::fatal("curl functions NOT available."); | |
674 | } | |
675 | ||
676 | //setting the curl parameters. | |
677 | $ch = curl_init(); | |
678 | curl_setopt($ch, CURLOPT_URL, $url); | |
679 | curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
680 | ||
681 | //turning off the server and peer verification(TrustManager Concept). | |
682 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL')); | |
17c04b52 | 683 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL') ? 2 : 0); |
6a488035 TO |
684 | |
685 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
686 | curl_setopt($ch, CURLOPT_POST, 1); | |
687 | ||
688 | $p = array(); | |
689 | foreach ($args as $n => $v) { | |
690 | $p[] = "$n=" . urlencode($v); | |
691 | } | |
692 | ||
693 | //NVPRequest for submitting to server | |
694 | $nvpreq = implode('&', $p); | |
695 | ||
696 | //setting the nvpreq as POST FIELD to curl | |
697 | curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); | |
698 | ||
699 | //getting response from server | |
700 | $response = curl_exec($ch); | |
701 | ||
702 | //converting NVPResponse to an Associative Array | |
703 | $result = self::deformat($response); | |
704 | ||
705 | if (curl_errno($ch)) { | |
706 | $e = CRM_Core_Error::singleton(); | |
707 | $e->push(curl_errno($ch), | |
708 | 0, NULL, | |
709 | curl_error($ch) | |
710 | ); | |
711 | return $e; | |
712 | } | |
713 | else { | |
714 | curl_close($ch); | |
715 | } | |
716 | ||
717 | if (strtolower($result['ack']) != 'success' && | |
718 | strtolower($result['ack']) != 'successwithwarning' | |
719 | ) { | |
720 | $e = CRM_Core_Error::singleton(); | |
721 | $e->push($result['l_errorcode0'], | |
722 | 0, NULL, | |
723 | "{$result['l_shortmessage0']} {$result['l_longmessage0']}" | |
724 | ); | |
725 | return $e; | |
726 | } | |
727 | ||
728 | return $result; | |
729 | } | |
730 | ||
731 | /** This function will take NVPString and convert it to an Associative Array and it will decode the response. | |
732 | * It is usefull to search for a particular key and displaying arrays. | |
733 | * @nvpstr is NVPString. | |
734 | * @nvpArray is Associative Array. | |
735 | */ | |
736 | static function deformat($str) { | |
737 | $result = array(); | |
738 | ||
739 | while (strlen($str)) { | |
740 | // postion of key | |
741 | $keyPos = strpos($str, '='); | |
742 | ||
743 | // position of value | |
744 | $valPos = strpos($str, '&') ? strpos($str, '&') : strlen($str); | |
745 | ||
746 | /*getting the Key and Value values and storing in a Associative Array*/ | |
747 | ||
748 | $key = substr($str, 0, $keyPos); | |
749 | $val = substr($str, $keyPos + 1, $valPos - $keyPos - 1); | |
750 | ||
751 | //decoding the respose | |
752 | $result[strtolower(urldecode($key))] = urldecode($val); | |
753 | $str = substr($str, $valPos + 1, strlen($str)); | |
754 | } | |
755 | ||
756 | return $result; | |
757 | } | |
758 | } | |
759 |