Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
PayPal: Execute full refund if amount=None
Browse files Browse the repository at this point in the history
  • Loading branch information
radekholy24 committed Apr 24, 2024
1 parent ff48ebd commit 3754fbb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
13 changes: 7 additions & 6 deletions payments/paypal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,16 @@ def release(self, payment):
self.post(payment, url)

def refund(self, payment, amount=None):
if amount is None:
amount = payment.captured_amount
amount_data = self.get_amount_data(payment, amount)
refund_data = {"amount": amount_data}
refund_data = {}
if amount is not None:
refund_data["amount"] = self.get_amount_data(payment, amount)
links = self._get_links(payment)
url = links["refund"]["href"]
self.post(payment, url, data=refund_data)
response = self.post(payment, url, data=refund_data)
payment.change_status(PaymentStatus.REFUNDED)
return amount
if response["amount"]["currency"] != payment.currency:
raise NotImplementedError(f"refund's currency other than {payment.currency} not supported yet: {response['amount']['currency']}")
return Decimal(response["amount"]["total"])


class PaypalCardProvider(PaypalProvider):
Expand Down
32 changes: 27 additions & 5 deletions payments/paypal/test_paypal.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,39 @@ def test_provider_handles_captured_payment(self, mocked_post):
self.assertEqual(self.payment.status, PaymentStatus.CONFIRMED)

@patch("requests.post")
def test_provider_refunds_payment(self, mocked_post):
def test_provider_refunds_payment_fully(self, mocked_post):
data = MagicMock()
data.return_value = {
"token_type": "test_token_type",
"access_token": "test_access_token",
}
data.side_effect = [
{
"token_type": "test_token_type",
"access_token": "test_access_token",
},
{"amount": {"total": "220.00", "currency": "USD"}},
]
post = MagicMock()
post.json = data
post.status_code = 200
mocked_post.return_value = post
self.provider.refund(self.payment)
mocked_post.assert_called_with("http://refund.com", headers={"Content-Type": "application/json", "Authorization": "test_token_type test_access_token"}, data="{}")
self.assertEqual(self.payment.status, PaymentStatus.REFUNDED)

@patch("requests.post")
def test_provider_refunds_payment_partially(self, mocked_post):
data = MagicMock()
data.side_effect = [
{
"token_type": "test_token_type",
"access_token": "test_access_token",
},
{"amount": {"total": "1.00", "currency": "USD"}},
]
post = MagicMock()
post.json = data
post.status_code = 200
mocked_post.return_value = post
self.provider.refund(self.payment, amount=Decimal(1))
mocked_post.assert_called_with("http://refund.com", headers={"Content-Type": "application/json", "Authorization": "test_token_type test_access_token"}, data='{"amount": {"currency": "USD", "total": "1.00"}}')
self.assertEqual(self.payment.status, PaymentStatus.REFUNDED)

@patch("requests.post")
Expand Down

0 comments on commit 3754fbb

Please sign in to comment.