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