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