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