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