| @@ -83,9 +83,11 @@ | ||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | FrmTransLiteAppHelper::add_note_to_payment( $payment_values, $note ); |
| 86 | 86 | |
| 87 | - $u = $frm_payment->update( $payment->id, $payment_values ); | |
| 87 | + // Read the status again right before the update, in case another request has already changed it. | |
| 88 | + $payment_status_still_does_not_match = $this->payment_status_still_does_not_match( $payment->id ); | |
| 89 | + $updated = $frm_payment->update( $payment->id, $payment_values ); | |
| 88 | 90 | |
| 89 | 91 | echo json_encode( |
| 90 | 92 | array( |
| 91 | 93 | 'response' => 'Payment ' . $payment->id . ' was updated', |
| @@ -92,9 +94,9 @@ | ||
| 92 | 94 | 'success' => true, |
| 93 | 95 | ) |
| 94 | 96 | ); |
| 95 | 97 | |
| 96 | - if ( ! $is_partial_refund ) { | |
| 98 | + if ( ! $is_partial_refund && $payment_status_still_does_not_match && $updated ) { | |
| 97 | 99 | $run_triggers = true; |
| 98 | 100 | } |
| 99 | 101 | }//end if |
| 100 | 102 | |
| @@ -108,8 +110,26 @@ | ||
| 108 | 110 | } |
| 109 | 111 | } |
| 110 | 112 | |
| 111 | 113 | /** |
| 114 | + * Double check that the payment status has not changed. | |
| 115 | + * This is to avoid running actions twice by mistake, since a Stripe Link | |
| 116 | + * return URL and a webhook event can both process the same payment. | |
| 117 | + * | |
| 118 | + * @since 6.35 | |
| 119 | + * | |
| 120 | + * @param int $payment_id The id of the payment to check. | |
| 121 | + * | |
| 122 | + * @return bool | |
| 123 | + */ | |
| 124 | + private function payment_status_still_does_not_match( $payment_id ) { | |
| 125 | + $frm_payment = new FrmTransLitePayment(); | |
| 126 | + $payment = $frm_payment->get_one( $payment_id ); | |
| 127 | + | |
| 128 | + return $payment && $payment->status !== $this->status; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 112 | 132 | * Skip updating the payment object for the first recurring payment. |
| 113 | 133 | * This is to prevent double notifications because the first recurring payment creates an invoice and that invoice triggers the payment events. |
| 114 | 134 | * |
| 115 | 135 | * @since 6.5, introduced in v2.07 of the Stripe add on. |
| @@ -309,10 +329,12 @@ | ||
| 309 | 329 | return true; |
| 310 | 330 | }; |
| 311 | 331 | |
| 312 | 332 | add_filter( $hook, $filter, 99 ); |
| 313 | - $cancelled = FrmStrpLiteApiHelper::cancel_subscription( $sub->sub_id ); | |
| 314 | 333 | |
| 334 | + // There is no logged in user when a webhook event is processed, so the customer check has to be skipped here. | |
| 335 | + $cancelled = FrmStrpLiteAppHelper::call_stripe_helper_class( 'cancel_subscription_without_customer_check', $sub->sub_id ); | |
| 336 | + | |
| 315 | 337 | if ( $cancelled ) { |
| 316 | 338 | FrmTransLiteSubscriptionsController::change_subscription_status( |
| 317 | 339 | array( |
| 318 | 340 | 'status' => 'future_cancel', |
| @@ -318,8 +340,10 @@ | ||
| 318 | 340 | 'status' => 'future_cancel', |
| 319 | 341 | 'sub' => $sub, |
| 320 | 342 | ) |
| 321 | 343 | ); |
| 344 | + } else { | |
| 345 | + FrmTransLiteLog::log_message( 'Stripe Webhook Message', 'Unable to cancel subscription ' . $sub->sub_id . ' after it reached its payment limit.' ); | |
| 322 | 346 | } |
| 323 | 347 | |
| 324 | 348 | remove_filter( $hook, $filter, 99 ); |
| 325 | 349 | } |
| @@ -426,17 +450,15 @@ | ||
| 426 | 450 | /** |
| 427 | 451 | * @return bool |
| 428 | 452 | */ |
| 429 | 453 | private function is_partial_refund() { |
| 430 | - $partial = false; | |
| 431 | - | |
| 432 | - if ( $this->status === 'refunded' ) { | |
| 433 | - $amount = $this->invoice->amount; | |
| 434 | - $amount_refunded = $this->invoice->amount_refunded; | |
| 435 | - $partial = $amount != $amount_refunded; // phpcs:ignore Universal.Operators.StrictComparisons | |
| 454 | + if ( $this->status !== 'refunded' ) { | |
| 455 | + return false; | |
| 436 | 456 | } |
| 437 | 457 | |
| 438 | - return $partial; | |
| 458 | + $amount = $this->invoice->amount; | |
| 459 | + $amount_refunded = $this->invoice->amount_refunded; | |
| 460 | + return $amount !== $amount_refunded; | |
| 439 | 461 | } |
| 440 | 462 | |
| 441 | 463 | /** |
| 442 | 464 | * @param array $payment_values |
| @@ -479,15 +501,16 @@ | ||
| 479 | 501 | set_transient( 'frm_last_process_' . $event_id, time(), 60 ); |
| 480 | 502 | |
| 481 | 503 | $this->event = FrmStrpLiteConnectHelper::get_event( $event_id ); |
| 482 | 504 | |
| 483 | - if ( is_object( $this->event ) ) { | |
| 484 | - $this->handle_event(); | |
| 485 | - $this->track_handled_event( $event_id ); | |
| 486 | - FrmStrpLiteConnectHelper::process_event( $event_id ); | |
| 487 | - } else { | |
| 505 | + if ( ! is_object( $this->event ) ) { | |
| 488 | 506 | $this->count_failed_event( $event_id ); |
| 507 | + continue; | |
| 489 | 508 | } |
| 509 | + | |
| 510 | + $this->handle_event(); | |
| 511 | + $this->track_handled_event( $event_id ); | |
| 512 | + FrmStrpLiteConnectHelper::process_event( $event_id ); | |
| 490 | 513 | } |
| 491 | 514 | } |
| 492 | 515 | |
| 493 | 516 | /** |