← All changes
|
square/controllers/FrmSquareLiteEventsController.php
+24
-20
6.25
→
trunk
View file →
| @@ -9,8 +9,11 @@ | ||
| 9 | 9 | * @var string |
| 10 | 10 | */ |
| 11 | 11 | public static $events_to_skip_option_name = 'frm_square_events_to_skip'; |
| 12 | 12 | |
| 13 | + /** | |
| 14 | + * @var object|null | |
| 15 | + */ | |
| 13 | 16 | private $event; |
| 14 | 17 | |
| 15 | 18 | /** |
| 16 | 19 | * Tell Square Connect API that the request came through by flushing early before processing. |
| @@ -51,8 +54,9 @@ | ||
| 51 | 54 | |
| 52 | 55 | if ( $unprocessed_event_ids ) { |
| 53 | 56 | $this->process_event_ids( $unprocessed_event_ids ); |
| 54 | 57 | } |
| 58 | + | |
| 55 | 59 | wp_send_json_success(); |
| 56 | 60 | } |
| 57 | 61 | |
| 58 | 62 | /** |
| @@ -58,8 +62,9 @@ | ||
| 58 | 62 | /** |
| 59 | 63 | * @since 6.22 |
| 60 | 64 | * |
| 61 | 65 | * @param array<string> $event_ids |
| 66 | + * | |
| 62 | 67 | * @return void |
| 63 | 68 | */ |
| 64 | 69 | private function process_event_ids( $event_ids ) { |
| 65 | 70 | foreach ( $event_ids as $event_id ) { |
| @@ -70,15 +75,16 @@ | ||
| 70 | 75 | set_transient( 'frm_square_last_process_' . $event_id, time(), 60 ); |
| 71 | 76 | |
| 72 | 77 | $this->event = FrmSquareLiteConnectHelper::get_event( $event_id ); |
| 73 | 78 | |
| 74 | - if ( is_object( $this->event ) ) { | |
| 75 | - $this->handle_event(); | |
| 76 | - $this->track_handled_event( $event_id ); | |
| 77 | - FrmSquareLiteConnectHelper::process_event( $event_id ); | |
| 78 | - } else { | |
| 79 | + if ( ! is_object( $this->event ) ) { | |
| 79 | 80 | $this->count_failed_event( $event_id ); |
| 81 | + continue; | |
| 80 | 82 | } |
| 83 | + | |
| 84 | + $this->handle_event(); | |
| 85 | + $this->track_handled_event( $event_id ); | |
| 86 | + FrmSquareLiteConnectHelper::process_event( $event_id ); | |
| 81 | 87 | } |
| 82 | 88 | } |
| 83 | 89 | |
| 84 | 90 | /** |
| @@ -84,8 +90,9 @@ | ||
| 84 | 90 | /** |
| 85 | 91 | * @since 6.22 |
| 86 | 92 | * |
| 87 | 93 | * @param string $event_id |
| 94 | + * | |
| 88 | 95 | * @return bool True if the event should be skipped. |
| 89 | 96 | */ |
| 90 | 97 | private function should_skip_event( $event_id ) { |
| 91 | 98 | if ( $this->last_attempt_to_process_event_is_too_recent( $event_id ) ) { |
| @@ -92,17 +99,15 @@ | ||
| 92 | 99 | return true; |
| 93 | 100 | } |
| 94 | 101 | |
| 95 | 102 | $option = get_option( self::$events_to_skip_option_name ); |
| 96 | - if ( ! is_array( $option ) ) { | |
| 97 | - return false; | |
| 98 | - } | |
| 99 | 103 | |
| 100 | - return in_array( $event_id, $option, true ); | |
| 104 | + return is_array( $option ) && in_array( $event_id, $option, true ); | |
| 101 | 105 | } |
| 102 | 106 | |
| 103 | 107 | /** |
| 104 | 108 | * @param string $event_id |
| 109 | + * | |
| 105 | 110 | * @return bool |
| 106 | 111 | */ |
| 107 | 112 | private function last_attempt_to_process_event_is_too_recent( $event_id ) { |
| 108 | 113 | $last_process_attempt = get_transient( 'frm_square_last_process_' . $event_id ); |
| @@ -112,20 +117,17 @@ | ||
| 112 | 117 | /** |
| 113 | 118 | * @since 6.22 |
| 114 | 119 | * |
| 115 | 120 | * @param string $event_id |
| 121 | + * | |
| 116 | 122 | * @return void |
| 117 | 123 | */ |
| 118 | 124 | private function count_failed_event( $event_id ) { |
| 119 | - $transient_name = 'frm_square_failed_event_' . $event_id; | |
| 120 | - $transient = get_transient( $transient_name ); | |
| 121 | - if ( is_int( $transient ) ) { | |
| 122 | - $failed_count = $transient + 1; | |
| 123 | - } else { | |
| 124 | - $failed_count = 1; | |
| 125 | - } | |
| 125 | + $transient_name = 'frm_square_failed_event_' . $event_id; | |
| 126 | + $transient = get_transient( $transient_name ); | |
| 127 | + $failed_count = is_int( $transient ) ? $transient + 1 : 1; | |
| 128 | + $maximum_retries = 3; | |
| 126 | 129 | |
| 127 | - $maximum_retries = 3; | |
| 128 | 130 | if ( $failed_count >= $maximum_retries ) { |
| 129 | 131 | $this->track_handled_event( $event_id ); |
| 130 | 132 | } else { |
| 131 | 133 | set_transient( $transient_name, $failed_count, 4 * DAY_IN_SECONDS ); |
| @@ -138,8 +140,9 @@ | ||
| 138 | 140 | * |
| 139 | 141 | * @since 6.22 |
| 140 | 142 | * |
| 141 | 143 | * @param string $event_id |
| 144 | + * | |
| 142 | 145 | * @return void |
| 143 | 146 | */ |
| 144 | 147 | private function track_handled_event( $event_id ) { |
| 145 | 148 | $option = get_option( self::$events_to_skip_option_name ); |
| @@ -191,8 +194,9 @@ | ||
| 191 | 194 | 'payment' => $payment, |
| 192 | 195 | ) |
| 193 | 196 | ); |
| 194 | 197 | } |
| 198 | + | |
| 195 | 199 | return; |
| 196 | 200 | } |
| 197 | 201 | break; |
| 198 | 202 | case 'subscription.updated': |
| @@ -220,13 +224,13 @@ | ||
| 220 | 224 | /** |
| 221 | 225 | * Add a payment row for the payments table. |
| 222 | 226 | * |
| 223 | 227 | * @param string $subscription_id The Square ID for the current subscription. |
| 228 | + * | |
| 224 | 229 | * @return void |
| 225 | 230 | */ |
| 226 | 231 | private function add_subscription_payment( $subscription_id ) { |
| 227 | - $payment_id = $this->event->data->id; | |
| 228 | - | |
| 232 | + $payment_id = $this->event->data->id; | |
| 229 | 233 | $frm_payment = new FrmTransLitePayment(); |
| 230 | 234 | $payment = $frm_payment->get_one_by( $payment_id, 'receipt_id' ); |
| 231 | 235 | |
| 232 | 236 | if ( $payment ) { |
| @@ -235,8 +239,9 @@ | ||
| 235 | 239 | } |
| 236 | 240 | |
| 237 | 241 | $frm_sub = new FrmTransLiteSubscription(); |
| 238 | 242 | $sub = $frm_sub->get_one_by( $subscription_id, 'sub_id' ); |
| 243 | + | |
| 239 | 244 | if ( ! $sub ) { |
| 240 | 245 | return; |
| 241 | 246 | } |
| 242 | 247 | |
| @@ -266,9 +271,8 @@ | ||
| 266 | 271 | array( 'next_bill_date' => gmdate( 'Y-m-d', strtotime( $expire_date ) ) ) |
| 267 | 272 | ); |
| 268 | 273 | } |
| 269 | 274 | } |
| 270 | - | |
| 271 | 275 | |
| 272 | 276 | $frm_payment = new FrmTransLitePayment(); |
| 273 | 277 | $frm_payment->create( |
| 274 | 278 | array( |