Skip to content

Commit

Permalink
Fix country code assignment on checkout (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkachel authored Jan 7, 2025
1 parent b5c0c91 commit 62279cc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
12 changes: 10 additions & 2 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,11 +919,19 @@ def _get_or_create(self, basket: Basket):
# (this re-uses a PendingOrder if it exists, so it might now be wrong)
order.tax_rate = basket.tax_rate
order.purchaser_ip = basket.user_ip
order.purchaser_taxable_country_code = basket.user_taxable_country_code
order.purchaser_taxable_country_code = (
basket.user_taxable_country_code
if basket.user_taxable_country_code
else ""
)
order.purchaser_taxable_geolocation_type = (
basket.user_taxable_geolocation_type
)
order.purchaser_blockable_country_code = basket.user_blockable_country_code
order.purchaser_blockable_country_code = (
basket.user_blockable_country_code
if basket.user_blockable_country_code
else ""
)
order.purchaser_blockable_geolocation_type = (
basket.user_blockable_geolocation_type
)
Expand Down
12 changes: 9 additions & 3 deletions payments/views/v0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from django_filters import rest_framework as filters
from drf_spectacular.utils import (
OpenApiParameter,
Expand All @@ -20,7 +19,7 @@
from mitol.payment_gateway.api import PaymentGateway
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet
Expand Down Expand Up @@ -272,14 +271,17 @@ def start_checkout(request, system_slug: str):


@method_decorator(csrf_exempt, name="dispatch")
class CheckoutCallbackView(View):
class CheckoutCallbackView(APIView):
"""
Handles the redirect from the payment gateway after the user has completed
checkout. This may not always happen as the redirect back to the app
occasionally fails. If it does, then the payment gateway should trigger
things via the backoffice webhook.
"""

authentication_classes = [] # disables authentication
permission_classes = [AllowAny] # disables permission

def _get_payment_process_redirect_url_from_line_items(self, request):
"""
Returns the payment process redirect URL
Expand Down Expand Up @@ -354,6 +356,7 @@ def post_checkout_redirect(self, order_state, request):
{"type": USER_MSG_TYPE_PAYMENT_ERROR_UNKNOWN},
)

@extend_schema(exclude=True)
def post(self, request):
"""
Handle successfully completed transactions.
Expand All @@ -364,6 +367,9 @@ def post(self, request):
2. Finds and fulfills the order in the system (which should also then
clear out the stored basket)
3. Perform any enrollments, account status changes, etc.
Excluded from the OpenAPI schema because it's not a public API - only
CyberSource should be generating the payload for this request.
"""

with transaction.atomic():
Expand Down
10 changes: 5 additions & 5 deletions payments/views/v0/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
BackofficeCallbackView.as_view(),
name="checkout-callback",
),
path(
"checkout/result/",
CheckoutCallbackView.as_view(),
name="checkout-result-callback",
),
path(
"checkout/<str:system_slug>/",
start_checkout,
Expand All @@ -64,9 +69,4 @@
router.urls,
),
),
path(
"checkout/result/",
CheckoutCallbackView.as_view(),
name="checkout-result-callback",
),
]
6 changes: 6 additions & 0 deletions system_meta/management/commands/generate_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.core.management import BaseCommand
from django.core.management.base import CommandParser
from django.db import transaction
from django.urls import reverse

from system_meta.models import IntegratedSystem, Product

Expand Down Expand Up @@ -111,6 +112,11 @@ def add_test_systems(self) -> None:
description=f"Test System {i} description.",
api_key=uuid.uuid4(),
)
system.payment_process_redirect_url = reverse(
"cart", kwargs={"system_slug": system.slug}
)
system.save()

self.stdout.write(f"Created system {system.name} - {system.slug}")

def add_test_products(self, system_slug: str) -> None:
Expand Down

0 comments on commit 62279cc

Please sign in to comment.