Skip to content

Treat additional Pinterest redeem error codes as terminal#1170

Open
dikshaasinghh wants to merge 2 commits intodevelopfrom
fix/ads-credit-redeem-terminal-errors
Open

Treat additional Pinterest redeem error codes as terminal#1170
dikshaasinghh wants to merge 2 commits intodevelopfrom
fix/ads-credit-redeem-terminal-errors

Conversation

@dikshaasinghh
Copy link
Copy Markdown
Contributor

@dikshaasinghh dikshaasinghh commented Apr 14, 2026

Summary

AdCredits::check_if_coupon_was_redeemed() only recognizes Pinterest error codes 2324 (OFFER_ALREADY_REDEEMED) and 2318 (OFFER_ALREADY_REDEEMED_BY_ANOTHER_ADVERTISER) as terminal. Support logs show Pinterest also returns 2322 for the same "already redeemed" condition. Because the check has no branch for 2322, handle_redeem_credit() keeps firing the redeem API every hour, each call returning an identical 400, forever.

Changes

  • src/PinterestApiException.php: add OFFER_ALREADY_REDEEMED_VARIANT = 2322 with a docblock noting where it was observed, and introduce a TERMINAL_REDEEM_ERRORS list holding all three already-redeemed codes.
  • src/AdCredits.php: replace the two hard-coded === checks in check_if_coupon_was_redeemed() with a single in_array( $error_id, PinterestApiException::TERMINAL_REDEEM_ERRORS, true ). Behavior for 2324 and 2318 is unchanged; 2322 now short-circuits retries the same way.

Bug

PIN4WOO-74: https://linear.app/a8c/issue/PIN4WOO-74

Context: relationship to PIN4WOO-73 / PR #1169

One of the reported sites shows the redeem API being hit every 6 seconds — far more often than the hourly heartbeat can account for. Tracing the code, this appears to be a downstream symptom of PIN4WOO-73:

  • Pinterest_For_Woocommerce::add_redeem_credits_info_to_account_data() (class-pinterest-for-woocommerce.php:1143) calls the redeem API, builds $redeem_information with error_id => 2324, then calls self::record_event(...) on line 1165 — which fatals per PIN4WOO-73.
  • The script dies before line 1174 ($account_data['coupon_redeem_info'] = $redeem_information), so the terminal error_id is never persisted. The next handle_redeem_credit() run still sees an empty coupon_redeem_info and fires the API again.
  • The 5–10 minute / 6-second cadence matches the Heartbeat::schedule_events() re-scheduling loop also described in PIN4WOO-73.

Once #1169 merges, the 2324 case will persist correctly and the hourly check will short-circuit as designed. This PR addresses the independent gap for 2322, which would still retry forever (at hourly cadence) even after #1169 is in.

Testing

  • vendor/bin/phpcs src/AdCredits.php src/PinterestApiException.php — no new violations. The errors that do show on src/AdCredits.php all pre-date this change (verified by running phpcs against develop at the same files).
  • No unit tests exist for check_if_coupon_was_redeemed() today. A meaningful regression test would need to seed account_data with a coupon_redeem_info.error_id = 2322 and assert that check_if_coupon_was_redeemed() returns true without invoking the API. Happy to add one if a reviewer would like it.

Follow-up (not in this PR)

The Linear issue mentions two related bugs:

  • PIN4WOO-28 — same missing back-off pattern on the create-merchant endpoint.
  • PIN4WOO-44 — same pattern on v3_create_commerce_partner_merchant 409 errors.

Both look like the same class of issue (retry forever on responses that are in fact terminal) on different endpoints; each deserves its own fix.

@github-actions github-actions Bot added changelog: fix Took care of something that wasn't working. type: bug The issue is a confirmed bug. labels Apr 14, 2026
@dikshaasinghh dikshaasinghh requested a review from Copilot April 14, 2026 10:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates ad-credit redemption handling to treat an additional Pinterest “already redeemed” error code as terminal, preventing repeated non-actionable retry calls to the redeem endpoint.

Changes:

  • Added Pinterest error code 2322 as an “already redeemed” variant and grouped terminal redeem error codes into a single constant list.
  • Refactored AdCredits::check_if_coupon_was_redeemed() to use the centralized terminal-error list via a strict in_array check.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/PinterestApiException.php Adds 2322 constant and introduces TERMINAL_REDEEM_ERRORS to centralize terminal redeem error codes.
src/AdCredits.php Uses TERMINAL_REDEEM_ERRORS to decide whether to short-circuit retries after terminal redeem errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/PinterestApiException.php Outdated
Comment on lines +65 to +68
* Observed in customer ticket #11013264 (PIN4WOO-74). Pinterest returns
* this code alongside 2324 / 2318 for offer-redemption conflicts; the
* exact distinction is not documented in the public API, but it has the
* same terminal semantics as the other already-redeemed codes.
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docblock references an internal/customer support ticket number (#11013264). If this code is in a public plugin repo, this can be considered internal metadata and is hard to audit for sensitivity over time. Suggest removing the ticket number and referencing only the public engineering issue (PIN4WOO-74) or a generic “observed in support logs” note.

Suggested change
* Observed in customer ticket #11013264 (PIN4WOO-74). Pinterest returns
* this code alongside 2324 / 2318 for offer-redemption conflicts; the
* exact distinction is not documented in the public API, but it has the
* same terminal semantics as the other already-redeemed codes.
* Observed during investigation for PIN4WOO-74. Pinterest returns this
* code alongside 2324 / 2318 for offer-redemption conflicts; the exact
* distinction is not documented in the public API, but it has the same
* terminal semantics as the other already-redeemed codes.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

@dikshaasinghh dikshaasinghh Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggestion in da96805 and also scrubbed two customer domain names from the PR description for the same reason.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dikshaasinghh dikshaasinghh self-assigned this Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: fix Took care of something that wasn't working. type: bug The issue is a confirmed bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants