Skip to content

Commit

Permalink
Show payment method on user booking admin, checkout from ticket booki…
Browse files Browse the repository at this point in the history
…ngs page
  • Loading branch information
rebkwok committed Sep 15, 2023
1 parent 5899a4d commit 8dbcaa9
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 142 deletions.
29 changes: 25 additions & 4 deletions booking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,34 @@ def has_unpaid_block(self):
return bool(available_blocks)

@cached_property
def paypal_paid(self):
def payment_method(self):
if not self.paid:
return ""
if self.block:
return "Block"

from payments.models import PaypalBookingTransaction
if PaypalBookingTransaction.objects.filter(
booking=self, transaction_id__isnull=False).exists():
return "PayPal"
from stripe_payments.models import Invoice
invoice = Invoice.objects.filter(bookings=self, paid=True).first()
if invoice:
if invoice.amount > 0:
return "Stripe"
else:
return "Voucher"
return ""

@cached_property
def stripe_paid(self):
from stripe_payments.models import Invoice
if not self.paid or self.block is not None:
return False
return PaypalBookingTransaction.objects.filter(
booking=self, transaction_id__isnull=False).exists()

return Invoice.objects.filter(
bookings=self, paid=True
).exists()

@property
def cost_with_voucher(self):
if self.voucher_code:
Expand Down
6 changes: 0 additions & 6 deletions booking/templatetags/bookingtags.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ def formatted_uk_date(date):
return date.astimezone(uk).strftime("%d %b %Y %H:%M")


@register.filter
def total_ticket_cost(ticket_booking):
num_tickets = ticket_booking.tickets.count()
return ticket_booking.ticketed_event.ticket_cost * num_tickets


@register.filter
def abbr_ref(ref):
return "{}...".format(ref[:5])
Expand Down
153 changes: 143 additions & 10 deletions booking/tests/test_checkout_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_anon_user_no_unpaid_items(self):
for data_dict in [
{"cart_bookings_total": 10},
{"cart_blocks_total": 10},
{"tbref": "1234"},
{"cart_ticket_bookings_total": 10},
]:
resp = self.client.post(self.url, data=data_dict)
assert resp.status_code == 302
Expand All @@ -82,7 +82,7 @@ def test_anon_user_no_unpaid_items(self):
# If no unpaid gift vouchers, ignore any cart total passed and return to
# gift voucher purchase page
# invalid gift voucher id
resp = self.client.post(self.url, data={"cart_gift_voucher": "9999"})
resp = self.client.post(self.url, data={"cart_gift_voucher": "9999", "cart_gift_voucher_total": 10})
assert resp.status_code == 302
assert resp.url == reverse("booking:buy_gift_voucher")

Expand Down Expand Up @@ -222,7 +222,13 @@ def test_creates_invoice_and_applies_to_unpaid_gift_voucher(self, mock_payment_i
assert Invoice.objects.exists() is False

# total is correct
resp = self.client.post(self.url, data={"cart_gift_voucher": self.gift_voucher.id})
resp = self.client.post(
self.url,
data={
"cart_gift_voucher": self.gift_voucher.id,
"cart_gift_voucher_total": 10
}
)
assert resp.status_code == 200
assert resp.context_data["cart_total"] == 10.00
assert resp.context_data["checkout_type"] == "gift_vouchers"
Expand All @@ -234,22 +240,52 @@ def test_creates_invoice_and_applies_to_unpaid_gift_voucher(self, mock_payment_i
assert invoice.amount == 10
assert self.gift_voucher.invoice == invoice

@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_creates_incorrect_gift_voucher_total(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo")
mock_payment_intent.create.return_value = mock_payment_intent_obj

assert Invoice.objects.exists() is False

# total is incorrect
resp = self.client.post(
self.url,
data={
"cart_gift_voucher": self.gift_voucher.id,
"cart_gift_voucher_total": 100
}
)
assert resp.status_code == 302
assert resp.url == reverse("booking:buy_gift_voucher")

@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_unconfigured_gift_voucher(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo", amount=2000)
mock_payment_intent.create.return_value = mock_payment_intent_obj

# no blocks configured, redirects
resp = self.client.post(self.url, data={"cart_gift_voucher": self.block_gift_voucher.id})
resp = self.client.post(
self.url,
data={
"cart_gift_voucher": self.block_gift_voucher.id,
"cart_gift_voucher_total": 20
}
)
assert resp.status_code == 302
assert resp.url == reverse("booking:buy_gift_voucher")

self.block_gift_voucher.block_types.add(self.block_type)
resp = self.client.post(self.url, data={"cart_gift_voucher": self.block_gift_voucher.id})
resp = self.client.post(
self.url,
data={
"cart_gift_voucher": self.block_gift_voucher.id,
"cart_gift_voucher_total": 20
}
)
assert resp.status_code == 200

@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_creates_invoice_and_applies_to_unpaid_ticket_bookings(self, mock_payment_intent):
def test_creates_invoice_and_applies_to_unpaid_ticket_booking(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo")
mock_payment_intent.create.return_value = mock_payment_intent_obj

Expand All @@ -261,7 +297,12 @@ def test_creates_invoice_and_applies_to_unpaid_ticket_bookings(self, mock_paymen
baker.make("booking.Ticket", ticket_booking=ticket_booking, _quantity=2)

# total is correct
resp = self.client.post(self.url, data={"cart_ticket_booking_ref": ticket_booking.booking_reference})
resp = self.client.post(
self.url,
data={
"cart_ticket_booking_ref": ticket_booking.booking_reference,
"cart_ticket_bookings_total": 10
})
assert resp.status_code == 200
assert resp.context_data["cart_total"] == 10.00
assert resp.context_data["checkout_type"] == "ticket_bookings"
Expand All @@ -274,13 +315,96 @@ def test_creates_invoice_and_applies_to_unpaid_ticket_bookings(self, mock_paymen
assert invoice.amount == 10
assert ticket_booking.invoice == invoice

@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_incorrect_ticket_booking_total(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo")
mock_payment_intent.create.return_value = mock_payment_intent_obj

assert Invoice.objects.exists() is False
ticket_booking = baker.make_recipe(
'booking.ticket_booking', ticketed_event__ticket_cost=5,
user=self.user, paid=False
)
baker.make("booking.Ticket", ticket_booking=ticket_booking, _quantity=2)

# total is incorrect
resp = self.client.post(
self.url,
data={
"cart_ticket_bookings_total": 20,
"cart_ticket_booking_ref": ticket_booking.booking_reference,
})
assert resp.status_code == 302
assert resp.url == reverse(
"booking:book_ticketed_event", args=(ticket_booking.ticketed_event.slug,)
)

@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_incorrect_ticket_bookings_total(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo")
mock_payment_intent.create.return_value = mock_payment_intent_obj

assert Invoice.objects.exists() is False
ticket_booking = baker.make_recipe(
'booking.ticket_booking', ticketed_event__ticket_cost=5,
user=self.user, paid=False
)
baker.make("booking.Ticket", ticket_booking=ticket_booking, _quantity=2)

# total is incorrect
resp = self.client.post(
self.url,
data={
"cart_ticket_bookings_total": 20
})
assert resp.status_code == 302
assert resp.url == reverse("booking:ticketed_events")


@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_creates_invoice_and_applies_to_unpaid_ticket_bookings(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo")
mock_payment_intent.create.return_value = mock_payment_intent_obj

assert Invoice.objects.exists() is False
ticket_booking = baker.make_recipe(
'booking.ticket_booking', ticketed_event__ticket_cost=5,
user=self.user, paid=False
)
baker.make("booking.Ticket", ticket_booking=ticket_booking, _quantity=2)
ticket_booking1 = baker.make_recipe(
'booking.ticket_booking', ticketed_event__ticket_cost=10,
user=self.user, paid=False
)
baker.make("booking.Ticket", ticket_booking=ticket_booking1, _quantity=3)

# total is correct
resp = self.client.post(self.url, data={"cart_ticket_bookings_total": 40})
assert resp.status_code == 200
assert resp.context_data["cart_total"] == 40.00
assert resp.context_data["checkout_type"] == "ticket_bookings"
assert resp.context_data["tbref"] == ""
ticket_booking.refresh_from_db()

assert Invoice.objects.exists()
invoice = Invoice.objects.first()
assert invoice.username == self.user.email
assert invoice.amount == 40
assert invoice.ticket_bookings.count() == 2

@patch("booking.views.checkout_views.stripe.PaymentIntent")
def test_no_ticket_bookings(self, mock_payment_intent):
mock_payment_intent_obj = self.get_mock_payment_intent(id="foo", amount=2000)
mock_payment_intent.create.return_value = mock_payment_intent_obj

# no ticket bookings, redirects
resp = self.client.post(self.url, data={"cart_ticket_booking_ref": "none"})
resp = self.client.post(
self.url,
data={
"cart_ticket_booking_ref": "none",
"cart_ticket_bookings_total": 10
}
)
assert resp.status_code == 302
assert resp.url == reverse("booking:ticketed_events")

Expand All @@ -298,7 +422,10 @@ def test_creates_invoice_and_applies_to_unpaid_gift_vouchers_anon_user(
assert Invoice.objects.exists() is False

# total is correct
resp = self.client.post(self.url, data={"cart_gift_voucher": self.gift_voucher.id})
resp = self.client.post(
self.url,
data={"cart_gift_voucher": self.gift_voucher.id, "cart_gift_voucher_total": 10}
)
assert resp.status_code == 200
assert resp.context_data["cart_total"] == 10.00
assert resp.context_data["checkout_type"] == "gift_vouchers"
Expand Down Expand Up @@ -403,7 +530,13 @@ def test_uses_existing_invoice_for_gift_voucher(self, mock_payment_intent):
self.gift_voucher.invoice = invoice
self.gift_voucher.save()
# total is correct
resp = self.client.post(self.url, data={"cart_gift_voucher": self.gift_voucher.id})
resp = self.client.post(
self.url,
data={
"cart_gift_voucher": self.gift_voucher.id,
"cart_gift_voucher_total": 10
}
)
self.gift_voucher.refresh_from_db()
assert Invoice.objects.count() == 1
assert self.gift_voucher.invoice == invoice
Expand Down

0 comments on commit 8dbcaa9

Please sign in to comment.