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