Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | 27 | |
ec022878 | 28 | use Civi\Payment\Exception\PaymentProcessorException; |
29 | ||
6a488035 TO |
30 | /** |
31 | * | |
32 | * @package CRM | |
0f03f337 | 33 | * @copyright CiviCRM LLC (c) 2004-2017 |
5fb28746 EM |
34 | */ |
35 | ||
36 | /** | |
37 | * Class CRM_Core_Payment_PayPalImpl for paypal pro, paypal standard & paypal express. | |
6a488035 TO |
38 | */ |
39 | class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { | |
7da04cde | 40 | const CHARSET = 'iso-8859-1'; |
6a488035 TO |
41 | |
42 | protected $_mode = NULL; | |
43 | ||
6a488035 | 44 | /** |
fe482240 | 45 | * Constructor. |
6a488035 | 46 | * |
6a0b768e TO |
47 | * @param string $mode |
48 | * The mode of operation: live or test. | |
6a488035 | 49 | * |
e58c1c1a | 50 | * @param CRM_Core_Payment $paymentProcessor |
77b97be7 EM |
51 | * |
52 | * @return \CRM_Core_Payment_PayPalImpl | |
6a488035 | 53 | */ |
00be9182 | 54 | public function __construct($mode, &$paymentProcessor) { |
6a488035 TO |
55 | $this->_mode = $mode; |
56 | $this->_paymentProcessor = $paymentProcessor; | |
57 | $this->_processorName = ts('PayPal Pro'); | |
2aa397bc | 58 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, NULL, 'name'); |
6a488035 TO |
59 | |
60 | if ($this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType)) { | |
61 | $this->_processorName = ts('PayPal Standard'); | |
62 | return; | |
63 | } | |
64 | elseif ($this->_paymentProcessor['payment_processor_type_id'] == CRM_Utils_Array::key('PayPal_Express', $paymentProcessorType)) { | |
65 | $this->_processorName = ts('PayPal Express'); | |
66 | } | |
67 | ||
6a488035 TO |
68 | } |
69 | ||
fbcb6fba | 70 | /** |
e58c1c1a | 71 | * Are back office payments supported. |
72 | * | |
73 | * E.g paypal standard won't permit you to enter a credit card associated | |
74 | * with someone else's login. | |
75 | * | |
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() { | |
ec022878 | 97 | if ($this->_processorName == ts('PayPal Express') || $this->_processorName == ts('PayPal Pro')) { |
3910048c EM |
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) { | |
aef7f33a | 112 | if ($this->supportsPreApproval()) { |
dbbd55dc | 113 | $this->addPaypalExpressCode($form); |
aef7f33a | 114 | if ($this->_processorName == ts('PayPal Express')) { |
31d31a05 EM |
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 | } | |
aef7f33a | 120 | if ($this->_processorName == ts('PayPal Pro')) { |
31d31a05 EM |
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 | /** | |
e58c1c1a | 130 | * Billing mode button is basically synonymous with paypal express. |
131 | * | |
132 | * This is probably a good example of 'odds & sods' code we | |
133 | * need to find a way for the payment processor to assign. | |
134 | * | |
135 | * A tricky aspect is that the payment processor may need to set the order | |
dbbd55dc | 136 | * |
416a7c99 | 137 | * @param CRM_Core_Form $form |
dbbd55dc | 138 | */ |
31d31a05 | 139 | protected function addPaypalExpressCode(&$form) { |
18135422 | 140 | // @todo use $this->isBackOffice() instead, test. |
dbbd55dc EM |
141 | if (empty($form->isBackOffice)) { |
142 | $form->_expressButtonName = $form->getButtonName('upload', 'express'); | |
143 | $form->assign('expressButtonName', $form->_expressButtonName); | |
144 | $form->add( | |
145 | 'image', | |
146 | $form->_expressButtonName, | |
31d31a05 | 147 | $this->_paymentProcessor['url_button'], |
dbbd55dc EM |
148 | array('class' => 'crm-form-submit') |
149 | ); | |
150 | } | |
151 | } | |
152 | ||
677fe56c EM |
153 | /** |
154 | * Can recurring contributions be set against pledges. | |
155 | * | |
156 | * In practice all processors that use the baseIPN function to finish transactions or | |
157 | * call the completetransaction api support this by looking up previous contributions in the | |
158 | * series and, if there is a prior contribution against a pledge, and the pledge is not complete, | |
159 | * adding the new payment to the pledge. | |
160 | * | |
161 | * However, only enabling for processors it has been tested against. | |
162 | * | |
163 | * @return bool | |
164 | */ | |
165 | protected function supportsRecurContributionsForPledges() { | |
166 | return TRUE; | |
167 | } | |
168 | ||
8ccce14a | 169 | /** |
170 | * Default payment instrument validation. | |
171 | * | |
172 | * Implement the usual Luhn algorithm via a static function in the CRM_Core_Payment_Form if it's a credit card | |
173 | * Not a static function, because I need to check for payment_type. | |
174 | * | |
175 | * @param array $values | |
176 | * @param array $errors | |
177 | */ | |
178 | public function validatePaymentInstrument($values, &$errors) { | |
1d292cf3 | 179 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal' && !$this->isPaypalExpress($values)) { |
06051ca4 | 180 | CRM_Core_Payment_Form::validateCreditCard($values, $errors, $this->_paymentProcessor['id']); |
c319039f | 181 | CRM_Core_Form::validateMandatoryFields($this->getMandatoryFields(), $values, $errors); |
8ccce14a | 182 | } |
183 | } | |
184 | ||
6a488035 | 185 | /** |
e58c1c1a | 186 | * Express checkout code. |
187 | * | |
188 | * Check PayPal documentation for more information | |
6a488035 | 189 | * |
6a0b768e TO |
190 | * @param array $params |
191 | * Assoc array of input parameters for this transaction. | |
6a488035 | 192 | * |
a6c01b45 CW |
193 | * @return array |
194 | * the result in an nice formatted array (or an error object) | |
dbb0d30b | 195 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6a488035 | 196 | */ |
3a80832a | 197 | protected function setExpressCheckOut(&$params) { |
6a488035 TO |
198 | $args = array(); |
199 | ||
200 | $this->initialize($args, 'SetExpressCheckout'); | |
201 | ||
aefd7f6b | 202 | $args['paymentAction'] = 'Sale'; |
6a488035 TO |
203 | $args['amt'] = $params['amount']; |
204 | $args['currencyCode'] = $params['currencyID']; | |
4233eb14 | 205 | $args['desc'] = CRM_Utils_Array::value('description', $params); |
6a488035 | 206 | $args['invnum'] = $params['invoiceID']; |
aefd7f6b EM |
207 | $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']); |
208 | $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL); | |
6a488035 TO |
209 | $args['version'] = '56.0'; |
210 | ||
211 | //LCD if recurring, collect additional data and set some values | |
a7488080 | 212 | if (!empty($params['is_recur'])) { |
6a488035 TO |
213 | $args['L_BILLINGTYPE0'] = 'RecurringPayments'; |
214 | //$args['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Recurring Contribution'; | |
215 | $args['L_BILLINGAGREEMENTDESCRIPTION0'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit']; | |
216 | $args['L_PAYMENTTYPE0'] = 'Any'; | |
217 | } | |
218 | ||
219 | // Allow further manipulation of the arguments via custom hooks .. | |
220 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $args); | |
221 | ||
222 | $result = $this->invokeAPI($args); | |
223 | ||
224 | if (is_a($result, 'CRM_Core_Error')) { | |
abfb35ee | 225 | throw new PaymentProcessorException($result->message); |
6a488035 TO |
226 | } |
227 | ||
228 | /* Success */ | |
229 | ||
230 | return $result['token']; | |
231 | } | |
232 | ||
3105efd2 EM |
233 | /** |
234 | * Get any details that may be available to the payment processor due to an approval process having happened. | |
235 | * | |
236 | * In some cases the browser is redirected to enter details on a processor site. Some details may be available as a | |
237 | * result. | |
238 | * | |
239 | * @param array $storedDetails | |
240 | * | |
241 | * @return array | |
242 | */ | |
243 | public function getPreApprovalDetails($storedDetails) { | |
ec022878 | 244 | return empty($storedDetails['token']) ? array() : $this->getExpressCheckoutDetails($storedDetails['token']); |
3105efd2 EM |
245 | } |
246 | ||
6a488035 | 247 | /** |
e58c1c1a | 248 | * Get details from paypal. |
249 | * | |
250 | * Check PayPal documentation for more information | |
6a488035 | 251 | * |
6a0b768e TO |
252 | * @param string $token |
253 | * The key associated with this transaction. | |
6a488035 | 254 | * |
a6c01b45 CW |
255 | * @return array |
256 | * the result in an nice formatted array (or an error object) | |
6a488035 | 257 | */ |
00be9182 | 258 | public function getExpressCheckoutDetails($token) { |
6a488035 TO |
259 | $args = array(); |
260 | ||
261 | $this->initialize($args, 'GetExpressCheckoutDetails'); | |
262 | $args['token'] = $token; | |
263 | // LCD | |
264 | $args['method'] = 'GetExpressCheckoutDetails'; | |
265 | ||
266 | $result = $this->invokeAPI($args); | |
267 | ||
268 | if (is_a($result, 'CRM_Core_Error')) { | |
abfb35ee | 269 | throw new PaymentProcessorException(CRM_Core_Error::getMessages($result)); |
6a488035 TO |
270 | } |
271 | ||
272 | /* Success */ | |
933da5b5 EM |
273 | $fieldMap = array( |
274 | 'token' => 'token', | |
275 | 'payer_status' => 'payerstatus', | |
276 | 'payer_id' => 'payerid', | |
277 | 'first_name' => 'firstname', | |
278 | 'middle_name' => 'middlename', | |
279 | 'last_name' => 'lastname', | |
280 | 'street_address' => 'shiptostreet', | |
281 | 'supplemental_address_1' => 'shiptostreet2', | |
282 | 'city' => 'shiptocity', | |
283 | 'postal_code' => 'shiptozip', | |
284 | 'state_province' => 'shiptostate', | |
285 | 'country' => 'shiptocountrycode', | |
286 | ); | |
287 | return $this->mapPaypalParamsToCivicrmParams($fieldMap, $result); | |
6a488035 TO |
288 | } |
289 | ||
290 | /** | |
e58c1c1a | 291 | * Do the express checkout at paypal. |
6a488035 | 292 | * |
e58c1c1a | 293 | * Check PayPal documentation for more information |
da6b46f4 | 294 | * |
e58c1c1a | 295 | * @param array $params |
6a488035 | 296 | * |
a6c01b45 | 297 | * @return array |
e58c1c1a | 298 | * The result in an nice formatted array. |
299 | * | |
300 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
6a488035 | 301 | */ |
00be9182 | 302 | public function doExpressCheckout(&$params) { |
f8453bef | 303 | $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); |
5fb28746 EM |
304 | if (!empty($params['is_recur'])) { |
305 | return $this->createRecurringPayments($params); | |
306 | } | |
6a488035 TO |
307 | $args = array(); |
308 | ||
309 | $this->initialize($args, 'DoExpressCheckoutPayment'); | |
6a488035 | 310 | $args['token'] = $params['token']; |
aefd7f6b | 311 | $args['paymentAction'] = 'Sale'; |
6a488035 TO |
312 | $args['amt'] = $params['amount']; |
313 | $args['currencyCode'] = $params['currencyID']; | |
314 | $args['payerID'] = $params['payer_id']; | |
315 | $args['invnum'] = $params['invoiceID']; | |
aefd7f6b EM |
316 | $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']); |
317 | $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL); | |
fc7063a4 | 318 | $args['desc'] = $params['description']; |
6a488035 | 319 | |
ebf695c3 | 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')) { | |
f8453bef | 326 | throw new PaymentProcessorException(CRM_Core_Error::getMessages($result)); |
6a488035 TO |
327 | } |
328 | ||
329 | /* Success */ | |
330 | ||
331 | $params['trxn_id'] = $result['transactionid']; | |
332 | $params['gross_amount'] = $result['amt']; | |
333 | $params['fee_amount'] = $result['feeamt']; | |
4233eb14 | 334 | $params['net_amount'] = CRM_Utils_Array::value('settleamt', $result); |
6a488035 | 335 | if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) { |
b26c99af | 336 | $params['net_amount'] = number_format(($params['gross_amount'] - $params['fee_amount']), 2); |
6a488035 TO |
337 | } |
338 | $params['payment_status'] = $result['paymentstatus']; | |
339 | $params['pending_reason'] = $result['pendingreason']; | |
f8453bef | 340 | if (!empty($params['is_recur'])) { |
341 | // See comment block. | |
2dec2069 | 342 | $params['payment_status_id'] = array_search('Pending', $statuses); |
f8453bef | 343 | } |
344 | else { | |
2dec2069 | 345 | $params['payment_status_id'] = array_search('Completed', $statuses); |
f8453bef | 346 | } |
6a488035 TO |
347 | return $params; |
348 | } | |
349 | ||
6c786a9b | 350 | /** |
e58c1c1a | 351 | * Create recurring payments. |
352 | * | |
c490a46a | 353 | * @param array $params |
6c786a9b EM |
354 | * |
355 | * @return mixed | |
356 | */ | |
00be9182 | 357 | public function createRecurringPayments(&$params) { |
6a488035 | 358 | $args = array(); |
933da5b5 | 359 | // @todo this function is riddled with enotices - perhaps use $this->mapPaypalParamsToCivicrmParams($fieldMap, $result) |
6a488035 TO |
360 | $this->initialize($args, 'CreateRecurringPaymentsProfile'); |
361 | ||
362 | $start_time = strtotime(date('m/d/Y')); | |
363 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
364 | ||
365 | $args['token'] = $params['token']; | |
aefd7f6b | 366 | $args['paymentAction'] = 'Sale'; |
6a488035 TO |
367 | $args['amt'] = $params['amount']; |
368 | $args['currencyCode'] = $params['currencyID']; | |
369 | $args['payerID'] = $params['payer_id']; | |
370 | $args['invnum'] = $params['invoiceID']; | |
371 | $args['returnURL'] = $params['returnURL']; | |
372 | $args['cancelURL'] = $params['cancelURL']; | |
373 | $args['profilestartdate'] = $start_date; | |
374 | $args['method'] = 'CreateRecurringPaymentsProfile'; | |
375 | $args['billingfrequency'] = $params['frequency_interval']; | |
376 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
377 | $args['desc'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit']; | |
378 | //$args['desc'] = 'Recurring Contribution'; | |
379 | $args['totalbillingcycles'] = $params['installments']; | |
380 | $args['version'] = '56.0'; | |
6c552737 | 381 | $args['profilereference'] = "i={$params['invoiceID']}" . |
933da5b5 | 382 | "&m=" . |
1ac462d0 DL |
383 | "&c={$params['contactID']}" . |
384 | "&r={$params['contributionRecurID']}" . | |
385 | "&b={$params['contributionID']}" . | |
386 | "&p={$params['contributionPageID']}"; | |
6a488035 | 387 | |
a3caf338 | 388 | // add CiviCRM BN code |
389 | $args['BUTTONSOURCE'] = 'CiviCRM_SP'; | |
390 | ||
6a488035 TO |
391 | $result = $this->invokeAPI($args); |
392 | ||
393 | if (is_a($result, 'CRM_Core_Error')) { | |
394 | return $result; | |
395 | } | |
396 | ||
397 | /* Success */ | |
6a488035 TO |
398 | $params['trxn_id'] = $result['transactionid']; |
399 | $params['gross_amount'] = $result['amt']; | |
400 | $params['fee_amount'] = $result['feeamt']; | |
401 | $params['net_amount'] = $result['settleamt']; | |
402 | if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) { | |
b26c99af | 403 | $params['net_amount'] = number_format(($params['gross_amount'] - $params['fee_amount']), 2); |
6a488035 TO |
404 | } |
405 | $params['payment_status'] = $result['paymentstatus']; | |
406 | $params['pending_reason'] = $result['pendingreason']; | |
407 | ||
408 | return $params; | |
409 | } | |
e58c1c1a | 410 | |
6c786a9b | 411 | /** |
e58c1c1a | 412 | * Initialise. |
413 | * | |
6c786a9b EM |
414 | * @param $args |
415 | * @param $method | |
416 | */ | |
00be9182 | 417 | public function initialize(&$args, $method) { |
353ffa53 TO |
418 | $args['user'] = $this->_paymentProcessor['user_name']; |
419 | $args['pwd'] = $this->_paymentProcessor['password']; | |
420 | $args['version'] = 3.0; | |
6a488035 | 421 | $args['signature'] = $this->_paymentProcessor['signature']; |
353ffa53 TO |
422 | $args['subject'] = CRM_Utils_Array::value('subject', $this->_paymentProcessor); |
423 | $args['method'] = $method; | |
6a488035 | 424 | } |
f8453bef | 425 | |
426 | /** | |
427 | * Process payment - this function wraps around both doTransferPayment and doDirectPayment. | |
428 | * | |
429 | * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms | |
430 | * more agnostic. | |
431 | * | |
432 | * Payment processors should set payment_status_id. This function adds some historical defaults ie. the | |
433 | * assumption that if a 'doDirectPayment' processors comes back it completed the transaction & in fact | |
434 | * doTransferCheckout would not traditionally come back. | |
435 | * | |
436 | * doDirectPayment does not do an immediate payment for Authorize.net or Paypal so the default is assumed | |
437 | * to be Pending. | |
438 | * | |
439 | * Once this function is fully rolled out then it will be preferred for processors to throw exceptions than to | |
440 | * return Error objects | |
441 | * | |
442 | * @param array $params | |
443 | * | |
444 | * @param string $component | |
445 | * | |
446 | * @return array | |
447 | * Result array | |
448 | * | |
449 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
450 | */ | |
451 | public function doPayment(&$params, $component = 'contribute') { | |
e58c1c1a | 452 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal_Express' |
c319039f | 453 | || ($this->_paymentProcessor['payment_processor_type'] == 'PayPal' && !empty($params['token'])) |
ec022878 | 454 | ) { |
e58c1c1a | 455 | $this->_component = $component; |
456 | return $this->doExpressCheckout($params); | |
457 | ||
f8453bef | 458 | } |
e58c1c1a | 459 | return parent::doPayment($params, $component); |
f8453bef | 460 | } |
6a488035 TO |
461 | |
462 | /** | |
463 | * This function collects all the information from a web/api form and invokes | |
464 | * the relevant payment processor specific functions to perform the transaction | |
465 | * | |
6a0b768e TO |
466 | * @param array $params |
467 | * Assoc array of input parameters for this transaction. | |
6a488035 | 468 | * |
da6b46f4 | 469 | * @param string $component |
a6c01b45 CW |
470 | * @return array |
471 | * the result in an nice formatted array (or an error object) | |
6a488035 | 472 | */ |
00be9182 | 473 | public function doDirectPayment(&$params, $component = 'contribute') { |
6a488035 TO |
474 | $args = array(); |
475 | ||
476 | $this->initialize($args, 'DoDirectPayment'); | |
477 | ||
aefd7f6b | 478 | $args['paymentAction'] = 'Sale'; |
6a488035 | 479 | $args['amt'] = $params['amount']; |
899c09b3 | 480 | $args['currencyCode'] = $this->getCurrency($params); |
6a488035 TO |
481 | $args['invnum'] = $params['invoiceID']; |
482 | $args['ipaddress'] = $params['ip_address']; | |
483 | $args['creditCardType'] = $params['credit_card_type']; | |
484 | $args['acct'] = $params['credit_card_number']; | |
485 | $args['expDate'] = sprintf('%02d', $params['month']) . $params['year']; | |
486 | $args['cvv2'] = $params['cvv2']; | |
487 | $args['firstName'] = $params['first_name']; | |
488 | $args['lastName'] = $params['last_name']; | |
489 | $args['email'] = CRM_Utils_Array::value('email', $params); | |
490 | $args['street'] = $params['street_address']; | |
491 | $args['city'] = $params['city']; | |
492 | $args['state'] = $params['state_province']; | |
493 | $args['countryCode'] = $params['country']; | |
494 | $args['zip'] = $params['postal_code']; | |
0ab1fbfb | 495 | $args['desc'] = substr(CRM_Utils_Array::value('description', $params), 0, 127); |
6a488035 TO |
496 | $args['custom'] = CRM_Utils_Array::value('accountingCode', $params); |
497 | ||
ebf695c3 | 498 | // add CiviCRM BN code |
499 | $args['BUTTONSOURCE'] = 'CiviCRM_SP'; | |
500 | ||
6a488035 TO |
501 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { |
502 | $start_time = strtotime(date('m/d/Y')); | |
503 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
504 | ||
505 | $args['PaymentAction'] = 'Sale'; | |
506 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
507 | $args['billingfrequency'] = $params['frequency_interval']; | |
508 | $args['method'] = "CreateRecurringPaymentsProfile"; | |
509 | $args['profilestartdate'] = $start_date; | |
6c552737 | 510 | $args['desc'] = "" . |
1ac462d0 DL |
511 | $params['description'] . ": " . |
512 | $params['amount'] . " Per " . | |
513 | $params['frequency_interval'] . " " . | |
514 | $params['frequency_unit']; | |
6a488035 | 515 | $args['amt'] = $params['amount']; |
0bde274f | 516 | $args['totalbillingcycles'] = CRM_Utils_Array::value('installments', $params); |
6a488035 | 517 | $args['version'] = 56.0; |
6c552737 | 518 | $args['PROFILEREFERENCE'] = "" . |
1ac462d0 DL |
519 | "i=" . $params['invoiceID'] . "&m=" . $component . |
520 | "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] . | |
521 | "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID']; | |
6a488035 TO |
522 | } |
523 | ||
524 | // Allow further manipulation of the arguments via custom hooks .. | |
525 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $args); | |
526 | ||
527 | $result = $this->invokeAPI($args); | |
528 | ||
529 | //WAG | |
530 | if (is_a($result, 'CRM_Core_Error')) { | |
531 | return $result; | |
532 | } | |
533 | ||
534 | $params['recurr_profile_id'] = NULL; | |
535 | ||
536 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
537 | $params['recurr_profile_id'] = $result['profileid']; | |
538 | } | |
539 | ||
540 | /* Success */ | |
541 | ||
542 | $params['trxn_id'] = CRM_Utils_Array::value('transactionid', $result); | |
543 | $params['gross_amount'] = CRM_Utils_Array::value('amt', $result); | |
9d2f24ee | 544 | $params = array_merge($params, $this->doQuery($params)); |
6a488035 TO |
545 | return $params; |
546 | } | |
547 | ||
9d2f24ee | 548 | /** |
549 | * Query payment processor for details about a transaction. | |
550 | * | |
551 | * For paypal see : https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/GetTransactionDetails_API_Operation_NVP/ | |
552 | * | |
553 | * @param array $params | |
554 | * Array of parameters containing one of: | |
555 | * - trxn_id Id of an individual transaction. | |
556 | * - processor_id Id of a recurring contribution series as stored in the civicrm_contribution_recur table. | |
557 | * | |
558 | * @return array | |
559 | * Extra parameters retrieved. | |
560 | * Any parameters retrievable through this should be documented in the function comments at | |
561 | * CRM_Core_Payment::doQuery. Currently | |
562 | * - fee_amount Amount of fee paid | |
563 | * | |
564 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
565 | */ | |
566 | public function doQuery($params) { | |
0bde274f | 567 | //CRM-18140 - trxn_id not returned for recurring paypal transaction |
568 | if (!empty($params['is_recur'])) { | |
569 | return array(); | |
570 | } | |
571 | elseif (empty($params['trxn_id'])) { | |
9d2f24ee | 572 | throw new \Civi\Payment\Exception\PaymentProcessorException('transaction id not set'); |
573 | } | |
574 | $args = array( | |
575 | 'TRANSACTIONID' => $params['trxn_id'], | |
576 | ); | |
577 | $this->initialize($args, 'GetTransactionDetails'); | |
578 | $result = $this->invokeAPI($args); | |
579 | return array( | |
580 | 'fee_amount' => $result['feeamt'], | |
8a2a56e9 | 581 | 'net_amount' => $params['gross_amount'] - $result['feeamt'], |
9d2f24ee | 582 | ); |
583 | } | |
584 | ||
6a488035 | 585 | /** |
fe482240 | 586 | * This function checks to see if we have the right config values. |
6a488035 | 587 | * |
a6c01b45 CW |
588 | * @return string |
589 | * the error message if any | |
6a488035 | 590 | */ |
00be9182 | 591 | public function checkConfig() { |
6a488035 | 592 | $error = array(); |
2aa397bc | 593 | $paymentProcessorType = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, NULL, 'name'); |
6a488035 TO |
594 | |
595 | if ($this->_paymentProcessor['payment_processor_type_id'] != CRM_Utils_Array::key('PayPal_Standard', $paymentProcessorType)) { | |
596 | if (empty($this->_paymentProcessor['signature'])) { | |
0501a7d6 | 597 | $error[] = ts('Signature is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
598 | } |
599 | ||
600 | if (empty($this->_paymentProcessor['password'])) { | |
0501a7d6 | 601 | $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
602 | } |
603 | } | |
75466733 | 604 | if (empty($this->_paymentProcessor['user_name'])) { |
1b9f9ca3 EM |
605 | $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.'); |
606 | } | |
6a488035 TO |
607 | |
608 | if (!empty($error)) { | |
609 | return implode('<p>', $error); | |
610 | } | |
611 | else { | |
612 | return NULL; | |
613 | } | |
614 | } | |
615 | ||
6c786a9b EM |
616 | /** |
617 | * @return null|string | |
618 | */ | |
00be9182 | 619 | public function cancelSubscriptionURL() { |
6a488035 TO |
620 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal_Standard') { |
621 | return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor['user_name']); | |
622 | } | |
623 | else { | |
624 | return NULL; | |
625 | } | |
626 | } | |
627 | ||
b5c2afd0 | 628 | /** |
100fef9d | 629 | * Check whether a method is present ( & supported ) by the payment processor object. |
b5c2afd0 | 630 | * |
6a0b768e TO |
631 | * @param string $method |
632 | * Method to check for. | |
b5c2afd0 | 633 | * |
5c766a0b | 634 | * @return bool |
b5c2afd0 | 635 | */ |
1524a007 | 636 | public function isSupported($method) { |
6a488035 TO |
637 | if ($this->_paymentProcessor['payment_processor_type'] != 'PayPal') { |
638 | // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported | |
639 | // by standard or express. | |
640 | return FALSE; | |
641 | } | |
642 | return parent::isSupported($method); | |
643 | } | |
644 | ||
1ba4a3aa EM |
645 | /** |
646 | * Paypal express replaces the submit button with it's own. | |
647 | * | |
648 | * @return bool | |
649 | * Should the form button by suppressed? | |
650 | */ | |
651 | public function isSuppressSubmitButtons() { | |
652 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal_Express') { | |
653 | return TRUE; | |
654 | } | |
655 | return FALSE; | |
656 | } | |
657 | ||
6c786a9b EM |
658 | /** |
659 | * @param string $message | |
660 | * @param array $params | |
661 | * | |
662 | * @return array|bool|object | |
663 | */ | |
00be9182 | 664 | public function cancelSubscription(&$message = '', $params = array()) { |
6a488035 TO |
665 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
666 | $args = array(); | |
667 | $this->initialize($args, 'ManageRecurringPaymentsProfileStatus'); | |
668 | ||
669 | $args['PROFILEID'] = CRM_Utils_Array::value('subscriptionId', $params); | |
353ffa53 TO |
670 | $args['ACTION'] = 'Cancel'; |
671 | $args['NOTE'] = CRM_Utils_Array::value('reason', $params); | |
6a488035 TO |
672 | |
673 | $result = $this->invokeAPI($args); | |
674 | if (is_a($result, 'CRM_Core_Error')) { | |
675 | return $result; | |
676 | } | |
677 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
678 | return TRUE; | |
679 | } | |
680 | return FALSE; | |
681 | } | |
682 | ||
5495e78a EM |
683 | /** |
684 | * Process incoming notification. | |
5495e78a EM |
685 | */ |
686 | static public function handlePaymentNotification() { | |
ddc4b5af | 687 | $params = array_merge($_GET, $_REQUEST); |
688 | $q = explode('/', CRM_Utils_Array::value('q', $params, '')); | |
689 | $lastParam = array_pop($q); | |
690 | if (is_numeric($lastParam)) { | |
691 | $params['processor_id'] = $lastParam; | |
692 | } | |
693 | if (civicrm_api3('PaymentProcessor', 'getvalue', array( | |
694 | 'id' => $params['processor_id'], | |
695 | 'return' => 'class_name') | |
696 | ) == 'Payment_PayPalImpl') { | |
697 | $paypalIPN = new CRM_Core_Payment_PayPalIPN($params); | |
698 | } | |
699 | else { | |
700 | $paypalIPN = new CRM_Core_Payment_PayPalProIPN($params); | |
701 | } | |
5495e78a EM |
702 | $paypalIPN->main(); |
703 | } | |
704 | ||
6c786a9b EM |
705 | /** |
706 | * @param string $message | |
707 | * @param array $params | |
708 | * | |
709 | * @return array|bool|object | |
710 | */ | |
00be9182 | 711 | public function updateSubscriptionBillingInfo(&$message = '', $params = array()) { |
6a488035 TO |
712 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
713 | $config = CRM_Core_Config::singleton(); | |
714 | $args = array(); | |
715 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
716 | ||
717 | $args['PROFILEID'] = $params['subscriptionId']; | |
718 | $args['AMT'] = $params['amount']; | |
719 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
720 | $args['CREDITCARDTYPE'] = $params['credit_card_type']; | |
721 | $args['ACCT'] = $params['credit_card_number']; | |
722 | $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year']; | |
723 | $args['CVV2'] = $params['cvv2']; | |
724 | ||
353ffa53 TO |
725 | $args['FIRSTNAME'] = $params['first_name']; |
726 | $args['LASTNAME'] = $params['last_name']; | |
727 | $args['STREET'] = $params['street_address']; | |
728 | $args['CITY'] = $params['city']; | |
729 | $args['STATE'] = $params['state_province']; | |
6a488035 | 730 | $args['COUNTRYCODE'] = $params['postal_code']; |
353ffa53 | 731 | $args['ZIP'] = $params['country']; |
6a488035 TO |
732 | |
733 | $result = $this->invokeAPI($args); | |
734 | if (is_a($result, 'CRM_Core_Error')) { | |
735 | return $result; | |
736 | } | |
737 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
738 | return TRUE; | |
739 | } | |
740 | return FALSE; | |
741 | } | |
742 | ||
6c786a9b EM |
743 | /** |
744 | * @param string $message | |
745 | * @param array $params | |
746 | * | |
747 | * @return array|bool|object | |
748 | */ | |
00be9182 | 749 | public function changeSubscriptionAmount(&$message = '', $params = array()) { |
6a488035 TO |
750 | if ($this->_paymentProcessor['payment_processor_type'] == 'PayPal') { |
751 | $config = CRM_Core_Config::singleton(); | |
752 | $args = array(); | |
753 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); | |
754 | ||
755 | $args['PROFILEID'] = $params['subscriptionId']; | |
756 | $args['AMT'] = $params['amount']; | |
757 | $args['CURRENCYCODE'] = $config->defaultCurrency; | |
758 | $args['BILLINGFREQUENCY'] = $params['installments']; | |
759 | ||
760 | $result = $this->invokeAPI($args); | |
761 | CRM_Core_Error::debug_var('$result', $result); | |
762 | if (is_a($result, 'CRM_Core_Error')) { | |
763 | return $result; | |
764 | } | |
765 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
766 | return TRUE; | |
767 | } | |
768 | return FALSE; | |
769 | } | |
770 | ||
38b66756 EM |
771 | /** |
772 | * Function to action pre-approval if supported | |
773 | * | |
774 | * @param array $params | |
775 | * Parameters from the form | |
776 | * | |
777 | * @return array | |
778 | * - pre_approval_parameters (this will be stored on the calling form & available later) | |
779 | * - redirect_url (if set the browser will be redirected to this. | |
780 | */ | |
3910048c | 781 | public function doPreApproval(&$params) { |
1d292cf3 | 782 | if (!$this->isPaypalExpress($params)) { |
ec022878 | 783 | return array(); |
784 | } | |
05c302ec | 785 | $this->_component = $params['component']; |
3910048c EM |
786 | $token = $this->setExpressCheckOut($params); |
787 | return array( | |
788 | 'pre_approval_parameters' => array('token' => $token), | |
789 | 'redirect_url' => $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token", | |
790 | ); | |
791 | } | |
792 | ||
6c786a9b | 793 | /** |
c490a46a | 794 | * @param array $params |
6c786a9b EM |
795 | * @param string $component |
796 | * | |
797 | * @throws Exception | |
798 | */ | |
00be9182 | 799 | public function doTransferCheckout(&$params, $component = 'contribute') { |
6a488035 | 800 | |
ddc4b5af | 801 | $notifyParameters = array('module' => $component); |
802 | $notifyParameterMap = array( | |
803 | 'contactID' => 'contactID', | |
804 | 'contributionID' => 'contributionID', | |
805 | 'eventID' => 'eventID', | |
806 | 'participantID' => 'participantID', | |
807 | 'membershipID' => 'membershipID', | |
808 | 'related_contact' => 'relatedContactID', | |
809 | 'onbehalf_dupe_alert' => 'onBehalfDupeAlert', | |
068fb0de | 810 | 'accountingCode' => 'accountingCode', |
11a4431c | 811 | 'contributionRecurID' => 'contributionRecurID', |
812 | 'contributionPageID' => 'contributionPageID', | |
ddc4b5af | 813 | ); |
814 | foreach ($notifyParameterMap as $paramsName => $notifyName) { | |
815 | if (!empty($params[$paramsName])) { | |
816 | $notifyParameters[$notifyName] = $params[$paramsName]; | |
6a488035 TO |
817 | } |
818 | } | |
ddc4b5af | 819 | $notifyURL = $this->getNotifyUrl(); |
6a488035 | 820 | |
ddc4b5af | 821 | $config = CRM_Core_Config::singleton(); |
353ffa53 TO |
822 | $url = ($component == 'event') ? 'civicrm/event/register' : 'civicrm/contribute/transact'; |
823 | $cancel = ($component == 'event') ? '_qf_Register_display' : '_qf_Main_display'; | |
6a488035 TO |
824 | |
825 | $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}"; | |
a7488080 | 826 | if (!empty($params['is_recur'])) { |
1ac462d0 | 827 | $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}"; |
6a488035 TO |
828 | } |
829 | ||
1ac462d0 DL |
830 | $cancelURL = CRM_Utils_System::url( |
831 | $url, | |
6a488035 TO |
832 | $cancelUrlString, |
833 | TRUE, NULL, FALSE | |
834 | ); | |
835 | ||
6a488035 TO |
836 | $paypalParams = array( |
837 | 'business' => $this->_paymentProcessor['user_name'], | |
838 | 'notify_url' => $notifyURL, | |
683538c8 | 839 | 'item_name' => $this->getPaymentDescription($params, 127), |
6a488035 TO |
840 | 'quantity' => 1, |
841 | 'undefined_quantity' => 0, | |
842 | 'cancel_return' => $cancelURL, | |
843 | 'no_note' => 1, | |
844 | 'no_shipping' => 1, | |
ddc4b5af | 845 | 'return' => $this->getReturnSuccessUrl($params['qfKey']), |
6a488035 TO |
846 | 'rm' => 2, |
847 | 'currency_code' => $params['currencyID'], | |
848 | 'invoice' => $params['invoiceID'], | |
849 | 'lc' => substr($config->lcMessages, -2), | |
850 | 'charset' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'UTF-8', | |
068fb0de | 851 | 'custom' => json_encode($notifyParameters), |
de753541 | 852 | 'bn' => 'CiviCRM_SP', |
6a488035 TO |
853 | ); |
854 | ||
855 | // add name and address if available, CRM-3130 | |
856 | $otherVars = array( | |
857 | 'first_name' => 'first_name', | |
858 | 'last_name' => 'last_name', | |
859 | 'street_address' => 'address1', | |
860 | 'country' => 'country', | |
861 | 'preferred_language' => 'lc', | |
862 | 'city' => 'city', | |
863 | 'state_province' => 'state', | |
864 | 'postal_code' => 'zip', | |
865 | 'email' => 'email', | |
866 | ); | |
867 | ||
868 | foreach (array_keys($params) as $p) { | |
869 | // get the base name without the location type suffixed to it | |
870 | $parts = explode('-', $p); | |
871 | $name = count($parts) > 1 ? $parts[0] : $p; | |
872 | if (isset($otherVars[$name])) { | |
873 | $value = $params[$p]; | |
874 | if ($value) { | |
875 | if ($name == 'state_province') { | |
876 | $stateName = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
877 | $value = $stateName; | |
878 | } | |
879 | if ($name == 'country') { | |
880 | $countryName = CRM_Core_PseudoConstant::countryIsoCode($value); | |
881 | $value = $countryName; | |
882 | } | |
883 | // ensure value is not an array | |
884 | // CRM-4174 | |
885 | if (!is_array($value)) { | |
886 | $paypalParams[$otherVars[$name]] = $value; | |
887 | } | |
888 | } | |
889 | } | |
890 | } | |
891 | ||
892 | // if recurring donations, add a few more items | |
893 | if (!empty($params['is_recur'])) { | |
11a4431c | 894 | if (!$params['contributionRecurID']) { |
6a488035 TO |
895 | CRM_Core_Error::fatal(ts('Recurring contribution, but no database id')); |
896 | } | |
897 | ||
898 | $paypalParams += array( | |
899 | 'cmd' => '_xclick-subscriptions', | |
353ffa53 TO |
900 | 'a3' => $params['amount'], |
901 | 'p3' => $params['frequency_interval'], | |
902 | 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)), | |
6a488035 TO |
903 | 'src' => 1, |
904 | 'sra' => 1, | |
14965416 | 905 | 'srt' => CRM_Utils_Array::value('installments', $params), |
6a488035 TO |
906 | 'no_note' => 1, |
907 | 'modify' => 0, | |
908 | ); | |
909 | } | |
910 | else { | |
911 | $paypalParams += array( | |
912 | 'cmd' => '_xclick', | |
913 | 'amount' => $params['amount'], | |
914 | ); | |
915 | } | |
916 | ||
917 | // Allow further manipulation of the arguments via custom hooks .. | |
918 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $paypalParams); | |
919 | ||
920 | $uri = ''; | |
921 | foreach ($paypalParams as $key => $value) { | |
922 | if ($value === NULL) { | |
923 | continue; | |
924 | } | |
925 | ||
926 | $value = urlencode($value); | |
927 | if ($key == 'return' || | |
928 | $key == 'cancel_return' || | |
929 | $key == 'notify_url' | |
930 | ) { | |
931 | $value = str_replace('%2F', '/', $value); | |
932 | } | |
933 | $uri .= "&{$key}={$value}"; | |
934 | } | |
935 | ||
353ffa53 TO |
936 | $uri = substr($uri, 1); |
937 | $url = $this->_paymentProcessor['url_site']; | |
938 | $sub = empty($params['is_recur']) ? 'cgi-bin/webscr' : 'subscriptions'; | |
6a488035 TO |
939 | $paypalURL = "{$url}{$sub}?$uri"; |
940 | ||
941 | CRM_Utils_System::redirect($paypalURL); | |
942 | } | |
943 | ||
944 | /** | |
ad37ac8e | 945 | * Hash_call: Function to perform the API call to PayPal using API signature. |
946 | * | |
6a488035 TO |
947 | * @methodName is name of API method. |
948 | * @nvpStr is nvp string. | |
ad37ac8e | 949 | * returns an associative array containing the response from the server. |
950 | * | |
951 | * @param array $args | |
952 | * @param null $url | |
953 | * | |
954 | * @return array|object | |
955 | * @throws \Exception | |
6a488035 | 956 | */ |
00be9182 | 957 | public function invokeAPI($args, $url = NULL) { |
6a488035 TO |
958 | |
959 | if ($url === NULL) { | |
960 | if (empty($this->_paymentProcessor['url_api'])) { | |
961 | CRM_Core_Error::fatal(ts('Please set the API URL. Please refer to the documentation for more details')); | |
962 | } | |
963 | ||
964 | $url = $this->_paymentProcessor['url_api'] . 'nvp'; | |
965 | } | |
966 | ||
967 | if (!function_exists('curl_init')) { | |
968 | CRM_Core_Error::fatal("curl functions NOT available."); | |
969 | } | |
970 | ||
971 | //setting the curl parameters. | |
972 | $ch = curl_init(); | |
973 | curl_setopt($ch, CURLOPT_URL, $url); | |
c20af6a3 | 974 | curl_setopt($ch, CURLOPT_VERBOSE, 0); |
6a488035 TO |
975 | |
976 | //turning off the server and peer verification(TrustManager Concept). | |
aaffa79f | 977 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Civi::settings()->get('verifySSL')); |
978 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Civi::settings()->get('verifySSL') ? 2 : 0); | |
6a488035 TO |
979 | |
980 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
981 | curl_setopt($ch, CURLOPT_POST, 1); | |
982 | ||
983 | $p = array(); | |
984 | foreach ($args as $n => $v) { | |
985 | $p[] = "$n=" . urlencode($v); | |
986 | } | |
987 | ||
988 | //NVPRequest for submitting to server | |
989 | $nvpreq = implode('&', $p); | |
990 | ||
991 | //setting the nvpreq as POST FIELD to curl | |
992 | curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); | |
993 | ||
994 | //getting response from server | |
995 | $response = curl_exec($ch); | |
996 | ||
997 | //converting NVPResponse to an Associative Array | |
998 | $result = self::deformat($response); | |
999 | ||
1000 | if (curl_errno($ch)) { | |
1001 | $e = CRM_Core_Error::singleton(); | |
1002 | $e->push(curl_errno($ch), | |
1003 | 0, NULL, | |
1004 | curl_error($ch) | |
1005 | ); | |
1006 | return $e; | |
1007 | } | |
1008 | else { | |
1009 | curl_close($ch); | |
1010 | } | |
1011 | ||
1012 | if (strtolower($result['ack']) != 'success' && | |
1013 | strtolower($result['ack']) != 'successwithwarning' | |
1014 | ) { | |
abfb35ee | 1015 | throw new PaymentProcessorException("{$result['l_shortmessage0']} {$result['l_longmessage0']}"); |
6a488035 TO |
1016 | $e = CRM_Core_Error::singleton(); |
1017 | $e->push($result['l_errorcode0'], | |
1018 | 0, NULL, | |
1019 | "{$result['l_shortmessage0']} {$result['l_longmessage0']}" | |
1020 | ); | |
1021 | return $e; | |
1022 | } | |
1023 | ||
1024 | return $result; | |
1025 | } | |
1026 | ||
be2e0c6a | 1027 | /** |
ad37ac8e | 1028 | * This function will take NVPString and convert it to an Associative Array. |
1029 | * | |
1030 | * It will decode the response. It is useful to search for a particular key and displaying arrays. | |
1031 | * | |
1032 | * @param string $str | |
1033 | * | |
1034 | * @return array | |
6a488035 | 1035 | */ |
00be9182 | 1036 | public static function deformat($str) { |
6a488035 TO |
1037 | $result = array(); |
1038 | ||
1039 | while (strlen($str)) { | |
b44e3f84 | 1040 | // position of key |
6a488035 TO |
1041 | $keyPos = strpos($str, '='); |
1042 | ||
1043 | // position of value | |
1044 | $valPos = strpos($str, '&') ? strpos($str, '&') : strlen($str); | |
1045 | ||
1046 | /*getting the Key and Value values and storing in a Associative Array*/ | |
1047 | ||
1048 | $key = substr($str, 0, $keyPos); | |
1049 | $val = substr($str, $keyPos + 1, $valPos - $keyPos - 1); | |
1050 | ||
1051 | //decoding the respose | |
1052 | $result[strtolower(urldecode($key))] = urldecode($val); | |
1053 | $str = substr($str, $valPos + 1, strlen($str)); | |
1054 | } | |
1055 | ||
1056 | return $result; | |
1057 | } | |
96025800 | 1058 | |
3cc8d80d | 1059 | /** |
1060 | * Get array of fields that should be displayed on the payment form. | |
1061 | * | |
1062 | * @return array | |
1063 | * @throws CiviCRM_API3_Exception | |
1064 | */ | |
1065 | public function getPaymentFormFields() { | |
1066 | if ($this->_processorName == ts('PayPal Pro')) { | |
1067 | return $this->getCreditCardFormFields(); | |
1068 | } | |
1069 | else { | |
1070 | return array(); | |
1071 | } | |
1072 | } | |
1073 | ||
933da5b5 EM |
1074 | /** |
1075 | * Map the paypal params to CiviCRM params using a field map. | |
1076 | * | |
1077 | * @param array $fieldMap | |
1078 | * @param array $paypalParams | |
1079 | * | |
1080 | * @return array | |
1081 | */ | |
1082 | protected function mapPaypalParamsToCivicrmParams($fieldMap, $paypalParams) { | |
1083 | $params = array(); | |
1084 | foreach ($fieldMap as $civicrmField => $paypalField) { | |
1085 | $params[$civicrmField] = isset($paypalParams[$paypalField]) ? $paypalParams[$paypalField] : NULL; | |
1086 | } | |
1087 | return $params; | |
1088 | } | |
1089 | ||
1d292cf3 | 1090 | /** |
1091 | * Is this being processed by payment express. | |
1092 | * | |
1093 | * Either because it is payment express or because is pro with paypal express in use. | |
1094 | * | |
1095 | * @param array $params | |
1096 | * | |
1097 | * @return bool | |
1098 | */ | |
1099 | protected function isPaypalExpress($params) { | |
1100 | if ($this->_processorName == ts('PayPal Express')) { | |
1101 | return TRUE; | |
1102 | } | |
1103 | ||
1104 | // This would occur postProcess. | |
1105 | if (!empty($params['token'])) { | |
1106 | return TRUE; | |
1107 | } | |
1108 | if (isset($params['button']) && stristr($params['button'], 'express')) { | |
1109 | return TRUE; | |
1110 | } | |
1111 | ||
1112 | // The contribution form passes a 'button' but the event form might still set one of these fields. | |
1113 | // @todo more standardisation & get paypal fully out of the form layer. | |
1114 | $possibleExpressFields = array( | |
1115 | '_qf_Register_upload_express_x', | |
1116 | '_qf_Payment_upload_express_x', | |
1117 | ); | |
1118 | if (array_intersect_key($params, array_fill_keys($possibleExpressFields, 1))) { | |
1119 | return TRUE; | |
1120 | } | |
1121 | return FALSE; | |
1122 | } | |
1123 | ||
6a488035 | 1124 | } |