Skip to content

feat: coupon recrediting when voiding invoices #3729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 27, 2025

Conversation

brunomiguelpinto
Copy link
Contributor

@brunomiguelpinto brunomiguelpinto commented May 26, 2025

Context

When an invoice is voided, any applied coupons associated with that invoice should be properly recredited. Previously, when an invoice was voided, the credits from applied coupons were not properly handled, which could lead to customers losing coupon benefits they were entitled to. This was particularly problematic for recurring coupons and coupons with a limited number of uses.

Description

This PR implements a comprehensive solution for handling coupon credits when invoices are voided:

Added active and voided scopes to filter credits based on invoice status
These scopes ensure that only credits from non-voided invoices are counted against the coupon's remaining amount

Added AppliedCoupons::RecreditService:
Implemented a new service that handles recrediting of applied coupons when an invoice is voided
For terminated coupons: reactivates the coupon if it was terminated due to usage, and the original coupon is still active
For recurring coupons: increments the frequency_duration_remaining to restore the usage count
Uses database locking to prevent race conditions during concurrent operations

Copy link
Collaborator

@vincent-pochet vincent-pochet left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@brunomiguelpinto brunomiguelpinto changed the title feat: added coupon recredit service feat: coupon recrediting when voiding invoices May 27, 2025
@brunomiguelpinto brunomiguelpinto self-assigned this May 27, 2025
@brunomiguelpinto brunomiguelpinto added Coupon Invoice Related to Invoices labels May 27, 2025
Copy link
Contributor

@julienbourdeau julienbourdeau left a comment

Choose a reason for hiding this comment

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

Awesome

@brunomiguelpinto brunomiguelpinto merged commit 5ffea8a into main May 27, 2025
14 checks passed
@brunomiguelpinto brunomiguelpinto deleted the feat-recredit-coupon branch May 27, 2025 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Coupon Invoice Related to Invoices
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants