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