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