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 | /** | |
100fef9d | 78 | * Singleton function used to manage this object |
6a488035 TO |
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 | 97 | /** |
100fef9d | 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 |
fbcb6fba EM |
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 | 107 | /** |
100fef9d | 108 | * Express checkout code. Check PayPal documentation for more information |
6a488035 TO |
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 | /** | |
100fef9d | 152 | * Get details from paypal. Check PayPal documentation for more information |
6a488035 TO |
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 | /** | |
100fef9d | 193 | * Do the express checkout at paypal. Check PayPal documentation for more information |
6a488035 | 194 | * |
c490a46a | 195 | * @param array $params |
da6b46f4 EM |
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 | 239 | /** |
c490a46a | 240 | * @param array $params |
6c786a9b EM |
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 | 439 | /** |
100fef9d | 440 | * Check whether a method is present ( & supported ) by the payment processor object. |
b5c2afd0 EM |
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 | */ | |
c490a46a | 462 | function cancelSubscription(&$message = '', $params = array()) { |
6a488035 TO |
463 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
464 | $args = array(); | |
465 | $this->initialize($args, 'ManageRecurringPaymentsProfileStatus'); | |
466 | ||
467 | $args['PROFILEID'] = CRM_Utils_Array::value('subscriptionId', $params); | |
468 | $args['ACTION'] = 'Cancel'; | |
469 | $args['NOTE'] = CRM_Utils_Array::value('reason', $params); | |
470 | ||
471 | $result = $this->invokeAPI($args); | |
472 | if (is_a($result, 'CRM_Core_Error')) { | |
473 | return $result; | |
474 | } | |
475 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
476 | return TRUE; | |
477 | } | |
478 | return FALSE; | |
479 | } | |
480 | ||
6c786a9b EM |
481 | /** |
482 | * @param string $message | |
483 | * @param array $params | |
484 | * | |
485 | * @return array|bool|object | |
486 | */ | |
c490a46a | 487 | function updateSubscriptionBillingInfo(&$message = '', $params = array()) { |
6a488035 TO |
488 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
489 | $config = CRM_Core_Config::singleton(); | |
490 | $args = array(); | |
491 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
492 | ||
493 | $args['PROFILEID'] = $params['subscriptionId']; | |
494 | $args['AMT'] = $params['amount']; | |
495 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
496 | $args['CREDITCARDTYPE'] = $params['credit_card_type']; | |
497 | $args['ACCT'] = $params['credit_card_number']; | |
498 | $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year']; | |
499 | $args['CVV2'] = $params['cvv2']; | |
500 | ||
501 | $args['FIRSTNAME'] = $params['first_name']; | |
502 | $args['LASTNAME'] = $params['last_name']; | |
503 | $args['STREET'] = $params['street_address']; | |
504 | $args['CITY'] = $params['city']; | |
505 | $args['STATE'] = $params['state_province']; | |
506 | $args['COUNTRYCODE'] = $params['postal_code']; | |
507 | $args['ZIP'] = $params['country']; | |
508 | ||
509 | $result = $this->invokeAPI($args); | |
510 | if (is_a($result, 'CRM_Core_Error')) { | |
511 | return $result; | |
512 | } | |
513 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
514 | return TRUE; | |
515 | } | |
516 | return FALSE; | |
517 | } | |
518 | ||
6c786a9b EM |
519 | /** |
520 | * @param string $message | |
521 | * @param array $params | |
522 | * | |
523 | * @return array|bool|object | |
524 | */ | |
14965416 | 525 | function changeSubscriptionAmount(&$message = '', $params = array()) { |
6a488035 TO |
526 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
527 | $config = CRM_Core_Config::singleton(); | |
528 | $args = array(); | |
529 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
530 | ||
531 | $args['PROFILEID'] = $params['subscriptionId']; | |
532 | $args['AMT'] = $params['amount']; | |
533 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
534 | $args['BILLINGFREQUENCY'] = $params['installments']; | |
535 | ||
536 | $result = $this->invokeAPI($args); | |
537 | CRM_Core_Error::debug_var('$result', $result); | |
538 | if (is_a($result, 'CRM_Core_Error')) { | |
539 | return $result; | |
540 | } | |
541 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
542 | return TRUE; | |
543 | } | |
544 | return FALSE; | |
545 | } | |
546 | ||
6c786a9b | 547 | /** |
c490a46a | 548 | * @param array $params |
6c786a9b EM |
549 | * @param string $component |
550 | * | |
551 | * @throws Exception | |
552 | */ | |
6a488035 TO |
553 | function doTransferCheckout(&$params, $component = 'contribute') { |
554 | $config = CRM_Core_Config::singleton(); | |
555 | ||
556 | if ($component != 'contribute' && $component != 'event') { | |
557 | CRM_Core_Error::fatal(ts('Component is invalid')); | |
558 | } | |
559 | ||
560 | $notifyURL = $config->userFrameworkResourceURL . "extern/ipn.php?reset=1&contactID={$params['contactID']}" . "&contributionID={$params['contributionID']}" . "&module={$component}"; | |
561 | ||
562 | if ($component == 'event') { | |
563 | $notifyURL .= "&eventID={$params['eventID']}&participantID={$params['participantID']}"; | |
564 | } | |
565 | else { | |
566 | $membershipID = CRM_Utils_Array::value('membershipID', $params); | |
567 | if ($membershipID) { | |
568 | $notifyURL .= "&membershipID=$membershipID"; | |
569 | } | |
570 | $relatedContactID = CRM_Utils_Array::value('related_contact', $params); | |
571 | if ($relatedContactID) { | |
572 | $notifyURL .= "&relatedContactID=$relatedContactID"; | |
573 | ||
574 | $onBehalfDupeAlert = CRM_Utils_Array::value('onbehalf_dupe_alert', $params); | |
575 | if ($onBehalfDupeAlert) { | |
576 | $notifyURL .= "&onBehalfDupeAlert=$onBehalfDupeAlert"; | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
581 | $url = ($component == 'event') ? 'civicrm/event/register' : 'civicrm/contribute/transact'; | |
582 | $cancel = ($component == 'event') ? '_qf_Register_display' : '_qf_Main_display'; | |
583 | $returnURL = CRM_Utils_System::url($url, | |
584 | "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", | |
585 | TRUE, NULL, FALSE | |
586 | ); | |
587 | ||
588 | $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}"; | |
a7488080 | 589 | if (!empty($params['is_recur'])) { |
1ac462d0 | 590 | $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}"; |
6a488035 TO |
591 | } |
592 | ||
1ac462d0 DL |
593 | $cancelURL = CRM_Utils_System::url( |
594 | $url, | |
6a488035 TO |
595 | $cancelUrlString, |
596 | TRUE, NULL, FALSE | |
597 | ); | |
598 | ||
599 | // ensure that the returnURL is absolute. | |
600 | if (substr($returnURL, 0, 4) != 'http') { | |
601 | $fixUrl = CRM_Utils_System::url("civicrm/admin/setting/url", '&reset=1'); | |
0501a7d6 | 602 | 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 |
603 | } |
604 | ||
605 | $paypalParams = array( | |
606 | 'business' => $this->_paymentProcessor['user_name'], | |
607 | 'notify_url' => $notifyURL, | |
608 | 'item_name' => $params['item_name'], | |
609 | 'quantity' => 1, | |
610 | 'undefined_quantity' => 0, | |
611 | 'cancel_return' => $cancelURL, | |
612 | 'no_note' => 1, | |
613 | 'no_shipping' => 1, | |
614 | 'return' => $returnURL, | |
615 | 'rm' => 2, | |
616 | 'currency_code' => $params['currencyID'], | |
617 | 'invoice' => $params['invoiceID'], | |
618 | 'lc' => substr($config->lcMessages, -2), | |
619 | 'charset' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'UTF-8', | |
620 | 'custom' => CRM_Utils_Array::value('accountingCode', | |
621 | $params | |
622 | ), | |
623 | ); | |
624 | ||
625 | // add name and address if available, CRM-3130 | |
626 | $otherVars = array( | |
627 | 'first_name' => 'first_name', | |
628 | 'last_name' => 'last_name', | |
629 | 'street_address' => 'address1', | |
630 | 'country' => 'country', | |
631 | 'preferred_language' => 'lc', | |
632 | 'city' => 'city', | |
633 | 'state_province' => 'state', | |
634 | 'postal_code' => 'zip', | |
635 | 'email' => 'email', | |
636 | ); | |
637 | ||
638 | foreach (array_keys($params) as $p) { | |
639 | // get the base name without the location type suffixed to it | |
640 | $parts = explode('-', $p); | |
641 | $name = count($parts) > 1 ? $parts[0] : $p; | |
642 | if (isset($otherVars[$name])) { | |
643 | $value = $params[$p]; | |
644 | if ($value) { | |
645 | if ($name == 'state_province') { | |
646 | $stateName = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
647 | $value = $stateName; | |
648 | } | |
649 | if ($name == 'country') { | |
650 | $countryName = CRM_Core_PseudoConstant::countryIsoCode($value); | |
651 | $value = $countryName; | |
652 | } | |
653 | // ensure value is not an array | |
654 | // CRM-4174 | |
655 | if (!is_array($value)) { | |
656 | $paypalParams[$otherVars[$name]] = $value; | |
657 | } | |
658 | } | |
659 | } | |
660 | } | |
661 | ||
662 | // if recurring donations, add a few more items | |
663 | if (!empty($params['is_recur'])) { | |
664 | if ($params['contributionRecurID']) { | |
665 | $notifyURL .= "&contributionRecurID={$params['contributionRecurID']}&contributionPageID={$params['contributionPageID']}"; | |
666 | $paypalParams['notify_url'] = $notifyURL; | |
667 | } | |
668 | else { | |
669 | CRM_Core_Error::fatal(ts('Recurring contribution, but no database id')); | |
670 | } | |
671 | ||
672 | $paypalParams += array( | |
673 | 'cmd' => '_xclick-subscriptions', | |
14965416 DL |
674 | 'a3' => $params['amount'], |
675 | 'p3' => $params['frequency_interval'], | |
676 | 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)), | |
6a488035 TO |
677 | 'src' => 1, |
678 | 'sra' => 1, | |
14965416 | 679 | 'srt' => CRM_Utils_Array::value('installments', $params), |
6a488035 TO |
680 | 'no_note' => 1, |
681 | 'modify' => 0, | |
682 | ); | |
683 | } | |
684 | else { | |
685 | $paypalParams += array( | |
686 | 'cmd' => '_xclick', | |
687 | 'amount' => $params['amount'], | |
688 | ); | |
689 | } | |
690 | ||
691 | // Allow further manipulation of the arguments via custom hooks .. | |
692 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $paypalParams); | |
693 | ||
694 | $uri = ''; | |
695 | foreach ($paypalParams as $key => $value) { | |
696 | if ($value === NULL) { | |
697 | continue; | |
698 | } | |
699 | ||
700 | $value = urlencode($value); | |
701 | if ($key == 'return' || | |
702 | $key == 'cancel_return' || | |
703 | $key == 'notify_url' | |
704 | ) { | |
705 | $value = str_replace('%2F', '/', $value); | |
706 | } | |
707 | $uri .= "&{$key}={$value}"; | |
708 | } | |
709 | ||
710 | $uri = substr($uri, 1); | |
711 | $url = $this->_paymentProcessor['url_site']; | |
712 | $sub = empty($params['is_recur']) ? 'cgi-bin/webscr' : 'subscriptions'; | |
713 | $paypalURL = "{$url}{$sub}?$uri"; | |
714 | ||
715 | CRM_Utils_System::redirect($paypalURL); | |
716 | } | |
717 | ||
718 | /** | |
100fef9d | 719 | * Hash_call: Function to perform the API call to PayPal using API signature |
6a488035 TO |
720 | * @methodName is name of API method. |
721 | * @nvpStr is nvp string. | |
722 | * returns an associtive array containing the response from the server. | |
723 | */ | |
724 | function invokeAPI($args, $url = NULL) { | |
725 | ||
726 | if ($url === NULL) { | |
727 | if (empty($this->_paymentProcessor['url_api'])) { | |
728 | CRM_Core_Error::fatal(ts('Please set the API URL. Please refer to the documentation for more details')); | |
729 | } | |
730 | ||
731 | $url = $this->_paymentProcessor['url_api'] . 'nvp'; | |
732 | } | |
733 | ||
734 | if (!function_exists('curl_init')) { | |
735 | CRM_Core_Error::fatal("curl functions NOT available."); | |
736 | } | |
737 | ||
738 | //setting the curl parameters. | |
739 | $ch = curl_init(); | |
740 | curl_setopt($ch, CURLOPT_URL, $url); | |
741 | curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
742 | ||
743 | //turning off the server and peer verification(TrustManager Concept). | |
744 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL')); | |
17c04b52 | 745 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL') ? 2 : 0); |
6a488035 TO |
746 | |
747 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
748 | curl_setopt($ch, CURLOPT_POST, 1); | |
749 | ||
750 | $p = array(); | |
751 | foreach ($args as $n => $v) { | |
752 | $p[] = "$n=" . urlencode($v); | |
753 | } | |
754 | ||
755 | //NVPRequest for submitting to server | |
756 | $nvpreq = implode('&', $p); | |
757 | ||
758 | //setting the nvpreq as POST FIELD to curl | |
759 | curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); | |
760 | ||
761 | //getting response from server | |
762 | $response = curl_exec($ch); | |
763 | ||
764 | //converting NVPResponse to an Associative Array | |
765 | $result = self::deformat($response); | |
766 | ||
767 | if (curl_errno($ch)) { | |
768 | $e = CRM_Core_Error::singleton(); | |
769 | $e->push(curl_errno($ch), | |
770 | 0, NULL, | |
771 | curl_error($ch) | |
772 | ); | |
773 | return $e; | |
774 | } | |
775 | else { | |
776 | curl_close($ch); | |
777 | } | |
778 | ||
779 | if (strtolower($result['ack']) != 'success' && | |
780 | strtolower($result['ack']) != 'successwithwarning' | |
781 | ) { | |
782 | $e = CRM_Core_Error::singleton(); | |
783 | $e->push($result['l_errorcode0'], | |
784 | 0, NULL, | |
785 | "{$result['l_shortmessage0']} {$result['l_longmessage0']}" | |
786 | ); | |
787 | return $e; | |
788 | } | |
789 | ||
790 | return $result; | |
791 | } | |
792 | ||
793 | /** This function will take NVPString and convert it to an Associative Array and it will decode the response. | |
794 | * It is usefull to search for a particular key and displaying arrays. | |
795 | * @nvpstr is NVPString. | |
796 | * @nvpArray is Associative Array. | |
797 | */ | |
798 | static function deformat($str) { | |
799 | $result = array(); | |
800 | ||
801 | while (strlen($str)) { | |
802 | // postion of key | |
803 | $keyPos = strpos($str, '='); | |
804 | ||
805 | // position of value | |
806 | $valPos = strpos($str, '&') ? strpos($str, '&') : strlen($str); | |
807 | ||
808 | /*getting the Key and Value values and storing in a Associative Array*/ | |
809 | ||
810 | $key = substr($str, 0, $keyPos); | |
811 | $val = substr($str, $keyPos + 1, $valPos - $keyPos - 1); | |
812 | ||
813 | //decoding the respose | |
814 | $result[strtolower(urldecode($key))] = urldecode($val); | |
815 | $str = substr($str, $valPos + 1, strlen($str)); | |
816 | } | |
817 | ||
818 | return $result; | |
819 | } | |
820 | } | |
821 |