Skip to content

Commit 980c295

Browse files
YounesOMKWhyNotHugo
authored andcommitted
Fix StripeProviderV3.refund() returning cents instead of currency units
1 parent 90775b8 commit 980c295

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

payments/stripe/providers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def create_session(self, payment):
144144
else:
145145
raise PaymentError("This payment has already been processed.")
146146

147-
def refund(self, payment, amount=None) -> int:
147+
def refund(self, payment, amount=None):
148148
if payment.status == PaymentStatus.CONFIRMED:
149149
to_refund = amount or payment.total
150150
try:
@@ -164,7 +164,7 @@ def refund(self, payment, amount=None) -> int:
164164
else:
165165
payment.attrs.refund = json.dumps(refund)
166166
payment.save()
167-
return self.convert_amount(payment.currency, to_refund)
167+
return to_refund
168168

169169
raise PaymentError("Only Confirmed payments can be refunded")
170170

payments/stripe/test_stripev3.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,75 @@ def test_provider_refund_success():
175175
provider.refund(payment)
176176

177177
assert payment.status == PaymentStatus.CONFIRMED
178+
179+
180+
def test_provider_refund_returns_currency_units():
181+
payment = Payment()
182+
payment.status = PaymentStatus.CONFIRMED
183+
payment.total = 30
184+
payment.currency = "USD"
185+
payment.attrs.session["payment_intent"] = "pi_..."
186+
provider = StripeProviderV3(api_key=API_KEY)
187+
return_value = {
188+
"id": "re_...",
189+
"payment_status": "succeeded",
190+
"amount": 3000,
191+
}
192+
193+
with patch("stripe.Refund.create", return_value=return_value) as mock_create:
194+
result = provider.refund(payment)
195+
mock_create.assert_called_once_with(
196+
payment_intent="pi_...",
197+
amount=3000,
198+
reason="requested_by_customer",
199+
)
200+
201+
assert result == 30
202+
203+
204+
def test_provider_refund_partial_returns_currency_units():
205+
payment = Payment()
206+
payment.status = PaymentStatus.CONFIRMED
207+
payment.total = 30
208+
payment.currency = "USD"
209+
payment.attrs.session["payment_intent"] = "pi_..."
210+
provider = StripeProviderV3(api_key=API_KEY)
211+
return_value = {
212+
"id": "re_...",
213+
"payment_status": "succeeded",
214+
"amount": 1500,
215+
}
216+
217+
with patch("stripe.Refund.create", return_value=return_value) as mock_create:
218+
result = provider.refund(payment, amount=15)
219+
mock_create.assert_called_once_with(
220+
payment_intent="pi_...",
221+
amount=1500,
222+
reason="requested_by_customer",
223+
)
224+
225+
assert result == 15
226+
227+
228+
def test_provider_refund_zero_decimal_currency_returns_currency_units():
229+
payment = Payment()
230+
payment.status = PaymentStatus.CONFIRMED
231+
payment.total = 3000
232+
payment.currency = "JPY"
233+
payment.attrs.session["payment_intent"] = "pi_..."
234+
provider = StripeProviderV3(api_key=API_KEY)
235+
return_value = {
236+
"id": "re_...",
237+
"payment_status": "succeeded",
238+
"amount": 3000,
239+
}
240+
241+
with patch("stripe.Refund.create", return_value=return_value) as mock_create:
242+
result = provider.refund(payment, amount=3000)
243+
mock_create.assert_called_once_with(
244+
payment_intent="pi_...",
245+
amount=3000,
246+
reason="requested_by_customer",
247+
)
248+
249+
assert result == 3000

0 commit comments

Comments
 (0)