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'; | |
244 | $args['profilereference'] = "i={$params['invoiceID']}" . "&m=$component" . "&c={$params['contactID']}" . "&r={$params['contributionRecurID']}" . "&b={$params['contributionID']}" . "&p={$params['contributionPageID']}"; | |
245 | ||
246 | $result = $this->invokeAPI($args); | |
247 | ||
248 | if (is_a($result, 'CRM_Core_Error')) { | |
249 | return $result; | |
250 | } | |
251 | ||
252 | /* Success */ | |
253 | ||
254 | $params['trxn_id'] = $result['transactionid']; | |
255 | $params['gross_amount'] = $result['amt']; | |
256 | $params['fee_amount'] = $result['feeamt']; | |
257 | $params['net_amount'] = $result['settleamt']; | |
258 | if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) { | |
259 | $params['net_amount'] = $params['gross_amount'] - $params['fee_amount']; | |
260 | } | |
261 | $params['payment_status'] = $result['paymentstatus']; | |
262 | $params['pending_reason'] = $result['pendingreason']; | |
263 | ||
264 | return $params; | |
265 | } | |
266 | //LCD end | |
267 | function initialize(&$args, $method) { | |
268 | $args['user'] = $this->_paymentProcessor['user_name']; | |
269 | $args['pwd'] = $this->_paymentProcessor['password']; | |
270 | $args['version'] = 3.0; | |
271 | $args['signature'] = $this->_paymentProcessor['signature']; | |
272 | $args['subject'] = $this->_paymentProcessor['subject']; | |
273 | $args['method'] = $method; | |
274 | } | |
275 | ||
276 | /** | |
277 | * This function collects all the information from a web/api form and invokes | |
278 | * the relevant payment processor specific functions to perform the transaction | |
279 | * | |
280 | * @param array $params assoc array of input parameters for this transaction | |
281 | * | |
282 | * @return array the result in an nice formatted array (or an error object) | |
283 | * @public | |
284 | */ | |
285 | function doDirectPayment(&$params, $component = 'contribute') { | |
286 | $args = array(); | |
287 | ||
288 | $this->initialize($args, 'DoDirectPayment'); | |
289 | ||
290 | $args['paymentAction'] = $params['payment_action']; | |
291 | $args['amt'] = $params['amount']; | |
292 | $args['currencyCode'] = $params['currencyID']; | |
293 | $args['invnum'] = $params['invoiceID']; | |
294 | $args['ipaddress'] = $params['ip_address']; | |
295 | $args['creditCardType'] = $params['credit_card_type']; | |
296 | $args['acct'] = $params['credit_card_number']; | |
297 | $args['expDate'] = sprintf('%02d', $params['month']) . $params['year']; | |
298 | $args['cvv2'] = $params['cvv2']; | |
299 | $args['firstName'] = $params['first_name']; | |
300 | $args['lastName'] = $params['last_name']; | |
301 | $args['email'] = CRM_Utils_Array::value('email', $params); | |
302 | $args['street'] = $params['street_address']; | |
303 | $args['city'] = $params['city']; | |
304 | $args['state'] = $params['state_province']; | |
305 | $args['countryCode'] = $params['country']; | |
306 | $args['zip'] = $params['postal_code']; | |
307 | $args['desc'] = CRM_Utils_Array::value('description', $params); | |
308 | $args['custom'] = CRM_Utils_Array::value('accountingCode', $params); | |
309 | ||
310 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
311 | $start_time = strtotime(date('m/d/Y')); | |
312 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
313 | ||
314 | $args['PaymentAction'] = 'Sale'; | |
315 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
316 | $args['billingfrequency'] = $params['frequency_interval']; | |
317 | $args['method'] = "CreateRecurringPaymentsProfile"; | |
318 | $args['profilestartdate'] = $start_date; | |
319 | $args['desc'] = $params['description'] . ": " . $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit']; | |
320 | $args['amt'] = $params['amount']; | |
321 | $args['totalbillingcycles'] = $params['installments']; | |
322 | $args['version'] = 56.0; | |
323 | $args['PROFILEREFERENCE'] = "i=" . $params['invoiceID'] . "&m=" . $component . "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] . "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID']; | |
324 | } | |
325 | ||
326 | // Allow further manipulation of the arguments via custom hooks .. | |
327 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $args); | |
328 | ||
329 | $result = $this->invokeAPI($args); | |
330 | ||
331 | //WAG | |
332 | if (is_a($result, 'CRM_Core_Error')) { | |
333 | return $result; | |
334 | } | |
335 | ||
336 | $params['recurr_profile_id'] = NULL; | |
337 | ||
338 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
339 | $params['recurr_profile_id'] = $result['profileid']; | |
340 | } | |
341 | ||
342 | /* Success */ | |
343 | ||
344 | $params['trxn_id'] = CRM_Utils_Array::value('transactionid', $result); | |
345 | $params['gross_amount'] = CRM_Utils_Array::value('amt', $result); | |
346 | return $params; | |
347 | } | |
348 | ||
349 | /** | |
350 | * This function checks to see if we have the right config values | |
351 | * | |
352 | * @return string the error message if any | |
353 | * @public | |
354 | */ | |
355 | function checkConfig() { | |
356 | $error = array(); | |
357 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(false, null, 'name'); | |
358 | if ($this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType) || | |
359 | $this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal', $paymentProcessorType) | |
360 | ) { | |
361 | if (empty($this->_paymentProcessor['user_name'])) { | |
362 | $error[] = ts('User Name is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
363 | } | |
364 | } | |
365 | ||
366 | if ($this->_paymentProcessor['payment_processor_type_id'] != CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType)) { | |
367 | if (empty($this->_paymentProcessor['signature'])) { | |
368 | $error[] = ts('Signature is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
369 | } | |
370 | ||
371 | if (empty($this->_paymentProcessor['password'])) { | |
372 | $error[] = ts('Password is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
373 | } | |
374 | } | |
375 | ||
376 | if (!empty($error)) { | |
377 | return implode('<p>', $error); | |
378 | } | |
379 | else { | |
380 | return NULL; | |
381 | } | |
382 | } | |
383 | ||
384 | function cancelSubscriptionURL() { | |
385 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal_Standard') { | |
386 | return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor['user_name']); | |
387 | } | |
388 | else { | |
389 | return NULL; | |
390 | } | |
391 | } | |
392 | ||
393 | function isSupported($method = 'cancelSubscription') { | |
394 | if ($this->_paymentProcessor['payment_processor_type'] != 'PayPal') { | |
395 | // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported | |
396 | // by standard or express. | |
397 | return FALSE; | |
398 | } | |
399 | return parent::isSupported($method); | |
400 | } | |
401 | ||
402 | function cancelSubscription(&$message = '', $params = array( | |
403 | )) { | |
404 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { | |
405 | $args = array(); | |
406 | $this->initialize($args, 'ManageRecurringPaymentsProfileStatus'); | |
407 | ||
408 | $args['PROFILEID'] = CRM_Utils_Array::value('subscriptionId', $params); | |
409 | $args['ACTION'] = 'Cancel'; | |
410 | $args['NOTE'] = CRM_Utils_Array::value('reason', $params); | |
411 | ||
412 | $result = $this->invokeAPI($args); | |
413 | if (is_a($result, 'CRM_Core_Error')) { | |
414 | return $result; | |
415 | } | |
416 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
417 | return TRUE; | |
418 | } | |
419 | return FALSE; | |
420 | } | |
421 | ||
422 | function updateSubscriptionBillingInfo(&$message = '', $params = array( | |
423 | )) { | |
424 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { | |
425 | $config = CRM_Core_Config::singleton(); | |
426 | $args = array(); | |
427 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
428 | ||
429 | $args['PROFILEID'] = $params['subscriptionId']; | |
430 | $args['AMT'] = $params['amount']; | |
431 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
432 | $args['CREDITCARDTYPE'] = $params['credit_card_type']; | |
433 | $args['ACCT'] = $params['credit_card_number']; | |
434 | $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year']; | |
435 | $args['CVV2'] = $params['cvv2']; | |
436 | ||
437 | $args['FIRSTNAME'] = $params['first_name']; | |
438 | $args['LASTNAME'] = $params['last_name']; | |
439 | $args['STREET'] = $params['street_address']; | |
440 | $args['CITY'] = $params['city']; | |
441 | $args['STATE'] = $params['state_province']; | |
442 | $args['COUNTRYCODE'] = $params['postal_code']; | |
443 | $args['ZIP'] = $params['country']; | |
444 | ||
445 | $result = $this->invokeAPI($args); | |
446 | if (is_a($result, 'CRM_Core_Error')) { | |
447 | return $result; | |
448 | } | |
449 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
450 | return TRUE; | |
451 | } | |
452 | return FALSE; | |
453 | } | |
454 | ||
455 | function changeSubscriptionAmount(&$message = '', $params = array( | |
456 | )) { | |
457 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { | |
458 | $config = CRM_Core_Config::singleton(); | |
459 | $args = array(); | |
460 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
461 | ||
462 | $args['PROFILEID'] = $params['subscriptionId']; | |
463 | $args['AMT'] = $params['amount']; | |
464 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
465 | $args['BILLINGFREQUENCY'] = $params['installments']; | |
466 | ||
467 | $result = $this->invokeAPI($args); | |
468 | CRM_Core_Error::debug_var('$result', $result); | |
469 | if (is_a($result, 'CRM_Core_Error')) { | |
470 | return $result; | |
471 | } | |
472 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
473 | return TRUE; | |
474 | } | |
475 | return FALSE; | |
476 | } | |
477 | ||
478 | function doTransferCheckout(&$params, $component = 'contribute') { | |
479 | $config = CRM_Core_Config::singleton(); | |
480 | ||
481 | if ($component != 'contribute' && $component != 'event') { | |
482 | CRM_Core_Error::fatal(ts('Component is invalid')); | |
483 | } | |
484 | ||
485 | $notifyURL = $config->userFrameworkResourceURL . "extern/ipn.php?reset=1&contactID={$params['contactID']}" . "&contributionID={$params['contributionID']}" . "&module={$component}"; | |
486 | ||
487 | if ($component == 'event') { | |
488 | $notifyURL .= "&eventID={$params['eventID']}&participantID={$params['participantID']}"; | |
489 | } | |
490 | else { | |
491 | $membershipID = CRM_Utils_Array::value('membershipID', $params); | |
492 | if ($membershipID) { | |
493 | $notifyURL .= "&membershipID=$membershipID"; | |
494 | } | |
495 | $relatedContactID = CRM_Utils_Array::value('related_contact', $params); | |
496 | if ($relatedContactID) { | |
497 | $notifyURL .= "&relatedContactID=$relatedContactID"; | |
498 | ||
499 | $onBehalfDupeAlert = CRM_Utils_Array::value('onbehalf_dupe_alert', $params); | |
500 | if ($onBehalfDupeAlert) { | |
501 | $notifyURL .= "&onBehalfDupeAlert=$onBehalfDupeAlert"; | |
502 | } | |
503 | } | |
504 | } | |
505 | ||
506 | $url = ($component == 'event') ? 'civicrm/event/register' : 'civicrm/contribute/transact'; | |
507 | $cancel = ($component == 'event') ? '_qf_Register_display' : '_qf_Main_display'; | |
508 | $returnURL = CRM_Utils_System::url($url, | |
509 | "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", | |
510 | TRUE, NULL, FALSE | |
511 | ); | |
512 | ||
513 | $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}"; | |
514 | if (CRM_Utils_Array::value('is_recur', $params)) { | |
515 | $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params[contributionID]}"; | |
516 | } | |
517 | ||
518 | $cancelURL = CRM_Utils_System::url($url, | |
519 | $cancelUrlString, | |
520 | TRUE, NULL, FALSE | |
521 | ); | |
522 | ||
523 | // ensure that the returnURL is absolute. | |
524 | if (substr($returnURL, 0, 4) != 'http') { | |
525 | $fixUrl = CRM_Utils_System::url("civicrm/admin/setting/url", '&reset=1'); | |
526 | CRM_Core_Error::fatal(ts('Sending a relative URL to PayPalIPN is erroneous. Please make your resource URL (in <a href="%1">Administer CiviCRM » System Settings » Resource URLs</a> ) complete.', array(1 => $fixUrl))); | |
527 | } | |
528 | ||
529 | $paypalParams = array( | |
530 | 'business' => $this->_paymentProcessor['user_name'], | |
531 | 'notify_url' => $notifyURL, | |
532 | 'item_name' => $params['item_name'], | |
533 | 'quantity' => 1, | |
534 | 'undefined_quantity' => 0, | |
535 | 'cancel_return' => $cancelURL, | |
536 | 'no_note' => 1, | |
537 | 'no_shipping' => 1, | |
538 | 'return' => $returnURL, | |
539 | 'rm' => 2, | |
540 | 'currency_code' => $params['currencyID'], | |
541 | 'invoice' => $params['invoiceID'], | |
542 | 'lc' => substr($config->lcMessages, -2), | |
543 | 'charset' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'UTF-8', | |
544 | 'custom' => CRM_Utils_Array::value('accountingCode', | |
545 | $params | |
546 | ), | |
547 | ); | |
548 | ||
549 | // add name and address if available, CRM-3130 | |
550 | $otherVars = array( | |
551 | 'first_name' => 'first_name', | |
552 | 'last_name' => 'last_name', | |
553 | 'street_address' => 'address1', | |
554 | 'country' => 'country', | |
555 | 'preferred_language' => 'lc', | |
556 | 'city' => 'city', | |
557 | 'state_province' => 'state', | |
558 | 'postal_code' => 'zip', | |
559 | 'email' => 'email', | |
560 | ); | |
561 | ||
562 | foreach (array_keys($params) as $p) { | |
563 | // get the base name without the location type suffixed to it | |
564 | $parts = explode('-', $p); | |
565 | $name = count($parts) > 1 ? $parts[0] : $p; | |
566 | if (isset($otherVars[$name])) { | |
567 | $value = $params[$p]; | |
568 | if ($value) { | |
569 | if ($name == 'state_province') { | |
570 | $stateName = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
571 | $value = $stateName; | |
572 | } | |
573 | if ($name == 'country') { | |
574 | $countryName = CRM_Core_PseudoConstant::countryIsoCode($value); | |
575 | $value = $countryName; | |
576 | } | |
577 | // ensure value is not an array | |
578 | // CRM-4174 | |
579 | if (!is_array($value)) { | |
580 | $paypalParams[$otherVars[$name]] = $value; | |
581 | } | |
582 | } | |
583 | } | |
584 | } | |
585 | ||
586 | // if recurring donations, add a few more items | |
587 | if (!empty($params['is_recur'])) { | |
588 | if ($params['contributionRecurID']) { | |
589 | $notifyURL .= "&contributionRecurID={$params['contributionRecurID']}&contributionPageID={$params['contributionPageID']}"; | |
590 | $paypalParams['notify_url'] = $notifyURL; | |
591 | } | |
592 | else { | |
593 | CRM_Core_Error::fatal(ts('Recurring contribution, but no database id')); | |
594 | } | |
595 | ||
596 | $paypalParams += array( | |
597 | 'cmd' => '_xclick-subscriptions', | |
598 | 'a3' => $params['amount'], | |
599 | 'p3' => $params['frequency_interval'], | |
600 | 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)), | |
601 | 'src' => 1, | |
602 | 'sra' => 1, | |
603 | 'srt' => ($params['installments'] > 0) ? $params['installments'] : NULL, | |
604 | 'no_note' => 1, | |
605 | 'modify' => 0, | |
606 | ); | |
607 | } | |
608 | else { | |
609 | $paypalParams += array( | |
610 | 'cmd' => '_xclick', | |
611 | 'amount' => $params['amount'], | |
612 | ); | |
613 | } | |
614 | ||
615 | // Allow further manipulation of the arguments via custom hooks .. | |
616 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $paypalParams); | |
617 | ||
618 | $uri = ''; | |
619 | foreach ($paypalParams as $key => $value) { | |
620 | if ($value === NULL) { | |
621 | continue; | |
622 | } | |
623 | ||
624 | $value = urlencode($value); | |
625 | if ($key == 'return' || | |
626 | $key == 'cancel_return' || | |
627 | $key == 'notify_url' | |
628 | ) { | |
629 | $value = str_replace('%2F', '/', $value); | |
630 | } | |
631 | $uri .= "&{$key}={$value}"; | |
632 | } | |
633 | ||
634 | $uri = substr($uri, 1); | |
635 | $url = $this->_paymentProcessor['url_site']; | |
636 | $sub = empty($params['is_recur']) ? 'cgi-bin/webscr' : 'subscriptions'; | |
637 | $paypalURL = "{$url}{$sub}?$uri"; | |
638 | ||
639 | CRM_Utils_System::redirect($paypalURL); | |
640 | } | |
641 | ||
642 | /** | |
643 | * hash_call: Function to perform the API call to PayPal using API signature | |
644 | * @methodName is name of API method. | |
645 | * @nvpStr is nvp string. | |
646 | * returns an associtive array containing the response from the server. | |
647 | */ | |
648 | function invokeAPI($args, $url = NULL) { | |
649 | ||
650 | if ($url === NULL) { | |
651 | if (empty($this->_paymentProcessor['url_api'])) { | |
652 | CRM_Core_Error::fatal(ts('Please set the API URL. Please refer to the documentation for more details')); | |
653 | } | |
654 | ||
655 | $url = $this->_paymentProcessor['url_api'] . 'nvp'; | |
656 | } | |
657 | ||
658 | if (!function_exists('curl_init')) { | |
659 | CRM_Core_Error::fatal("curl functions NOT available."); | |
660 | } | |
661 | ||
662 | //setting the curl parameters. | |
663 | $ch = curl_init(); | |
664 | curl_setopt($ch, CURLOPT_URL, $url); | |
665 | curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
666 | ||
667 | //turning off the server and peer verification(TrustManager Concept). | |
668 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL')); | |
669 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); | |
670 | ||
671 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
672 | curl_setopt($ch, CURLOPT_POST, 1); | |
673 | ||
674 | $p = array(); | |
675 | foreach ($args as $n => $v) { | |
676 | $p[] = "$n=" . urlencode($v); | |
677 | } | |
678 | ||
679 | //NVPRequest for submitting to server | |
680 | $nvpreq = implode('&', $p); | |
681 | ||
682 | //setting the nvpreq as POST FIELD to curl | |
683 | curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); | |
684 | ||
685 | //getting response from server | |
686 | $response = curl_exec($ch); | |
687 | ||
688 | //converting NVPResponse to an Associative Array | |
689 | $result = self::deformat($response); | |
690 | ||
691 | if (curl_errno($ch)) { | |
692 | $e = CRM_Core_Error::singleton(); | |
693 | $e->push(curl_errno($ch), | |
694 | 0, NULL, | |
695 | curl_error($ch) | |
696 | ); | |
697 | return $e; | |
698 | } | |
699 | else { | |
700 | curl_close($ch); | |
701 | } | |
702 | ||
703 | if (strtolower($result['ack']) != 'success' && | |
704 | strtolower($result['ack']) != 'successwithwarning' | |
705 | ) { | |
706 | $e = CRM_Core_Error::singleton(); | |
707 | $e->push($result['l_errorcode0'], | |
708 | 0, NULL, | |
709 | "{$result['l_shortmessage0']} {$result['l_longmessage0']}" | |
710 | ); | |
711 | return $e; | |
712 | } | |
713 | ||
714 | return $result; | |
715 | } | |
716 | ||
717 | /** This function will take NVPString and convert it to an Associative Array and it will decode the response. | |
718 | * It is usefull to search for a particular key and displaying arrays. | |
719 | * @nvpstr is NVPString. | |
720 | * @nvpArray is Associative Array. | |
721 | */ | |
722 | static function deformat($str) { | |
723 | $result = array(); | |
724 | ||
725 | while (strlen($str)) { | |
726 | // postion of key | |
727 | $keyPos = strpos($str, '='); | |
728 | ||
729 | // position of value | |
730 | $valPos = strpos($str, '&') ? strpos($str, '&') : strlen($str); | |
731 | ||
732 | /*getting the Key and Value values and storing in a Associative Array*/ | |
733 | ||
734 | $key = substr($str, 0, $keyPos); | |
735 | $val = substr($str, $keyPos + 1, $valPos - $keyPos - 1); | |
736 | ||
737 | //decoding the respose | |
738 | $result[strtolower(urldecode($key))] = urldecode($val); | |
739 | $str = substr($str, $valPos + 1, strlen($str)); | |
740 | } | |
741 | ||
742 | return $result; | |
743 | } | |
744 | } | |
745 |