Skip to content

Commit e5f3d96

Browse files
committed
PayPal: Perform full refund if amount=None
1 parent cdb7281 commit e5f3d96

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ v3.0.0
1414
- Stripe backends now supports webhooks
1515
- New :ref:`webhook settings <webhooks>`
1616
- Fixed PayPal backends not saving captured_amount when processing data.
17+
- PayPal backends now perform a full refund if ``amount=None``.
1718

1819
v2.0.0
1920
------

payments/paypal/__init__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,19 @@ def release(self, payment):
313313
self.post(payment, url)
314314

315315
def refund(self, payment, amount=None):
316-
if amount is None:
317-
amount = payment.captured_amount
318-
amount_data = self.get_amount_data(payment, amount)
319-
refund_data = {"amount": amount_data}
316+
refund_data = {}
317+
if amount is not None:
318+
refund_data["amount"] = self.get_amount_data(payment, amount)
320319
links = self._get_links(payment)
321320
url = links["refund"]["href"]
322-
self.post(payment, url, data=refund_data)
321+
response = self.post(payment, url, data=refund_data)
323322
payment.change_status(PaymentStatus.REFUNDED)
324-
return amount
323+
if response["amount"]["currency"] != payment.currency:
324+
raise NotImplementedError(
325+
f"refund's currency other than {payment.currency} not supported yet: "
326+
f"{response['amount']['currency']}"
327+
)
328+
return Decimal(response["amount"]["total"])
325329

326330

327331
class PaypalCardProvider(PaypalProvider):

payments/paypal/test_paypal.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,53 @@ def test_provider_handles_captured_payment(self, mocked_post):
234234
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)
235235

236236
@patch("requests.post")
237-
def test_provider_refunds_payment(self, mocked_post):
237+
def test_provider_refunds_payment_fully(self, mocked_post):
238238
data = MagicMock()
239-
data.return_value = {
240-
"token_type": "test_token_type",
241-
"access_token": "test_access_token",
242-
}
239+
data.side_effect = [
240+
{
241+
"token_type": "test_token_type",
242+
"access_token": "test_access_token",
243+
},
244+
{"amount": {"total": "220.00", "currency": "USD"}},
245+
]
243246
post = MagicMock()
244247
post.json = data
245248
post.status_code = 200
246249
mocked_post.return_value = post
247250
self.provider.refund(self.payment)
251+
mocked_post.assert_called_with(
252+
"http://refund.com",
253+
headers={
254+
"Content-Type": "application/json",
255+
"Authorization": "test_token_type test_access_token",
256+
},
257+
data="{}",
258+
)
259+
self.assertEqual(self.payment.status, PaymentStatus.REFUNDED)
260+
261+
@patch("requests.post")
262+
def test_provider_refunds_payment_partially(self, mocked_post):
263+
data = MagicMock()
264+
data.side_effect = [
265+
{
266+
"token_type": "test_token_type",
267+
"access_token": "test_access_token",
268+
},
269+
{"amount": {"total": "1.00", "currency": "USD"}},
270+
]
271+
post = MagicMock()
272+
post.json = data
273+
post.status_code = 200
274+
mocked_post.return_value = post
275+
self.provider.refund(self.payment, amount=Decimal(1))
276+
mocked_post.assert_called_with(
277+
"http://refund.com",
278+
headers={
279+
"Content-Type": "application/json",
280+
"Authorization": "test_token_type test_access_token",
281+
},
282+
data='{"amount": {"currency": "USD", "total": "1.00"}}',
283+
)
248284
self.assertEqual(self.payment.status, PaymentStatus.REFUNDED)
249285

250286
@patch("requests.post")

0 commit comments

Comments
 (0)