# Path: src/oscarapicheckout/signals.py

# 
# Path: src/oscarapicheckout/utils.py
# def _set_order_payment_declined(order, request):
#     # Set the order status
#     order.set_status(ORDER_STATUS_PAYMENT_DECLINED)
# 
#     voucher_applications = order.voucherapplication_set.all()
# 
#     for voucher_application in voucher_applications:
#         voucher = voucher_application.voucher
# 
#         parent = getattr(voucher, "parent", None)
#         if parent:
#             parent.num_orders = F("num_orders") - 1
#             parent.save(update_children=False)
# 
#         voucher.num_orders = F("num_orders") - 1
#         voucher.save()
# 
#     # Delete some related objects
#     order.discounts.all().delete()
#     order.line_prices.all().delete()
#     voucher_applications.delete()
# 
#     # Thaw the basket and put it back into the request.session so that it can be retried
#     order.basket.thaw()
#     operations.store_basket_in_session(order.basket, request.session)
# 
#     # Send a signal
#     order_payment_declined.send(sender=order, order=order, request=request)
# 
# Path: src/oscarapicheckout/serializers.py
# class OrderTokenField(SignedTokenRelatedField):
#     @classmethod
#     def get_order_token(cls, order):
#         return cls().get_token(order)
# 
#     def __init__(self, **kwargs):
#         kwargs["queryset"] = Order.objects.all()
#         kwargs["slug_field"] = "number"
#         super().__init__(**kwargs)
# 
# Path: src/oscarapicheckout/tests/base.py
# class BaseTest(APITestCase):
#     def setUp(self):
#         Country.objects.create(
#             display_order=0,
#             is_shipping_country=True,
#             iso_3166_1_a2="US",
#             iso_3166_1_a3="USA",
#             iso_3166_1_numeric="840",
#             name="United States of America",
#             printable_name="United States",
#         )
# 
#     def login(self, is_staff=False, email="joe@example.com"):
#         user = User.objects.create_user(username="joe", password="schmoe", email=email)
#         user.is_staff = is_staff
#         user.save()
#         self.client.login(username="joe", password="schmoe")
#         return user


from decimal import Decimal as D
from django.contrib.auth.models import User
from oscar.core.loading import get_model, get_class
from oscar.test import factories
from rest_framework import status
from rest_framework.reverse import reverse
from ..signals import pre_calculate_total, order_placed, order_payment_authorized
from ..utils import _set_order_payment_declined
from ..serializers import OrderTokenField
from .base import BaseTest
from unittest import mock
        # Fetch payment states
        states_resp = self.client.get(order_resp.data["payment_url"])
        self.assertEqual(states_resp.status_code, status.HTTP_200_OK)
        self.assertEqual(states_resp.data["order_status"], "Authorized")
        self.assertEqual(
            states_resp.data["payment_method_states"].keys(), set(["cash"])
        )
        self.assertEqual(
            states_resp.data["payment_method_states"]["cash"]["status"], "Consumed"
        )
        self.assertEqual(
            states_resp.data["payment_method_states"]["cash"]["amount"], "5.00"
        )
        self.assertIsNone(
            states_resp.data["payment_method_states"]["cash"]["required_action"]
        )
        self.assertPaymentSources(
            order_resp.data["number"],
            sources=[
                dict(
                    source_name="Cash",
                    reference="",
                    allocated=D("5.00"),
                    debited=D("5.00"),
                ),
            ],
        )

        # Cleanup signal handlers
        pre_calculate_total.disconnect(handle_pre_calculate_total)

# Path: src/oscarapicheckout/signals.py

# 
# Path: src/oscarapicheckout/utils.py
# def _set_order_payment_declined(order, request):
#     # Set the order status
#     order.set_status(ORDER_STATUS_PAYMENT_DECLINED)
# 
#     voucher_applications = order.voucherapplication_set.all()
# 
#     for voucher_application in voucher_applications:
#         voucher = voucher_application.voucher
# 
#         parent = getattr(voucher, "parent", None)
#         if parent:
#             parent.num_orders = F("num_orders") - 1
#             parent.save(update_children=False)
# 
#         voucher.num_orders = F("num_orders") - 1
#         voucher.save()
# 
#     # Delete some related objects
#     order.discounts.all().delete()
#     order.line_prices.all().delete()
#     voucher_applications.delete()
# 
#     # Thaw the basket and put it back into the request.session so that it can be retried
#     order.basket.thaw()
#     operations.store_basket_in_session(order.basket, request.session)
# 
#     # Send a signal
#     order_payment_declined.send(sender=order, order=order, request=request)
# 
# Path: src/oscarapicheckout/serializers.py
# class OrderTokenField(SignedTokenRelatedField):
#     @classmethod
#     def get_order_token(cls, order):
#         return cls().get_token(order)
# 
#     def __init__(self, **kwargs):
#         kwargs["queryset"] = Order.objects.all()
#         kwargs["slug_field"] = "number"
#         super().__init__(**kwargs)
# 
# Path: src/oscarapicheckout/tests/base.py
# class BaseTest(APITestCase):
#     def setUp(self):
#         Country.objects.create(
#             display_order=0,
#             is_shipping_country=True,
#             iso_3166_1_a2="US",
#             iso_3166_1_a3="USA",
#             iso_3166_1_numeric="840",
#             name="United States of America",
#             printable_name="United States",
#         )
# 
#     def login(self, is_staff=False, email="joe@example.com"):
#         user = User.objects.create_user(username="joe", password="schmoe", email=email)
#         user.is_staff = is_staff
#         user.save()
#         self.client.login(username="joe", password="schmoe")
#         return user


# Path: src/oscarapicheckout/tests/test_checkout.py
from decimal import Decimal as D
from django.contrib.auth.models import User
from oscar.core.loading import get_model, get_class
from oscar.test import factories
from rest_framework import status
from rest_framework.reverse import reverse
from ..signals import pre_calculate_total, order_placed, order_payment_authorized
from ..utils import _set_order_payment_declined
from ..serializers import OrderTokenField
from .base import BaseTest
from unittest import mock

        # Fetch payment states
        states_resp = self.client.get(order_resp.data["payment_url"])
        self.assertEqual(states_resp.status_code, status.HTTP_200_OK)
        self.assertEqual(states_resp.data["order_status"], "Authorized")
        self.assertEqual(
            states_resp.data["payment_method_states"].keys(), set(["cash"])
        )
        self.assertEqual(
            states_resp.data["payment_method_states"]["cash"]["status"], "Consumed"
        )
        self.assertEqual(
            states_resp.data["payment_method_states"]["cash"]["amount"], "5.00"
        )
        self.assertIsNone(
            states_resp.data["payment_method_states"]["cash"]["required_action"]
        )
        self.assertPaymentSources(
            order_resp.data["number"],
            sources=[
                dict(
                    source_name="Cash",
                    reference="",
                    allocated=D("5.00"),
                    debited=D("5.00"),
                ),
            ],
        )

        # Cleanup signal handlers
        pre_calculate_total.disconnect(handle_pre_calculate_total)

        order_placed.disconnect(handle_order_placed)