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']; | |
4233eb14 | 239 | $args['desc'] = CRM_Utils_Array::value('description', $params); |
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 | ||
259 | if (is_a($result, 'CRM_Core_Error')) { | |
abfb35ee | 260 | throw new PaymentProcessorException($result->message); |
6a488035 TO |
261 | } |
262 | ||
263 | /* Success */ | |
264 | ||
265 | return $result['token']; | |
266 | } | |
267 | ||
3105efd2 EM |
268 | /** |
269 | * Get any details that may be available to the payment processor due to an approval process having happened. | |
270 | * | |
271 | * In some cases the browser is redirected to enter details on a processor site. Some details may be available as a | |
272 | * result. | |
273 | * | |
274 | * @param array $storedDetails | |
275 | * | |
276 | * @return array | |
8acddb67 | 277 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
3105efd2 EM |
278 | */ |
279 | public function getPreApprovalDetails($storedDetails) { | |
be2fb01f | 280 | return empty($storedDetails['token']) ? [] : $this->getExpressCheckoutDetails($storedDetails['token']); |
3105efd2 EM |
281 | } |
282 | ||
6a488035 | 283 | /** |
e58c1c1a | 284 | * Get details from paypal. |
285 | * | |
286 | * Check PayPal documentation for more information | |
6a488035 | 287 | * |
6a0b768e TO |
288 | * @param string $token |
289 | * The key associated with this transaction. | |
6a488035 | 290 | * |
a6c01b45 CW |
291 | * @return array |
292 | * the result in an nice formatted array (or an error object) | |
8acddb67 | 293 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6a488035 | 294 | */ |
00be9182 | 295 | public function getExpressCheckoutDetails($token) { |
be2fb01f | 296 | $args = []; |
6a488035 TO |
297 | |
298 | $this->initialize($args, 'GetExpressCheckoutDetails'); | |
299 | $args['token'] = $token; | |
300 | // LCD | |
301 | $args['method'] = 'GetExpressCheckoutDetails'; | |
302 | ||
303 | $result = $this->invokeAPI($args); | |
304 | ||
305 | if (is_a($result, 'CRM_Core_Error')) { | |
abfb35ee | 306 | throw new PaymentProcessorException(CRM_Core_Error::getMessages($result)); |
6a488035 TO |
307 | } |
308 | ||
309 | /* Success */ | |
be2fb01f | 310 | $fieldMap = [ |
933da5b5 EM |
311 | 'token' => 'token', |
312 | 'payer_status' => 'payerstatus', | |
313 | 'payer_id' => 'payerid', | |
314 | 'first_name' => 'firstname', | |
315 | 'middle_name' => 'middlename', | |
316 | 'last_name' => 'lastname', | |
317 | 'street_address' => 'shiptostreet', | |
318 | 'supplemental_address_1' => 'shiptostreet2', | |
319 | 'city' => 'shiptocity', | |
320 | 'postal_code' => 'shiptozip', | |
321 | 'state_province' => 'shiptostate', | |
322 | 'country' => 'shiptocountrycode', | |
be2fb01f | 323 | ]; |
933da5b5 | 324 | return $this->mapPaypalParamsToCivicrmParams($fieldMap, $result); |
6a488035 TO |
325 | } |
326 | ||
327 | /** | |
e58c1c1a | 328 | * Do the express checkout at paypal. |
6a488035 | 329 | * |
e58c1c1a | 330 | * Check PayPal documentation for more information |
da6b46f4 | 331 | * |
e58c1c1a | 332 | * @param array $params |
6a488035 | 333 | * |
a6c01b45 | 334 | * @return array |
e58c1c1a | 335 | * The result in an nice formatted array. |
336 | * | |
337 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
6a488035 | 338 | */ |
00be9182 | 339 | public function doExpressCheckout(&$params) { |
5fb28746 EM |
340 | if (!empty($params['is_recur'])) { |
341 | return $this->createRecurringPayments($params); | |
342 | } | |
be2fb01f | 343 | $args = []; |
6a488035 TO |
344 | |
345 | $this->initialize($args, 'DoExpressCheckoutPayment'); | |
6a488035 | 346 | $args['token'] = $params['token']; |
aefd7f6b | 347 | $args['paymentAction'] = 'Sale'; |
6a488035 TO |
348 | $args['amt'] = $params['amount']; |
349 | $args['currencyCode'] = $params['currencyID']; | |
350 | $args['payerID'] = $params['payer_id']; | |
351 | $args['invnum'] = $params['invoiceID']; | |
aefd7f6b EM |
352 | $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']); |
353 | $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL); | |
fc7063a4 | 354 | $args['desc'] = $params['description']; |
6a488035 | 355 | |
ebf695c3 | 356 | // add CiviCRM BN code |
357 | $args['BUTTONSOURCE'] = 'CiviCRM_SP'; | |
358 | ||
6a488035 TO |
359 | $result = $this->invokeAPI($args); |
360 | ||
361 | if (is_a($result, 'CRM_Core_Error')) { | |
f8453bef | 362 | throw new PaymentProcessorException(CRM_Core_Error::getMessages($result)); |
6a488035 TO |
363 | } |
364 | ||
365 | /* Success */ | |
6a488035 TO |
366 | $params['trxn_id'] = $result['transactionid']; |
367 | $params['gross_amount'] = $result['amt']; | |
368 | $params['fee_amount'] = $result['feeamt']; | |
4233eb14 | 369 | $params['net_amount'] = CRM_Utils_Array::value('settleamt', $result); |
6a488035 | 370 | if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) { |
b26c99af | 371 | $params['net_amount'] = number_format(($params['gross_amount'] - $params['fee_amount']), 2); |
6a488035 TO |
372 | } |
373 | $params['payment_status'] = $result['paymentstatus']; | |
374 | $params['pending_reason'] = $result['pendingreason']; | |
f8453bef | 375 | if (!empty($params['is_recur'])) { |
376 | // See comment block. | |
455cd95d | 377 | $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); |
f8453bef | 378 | } |
379 | else { | |
455cd95d | 380 | $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
f8453bef | 381 | } |
6a488035 TO |
382 | return $params; |
383 | } | |
384 | ||
6c786a9b | 385 | /** |
e58c1c1a | 386 | * Create recurring payments. |
387 | * | |
8acddb67 | 388 | * Use a pre-authorisation token to activate a recurring payment profile |
389 | * https://developer.paypal.com/docs/classic/api/merchant/CreateRecurringPaymentsProfile_API_Operation_NVP/ | |
390 | * | |
c490a46a | 391 | * @param array $params |
6c786a9b EM |
392 | * |
393 | * @return mixed | |
8acddb67 | 394 | * @throws \Exception |
6c786a9b | 395 | */ |
00be9182 | 396 | public function createRecurringPayments(&$params) { |
be2fb01f | 397 | $args = []; |
6a488035 TO |
398 | $this->initialize($args, 'CreateRecurringPaymentsProfile'); |
399 | ||
400 | $start_time = strtotime(date('m/d/Y')); | |
401 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
402 | ||
403 | $args['token'] = $params['token']; | |
aefd7f6b | 404 | $args['paymentAction'] = 'Sale'; |
6a488035 TO |
405 | $args['amt'] = $params['amount']; |
406 | $args['currencyCode'] = $params['currencyID']; | |
407 | $args['payerID'] = $params['payer_id']; | |
408 | $args['invnum'] = $params['invoiceID']; | |
6a488035 TO |
409 | $args['profilestartdate'] = $start_date; |
410 | $args['method'] = 'CreateRecurringPaymentsProfile'; | |
411 | $args['billingfrequency'] = $params['frequency_interval']; | |
412 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
413 | $args['desc'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit']; | |
819c0bd6 | 414 | $args['totalbillingcycles'] = CRM_Utils_Array::value('installments', $params); |
6a488035 | 415 | $args['version'] = '56.0'; |
6c552737 | 416 | $args['profilereference'] = "i={$params['invoiceID']}" . |
933da5b5 | 417 | "&m=" . |
1ac462d0 DL |
418 | "&c={$params['contactID']}" . |
419 | "&r={$params['contributionRecurID']}" . | |
420 | "&b={$params['contributionID']}" . | |
421 | "&p={$params['contributionPageID']}"; | |
6a488035 | 422 | |
a3caf338 | 423 | // add CiviCRM BN code |
424 | $args['BUTTONSOURCE'] = 'CiviCRM_SP'; | |
425 | ||
6a488035 TO |
426 | $result = $this->invokeAPI($args); |
427 | ||
428 | if (is_a($result, 'CRM_Core_Error')) { | |
429 | return $result; | |
430 | } | |
431 | ||
819c0bd6 | 432 | /* Success - result looks like" |
433 | * array ( | |
434 | * 'profileid' => 'I-CP1U0PLG91R2', | |
435 | * 'profilestatus' => 'ActiveProfile', | |
436 | * 'timestamp' => '2018-05-07T03:55:52Z', | |
437 | * 'correlationid' => 'e717999e9bf62', | |
438 | * 'ack' => 'Success', | |
439 | * 'version' => '56.0', | |
440 | * 'build' => '39949200',) | |
6439290e | 441 | */ |
819c0bd6 | 442 | $params['trxn_id'] = $result['profileid']; |
443 | $params['payment_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); | |
6a488035 TO |
444 | |
445 | return $params; | |
446 | } | |
e58c1c1a | 447 | |
6c786a9b | 448 | /** |
e58c1c1a | 449 | * Initialise. |
450 | * | |
6c786a9b EM |
451 | * @param $args |
452 | * @param $method | |
453 | */ | |
00be9182 | 454 | public function initialize(&$args, $method) { |
353ffa53 TO |
455 | $args['user'] = $this->_paymentProcessor['user_name']; |
456 | $args['pwd'] = $this->_paymentProcessor['password']; | |
457 | $args['version'] = 3.0; | |
6a488035 | 458 | $args['signature'] = $this->_paymentProcessor['signature']; |
353ffa53 TO |
459 | $args['subject'] = CRM_Utils_Array::value('subject', $this->_paymentProcessor); |
460 | $args['method'] = $method; | |
6a488035 | 461 | } |
f8453bef | 462 | |
463 | /** | |
e1268958 | 464 | * Process payment - this function wraps around both doTransferCheckout and doDirectPayment. |
f8453bef | 465 | * |
466 | * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms | |
467 | * more agnostic. | |
468 | * | |
469 | * Payment processors should set payment_status_id. This function adds some historical defaults ie. the | |
470 | * assumption that if a 'doDirectPayment' processors comes back it completed the transaction & in fact | |
471 | * doTransferCheckout would not traditionally come back. | |
472 | * | |
473 | * doDirectPayment does not do an immediate payment for Authorize.net or Paypal so the default is assumed | |
474 | * to be Pending. | |
475 | * | |
476 | * Once this function is fully rolled out then it will be preferred for processors to throw exceptions than to | |
477 | * return Error objects | |
478 | * | |
479 | * @param array $params | |
480 | * | |
481 | * @param string $component | |
482 | * | |
483 | * @return array | |
484 | * Result array | |
485 | * | |
486 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
487 | */ | |
488 | public function doPayment(&$params, $component = 'contribute') { | |
e491d0c9 | 489 | if ($this->isPayPalType($this::PAYPAL_EXPRESS) || ($this->isPayPalType($this::PAYPAL_PRO) && !empty($params['token']))) { |
e58c1c1a | 490 | $this->_component = $component; |
491 | return $this->doExpressCheckout($params); | |
492 | ||
f8453bef | 493 | } |
e58c1c1a | 494 | return parent::doPayment($params, $component); |
f8453bef | 495 | } |
6a488035 TO |
496 | |
497 | /** | |
498 | * This function collects all the information from a web/api form and invokes | |
499 | * the relevant payment processor specific functions to perform the transaction | |
500 | * | |
6a0b768e TO |
501 | * @param array $params |
502 | * Assoc array of input parameters for this transaction. | |
6a488035 | 503 | * |
da6b46f4 | 504 | * @param string $component |
a6c01b45 CW |
505 | * @return array |
506 | * the result in an nice formatted array (or an error object) | |
8acddb67 | 507 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6a488035 | 508 | */ |
00be9182 | 509 | public function doDirectPayment(&$params, $component = 'contribute') { |
be2fb01f | 510 | $args = []; |
6a488035 TO |
511 | |
512 | $this->initialize($args, 'DoDirectPayment'); | |
513 | ||
aefd7f6b | 514 | $args['paymentAction'] = 'Sale'; |
88afada7 | 515 | $args['amt'] = $this->getAmount($params); |
899c09b3 | 516 | $args['currencyCode'] = $this->getCurrency($params); |
6a488035 TO |
517 | $args['invnum'] = $params['invoiceID']; |
518 | $args['ipaddress'] = $params['ip_address']; | |
519 | $args['creditCardType'] = $params['credit_card_type']; | |
520 | $args['acct'] = $params['credit_card_number']; | |
521 | $args['expDate'] = sprintf('%02d', $params['month']) . $params['year']; | |
522 | $args['cvv2'] = $params['cvv2']; | |
523 | $args['firstName'] = $params['first_name']; | |
524 | $args['lastName'] = $params['last_name']; | |
525 | $args['email'] = CRM_Utils_Array::value('email', $params); | |
526 | $args['street'] = $params['street_address']; | |
527 | $args['city'] = $params['city']; | |
528 | $args['state'] = $params['state_province']; | |
529 | $args['countryCode'] = $params['country']; | |
530 | $args['zip'] = $params['postal_code']; | |
0ab1fbfb | 531 | $args['desc'] = substr(CRM_Utils_Array::value('description', $params), 0, 127); |
6a488035 TO |
532 | $args['custom'] = CRM_Utils_Array::value('accountingCode', $params); |
533 | ||
ebf695c3 | 534 | // add CiviCRM BN code |
535 | $args['BUTTONSOURCE'] = 'CiviCRM_SP'; | |
536 | ||
6a488035 TO |
537 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { |
538 | $start_time = strtotime(date('m/d/Y')); | |
539 | $start_date = date('Y-m-d\T00:00:00\Z', $start_time); | |
540 | ||
541 | $args['PaymentAction'] = 'Sale'; | |
542 | $args['billingperiod'] = ucwords($params['frequency_unit']); | |
543 | $args['billingfrequency'] = $params['frequency_interval']; | |
544 | $args['method'] = "CreateRecurringPaymentsProfile"; | |
545 | $args['profilestartdate'] = $start_date; | |
6c552737 | 546 | $args['desc'] = "" . |
1ac462d0 DL |
547 | $params['description'] . ": " . |
548 | $params['amount'] . " Per " . | |
549 | $params['frequency_interval'] . " " . | |
550 | $params['frequency_unit']; | |
88afada7 | 551 | $args['amt'] = $this->getAmount($params); |
0bde274f | 552 | $args['totalbillingcycles'] = CRM_Utils_Array::value('installments', $params); |
6a488035 | 553 | $args['version'] = 56.0; |
6c552737 | 554 | $args['PROFILEREFERENCE'] = "" . |
1ac462d0 DL |
555 | "i=" . $params['invoiceID'] . "&m=" . $component . |
556 | "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] . | |
557 | "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID']; | |
6a488035 TO |
558 | } |
559 | ||
560 | // Allow further manipulation of the arguments via custom hooks .. | |
561 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $args); | |
562 | ||
563 | $result = $this->invokeAPI($args); | |
564 | ||
8acddb67 | 565 | // WAG |
6a488035 TO |
566 | if (is_a($result, 'CRM_Core_Error')) { |
567 | return $result; | |
568 | } | |
569 | ||
570 | $params['recurr_profile_id'] = NULL; | |
571 | ||
572 | if (CRM_Utils_Array::value('is_recur', $params) == 1) { | |
573 | $params['recurr_profile_id'] = $result['profileid']; | |
574 | } | |
575 | ||
576 | /* Success */ | |
577 | ||
578 | $params['trxn_id'] = CRM_Utils_Array::value('transactionid', $result); | |
579 | $params['gross_amount'] = CRM_Utils_Array::value('amt', $result); | |
9d2f24ee | 580 | $params = array_merge($params, $this->doQuery($params)); |
6a488035 TO |
581 | return $params; |
582 | } | |
583 | ||
9d2f24ee | 584 | /** |
585 | * Query payment processor for details about a transaction. | |
586 | * | |
587 | * For paypal see : https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/GetTransactionDetails_API_Operation_NVP/ | |
588 | * | |
589 | * @param array $params | |
590 | * Array of parameters containing one of: | |
591 | * - trxn_id Id of an individual transaction. | |
592 | * - processor_id Id of a recurring contribution series as stored in the civicrm_contribution_recur table. | |
593 | * | |
594 | * @return array | |
595 | * Extra parameters retrieved. | |
596 | * Any parameters retrievable through this should be documented in the function comments at | |
597 | * CRM_Core_Payment::doQuery. Currently | |
598 | * - fee_amount Amount of fee paid | |
599 | * | |
600 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
601 | */ | |
602 | public function doQuery($params) { | |
0bde274f | 603 | //CRM-18140 - trxn_id not returned for recurring paypal transaction |
604 | if (!empty($params['is_recur'])) { | |
be2fb01f | 605 | return []; |
0bde274f | 606 | } |
607 | elseif (empty($params['trxn_id'])) { | |
9d2f24ee | 608 | throw new \Civi\Payment\Exception\PaymentProcessorException('transaction id not set'); |
609 | } | |
be2fb01f | 610 | $args = [ |
9d2f24ee | 611 | 'TRANSACTIONID' => $params['trxn_id'], |
be2fb01f | 612 | ]; |
9d2f24ee | 613 | $this->initialize($args, 'GetTransactionDetails'); |
614 | $result = $this->invokeAPI($args); | |
be2fb01f | 615 | return [ |
9d2f24ee | 616 | 'fee_amount' => $result['feeamt'], |
8a2a56e9 | 617 | 'net_amount' => $params['gross_amount'] - $result['feeamt'], |
be2fb01f | 618 | ]; |
9d2f24ee | 619 | } |
620 | ||
6a488035 | 621 | /** |
fe482240 | 622 | * This function checks to see if we have the right config values. |
6a488035 | 623 | * |
8acddb67 | 624 | * @return null|string |
a6c01b45 | 625 | * the error message if any |
8acddb67 | 626 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6a488035 | 627 | */ |
00be9182 | 628 | public function checkConfig() { |
be2fb01f | 629 | $error = []; |
6a488035 | 630 | |
e491d0c9 | 631 | if (!$this->isPayPalType($this::PAYPAL_STANDARD)) { |
6a488035 | 632 | if (empty($this->_paymentProcessor['signature'])) { |
0501a7d6 | 633 | $error[] = ts('Signature is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
634 | } |
635 | ||
636 | if (empty($this->_paymentProcessor['password'])) { | |
0501a7d6 | 637 | $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.'); |
6a488035 TO |
638 | } |
639 | } | |
75466733 | 640 | if (empty($this->_paymentProcessor['user_name'])) { |
1b9f9ca3 EM |
641 | $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.'); |
642 | } | |
6a488035 TO |
643 | |
644 | if (!empty($error)) { | |
645 | return implode('<p>', $error); | |
646 | } | |
647 | else { | |
648 | return NULL; | |
649 | } | |
650 | } | |
651 | ||
6c786a9b EM |
652 | /** |
653 | * @return null|string | |
8acddb67 | 654 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6c786a9b | 655 | */ |
00be9182 | 656 | public function cancelSubscriptionURL() { |
e491d0c9 | 657 | if ($this->isPayPalType($this::PAYPAL_STANDARD)) { |
6a488035 TO |
658 | return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor['user_name']); |
659 | } | |
660 | else { | |
661 | return NULL; | |
662 | } | |
663 | } | |
664 | ||
b5c2afd0 | 665 | /** |
100fef9d | 666 | * Check whether a method is present ( & supported ) by the payment processor object. |
b5c2afd0 | 667 | * |
6a0b768e TO |
668 | * @param string $method |
669 | * Method to check for. | |
b5c2afd0 | 670 | * |
5c766a0b | 671 | * @return bool |
e491d0c9 | 672 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
b5c2afd0 | 673 | */ |
1524a007 | 674 | public function isSupported($method) { |
e491d0c9 | 675 | if (!$this->isPayPalType($this::PAYPAL_PRO)) { |
6a488035 TO |
676 | // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported |
677 | // by standard or express. | |
678 | return FALSE; | |
679 | } | |
680 | return parent::isSupported($method); | |
681 | } | |
682 | ||
1ba4a3aa EM |
683 | /** |
684 | * Paypal express replaces the submit button with it's own. | |
685 | * | |
686 | * @return bool | |
687 | * Should the form button by suppressed? | |
e491d0c9 | 688 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
1ba4a3aa EM |
689 | */ |
690 | public function isSuppressSubmitButtons() { | |
e491d0c9 | 691 | if ($this->isPayPalType($this::PAYPAL_EXPRESS)) { |
1ba4a3aa EM |
692 | return TRUE; |
693 | } | |
694 | return FALSE; | |
695 | } | |
696 | ||
6c786a9b EM |
697 | /** |
698 | * @param string $message | |
699 | * @param array $params | |
700 | * | |
701 | * @return array|bool|object | |
e491d0c9 | 702 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6c786a9b | 703 | */ |
be2fb01f | 704 | public function cancelSubscription(&$message = '', $params = []) { |
ccc7f59c | 705 | if ($this->isPayPalType($this::PAYPAL_PRO) || $this->isPayPalType($this::PAYPAL_EXPRESS)) { |
be2fb01f | 706 | $args = []; |
6a488035 TO |
707 | $this->initialize($args, 'ManageRecurringPaymentsProfileStatus'); |
708 | ||
709 | $args['PROFILEID'] = CRM_Utils_Array::value('subscriptionId', $params); | |
353ffa53 TO |
710 | $args['ACTION'] = 'Cancel'; |
711 | $args['NOTE'] = CRM_Utils_Array::value('reason', $params); | |
6a488035 TO |
712 | |
713 | $result = $this->invokeAPI($args); | |
714 | if (is_a($result, 'CRM_Core_Error')) { | |
715 | return $result; | |
716 | } | |
717 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
718 | return TRUE; | |
719 | } | |
720 | return FALSE; | |
721 | } | |
722 | ||
5495e78a EM |
723 | /** |
724 | * Process incoming notification. | |
e491d0c9 MW |
725 | * |
726 | * @throws \CRM_Core_Exception | |
727 | * @throws \CiviCRM_API3_Exception | |
5495e78a | 728 | */ |
518fa0ee | 729 | public static function handlePaymentNotification() { |
ddc4b5af | 730 | $params = array_merge($_GET, $_REQUEST); |
731 | $q = explode('/', CRM_Utils_Array::value('q', $params, '')); | |
732 | $lastParam = array_pop($q); | |
733 | if (is_numeric($lastParam)) { | |
734 | $params['processor_id'] = $lastParam; | |
735 | } | |
be2fb01f | 736 | $result = civicrm_api3('PaymentProcessor', 'get', [ |
08e6409d AS |
737 | 'sequential' => 1, |
738 | 'id' => $params['processor_id'], | |
be2fb01f CW |
739 | 'api.PaymentProcessorType.getvalue' => ['return' => "name"], |
740 | ]); | |
b501170c AS |
741 | if (!$result['count']) { |
742 | throw new CRM_Core_Exception("Could not find a processor with the given processor_id value '{$params['processor_id']}'."); | |
743 | } | |
744 | ||
745 | $paymentProcessorType = CRM_Utils_Array::value('api.PaymentProcessorType.getvalue', $result['values'][0]); | |
746 | switch ($paymentProcessorType) { | |
08e6409d AS |
747 | case 'PayPal': |
748 | // "PayPal - Website Payments Pro" | |
749 | $paypalIPN = new CRM_Core_Payment_PayPalProIPN($params); | |
750 | break; | |
751 | ||
752 | case 'PayPal_Standard': | |
753 | // "PayPal - Website Payments Standard" | |
754 | $paypalIPN = new CRM_Core_Payment_PayPalIPN($params); | |
755 | break; | |
756 | ||
757 | default: | |
758 | // If we don't have PayPal Standard or PayPal Pro, something's wrong. | |
759 | // Log an error and exit. | |
b501170c | 760 | 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 | 761 | } |
08e6409d | 762 | |
5495e78a EM |
763 | $paypalIPN->main(); |
764 | } | |
765 | ||
6c786a9b EM |
766 | /** |
767 | * @param string $message | |
768 | * @param array $params | |
769 | * | |
770 | * @return array|bool|object | |
8acddb67 | 771 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6c786a9b | 772 | */ |
be2fb01f | 773 | public function updateSubscriptionBillingInfo(&$message = '', $params = []) { |
e491d0c9 | 774 | if ($this->isPayPalType($this::PAYPAL_PRO)) { |
6a488035 | 775 | $config = CRM_Core_Config::singleton(); |
be2fb01f | 776 | $args = []; |
6a488035 TO |
777 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); |
778 | ||
779 | $args['PROFILEID'] = $params['subscriptionId']; | |
88afada7 | 780 | $args['AMT'] = $this->getAmount($params); |
6a488035 TO |
781 | $args['CURRENCYCODE'] = $config->defaultCurrency; |
782 | $args['CREDITCARDTYPE'] = $params['credit_card_type']; | |
783 | $args['ACCT'] = $params['credit_card_number']; | |
784 | $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year']; | |
785 | $args['CVV2'] = $params['cvv2']; | |
786 | ||
353ffa53 TO |
787 | $args['FIRSTNAME'] = $params['first_name']; |
788 | $args['LASTNAME'] = $params['last_name']; | |
789 | $args['STREET'] = $params['street_address']; | |
790 | $args['CITY'] = $params['city']; | |
791 | $args['STATE'] = $params['state_province']; | |
6a488035 | 792 | $args['COUNTRYCODE'] = $params['postal_code']; |
353ffa53 | 793 | $args['ZIP'] = $params['country']; |
6a488035 TO |
794 | |
795 | $result = $this->invokeAPI($args); | |
796 | if (is_a($result, 'CRM_Core_Error')) { | |
797 | return $result; | |
798 | } | |
799 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
800 | return TRUE; | |
801 | } | |
802 | return FALSE; | |
803 | } | |
804 | ||
6c786a9b EM |
805 | /** |
806 | * @param string $message | |
807 | * @param array $params | |
808 | * | |
809 | * @return array|bool|object | |
e491d0c9 | 810 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
6c786a9b | 811 | */ |
be2fb01f | 812 | public function changeSubscriptionAmount(&$message = '', $params = []) { |
e491d0c9 | 813 | if ($this->isPayPalType($this::PAYPAL_PRO)) { |
6a488035 | 814 | $config = CRM_Core_Config::singleton(); |
be2fb01f | 815 | $args = []; |
6a488035 TO |
816 | $this->initialize($args, 'UpdateRecurringPaymentsProfile'); |
817 | ||
818 | $args['PROFILEID'] = $params['subscriptionId']; | |
88afada7 | 819 | $args['AMT'] = $this->getAmount($params); |
6a488035 TO |
820 | $args['CURRENCYCODE'] = $config->defaultCurrency; |
821 | $args['BILLINGFREQUENCY'] = $params['installments']; | |
822 | ||
823 | $result = $this->invokeAPI($args); | |
824 | CRM_Core_Error::debug_var('$result', $result); | |
825 | if (is_a($result, 'CRM_Core_Error')) { | |
826 | return $result; | |
827 | } | |
828 | $message = "{$result['ack']}: profileid={$result['profileid']}"; | |
829 | return TRUE; | |
830 | } | |
831 | return FALSE; | |
832 | } | |
833 | ||
38b66756 EM |
834 | /** |
835 | * Function to action pre-approval if supported | |
836 | * | |
837 | * @param array $params | |
838 | * Parameters from the form | |
839 | * | |
840 | * @return array | |
841 | * - pre_approval_parameters (this will be stored on the calling form & available later) | |
842 | * - redirect_url (if set the browser will be redirected to this. | |
8acddb67 | 843 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
38b66756 | 844 | */ |
3910048c | 845 | public function doPreApproval(&$params) { |
1d292cf3 | 846 | if (!$this->isPaypalExpress($params)) { |
be2fb01f | 847 | return []; |
ec022878 | 848 | } |
05c302ec | 849 | $this->_component = $params['component']; |
3910048c | 850 | $token = $this->setExpressCheckOut($params); |
be2fb01f CW |
851 | return [ |
852 | 'pre_approval_parameters' => ['token' => $token], | |
3910048c | 853 | 'redirect_url' => $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token", |
be2fb01f | 854 | ]; |
3910048c EM |
855 | } |
856 | ||
6c786a9b | 857 | /** |
c490a46a | 858 | * @param array $params |
6c786a9b EM |
859 | * @param string $component |
860 | * | |
861 | * @throws Exception | |
862 | */ | |
00be9182 | 863 | public function doTransferCheckout(&$params, $component = 'contribute') { |
6a488035 | 864 | |
be2fb01f CW |
865 | $notifyParameters = ['module' => $component]; |
866 | $notifyParameterMap = [ | |
ddc4b5af | 867 | 'contactID' => 'contactID', |
868 | 'contributionID' => 'contributionID', | |
869 | 'eventID' => 'eventID', | |
870 | 'participantID' => 'participantID', | |
871 | 'membershipID' => 'membershipID', | |
872 | 'related_contact' => 'relatedContactID', | |
873 | 'onbehalf_dupe_alert' => 'onBehalfDupeAlert', | |
068fb0de | 874 | 'accountingCode' => 'accountingCode', |
11a4431c | 875 | 'contributionRecurID' => 'contributionRecurID', |
876 | 'contributionPageID' => 'contributionPageID', | |
be2fb01f | 877 | ]; |
ddc4b5af | 878 | foreach ($notifyParameterMap as $paramsName => $notifyName) { |
879 | if (!empty($params[$paramsName])) { | |
880 | $notifyParameters[$notifyName] = $params[$paramsName]; | |
6a488035 TO |
881 | } |
882 | } | |
ddc4b5af | 883 | $notifyURL = $this->getNotifyUrl(); |
6a488035 | 884 | |
ddc4b5af | 885 | $config = CRM_Core_Config::singleton(); |
353ffa53 TO |
886 | $url = ($component == 'event') ? 'civicrm/event/register' : 'civicrm/contribute/transact'; |
887 | $cancel = ($component == 'event') ? '_qf_Register_display' : '_qf_Main_display'; | |
6a488035 TO |
888 | |
889 | $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}"; | |
a7488080 | 890 | if (!empty($params['is_recur'])) { |
1ac462d0 | 891 | $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}"; |
6a488035 TO |
892 | } |
893 | ||
1ac462d0 DL |
894 | $cancelURL = CRM_Utils_System::url( |
895 | $url, | |
6a488035 TO |
896 | $cancelUrlString, |
897 | TRUE, NULL, FALSE | |
898 | ); | |
899 | ||
be2fb01f | 900 | $paypalParams = [ |
6a488035 TO |
901 | 'business' => $this->_paymentProcessor['user_name'], |
902 | 'notify_url' => $notifyURL, | |
683538c8 | 903 | 'item_name' => $this->getPaymentDescription($params, 127), |
6a488035 TO |
904 | 'quantity' => 1, |
905 | 'undefined_quantity' => 0, | |
906 | 'cancel_return' => $cancelURL, | |
907 | 'no_note' => 1, | |
908 | 'no_shipping' => 1, | |
ddc4b5af | 909 | 'return' => $this->getReturnSuccessUrl($params['qfKey']), |
6a488035 TO |
910 | 'rm' => 2, |
911 | 'currency_code' => $params['currencyID'], | |
912 | 'invoice' => $params['invoiceID'], | |
913 | 'lc' => substr($config->lcMessages, -2), | |
914 | 'charset' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'UTF-8', | |
068fb0de | 915 | 'custom' => json_encode($notifyParameters), |
de753541 | 916 | 'bn' => 'CiviCRM_SP', |
be2fb01f | 917 | ]; |
6a488035 TO |
918 | |
919 | // add name and address if available, CRM-3130 | |
be2fb01f | 920 | $otherVars = [ |
6a488035 TO |
921 | 'first_name' => 'first_name', |
922 | 'last_name' => 'last_name', | |
923 | 'street_address' => 'address1', | |
924 | 'country' => 'country', | |
925 | 'preferred_language' => 'lc', | |
926 | 'city' => 'city', | |
927 | 'state_province' => 'state', | |
928 | 'postal_code' => 'zip', | |
929 | 'email' => 'email', | |
be2fb01f | 930 | ]; |
6a488035 TO |
931 | |
932 | foreach (array_keys($params) as $p) { | |
933 | // get the base name without the location type suffixed to it | |
934 | $parts = explode('-', $p); | |
935 | $name = count($parts) > 1 ? $parts[0] : $p; | |
936 | if (isset($otherVars[$name])) { | |
937 | $value = $params[$p]; | |
938 | if ($value) { | |
939 | if ($name == 'state_province') { | |
940 | $stateName = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
941 | $value = $stateName; | |
942 | } | |
943 | if ($name == 'country') { | |
944 | $countryName = CRM_Core_PseudoConstant::countryIsoCode($value); | |
945 | $value = $countryName; | |
946 | } | |
947 | // ensure value is not an array | |
948 | // CRM-4174 | |
949 | if (!is_array($value)) { | |
950 | $paypalParams[$otherVars[$name]] = $value; | |
951 | } | |
952 | } | |
953 | } | |
954 | } | |
955 | ||
956 | // if recurring donations, add a few more items | |
957 | if (!empty($params['is_recur'])) { | |
11a4431c | 958 | if (!$params['contributionRecurID']) { |
6a488035 TO |
959 | CRM_Core_Error::fatal(ts('Recurring contribution, but no database id')); |
960 | } | |
961 | ||
be2fb01f | 962 | $paypalParams += [ |
6a488035 | 963 | 'cmd' => '_xclick-subscriptions', |
88afada7 | 964 | 'a3' => $this->getAmount($params), |
353ffa53 TO |
965 | 'p3' => $params['frequency_interval'], |
966 | 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)), | |
6a488035 TO |
967 | 'src' => 1, |
968 | 'sra' => 1, | |
14965416 | 969 | 'srt' => CRM_Utils_Array::value('installments', $params), |
6a488035 TO |
970 | 'no_note' => 1, |
971 | 'modify' => 0, | |
be2fb01f | 972 | ]; |
6a488035 TO |
973 | } |
974 | else { | |
be2fb01f | 975 | $paypalParams += [ |
6a488035 TO |
976 | 'cmd' => '_xclick', |
977 | 'amount' => $params['amount'], | |
be2fb01f | 978 | ]; |
6a488035 TO |
979 | } |
980 | ||
981 | // Allow further manipulation of the arguments via custom hooks .. | |
982 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $paypalParams); | |
983 | ||
984 | $uri = ''; | |
985 | foreach ($paypalParams as $key => $value) { | |
986 | if ($value === NULL) { | |
987 | continue; | |
988 | } | |
989 | ||
990 | $value = urlencode($value); | |
991 | if ($key == 'return' || | |
992 | $key == 'cancel_return' || | |
993 | $key == 'notify_url' | |
994 | ) { | |
995 | $value = str_replace('%2F', '/', $value); | |
996 | } | |
997 | $uri .= "&{$key}={$value}"; | |
998 | } | |
999 | ||
353ffa53 TO |
1000 | $uri = substr($uri, 1); |
1001 | $url = $this->_paymentProcessor['url_site']; | |
1002 | $sub = empty($params['is_recur']) ? 'cgi-bin/webscr' : 'subscriptions'; | |
6a488035 TO |
1003 | $paypalURL = "{$url}{$sub}?$uri"; |
1004 | ||
1005 | CRM_Utils_System::redirect($paypalURL); | |
1006 | } | |
1007 | ||
1008 | /** | |
ad37ac8e | 1009 | * Hash_call: Function to perform the API call to PayPal using API signature. |
1010 | * | |
6a488035 TO |
1011 | * @methodName is name of API method. |
1012 | * @nvpStr is nvp string. | |
ad37ac8e | 1013 | * returns an associative array containing the response from the server. |
1014 | * | |
1015 | * @param array $args | |
1016 | * @param null $url | |
1017 | * | |
1018 | * @return array|object | |
1019 | * @throws \Exception | |
6a488035 | 1020 | */ |
00be9182 | 1021 | public function invokeAPI($args, $url = NULL) { |
6a488035 TO |
1022 | |
1023 | if ($url === NULL) { | |
1024 | if (empty($this->_paymentProcessor['url_api'])) { | |
1025 | CRM_Core_Error::fatal(ts('Please set the API URL. Please refer to the documentation for more details')); | |
1026 | } | |
1027 | ||
1028 | $url = $this->_paymentProcessor['url_api'] . 'nvp'; | |
1029 | } | |
1030 | ||
be2fb01f | 1031 | $p = []; |
fa2c4d0b | 1032 | foreach ($args as $n => $v) { |
1033 | $p[] = "$n=" . urlencode($v); | |
1034 | } | |
1035 | ||
1036 | //NVPRequest for submitting to server | |
1037 | $nvpreq = implode('&', $p); | |
1038 | ||
6a488035 TO |
1039 | if (!function_exists('curl_init')) { |
1040 | CRM_Core_Error::fatal("curl functions NOT available."); | |
1041 | } | |
1042 | ||
1043 | //setting the curl parameters. | |
1044 | $ch = curl_init(); | |
1045 | curl_setopt($ch, CURLOPT_URL, $url); | |
c20af6a3 | 1046 | curl_setopt($ch, CURLOPT_VERBOSE, 0); |
6a488035 TO |
1047 | |
1048 | //turning off the server and peer verification(TrustManager Concept). | |
aaffa79f | 1049 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, Civi::settings()->get('verifySSL')); |
1050 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, Civi::settings()->get('verifySSL') ? 2 : 0); | |
6a488035 TO |
1051 | |
1052 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
1053 | curl_setopt($ch, CURLOPT_POST, 1); | |
1054 | ||
6a488035 TO |
1055 | //setting the nvpreq as POST FIELD to curl |
1056 | curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); | |
1057 | ||
1058 | //getting response from server | |
1059 | $response = curl_exec($ch); | |
1060 | ||
1061 | //converting NVPResponse to an Associative Array | |
1062 | $result = self::deformat($response); | |
1063 | ||
1064 | if (curl_errno($ch)) { | |
1065 | $e = CRM_Core_Error::singleton(); | |
1066 | $e->push(curl_errno($ch), | |
1067 | 0, NULL, | |
1068 | curl_error($ch) | |
1069 | ); | |
1070 | return $e; | |
1071 | } | |
1072 | else { | |
1073 | curl_close($ch); | |
1074 | } | |
1075 | ||
fa2c4d0b | 1076 | $outcome = strtolower(CRM_Utils_Array::value('ack', $result)); |
1077 | ||
1078 | if ($outcome != 'success' && $outcome != 'successwithwarning') { | |
abfb35ee | 1079 | throw new PaymentProcessorException("{$result['l_shortmessage0']} {$result['l_longmessage0']}"); |
6a488035 TO |
1080 | } |
1081 | ||
1082 | return $result; | |
1083 | } | |
1084 | ||
be2e0c6a | 1085 | /** |
ad37ac8e | 1086 | * This function will take NVPString and convert it to an Associative Array. |
1087 | * | |
1088 | * It will decode the response. It is useful to search for a particular key and displaying arrays. | |
1089 | * | |
1090 | * @param string $str | |
1091 | * | |
1092 | * @return array | |
6a488035 | 1093 | */ |
00be9182 | 1094 | public static function deformat($str) { |
be2fb01f | 1095 | $result = []; |
6a488035 TO |
1096 | |
1097 | while (strlen($str)) { | |
b44e3f84 | 1098 | // position of key |
6a488035 TO |
1099 | $keyPos = strpos($str, '='); |
1100 | ||
1101 | // position of value | |
1102 | $valPos = strpos($str, '&') ? strpos($str, '&') : strlen($str); | |
1103 | ||
1104 | /*getting the Key and Value values and storing in a Associative Array*/ | |
1105 | ||
1106 | $key = substr($str, 0, $keyPos); | |
1107 | $val = substr($str, $keyPos + 1, $valPos - $keyPos - 1); | |
1108 | ||
1109 | //decoding the respose | |
1110 | $result[strtolower(urldecode($key))] = urldecode($val); | |
1111 | $str = substr($str, $valPos + 1, strlen($str)); | |
1112 | } | |
1113 | ||
1114 | return $result; | |
1115 | } | |
96025800 | 1116 | |
3cc8d80d | 1117 | /** |
1118 | * Get array of fields that should be displayed on the payment form. | |
1119 | * | |
1120 | * @return array | |
e491d0c9 | 1121 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
3cc8d80d | 1122 | */ |
1123 | public function getPaymentFormFields() { | |
e491d0c9 | 1124 | if ($this->isPayPalType($this::PAYPAL_PRO)) { |
3cc8d80d | 1125 | return $this->getCreditCardFormFields(); |
1126 | } | |
1127 | else { | |
be2fb01f | 1128 | return []; |
3cc8d80d | 1129 | } |
1130 | } | |
1131 | ||
933da5b5 EM |
1132 | /** |
1133 | * Map the paypal params to CiviCRM params using a field map. | |
1134 | * | |
1135 | * @param array $fieldMap | |
1136 | * @param array $paypalParams | |
1137 | * | |
1138 | * @return array | |
1139 | */ | |
1140 | protected function mapPaypalParamsToCivicrmParams($fieldMap, $paypalParams) { | |
be2fb01f | 1141 | $params = []; |
933da5b5 EM |
1142 | foreach ($fieldMap as $civicrmField => $paypalField) { |
1143 | $params[$civicrmField] = isset($paypalParams[$paypalField]) ? $paypalParams[$paypalField] : NULL; | |
1144 | } | |
1145 | return $params; | |
1146 | } | |
1147 | ||
1d292cf3 | 1148 | /** |
1149 | * Is this being processed by payment express. | |
1150 | * | |
1151 | * Either because it is payment express or because is pro with paypal express in use. | |
1152 | * | |
1153 | * @param array $params | |
1154 | * | |
1155 | * @return bool | |
8acddb67 | 1156 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
1d292cf3 | 1157 | */ |
1158 | protected function isPaypalExpress($params) { | |
e491d0c9 | 1159 | if ($this->isPayPalType($this::PAYPAL_EXPRESS)) { |
1d292cf3 | 1160 | return TRUE; |
1161 | } | |
1162 | ||
1163 | // This would occur postProcess. | |
1164 | if (!empty($params['token'])) { | |
1165 | return TRUE; | |
1166 | } | |
1167 | if (isset($params['button']) && stristr($params['button'], 'express')) { | |
1168 | return TRUE; | |
1169 | } | |
1170 | ||
1171 | // The contribution form passes a 'button' but the event form might still set one of these fields. | |
1172 | // @todo more standardisation & get paypal fully out of the form layer. | |
be2fb01f | 1173 | $possibleExpressFields = [ |
1d292cf3 | 1174 | '_qf_Register_upload_express_x', |
1175 | '_qf_Payment_upload_express_x', | |
be2fb01f | 1176 | ]; |
1d292cf3 | 1177 | if (array_intersect_key($params, array_fill_keys($possibleExpressFields, 1))) { |
1178 | return TRUE; | |
1179 | } | |
1180 | return FALSE; | |
1181 | } | |
1182 | ||
6a488035 | 1183 | } |