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