@@ -20,413 +20,413 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class EE_Payment_Processor extends EE_Processor_Base implements ResettableInterface |
| 22 | 22 | { |
| 23 | - private static ?EE_Payment_Processor $_instance; |
|
| 24 | - |
|
| 25 | - private IpnHandler $ipn_handler; |
|
| 26 | - |
|
| 27 | - private PaymentProcessor $payment_processor; |
|
| 28 | - |
|
| 29 | - private PostPaymentProcessor $post_payment_processor; |
|
| 30 | - |
|
| 31 | - private RegistrationPayments $registration_payments; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @singleton method used to instantiate class object |
|
| 36 | - * @param PaymentProcessor|null $payment_processor |
|
| 37 | - * @param PostPaymentProcessor|null $post_payment_processor |
|
| 38 | - * @param RegistrationPayments|null $registration_payments |
|
| 39 | - * @param IpnHandler|null $ipn_handler |
|
| 40 | - * @return EE_Payment_Processor instance |
|
| 41 | - */ |
|
| 42 | - public static function instance( |
|
| 43 | - ?PaymentProcessor $payment_processor = null, |
|
| 44 | - ?PostPaymentProcessor $post_payment_processor = null, |
|
| 45 | - ?RegistrationPayments $registration_payments = null, |
|
| 46 | - ?IpnHandler $ipn_handler = null |
|
| 47 | - ): EE_Payment_Processor { |
|
| 48 | - // check if class object is instantiated |
|
| 49 | - if (! EE_Payment_Processor::$_instance instanceof EE_Payment_Processor) { |
|
| 50 | - EE_Payment_Processor::$_instance = new EE_Payment_Processor( |
|
| 51 | - $payment_processor, |
|
| 52 | - $post_payment_processor, |
|
| 53 | - $registration_payments, |
|
| 54 | - $ipn_handler |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - return EE_Payment_Processor::$_instance; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @return EE_Payment_Processor |
|
| 63 | - */ |
|
| 64 | - public static function reset(): EE_Payment_Processor |
|
| 65 | - { |
|
| 66 | - EE_Payment_Processor::$_instance = null; |
|
| 67 | - return EE_Payment_Processor::instance(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @param PaymentProcessor|null $payment_processor |
|
| 73 | - * @param PostPaymentProcessor|null $post_payment_processor |
|
| 74 | - * @param RegistrationPayments|null $registration_payments |
|
| 75 | - * @param IpnHandler|null $ipn_handler |
|
| 76 | - */ |
|
| 77 | - private function __construct( |
|
| 78 | - ?PaymentProcessor $payment_processor, |
|
| 79 | - ?PostPaymentProcessor $post_payment_processor, |
|
| 80 | - ?RegistrationPayments $registration_payments, |
|
| 81 | - ?IpnHandler $ipn_handler |
|
| 82 | - ) { |
|
| 83 | - $this->payment_processor = $payment_processor instanceof PaymentProcessor |
|
| 84 | - ? $payment_processor |
|
| 85 | - : LoaderFactory::getShared(PaymentProcessor::class); |
|
| 86 | - $this->post_payment_processor = $post_payment_processor instanceof PostPaymentProcessor |
|
| 87 | - ? $post_payment_processor |
|
| 88 | - : LoaderFactory::getShared(PostPaymentProcessor::class); |
|
| 89 | - $this->registration_payments = $registration_payments instanceof RegistrationPayments |
|
| 90 | - ? $registration_payments |
|
| 91 | - : LoaderFactory::getShared(RegistrationPayments::class); |
|
| 92 | - $this->ipn_handler = $ipn_handler instanceof IpnHandler |
|
| 93 | - ? $ipn_handler |
|
| 94 | - : LoaderFactory::getShared(IpnHandler::class); |
|
| 95 | - do_action('AHEE__EE_Payment_Processor__construct'); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Using the selected gateway, processes the payment for that transaction, and updates the transaction |
|
| 101 | - * appropriately. Saves the payment that is generated |
|
| 102 | - * |
|
| 103 | - * @param EE_Payment_Method $payment_method |
|
| 104 | - * @param EE_Transaction $transaction |
|
| 105 | - * @param float|null $amount if only part of the transaction is to be paid for, how much. |
|
| 106 | - * Leave null if payment is for the full amount owing |
|
| 107 | - * @param EE_Billing_Info_Form|null $billing_form (or probably null, if it's an offline or offsite payment method). |
|
| 108 | - * Receive_form_submission() should have |
|
| 109 | - * already been called on the billing form |
|
| 110 | - * (ie, its inputs should have their normalized values set). |
|
| 111 | - * @param string|null $return_url string used mostly by offsite gateways to specify |
|
| 112 | - * where to go AFTER the offsite gateway |
|
| 113 | - * @param string $method like 'CART', indicates who the client who called this was |
|
| 114 | - * @param bool $by_admin TRUE if payment is being attempted from the admin |
|
| 115 | - * @param bool $update_txn whether to call |
|
| 116 | - * EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment() |
|
| 117 | - * @param string $cancel_url URL to return to if off-site payments are cancelled |
|
| 118 | - * @return EE_Payment |
|
| 119 | - * @throws EE_Error |
|
| 120 | - * @throws ReflectionException |
|
| 121 | - */ |
|
| 122 | - public function process_payment( |
|
| 123 | - EE_Payment_Method $payment_method, |
|
| 124 | - EE_Transaction $transaction, |
|
| 125 | - ?float $amount = null, |
|
| 126 | - ?EE_Billing_Info_Form $billing_form = null, |
|
| 127 | - ?string $return_url = null, |
|
| 128 | - string $method = 'CART', |
|
| 129 | - bool $by_admin = false, |
|
| 130 | - bool $update_txn = true, |
|
| 131 | - string $cancel_url = '' |
|
| 132 | - ): ?EE_Payment { |
|
| 133 | - return $this->payment_processor->processPayment( |
|
| 134 | - $payment_method, |
|
| 135 | - $transaction, |
|
| 136 | - $billing_form, |
|
| 137 | - (float) $amount, |
|
| 138 | - $by_admin, |
|
| 139 | - $update_txn, |
|
| 140 | - (string) $return_url, |
|
| 141 | - $cancel_url, |
|
| 142 | - $method |
|
| 143 | - ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param EE_Transaction|int $transaction |
|
| 149 | - * @param EE_Payment_Method $payment_method |
|
| 150 | - * @return string |
|
| 151 | - * @throws EE_Error |
|
| 152 | - * @throws ReflectionException |
|
| 153 | - */ |
|
| 154 | - public function get_ipn_url_for_payment_method($transaction, $payment_method): string |
|
| 155 | - { |
|
| 156 | - /** @type EE_Transaction $transaction */ |
|
| 157 | - $transaction = EEM_Transaction::instance()->ensure_is_obj($transaction); |
|
| 158 | - return $this->ipn_handler->getIpnUrlForPaymentMethod($transaction, $payment_method); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Process the IPN. Firstly, we'll hope we put the standard args into the IPN URL so |
|
| 164 | - * we can easily find what registration the IPN is for and what payment method. |
|
| 165 | - * However, if not, we'll give all payment methods a chance to claim it and process it. |
|
| 166 | - * If a payment is found for the IPN info, it is saved. |
|
| 167 | - * |
|
| 168 | - * @param array $_req_data form post data |
|
| 169 | - * @param EE_Transaction|int $transaction optional (or a transactions id) |
|
| 170 | - * @param EE_Payment_Method $payment_method (or a slug or id of one) |
|
| 171 | - * @param boolean $update_txn whether to call |
|
| 172 | - * EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment() |
|
| 173 | - * @param bool $separate_IPN_request whether the IPN uses a separate request (true, like PayPal) |
|
| 174 | - * or is processed manually (false, like Authorize.net) |
|
| 175 | - * @return EE_Payment |
|
| 176 | - * @throws EE_Error |
|
| 177 | - * @throws Exception |
|
| 178 | - * @throws ReflectionException |
|
| 179 | - */ |
|
| 180 | - public function process_ipn( |
|
| 181 | - $_req_data, |
|
| 182 | - $transaction = null, |
|
| 183 | - $payment_method = null, |
|
| 184 | - $update_txn = true, |
|
| 185 | - $separate_IPN_request = true |
|
| 186 | - ) { |
|
| 187 | - return $this->ipn_handler->processIPN( |
|
| 188 | - (array) $_req_data, |
|
| 189 | - $transaction, |
|
| 190 | - $payment_method, |
|
| 191 | - $update_txn, |
|
| 192 | - $separate_IPN_request |
|
| 193 | - ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Processes a direct refund request, saves the payment, and updates the transaction appropriately. |
|
| 199 | - * |
|
| 200 | - * @param EE_Payment_Method|null $payment_method |
|
| 201 | - * @param EE_Payment $payment_to_refund |
|
| 202 | - * @param array $refund_info |
|
| 203 | - * @return EE_Payment |
|
| 204 | - * @throws EE_Error |
|
| 205 | - * @throws ReflectionException |
|
| 206 | - */ |
|
| 207 | - public function process_refund( |
|
| 208 | - ?EE_Payment_Method $payment_method, |
|
| 209 | - EE_Payment $payment_to_refund, |
|
| 210 | - array $refund_info = [] |
|
| 211 | - ): EE_Payment { |
|
| 212 | - return $this->payment_processor->processRefund($payment_method, $payment_to_refund, $refund_info); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * This should be called each time there may have been an update to a |
|
| 218 | - * payment on a transaction (ie, we asked for a payment to process a |
|
| 219 | - * payment for a transaction, or we told a payment method about an IPN, or |
|
| 220 | - * we told a payment method to |
|
| 221 | - * "finalize_payment_for" (a transaction), or we told a payment method to |
|
| 222 | - * process a refund. This should handle firing the correct hooks to |
|
| 223 | - * indicate |
|
| 224 | - * what exactly happened and updating the transaction appropriately). This |
|
| 225 | - * could be integrated directly into EE_Transaction upon save, but we want |
|
| 226 | - * this logic to be separate from 'normal' plain-jane saving and updating |
|
| 227 | - * of transactions and payments, and to be tied to payment processing. |
|
| 228 | - * Note: this method DOES NOT save the payment passed into it. It is the responsibility |
|
| 229 | - * of previous code to decide whether to save (because the payment passed into |
|
| 230 | - * this method might be a temporary, never-to-be-saved payment from an offline gateway, |
|
| 231 | - * in which case we only want that payment object for some temporary usage during this request, |
|
| 232 | - * but we don't want it to be saved). |
|
| 233 | - * |
|
| 234 | - * @param EE_Transaction|int $transaction |
|
| 235 | - * @param EE_Payment $payment |
|
| 236 | - * @param bool $update_txn whether to call |
|
| 237 | - * EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment() |
|
| 238 | - * (you can save 1 DB query if you know you're going to save it later |
|
| 239 | - * instead) |
|
| 240 | - * @param bool $IPN if processing IPNs or other similar payment related activities that occur |
|
| 241 | - * in alternate requests than the main one that is processing the TXN, |
|
| 242 | - * then set this to true to check whether the TXN is locked before updating |
|
| 243 | - * @throws EE_Error |
|
| 244 | - * @throws ReflectionException |
|
| 245 | - */ |
|
| 246 | - public function update_txn_based_on_payment($transaction, $payment, $update_txn = true, $IPN = false) |
|
| 247 | - { |
|
| 248 | - $this->payment_processor->updateTransactionBasedOnPayment( |
|
| 249 | - $transaction, |
|
| 250 | - $payment, |
|
| 251 | - (bool) $update_txn, |
|
| 252 | - (bool) $IPN |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * update registrations REG_paid field after successful payment and link registrations with payment |
|
| 259 | - * |
|
| 260 | - * @param EE_Transaction $transaction |
|
| 261 | - * @param EE_Payment $payment |
|
| 262 | - * @param EE_Registration[] $registrations |
|
| 263 | - * @throws EE_Error |
|
| 264 | - * @throws ReflectionException |
|
| 265 | - */ |
|
| 266 | - public function process_registration_payments( |
|
| 267 | - EE_Transaction $transaction, |
|
| 268 | - EE_Payment $payment, |
|
| 269 | - array $registrations = [] |
|
| 270 | - ) { |
|
| 271 | - $this->registration_payments->processRegistrationPayments($transaction, $payment, $registrations); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * update registration REG_paid field after successful payment and link registration with payment |
|
| 277 | - * |
|
| 278 | - * @param EE_Registration $registration |
|
| 279 | - * @param EE_Payment $payment |
|
| 280 | - * @param float|int|string $available_payment_amount |
|
| 281 | - * @return float |
|
| 282 | - * @throws EE_Error |
|
| 283 | - * @throws ReflectionException |
|
| 284 | - */ |
|
| 285 | - public function process_registration_payment( |
|
| 286 | - EE_Registration $registration, |
|
| 287 | - EE_Payment $payment, |
|
| 288 | - $available_payment_amount = 0.00 |
|
| 289 | - ) { |
|
| 290 | - return $this->registration_payments->processRegistrationPayment( |
|
| 291 | - $registration, |
|
| 292 | - $payment, |
|
| 293 | - (float) $available_payment_amount |
|
| 294 | - ); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * update registration REG_paid field after refund and link registration with payment |
|
| 300 | - * |
|
| 301 | - * @param EE_Registration $registration |
|
| 302 | - * @param EE_Payment $payment |
|
| 303 | - * @param float $available_refund_amount - IMPORTANT !!! SEND AVAILABLE REFUND AMOUNT AS A POSITIVE NUMBER |
|
| 304 | - * @return float |
|
| 305 | - * @throws EE_Error |
|
| 306 | - * @throws ReflectionException |
|
| 307 | - */ |
|
| 308 | - public function process_registration_refund( |
|
| 309 | - EE_Registration $registration, |
|
| 310 | - EE_Payment $payment, |
|
| 311 | - float $available_refund_amount = 0.00 |
|
| 312 | - ): float { |
|
| 313 | - return $this->registration_payments->processRegistrationRefund( |
|
| 314 | - $registration, |
|
| 315 | - $payment, |
|
| 316 | - $available_refund_amount |
|
| 317 | - ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Force posts to PayPal to use TLS v1.2. See: |
|
| 323 | - * https://core.trac.wordpress.org/ticket/36320 |
|
| 324 | - * https://core.trac.wordpress.org/ticket/34924#comment:15 |
|
| 325 | - * https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US |
|
| 326 | - * This will affect PayPal standard, pro, express, and Payflow. |
|
| 327 | - * |
|
| 328 | - * @param $handle |
|
| 329 | - * @param $r |
|
| 330 | - * @param $url |
|
| 331 | - */ |
|
| 332 | - public static function _curl_requests_to_paypal_use_tls($handle, $r, $url) |
|
| 333 | - { |
|
| 334 | - if (strpos($url, 'https://') !== false && strpos($url, '.paypal.com') !== false) { |
|
| 335 | - // Use the value of the constant CURL_SSLVERSION_TLSv1 = 1 |
|
| 336 | - // instead of the constant because it might not be defined |
|
| 337 | - curl_setopt($handle, CURLOPT_SSLVERSION, 6); |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Process payments and transaction after payment process completed. |
|
| 344 | - * ultimately this will send the TXN and payment details off so that notifications can be sent out. |
|
| 345 | - * if this request happens to be processing an IPN, |
|
| 346 | - * then we will also set the Payment Options Reg Step to completed, |
|
| 347 | - * and attempt to completely finalize the TXN if all the other Reg Steps are completed as well. |
|
| 348 | - * |
|
| 349 | - * @param EE_Transaction $transaction |
|
| 350 | - * @param EE_Payment $payment |
|
| 351 | - * @param bool $IPN |
|
| 352 | - * @throws EE_Error |
|
| 353 | - * @throws ReflectionException |
|
| 354 | - * @depecated 5.0.22.p |
|
| 355 | - */ |
|
| 356 | - protected function _post_payment_processing(EE_Transaction $transaction, EE_Payment $payment, $IPN = false) |
|
| 357 | - { |
|
| 358 | - $this->post_payment_processor->updateTransactionAndPayment($transaction, $payment, (bool) $IPN); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * update registration REG_paid field after successful payment and link registration with payment |
|
| 364 | - * |
|
| 365 | - * @param EE_Registration $registration |
|
| 366 | - * @param EE_Payment $payment |
|
| 367 | - * @param float $payment_amount |
|
| 368 | - * @return void |
|
| 369 | - * @throws EE_Error |
|
| 370 | - * @throws ReflectionException |
|
| 371 | - * @depecated 5.0.22.p |
|
| 372 | - */ |
|
| 373 | - protected function _apply_registration_payment( |
|
| 374 | - EE_Registration $registration, |
|
| 375 | - EE_Payment $payment, |
|
| 376 | - $payment_amount = 0.00 |
|
| 377 | - ) { |
|
| 378 | - // find any existing reg payment records for this registration and payment |
|
| 379 | - $existing_reg_payment = EEM_Registration_Payment::instance()->get_one( |
|
| 380 | - [['REG_ID' => $registration->ID(), 'PAY_ID' => $payment->ID()]] |
|
| 381 | - ); |
|
| 382 | - // if existing registration payment exists |
|
| 383 | - if ($existing_reg_payment instanceof EE_Registration_Payment) { |
|
| 384 | - // then update that record |
|
| 385 | - $existing_reg_payment->set_amount($payment_amount); |
|
| 386 | - $existing_reg_payment->save(); |
|
| 387 | - } else { |
|
| 388 | - // or add new relation between registration and payment and set amount |
|
| 389 | - $registration->_add_relation_to( |
|
| 390 | - $payment, |
|
| 391 | - 'Payment', |
|
| 392 | - ['RPY_amount' => $payment_amount] |
|
| 393 | - ); |
|
| 394 | - // make it stick |
|
| 395 | - $registration->save(); |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Removes any non-printable illegal characters from the input, |
|
| 402 | - * which might cause a raucous when trying to insert into the database |
|
| 403 | - * |
|
| 404 | - * @param array $request_data |
|
| 405 | - * @return array |
|
| 406 | - * @depecated 5.0.22.p |
|
| 407 | - */ |
|
| 408 | - protected function _remove_unusable_characters_from_array(array $request_data) |
|
| 409 | - { |
|
| 410 | - $return_data = []; |
|
| 411 | - foreach ($request_data as $key => $value) { |
|
| 412 | - $return_data[ $this->_remove_unusable_characters($key) ] = $this->_remove_unusable_characters( |
|
| 413 | - $value |
|
| 414 | - ); |
|
| 415 | - } |
|
| 416 | - return $return_data; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Removes any non-printable illegal characters from the input, |
|
| 422 | - * which might cause a raucous when trying to insert into the database |
|
| 423 | - * |
|
| 424 | - * @param string $request_data |
|
| 425 | - * @return string |
|
| 426 | - * @depecated 5.0.22.p |
|
| 427 | - */ |
|
| 428 | - protected function _remove_unusable_characters($request_data) |
|
| 429 | - { |
|
| 430 | - return preg_replace('/[^[:print:]]/', '', $request_data); |
|
| 431 | - } |
|
| 23 | + private static ?EE_Payment_Processor $_instance; |
|
| 24 | + |
|
| 25 | + private IpnHandler $ipn_handler; |
|
| 26 | + |
|
| 27 | + private PaymentProcessor $payment_processor; |
|
| 28 | + |
|
| 29 | + private PostPaymentProcessor $post_payment_processor; |
|
| 30 | + |
|
| 31 | + private RegistrationPayments $registration_payments; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @singleton method used to instantiate class object |
|
| 36 | + * @param PaymentProcessor|null $payment_processor |
|
| 37 | + * @param PostPaymentProcessor|null $post_payment_processor |
|
| 38 | + * @param RegistrationPayments|null $registration_payments |
|
| 39 | + * @param IpnHandler|null $ipn_handler |
|
| 40 | + * @return EE_Payment_Processor instance |
|
| 41 | + */ |
|
| 42 | + public static function instance( |
|
| 43 | + ?PaymentProcessor $payment_processor = null, |
|
| 44 | + ?PostPaymentProcessor $post_payment_processor = null, |
|
| 45 | + ?RegistrationPayments $registration_payments = null, |
|
| 46 | + ?IpnHandler $ipn_handler = null |
|
| 47 | + ): EE_Payment_Processor { |
|
| 48 | + // check if class object is instantiated |
|
| 49 | + if (! EE_Payment_Processor::$_instance instanceof EE_Payment_Processor) { |
|
| 50 | + EE_Payment_Processor::$_instance = new EE_Payment_Processor( |
|
| 51 | + $payment_processor, |
|
| 52 | + $post_payment_processor, |
|
| 53 | + $registration_payments, |
|
| 54 | + $ipn_handler |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + return EE_Payment_Processor::$_instance; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @return EE_Payment_Processor |
|
| 63 | + */ |
|
| 64 | + public static function reset(): EE_Payment_Processor |
|
| 65 | + { |
|
| 66 | + EE_Payment_Processor::$_instance = null; |
|
| 67 | + return EE_Payment_Processor::instance(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @param PaymentProcessor|null $payment_processor |
|
| 73 | + * @param PostPaymentProcessor|null $post_payment_processor |
|
| 74 | + * @param RegistrationPayments|null $registration_payments |
|
| 75 | + * @param IpnHandler|null $ipn_handler |
|
| 76 | + */ |
|
| 77 | + private function __construct( |
|
| 78 | + ?PaymentProcessor $payment_processor, |
|
| 79 | + ?PostPaymentProcessor $post_payment_processor, |
|
| 80 | + ?RegistrationPayments $registration_payments, |
|
| 81 | + ?IpnHandler $ipn_handler |
|
| 82 | + ) { |
|
| 83 | + $this->payment_processor = $payment_processor instanceof PaymentProcessor |
|
| 84 | + ? $payment_processor |
|
| 85 | + : LoaderFactory::getShared(PaymentProcessor::class); |
|
| 86 | + $this->post_payment_processor = $post_payment_processor instanceof PostPaymentProcessor |
|
| 87 | + ? $post_payment_processor |
|
| 88 | + : LoaderFactory::getShared(PostPaymentProcessor::class); |
|
| 89 | + $this->registration_payments = $registration_payments instanceof RegistrationPayments |
|
| 90 | + ? $registration_payments |
|
| 91 | + : LoaderFactory::getShared(RegistrationPayments::class); |
|
| 92 | + $this->ipn_handler = $ipn_handler instanceof IpnHandler |
|
| 93 | + ? $ipn_handler |
|
| 94 | + : LoaderFactory::getShared(IpnHandler::class); |
|
| 95 | + do_action('AHEE__EE_Payment_Processor__construct'); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Using the selected gateway, processes the payment for that transaction, and updates the transaction |
|
| 101 | + * appropriately. Saves the payment that is generated |
|
| 102 | + * |
|
| 103 | + * @param EE_Payment_Method $payment_method |
|
| 104 | + * @param EE_Transaction $transaction |
|
| 105 | + * @param float|null $amount if only part of the transaction is to be paid for, how much. |
|
| 106 | + * Leave null if payment is for the full amount owing |
|
| 107 | + * @param EE_Billing_Info_Form|null $billing_form (or probably null, if it's an offline or offsite payment method). |
|
| 108 | + * Receive_form_submission() should have |
|
| 109 | + * already been called on the billing form |
|
| 110 | + * (ie, its inputs should have their normalized values set). |
|
| 111 | + * @param string|null $return_url string used mostly by offsite gateways to specify |
|
| 112 | + * where to go AFTER the offsite gateway |
|
| 113 | + * @param string $method like 'CART', indicates who the client who called this was |
|
| 114 | + * @param bool $by_admin TRUE if payment is being attempted from the admin |
|
| 115 | + * @param bool $update_txn whether to call |
|
| 116 | + * EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment() |
|
| 117 | + * @param string $cancel_url URL to return to if off-site payments are cancelled |
|
| 118 | + * @return EE_Payment |
|
| 119 | + * @throws EE_Error |
|
| 120 | + * @throws ReflectionException |
|
| 121 | + */ |
|
| 122 | + public function process_payment( |
|
| 123 | + EE_Payment_Method $payment_method, |
|
| 124 | + EE_Transaction $transaction, |
|
| 125 | + ?float $amount = null, |
|
| 126 | + ?EE_Billing_Info_Form $billing_form = null, |
|
| 127 | + ?string $return_url = null, |
|
| 128 | + string $method = 'CART', |
|
| 129 | + bool $by_admin = false, |
|
| 130 | + bool $update_txn = true, |
|
| 131 | + string $cancel_url = '' |
|
| 132 | + ): ?EE_Payment { |
|
| 133 | + return $this->payment_processor->processPayment( |
|
| 134 | + $payment_method, |
|
| 135 | + $transaction, |
|
| 136 | + $billing_form, |
|
| 137 | + (float) $amount, |
|
| 138 | + $by_admin, |
|
| 139 | + $update_txn, |
|
| 140 | + (string) $return_url, |
|
| 141 | + $cancel_url, |
|
| 142 | + $method |
|
| 143 | + ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param EE_Transaction|int $transaction |
|
| 149 | + * @param EE_Payment_Method $payment_method |
|
| 150 | + * @return string |
|
| 151 | + * @throws EE_Error |
|
| 152 | + * @throws ReflectionException |
|
| 153 | + */ |
|
| 154 | + public function get_ipn_url_for_payment_method($transaction, $payment_method): string |
|
| 155 | + { |
|
| 156 | + /** @type EE_Transaction $transaction */ |
|
| 157 | + $transaction = EEM_Transaction::instance()->ensure_is_obj($transaction); |
|
| 158 | + return $this->ipn_handler->getIpnUrlForPaymentMethod($transaction, $payment_method); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Process the IPN. Firstly, we'll hope we put the standard args into the IPN URL so |
|
| 164 | + * we can easily find what registration the IPN is for and what payment method. |
|
| 165 | + * However, if not, we'll give all payment methods a chance to claim it and process it. |
|
| 166 | + * If a payment is found for the IPN info, it is saved. |
|
| 167 | + * |
|
| 168 | + * @param array $_req_data form post data |
|
| 169 | + * @param EE_Transaction|int $transaction optional (or a transactions id) |
|
| 170 | + * @param EE_Payment_Method $payment_method (or a slug or id of one) |
|
| 171 | + * @param boolean $update_txn whether to call |
|
| 172 | + * EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment() |
|
| 173 | + * @param bool $separate_IPN_request whether the IPN uses a separate request (true, like PayPal) |
|
| 174 | + * or is processed manually (false, like Authorize.net) |
|
| 175 | + * @return EE_Payment |
|
| 176 | + * @throws EE_Error |
|
| 177 | + * @throws Exception |
|
| 178 | + * @throws ReflectionException |
|
| 179 | + */ |
|
| 180 | + public function process_ipn( |
|
| 181 | + $_req_data, |
|
| 182 | + $transaction = null, |
|
| 183 | + $payment_method = null, |
|
| 184 | + $update_txn = true, |
|
| 185 | + $separate_IPN_request = true |
|
| 186 | + ) { |
|
| 187 | + return $this->ipn_handler->processIPN( |
|
| 188 | + (array) $_req_data, |
|
| 189 | + $transaction, |
|
| 190 | + $payment_method, |
|
| 191 | + $update_txn, |
|
| 192 | + $separate_IPN_request |
|
| 193 | + ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Processes a direct refund request, saves the payment, and updates the transaction appropriately. |
|
| 199 | + * |
|
| 200 | + * @param EE_Payment_Method|null $payment_method |
|
| 201 | + * @param EE_Payment $payment_to_refund |
|
| 202 | + * @param array $refund_info |
|
| 203 | + * @return EE_Payment |
|
| 204 | + * @throws EE_Error |
|
| 205 | + * @throws ReflectionException |
|
| 206 | + */ |
|
| 207 | + public function process_refund( |
|
| 208 | + ?EE_Payment_Method $payment_method, |
|
| 209 | + EE_Payment $payment_to_refund, |
|
| 210 | + array $refund_info = [] |
|
| 211 | + ): EE_Payment { |
|
| 212 | + return $this->payment_processor->processRefund($payment_method, $payment_to_refund, $refund_info); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * This should be called each time there may have been an update to a |
|
| 218 | + * payment on a transaction (ie, we asked for a payment to process a |
|
| 219 | + * payment for a transaction, or we told a payment method about an IPN, or |
|
| 220 | + * we told a payment method to |
|
| 221 | + * "finalize_payment_for" (a transaction), or we told a payment method to |
|
| 222 | + * process a refund. This should handle firing the correct hooks to |
|
| 223 | + * indicate |
|
| 224 | + * what exactly happened and updating the transaction appropriately). This |
|
| 225 | + * could be integrated directly into EE_Transaction upon save, but we want |
|
| 226 | + * this logic to be separate from 'normal' plain-jane saving and updating |
|
| 227 | + * of transactions and payments, and to be tied to payment processing. |
|
| 228 | + * Note: this method DOES NOT save the payment passed into it. It is the responsibility |
|
| 229 | + * of previous code to decide whether to save (because the payment passed into |
|
| 230 | + * this method might be a temporary, never-to-be-saved payment from an offline gateway, |
|
| 231 | + * in which case we only want that payment object for some temporary usage during this request, |
|
| 232 | + * but we don't want it to be saved). |
|
| 233 | + * |
|
| 234 | + * @param EE_Transaction|int $transaction |
|
| 235 | + * @param EE_Payment $payment |
|
| 236 | + * @param bool $update_txn whether to call |
|
| 237 | + * EE_Transaction_Processor::update_transaction_and_registrations_after_checkout_or_payment() |
|
| 238 | + * (you can save 1 DB query if you know you're going to save it later |
|
| 239 | + * instead) |
|
| 240 | + * @param bool $IPN if processing IPNs or other similar payment related activities that occur |
|
| 241 | + * in alternate requests than the main one that is processing the TXN, |
|
| 242 | + * then set this to true to check whether the TXN is locked before updating |
|
| 243 | + * @throws EE_Error |
|
| 244 | + * @throws ReflectionException |
|
| 245 | + */ |
|
| 246 | + public function update_txn_based_on_payment($transaction, $payment, $update_txn = true, $IPN = false) |
|
| 247 | + { |
|
| 248 | + $this->payment_processor->updateTransactionBasedOnPayment( |
|
| 249 | + $transaction, |
|
| 250 | + $payment, |
|
| 251 | + (bool) $update_txn, |
|
| 252 | + (bool) $IPN |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * update registrations REG_paid field after successful payment and link registrations with payment |
|
| 259 | + * |
|
| 260 | + * @param EE_Transaction $transaction |
|
| 261 | + * @param EE_Payment $payment |
|
| 262 | + * @param EE_Registration[] $registrations |
|
| 263 | + * @throws EE_Error |
|
| 264 | + * @throws ReflectionException |
|
| 265 | + */ |
|
| 266 | + public function process_registration_payments( |
|
| 267 | + EE_Transaction $transaction, |
|
| 268 | + EE_Payment $payment, |
|
| 269 | + array $registrations = [] |
|
| 270 | + ) { |
|
| 271 | + $this->registration_payments->processRegistrationPayments($transaction, $payment, $registrations); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * update registration REG_paid field after successful payment and link registration with payment |
|
| 277 | + * |
|
| 278 | + * @param EE_Registration $registration |
|
| 279 | + * @param EE_Payment $payment |
|
| 280 | + * @param float|int|string $available_payment_amount |
|
| 281 | + * @return float |
|
| 282 | + * @throws EE_Error |
|
| 283 | + * @throws ReflectionException |
|
| 284 | + */ |
|
| 285 | + public function process_registration_payment( |
|
| 286 | + EE_Registration $registration, |
|
| 287 | + EE_Payment $payment, |
|
| 288 | + $available_payment_amount = 0.00 |
|
| 289 | + ) { |
|
| 290 | + return $this->registration_payments->processRegistrationPayment( |
|
| 291 | + $registration, |
|
| 292 | + $payment, |
|
| 293 | + (float) $available_payment_amount |
|
| 294 | + ); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * update registration REG_paid field after refund and link registration with payment |
|
| 300 | + * |
|
| 301 | + * @param EE_Registration $registration |
|
| 302 | + * @param EE_Payment $payment |
|
| 303 | + * @param float $available_refund_amount - IMPORTANT !!! SEND AVAILABLE REFUND AMOUNT AS A POSITIVE NUMBER |
|
| 304 | + * @return float |
|
| 305 | + * @throws EE_Error |
|
| 306 | + * @throws ReflectionException |
|
| 307 | + */ |
|
| 308 | + public function process_registration_refund( |
|
| 309 | + EE_Registration $registration, |
|
| 310 | + EE_Payment $payment, |
|
| 311 | + float $available_refund_amount = 0.00 |
|
| 312 | + ): float { |
|
| 313 | + return $this->registration_payments->processRegistrationRefund( |
|
| 314 | + $registration, |
|
| 315 | + $payment, |
|
| 316 | + $available_refund_amount |
|
| 317 | + ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Force posts to PayPal to use TLS v1.2. See: |
|
| 323 | + * https://core.trac.wordpress.org/ticket/36320 |
|
| 324 | + * https://core.trac.wordpress.org/ticket/34924#comment:15 |
|
| 325 | + * https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US |
|
| 326 | + * This will affect PayPal standard, pro, express, and Payflow. |
|
| 327 | + * |
|
| 328 | + * @param $handle |
|
| 329 | + * @param $r |
|
| 330 | + * @param $url |
|
| 331 | + */ |
|
| 332 | + public static function _curl_requests_to_paypal_use_tls($handle, $r, $url) |
|
| 333 | + { |
|
| 334 | + if (strpos($url, 'https://') !== false && strpos($url, '.paypal.com') !== false) { |
|
| 335 | + // Use the value of the constant CURL_SSLVERSION_TLSv1 = 1 |
|
| 336 | + // instead of the constant because it might not be defined |
|
| 337 | + curl_setopt($handle, CURLOPT_SSLVERSION, 6); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Process payments and transaction after payment process completed. |
|
| 344 | + * ultimately this will send the TXN and payment details off so that notifications can be sent out. |
|
| 345 | + * if this request happens to be processing an IPN, |
|
| 346 | + * then we will also set the Payment Options Reg Step to completed, |
|
| 347 | + * and attempt to completely finalize the TXN if all the other Reg Steps are completed as well. |
|
| 348 | + * |
|
| 349 | + * @param EE_Transaction $transaction |
|
| 350 | + * @param EE_Payment $payment |
|
| 351 | + * @param bool $IPN |
|
| 352 | + * @throws EE_Error |
|
| 353 | + * @throws ReflectionException |
|
| 354 | + * @depecated 5.0.22.p |
|
| 355 | + */ |
|
| 356 | + protected function _post_payment_processing(EE_Transaction $transaction, EE_Payment $payment, $IPN = false) |
|
| 357 | + { |
|
| 358 | + $this->post_payment_processor->updateTransactionAndPayment($transaction, $payment, (bool) $IPN); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * update registration REG_paid field after successful payment and link registration with payment |
|
| 364 | + * |
|
| 365 | + * @param EE_Registration $registration |
|
| 366 | + * @param EE_Payment $payment |
|
| 367 | + * @param float $payment_amount |
|
| 368 | + * @return void |
|
| 369 | + * @throws EE_Error |
|
| 370 | + * @throws ReflectionException |
|
| 371 | + * @depecated 5.0.22.p |
|
| 372 | + */ |
|
| 373 | + protected function _apply_registration_payment( |
|
| 374 | + EE_Registration $registration, |
|
| 375 | + EE_Payment $payment, |
|
| 376 | + $payment_amount = 0.00 |
|
| 377 | + ) { |
|
| 378 | + // find any existing reg payment records for this registration and payment |
|
| 379 | + $existing_reg_payment = EEM_Registration_Payment::instance()->get_one( |
|
| 380 | + [['REG_ID' => $registration->ID(), 'PAY_ID' => $payment->ID()]] |
|
| 381 | + ); |
|
| 382 | + // if existing registration payment exists |
|
| 383 | + if ($existing_reg_payment instanceof EE_Registration_Payment) { |
|
| 384 | + // then update that record |
|
| 385 | + $existing_reg_payment->set_amount($payment_amount); |
|
| 386 | + $existing_reg_payment->save(); |
|
| 387 | + } else { |
|
| 388 | + // or add new relation between registration and payment and set amount |
|
| 389 | + $registration->_add_relation_to( |
|
| 390 | + $payment, |
|
| 391 | + 'Payment', |
|
| 392 | + ['RPY_amount' => $payment_amount] |
|
| 393 | + ); |
|
| 394 | + // make it stick |
|
| 395 | + $registration->save(); |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Removes any non-printable illegal characters from the input, |
|
| 402 | + * which might cause a raucous when trying to insert into the database |
|
| 403 | + * |
|
| 404 | + * @param array $request_data |
|
| 405 | + * @return array |
|
| 406 | + * @depecated 5.0.22.p |
|
| 407 | + */ |
|
| 408 | + protected function _remove_unusable_characters_from_array(array $request_data) |
|
| 409 | + { |
|
| 410 | + $return_data = []; |
|
| 411 | + foreach ($request_data as $key => $value) { |
|
| 412 | + $return_data[ $this->_remove_unusable_characters($key) ] = $this->_remove_unusable_characters( |
|
| 413 | + $value |
|
| 414 | + ); |
|
| 415 | + } |
|
| 416 | + return $return_data; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Removes any non-printable illegal characters from the input, |
|
| 422 | + * which might cause a raucous when trying to insert into the database |
|
| 423 | + * |
|
| 424 | + * @param string $request_data |
|
| 425 | + * @return string |
|
| 426 | + * @depecated 5.0.22.p |
|
| 427 | + */ |
|
| 428 | + protected function _remove_unusable_characters($request_data) |
|
| 429 | + { |
|
| 430 | + return preg_replace('/[^[:print:]]/', '', $request_data); |
|
| 431 | + } |
|
| 432 | 432 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | ?IpnHandler $ipn_handler = null |
| 47 | 47 | ): EE_Payment_Processor { |
| 48 | 48 | // check if class object is instantiated |
| 49 | - if (! EE_Payment_Processor::$_instance instanceof EE_Payment_Processor) { |
|
| 49 | + if ( ! EE_Payment_Processor::$_instance instanceof EE_Payment_Processor) { |
|
| 50 | 50 | EE_Payment_Processor::$_instance = new EE_Payment_Processor( |
| 51 | 51 | $payment_processor, |
| 52 | 52 | $post_payment_processor, |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | { |
| 410 | 410 | $return_data = []; |
| 411 | 411 | foreach ($request_data as $key => $value) { |
| 412 | - $return_data[ $this->_remove_unusable_characters($key) ] = $this->_remove_unusable_characters( |
|
| 412 | + $return_data[$this->_remove_unusable_characters($key)] = $this->_remove_unusable_characters( |
|
| 413 | 413 | $value |
| 414 | 414 | ); |
| 415 | 415 | } |