← All changes
|
stripe/controllers/FrmStrpLiteEventsController.php
+27
-3
6.32.1
→
trunk
View file →
| @@ -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 | } |