Skip to content

fix(payment): consolidate duplicate refund method and support flexible payloads - #345

Open
tushar-hub wants to merge 1 commit into
razorpay:masterfrom
tushar-hub:fix/consolidate-duplicate-refund-method
Open

tushar-hub wants to merge 1 commit into
razorpay:masterfrom
tushar-hub:fix/consolidate-duplicate-refund-method

Conversation

@tushar-hub

Copy link
Copy Markdown

Summary

  • In razorpay/resources/payment.py, the Payment class previously contained two conflicting definitions of the refund method:
    1. def refund(self, payment_id, amount, data={}, **kwargs)
    2. def refund(self, payment_id, data={}, **kwargs)
  • Because Python evaluates class bodies sequentially, the second definition silently overwrote the first.
  • This PR consolidates both definitions into a single, robust, backward-compatible refund method:
    • Supports positional amounts (client.payment.refund(payment_id, 2000)).
    • Supports options dictionaries (client.payment.refund(payment_id, {"amount": 2000, "speed": "normal"})).
    • Supports positional amount with additional options (client.payment.refund(payment_id, 2000, {"speed": "normal"})).
    • Supports keyword amount (client.payment.refund(payment_id, amount=2000)).
    • Supports full refund with no extra arguments (client.payment.refund(payment_id)).
    • Replaces dangerous mutable default argument data={} with None and non-mutating payload construction.
    • Fixes copy-paste docstring on fetch_refund_id.
    • Fixes Duplicate refund Function Definitions Overwriting Each Other #289.

Motivation & Impact

  • Prevents TypeErrors & Serialization Bugs: Previously, passing a positional integer amount to refund() serialized the raw integer instead of an object with "amount", while passing amount as a keyword argument threw TypeError: refund() got an unexpected keyword argument 'amount'.
  • Eliminates Shared Mutable State: Eliminates the classic Python anti-pattern where in-place modification of default data={} persists across calls.
  • 100% Backward Compatible: All existing invocation patterns across both v1 and v2 SDK usage continue to work as expected without breaking existing consumers.

Testing Done

  • Ran python -m unittest on the full suite: 162 tests passed, 0 failures, 1 skipped.
  • Asserted serialized JSON request body in test_refund_create and test_payment_refund.
  • Added 4 dedicated test cases to tests/test_client_payment.py:
    • test_payment_refund_with_amount_and_options: verifies refund(payment_id, 2000, {'speed': 'normal', 'receipt': '#rec_1'}) sends {"amount": 2000, "speed": "normal", "receipt": "#rec_1"} and does not mutate caller dictionary.
    • test_payment_refund_with_keyword_amount: verifies refund(payment_id, amount=3000) sends {"amount": 3000}.
    • test_payment_refund_with_keyword_amount_and_data: verifies refund(payment_id, amount=3000, data={'speed': 'optimum'}) sends {"amount": 3000, "speed": "optimum"}.
    • test_payment_refund_without_arguments: verifies refund(payment_id) sends {}.
  • Verified zero whitespace/lint issues via git diff --check.

…e payloads

Consolidate the two conflicting Payment.refund definitions into a single backward-compatible method. Supports positional amount (int/float/str), options dictionary, keyword amount, and parameter combinations without mutating caller arguments or failing with TypeErrors. Fixes razorpay#289.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate refund Function Definitions Overwriting Each Other

1 participant