Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support full refunds #1

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion payments/authorizenet/test_authorizenet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import Mock
Expand All @@ -15,7 +16,7 @@
PROCESS_DATA = {
"number": "4007000000027",
"expiration_0": "5",
"expiration_1": "2023",
"expiration_1": date.today().year + 1,
"cvv2": "123",
}

Expand Down
3 changes: 2 additions & 1 deletion payments/braintree/test_braintree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import Mock
Expand All @@ -15,7 +16,7 @@
"name": "John Doe",
"number": "371449635398431",
"expiration_0": "5",
"expiration_1": "2023",
"expiration_1": date.today().year + 1,
"cvv2": "1234",
}

Expand Down
3 changes: 2 additions & 1 deletion payments/cybersource/test_cybersource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
from decimal import Decimal
from typing import Dict
from unittest import TestCase
Expand All @@ -23,7 +24,7 @@
"name": "John Doe",
"number": "371449635398431",
"expiration_0": "5",
"expiration_1": "2023",
"expiration_1": date.today().year + 1,
"cvv2": "1234",
"fingerprint": "abcd1234",
}
Expand Down
6 changes: 3 additions & 3 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def refund(self, amount=None):
raise ValueError(
"Refund amount can not be greater then captured amount"
)
provider = provider_factory(self.variant, self)
amount = provider.refund(self, amount)
self.captured_amount -= amount
provider = provider_factory(self.variant, self)
amount = provider.refund(self, amount)
self.captured_amount -= amount
if self.captured_amount == 0 and self.status != PaymentStatus.REFUNDED:
self.change_status(PaymentStatus.REFUNDED)
self.save()
Expand Down
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
35 changes: 29 additions & 6 deletions payments/paypal/test_paypal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import date
from decimal import Decimal
from unittest import TestCase
from unittest.mock import MagicMock
Expand All @@ -24,7 +25,7 @@
"name": "John Doe",
"number": "371449635398431",
"expiration_0": "5",
"expiration_1": "2023",
"expiration_1": date.today().year + 1,
"cvv2": "1234",
}

Expand Down Expand Up @@ -125,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
19 changes: 9 additions & 10 deletions payments/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
from decimal import Decimal
from unittest import TestCase
from unittest.mock import NonCallableMock
Expand Down Expand Up @@ -110,20 +111,18 @@ def test_refund_too_high_amount(self):

@patch("payments.dummy.DummyProvider.refund")
def test_refund_without_amount(self, mocked_refund_method):
refund_amount = None
captured_amount = Decimal("200")
with patch.object(BasePayment, "save") as mocked_save_method:
mocked_save_method.return_value = None
mocked_refund_method.return_value = refund_amount
mocked_refund_method.return_value = captured_amount

captured_amount = Decimal("200")
status = PaymentStatus.CONFIRMED
payment = Payment(
variant="default", status=status, captured_amount=captured_amount
variant="default", status=PaymentStatus.CONFIRMED, captured_amount=captured_amount
)
payment.refund(refund_amount)
self.assertEqual(payment.status, status)
self.assertEqual(payment.captured_amount, captured_amount)
self.assertEqual(mocked_refund_method.call_count, 0)
payment.refund()
self.assertEqual(payment.status, PaymentStatus.REFUNDED)
self.assertEqual(payment.captured_amount, Decimal(0))
self.assertEqual(mocked_refund_method.call_count, 1)

@patch("payments.dummy.DummyProvider.refund")
def test_refund_partial_success(self, mocked_refund_method):
Expand Down Expand Up @@ -167,7 +166,7 @@ def setUp(self):
"name": "John Doe",
"number": "4716124728800975",
"expiration_0": "5",
"expiration_1": "2023",
"expiration_1": date.today().year + 1,
"cvv2": "123",
}

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ addopts =
--color=yes
testpaths = payments
DJANGO_SETTINGS_MODULE = test_settings
pythonpath = .
Loading