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