|
| 1 | +Referral Service Logic |
| 2 | +====================== |
| 3 | + |
| 4 | +This section explains the backend logic of the `ReferralService` class, which handles various operations related to referral programs. |
| 5 | + |
| 6 | +Getting the Referrer by User ID |
| 7 | +---------------------------------- |
| 8 | + |
| 9 | +The service can retrieve the referrer (promoter) associated with a specific user ID. |
| 10 | + |
| 11 | +- **Method**: `get_referrer_by_user_id` |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + referrer = ReferralService.get_referrer_by_user_id(user_id=1) |
| 16 | +
|
| 17 | +
|
| 18 | +Handling Subscription Purchases |
| 19 | +---------------------------------- |
| 20 | + |
| 21 | +When a referred user purchases a subscription, the `ReferralService` updates the referral status to `Active` and generates a commission for the promoter. |
| 22 | + |
| 23 | +- **Method**: `handle_purchase_subscription` |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + ReferralService.handle_purchase_subscription( |
| 28 | + user=user_instance, |
| 29 | + amount_paid=10000, |
| 30 | + invoice_external_id=12345 |
| 31 | + ) |
| 32 | +
|
| 33 | +.. note:: |
| 34 | + |
| 35 | + Field amount_paid must be in cents |
| 36 | + |
| 37 | +The method updates the user's referral status and calculates the commission based on the amount paid. |
| 38 | + |
| 39 | +Handling Refunds |
| 40 | +-------------------- |
| 41 | + |
| 42 | +If a referred user requests a refund, the service updates the referral status to `Refund` and processes a refund commission. |
| 43 | + |
| 44 | +- **Method**: `handle_user_refund` |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + ReferralService.handle_user_refund( |
| 49 | + user=user_instance, |
| 50 | + amount_refunded=5000, |
| 51 | + amount_paid=10000, |
| 52 | + invoice_external_id=12345 |
| 53 | + ) |
| 54 | +
|
| 55 | +
|
| 56 | +.. note:: |
| 57 | + |
| 58 | + Field amount_paid and amount_refunded must be in cents |
| 59 | + |
| 60 | +The method sets the referral status to `Refund` and adjusts the commission accordingly. |
| 61 | + |
| 62 | +Example refund scenario: |
| 63 | + |
| 64 | +- Original Payment: $100 (commission: $10) |
| 65 | +- Amount Refunded: $50 |
| 66 | +- Adjusted Commission: The promoter’s commission is reduced proportionally to $5, and a new refund commission of -$5 is recorded. |
| 67 | + |
| 68 | +### Example |
| 69 | + |
| 70 | +If a promoter originally earned a $10 commission on a $100 subscription, and the user is refunded 50% ($50), the promoter’s adjusted commission will be: |
| 71 | + |
| 72 | +Example refund record: |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + { |
| 77 | + "amount": -5, # negative value representing the refund |
| 78 | + "status": "refund", |
| 79 | + "invoice_external_id": 12345 |
| 80 | + } |
| 81 | +
|
| 82 | +
|
| 83 | +In this case, the promoter's final commission will be $5, reflecting the amount that corresponds to the portion of the user's payment that was not refunded. |
| 84 | + |
| 85 | +.. note:: |
| 86 | + |
| 87 | + It is possible for a promoter to have a negative balance if multiple refunds are processed and the refunded amounts exceed the promoter’s total earned commissions. This can occur if the promoter has already been paid for referrals, but the referred users later request refunds. |
0 commit comments