示例#1
0
    def test_create_ticket_booking_with_duplicate_invoice_number(self):
        user = mommy.make_recipe('booking.user', username="******")
        tbooking = mommy.make_recipe('booking.ticket_booking', user=user)
        tbooking.booking_reference = "ref"
        tbooking.save()
        tbooking1 = mommy.make_recipe('booking.ticket_booking', user=user)
        tbooking1.booking_reference = "ref"
        tbooking1.save()

        tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)

        self.assertEqual(tbooking_txn.ticket_booking, tbooking)
        self.assertEqual(
            tbooking_txn.invoice_id,
            '{}001'.format(
                tbooking.booking_reference
            )
        )

        tbooking1_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking1)

        self.assertEqual(tbooking1_txn.ticket_booking, tbooking1)
        self.assertNotEqual(
            tbooking1_txn.invoice_id,
            '{}001'.format(
                tbooking.booking_reference
            )
        )
        # to avoid duplication, the counter is set to 6 digits, the first 3
        # random between 100 and 999
        self.assertEqual(len(tbooking_txn.invoice_id), 6)
        self.assertEqual(len(tbooking1_txn.invoice_id), 9)
示例#2
0
    def test_create_ticket_booking_with_duplicate_invoice_number(self):
        user = mommy.make_recipe('booking.user', username="******")
        tbooking = mommy.make_recipe('booking.ticket_booking', user=user)
        tbooking.booking_reference = "ref"
        tbooking.save()
        tbooking1 = mommy.make_recipe('booking.ticket_booking', user=user)
        tbooking1.booking_reference = "ref"
        tbooking1.save()

        tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)

        self.assertEqual(tbooking_txn.ticket_booking, tbooking)
        self.assertEqual(tbooking_txn.invoice_id,
                         '{}001'.format(tbooking.booking_reference))

        tbooking1_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking1)

        self.assertEqual(tbooking1_txn.ticket_booking, tbooking1)
        self.assertNotEqual(tbooking1_txn.invoice_id,
                            '{}001'.format(tbooking.booking_reference))
        # to avoid duplication, the counter is set to 6 digits, the first 3
        # random between 100 and 999
        self.assertEqual(len(tbooking_txn.invoice_id), 6)
        self.assertEqual(len(tbooking1_txn.invoice_id), 9)
示例#3
0
    def test_create_existing_ticket_booking_transaction(self):
        user = mommy.make_recipe('booking.user', username="******")
        tbooking = mommy.make_recipe('booking.ticket_booking', user=user)
        tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)
        self.assertEqual(tbooking_txn.ticket_booking, tbooking)
        self.assertEqual(tbooking_txn.invoice_id,
                         '{}001'.format(tbooking.booking_reference))
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 1)

        dp_tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 1)
        self.assertEqual(tbooking_txn, dp_tbooking_txn)
示例#4
0
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TicketBookingListView, self).get_context_data(**kwargs)

        ticketbookinglist = []
        for ticket_booking in self.object_list:
            if not ticket_booking.cancelled and not ticket_booking.paid \
                    and ticket_booking.ticketed_event.payment_open:
                # ONLY DO THIS IF PAYPAL BUTTON NEEDED
                invoice_id = create_ticket_booking_paypal_transaction(
                    self.request.user, ticket_booking).invoice_id
                host = 'http://{}'.format(self.request.META.get('HTTP_HOST'))
                paypal_form = PayPalPaymentsListForm(
                    initial=context_helpers.get_paypal_dict(
                        host,
                        ticket_booking.ticketed_event.ticket_cost,
                        ticket_booking.ticketed_event,
                        invoice_id,
                        '{} {}'.format('ticket_booking', ticket_booking.id),
                        paypal_email=ticket_booking.ticketed_event.
                        paypal_email,
                        quantity=ticket_booking.tickets.count()))
            else:
                paypal_form = None

            ticketbookingform = {
                'ticket_booking': ticket_booking,
                'paypalform': paypal_form
            }
            ticketbookinglist.append(ticketbookingform)
        context['ticketbookinglist'] = ticketbookinglist
        return context
示例#5
0
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(TicketBookingListView, self).get_context_data(**kwargs)

        ticketbookinglist = []
        for ticket_booking in self.object_list:
            if not ticket_booking.cancelled and not ticket_booking.paid \
                    and ticket_booking.ticketed_event.payment_open:
                # ONLY DO THIS IF PAYPAL BUTTON NEEDED
                invoice_id = create_ticket_booking_paypal_transaction(
                    self.request.user, ticket_booking).invoice_id
                host = 'http://{}'.format(self.request.META.get('HTTP_HOST'))
                paypal_form = PayPalPaymentsListForm(
                    initial=context_helpers.get_paypal_dict(
                        host,
                        ticket_booking.ticketed_event.ticket_cost,
                        ticket_booking.ticketed_event,
                        invoice_id,
                        '{} {}'.format('ticket_booking', ticket_booking.id),
                        paypal_email=ticket_booking.ticketed_event.paypal_email,
                        quantity=ticket_booking.tickets.count()
                    )
                )
            else:
                paypal_form = None

            ticketbookingform = {
                'ticket_booking': ticket_booking,
                'paypalform': paypal_form
                }
            ticketbookinglist.append(ticketbookingform)
        context['ticketbookinglist'] = ticketbookinglist
        return context
示例#6
0
    def test_paypal_ticket_admin_display(self):
        user = mommy.make_recipe('booking.user',
                                 first_name='Test',
                                 last_name='User')
        ticketed_event = mommy.make(TicketedEvent, ticket_cost=10)
        ticket_booking = mommy.make(TicketBooking,
                                    user=user,
                                    ticketed_event=ticketed_event)
        mommy.make(Ticket, ticket_booking=ticket_booking, _quantity=2)
        pptrans = helpers.create_ticket_booking_paypal_transaction(
            user, ticket_booking)

        pptbooking_admin = admin.PaypalTicketBookingTransactionAdmin(
            PaypalTicketBookingTransaction, AdminSite())
        query = pptbooking_admin.get_queryset(None)[0]

        self.assertEqual(pptbooking_admin.get_ticket_booking_id(query),
                         ticket_booking.id)
        self.assertEqual(pptbooking_admin.get_user(query), 'Test User')
        self.assertEqual(pptbooking_admin.get_ticketed_event(query),
                         ticket_booking.ticketed_event)
        self.assertEqual(pptbooking_admin.ticket_cost(query), "£10.00")
        self.assertEqual(pptbooking_admin.number_of_tickets(query),
                         ticket_booking.tickets.count())
        self.assertEqual(pptbooking_admin.total_cost(query), "£20.00")
示例#7
0
    def test_paypal_ticket_admin_display(self):
        user = mommy.make_recipe(
            'booking.user', first_name='Test', last_name='User')
        ticketed_event = mommy.make(TicketedEvent, ticket_cost=10)
        ticket_booking = mommy.make(
            TicketBooking, user=user, ticketed_event=ticketed_event
        )
        mommy.make(Ticket, ticket_booking=ticket_booking, _quantity=2)
        pptrans = helpers.create_ticket_booking_paypal_transaction(
            user, ticket_booking
        )

        pptbooking_admin = admin.PaypalTicketBookingTransactionAdmin(
            PaypalTicketBookingTransaction, AdminSite()
        )
        query = pptbooking_admin.get_queryset(None)[0]

        self.assertEqual(
            pptbooking_admin.get_ticket_booking_id(query), ticket_booking.id
        )
        self.assertEqual(
            pptbooking_admin.get_user(query), 'Test User'
        )
        self.assertEqual(
            pptbooking_admin.get_ticketed_event(query),
            ticket_booking.ticketed_event
        )
        self.assertEqual(pptbooking_admin.ticket_cost(query), "£10.00")
        self.assertEqual(
            pptbooking_admin.number_of_tickets(query),
            ticket_booking.tickets.count()
        )
        self.assertEqual(pptbooking_admin.total_cost(query), "£20.00")
示例#8
0
    def test_create_existing_ticket_booking_transation_with_txn_id(self):
        user = mommy.make_recipe('booking.user', username="******")
        tbooking = mommy.make_recipe('booking.ticket_booking', user=user)
        tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)
        tbooking_txn.transaction_id = "test txn id"
        tbooking_txn.save()
        self.assertEqual(tbooking_txn.ticket_booking, tbooking)
        self.assertEqual(tbooking_txn.invoice_id,
                         '{}001'.format(tbooking.booking_reference))
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 1)

        second_tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 2)
        self.assertNotEqual(tbooking_txn, second_tbooking_txn)
        self.assertEqual(second_tbooking_txn.invoice_id,
                         '{}002'.format(tbooking.booking_reference))
示例#9
0
    def test_create_existing_ticket_booking_transaction(self):
        user = mommy.make_recipe('booking.user', username="******")
        tbooking = mommy.make_recipe(
            'booking.ticket_booking', user=user
        )
        tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)
        self.assertEqual(tbooking_txn.ticket_booking, tbooking)
        self.assertEqual(
            tbooking_txn.invoice_id, '{}001'.format(tbooking.booking_reference)
        )
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 1)

        dp_tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking
        )
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 1)
        self.assertEqual(tbooking_txn, dp_tbooking_txn)
示例#10
0
 def test_create_ticket_booking_transaction(self):
     user = mommy.make_recipe('booking.user', username="******")
     tbooking = mommy.make_recipe('booking.ticket_booking', user=user)
     tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
         user, tbooking)
     self.assertEqual(tbooking_txn.ticket_booking, tbooking)
     self.assertEqual(tbooking_txn.invoice_id,
                      '{}001'.format(tbooking.booking_reference))
     # str returns invoice id
     self.assertEqual(str(tbooking_txn), tbooking_txn.invoice_id)
示例#11
0
 def test_create_ticket_booking_transaction(self):
     user = mommy.make_recipe('booking.user', username="******")
     tbooking = mommy.make_recipe(
         'booking.ticket_booking', user=user
     )
     tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
         user, tbooking)
     self.assertEqual(tbooking_txn.ticket_booking, tbooking)
     self.assertEqual(
         tbooking_txn.invoice_id, '{}001'.format(tbooking.booking_reference)
     )
     # str returns invoice id
     self.assertEqual(str(tbooking_txn), tbooking_txn.invoice_id)
示例#12
0
    def test_create_existing_ticket_booking_transation_with_txn_id(self):
        user = mommy.make_recipe('booking.user', username="******")
        tbooking = mommy.make_recipe(
            'booking.ticket_booking', user=user
        )
        tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking)
        tbooking_txn.transaction_id = "test txn id"
        tbooking_txn.save()
        self.assertEqual(tbooking_txn.ticket_booking, tbooking)
        self.assertEqual(
            tbooking_txn.invoice_id, '{}001'.format(tbooking.booking_reference)
        )
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 1)

        second_tbooking_txn = helpers.create_ticket_booking_paypal_transaction(
            user, tbooking
        )
        self.assertEqual(PaypalTicketBookingTransaction.objects.count(), 2)
        self.assertNotEqual(tbooking_txn, second_tbooking_txn)
        self.assertEqual(
            second_tbooking_txn.invoice_id, '{}002'.format(tbooking.booking_reference)
        )
示例#13
0
def get_obj(ipn_obj):
    from payments import helpers
    additional_data = {}
    try:
        custom = ipn_obj.custom.split()
        obj_type = custom[0]
        obj_id = int(custom[1])
        voucher_code = custom[2] if len(custom) == 3 and \
            obj_type != 'test' else None
    except IndexError:  # in case custom not included in paypal response
        raise PayPalTransactionError('Unknown object type for payment')

    if obj_type == 'paypal_test':
        # a test payment for paypal email
        # custom in a test payment is in form
        # 'test 0 <invoice_id> <paypal email being tested> <user's email>'
        obj = None
        paypal_trans = None
        additional_data['test_invoice'] = custom[2]
        additional_data['test_paypal_email'] = custom[3]
        additional_data['user_email'] = custom[4]

    elif obj_type == 'booking':
        try:
            obj = Booking.objects.get(id=obj_id)
        except Booking.DoesNotExist:
            raise PayPalTransactionError(
                'Booking with id {} does not exist'.format(obj_id)
            )

        paypal_trans = PaypalBookingTransaction.objects.filter(booking=obj)
        if not paypal_trans:
            paypal_trans = helpers.create_booking_paypal_transaction(
                user=obj.user, booking=obj
            )
        elif paypal_trans.count() > 1:
            # we may have two ppb transactions created if user changed their
            # username between booking and paying (invoice_id is created and
            # retrieved using username)
            if ipn_obj.invoice:
                paypal_trans = PaypalBookingTransaction.objects.get(
                    booking=obj, invoice_id=ipn_obj.invoice
                )
            else:
                paypal_trans = paypal_trans.latest('id')
        else:  # we got one paypaltrans, as we should have
            paypal_trans = paypal_trans[0]

    elif obj_type == 'block':
        try:
            obj = Block.objects.get(id=obj_id)
        except Block.DoesNotExist:
            raise PayPalTransactionError(
                'Block with id {} does not exist'.format(obj_id)
            )

        paypal_trans = PaypalBlockTransaction.objects.filter(block=obj)
        if not paypal_trans:
            paypal_trans = helpers.create_block_paypal_transaction(
                user=obj.user, block=obj
            )
        elif paypal_trans.count() > 1:
            # we may have two ppb transactions created if user changed their
            # username between booking block and paying (invoice_id is created and
            # retrieved using username)
            if ipn_obj.invoice:
                paypal_trans = PaypalBlockTransaction.objects.get(
                    block=obj, invoice_id=ipn_obj.invoice
                )
            else:
                paypal_trans = paypal_trans.latest('id')
        else:  # we got one paypaltrans, as we should have
            paypal_trans = paypal_trans[0]


    elif obj_type == 'ticket_booking':
        try:
            obj = TicketBooking.objects.get(id=obj_id)
        except TicketBooking.DoesNotExist:
            raise PayPalTransactionError(
                'Ticket Booking with id {} does not exist'.format(obj_id)
            )

        paypal_trans = PaypalTicketBookingTransaction.objects.filter(
            ticket_booking=obj
        )
        if not paypal_trans:
            paypal_trans = helpers.create_ticket_booking_paypal_transaction(
                user=obj.user, ticket_booking=obj
            )
        elif paypal_trans.count() > 1:
            # we may have two ppb transactions created if user changed their
            # username between booking and paying (invoice_id is created and
            # retrieved using username)
            if ipn_obj.invoice:
                paypal_trans = PaypalTicketBookingTransaction.objects.get(
                    ticket_booking=obj, invoice_id=ipn_obj.invoice
                )
            else:
                paypal_trans = paypal_trans.latest('id')
        else:  # we got one paypaltrans, as we should have
            paypal_trans = paypal_trans[0]

    else:
        raise PayPalTransactionError('Unknown object type for payment')

    return {
        'obj_type': obj_type,
        'obj': obj,
        'paypal_trans': paypal_trans,
        'voucher_code': voucher_code,
        'additional_data': additional_data
    }
示例#14
0
    def post(self, request, *args, **kwargs):
        if 'cancel' in request.POST:
            self.ticket_booking.delete()
            messages.info(
                request, 'Ticket booking for {} has been cancelled.'.format(
                    self.ticketed_event))
            return HttpResponseRedirect(reverse('booking:ticketed_events'))

        context = self.get_context_data()
        ticket_purchase_form = context['ticket_purchase_form']
        ticket_formset = context['ticket_formset']

        if ticket_purchase_form.has_changed():
            # tickets on current booking are only included in the tickets_left
            # calculation if purchase has been confirmed
            old_tickets = self.ticket_booking.tickets.all()
            old_ticket_count = old_tickets.count()

            if self.ticket_booking.purchase_confirmed:
                tickets_left_excl_this = self.ticketed_event.tickets_left() \
                                            + old_ticket_count
            else:
                tickets_left_excl_this = self.ticketed_event.tickets_left()

            new_quantity = int(
                request.POST.get('ticket_purchase_form-quantity'))

            if new_quantity > tickets_left_excl_this:
                messages.error(
                    request,
                    'Cannot purchase the number of tickets requested.  '
                    'Only {} tickets left.'.format(
                        self.ticketed_event.tickets_left()))
            else:
                # create the correct number of tickets on this booking
                if old_ticket_count < new_quantity:
                    for i in range(new_quantity - old_ticket_count):
                        Ticket.objects.create(
                            ticket_booking=self.ticket_booking)
                if old_ticket_count > new_quantity:
                    for ticket in old_tickets[new_quantity:]:
                        ticket.delete()

                if old_ticket_count > 0:
                    ActivityLog.objects.create(
                        log="Ticket quantity updated on booking ref {}".format(
                            self.ticket_booking.booking_reference))

            tickets = self.ticket_booking.tickets.all()
            context['tickets'] = tickets

            return TemplateResponse(request, self.template_name, context)

        if 'ticket_formset-submit' in request.POST:
            if ticket_formset.is_valid():
                ticket_formset.save()

                # we only create the paypal form if there is a ticket cost and
                # online payments are open
                if self.ticketed_event.ticket_cost and \
                        self.ticketed_event.payment_open:
                    invoice_id = create_ticket_booking_paypal_transaction(
                        self.request.user, self.ticket_booking).invoice_id
                    host = 'http://{}'.format(
                        self.request.META.get('HTTP_HOST'))
                    paypal_form = PayPalPaymentsUpdateForm(
                        initial=context_helpers.get_paypal_dict(
                            host,
                            self.ticketed_event.ticket_cost,
                            self.ticketed_event,
                            invoice_id,
                            '{} {}'.format('ticket_booking',
                                           self.ticket_booking.id),
                            paypal_email=self.ticketed_event.paypal_email,
                            quantity=self.ticket_booking.tickets.count()))
                    context["paypalform"] = paypal_form
                self.ticket_booking.purchase_confirmed = True
                # reset the ticket_booking booked date to the date user confirms
                context['purchase_confirmed'] = True
                self.ticket_booking.date_booked = timezone.now()
                self.ticket_booking.save()
                ActivityLog.objects.create(
                    log="Ticket Purchase confirmed: event {}, user {}, "
                    "booking ref {}".format(
                        self.ticketed_event.name, request.user.username,
                        self.ticket_booking.booking_reference))

                host = 'http://{}'.format(request.META.get('HTTP_HOST'))
                ctx = {
                    'host': host,
                    'ticketed_event': self.ticketed_event,
                    'ticket_booking': self.ticket_booking,
                    'ticket_count': self.ticket_booking.tickets.count(),
                    'user': request.user,
                }

                try:
                    # send notification email to user
                    send_mail(
                        '{} Ticket booking confirmed for {}: ref {}'.format(
                            settings.ACCOUNT_EMAIL_SUBJECT_PREFIX,
                            self.ticketed_event,
                            self.ticket_booking.booking_reference,
                        ),
                        get_template('booking/email/ticket_booking_made.txt'
                                     ).render(ctx),
                        settings.DEFAULT_FROM_EMAIL, [request.user.email],
                        html_message=get_template(
                            'booking/email/ticket_booking_made.html').render(
                                ctx),
                        fail_silently=False)
                except Exception as e:
                    # send mail to tech support with Exception
                    send_support_email(
                        e, __name__, "ticket booking created - "
                        "send email to user")

                if self.ticketed_event.email_studio_when_purchased:
                    try:
                        send_mail(
                            '{} Ticket booking confirmed for {}: ref {}'.
                            format(
                                settings.ACCOUNT_EMAIL_SUBJECT_PREFIX,
                                self.ticketed_event,
                                self.ticket_booking.booking_reference,
                            ),
                            get_template(
                                'booking/email/to_studio_ticket_booking_made.txt'
                            ).render(ctx),
                            settings.DEFAULT_FROM_EMAIL,
                            [settings.DEFAULT_STUDIO_EMAIL],
                            html_message=get_template(
                                'booking/email/to_studio_ticket_booking_made.html'
                            ).render(ctx),
                            fail_silently=False)
                    except Exception as e:
                        # send mail to tech support with Exception
                        send_support_email(
                            e, __name__, "ticket booking created - "
                            "send email to studio")
            else:
                messages.error(request,
                               "Please correct errors in the form below")

            tickets = self.ticket_booking.tickets.all()
            context['tickets'] = tickets
            ticket_purchase_form = TicketPurchaseForm(
                prefix='ticket_purchase_form',
                ticketed_event=self.ticketed_event,
                ticket_booking=self.ticket_booking,
                initial={'quantity': self.ticket_booking.tickets.count()})
            context["ticket_purchase_form"] = ticket_purchase_form
            return TemplateResponse(request, self.template_name, context)
示例#15
0
    def post(self, request, *args, **kwargs):
        if 'cancel' in request.POST:
            self.ticket_booking.delete()
            messages.info(
                request,
                'Ticket booking for {} has been cancelled.'.format(
                    self.ticketed_event
                )
            )
            return HttpResponseRedirect(reverse('booking:ticketed_events'))

        context = self.get_context_data()
        ticket_purchase_form = context['ticket_purchase_form']
        ticket_formset = context['ticket_formset']

        if ticket_purchase_form.has_changed():
            # tickets on current booking are only included in the tickets_left
            # calculation if purchase has been confirmed
            old_tickets = self.ticket_booking.tickets.all()
            old_ticket_count = old_tickets.count()

            if self.ticket_booking.purchase_confirmed:
                tickets_left_excl_this = self.ticketed_event.tickets_left() \
                                            + old_ticket_count
            else:
                tickets_left_excl_this = self.ticketed_event.tickets_left()

            new_quantity = int(request.POST.get('ticket_purchase_form-quantity'))

            if new_quantity > tickets_left_excl_this:
                messages.error(
                    request, 'Cannot purchase the number of tickets requested.  '
                             'Only {} tickets left.'.format(
                        self.ticketed_event.tickets_left()
                    )
                )
            else:
                # create the correct number of tickets on this booking
                if old_ticket_count < new_quantity:
                    for i in range(new_quantity-old_ticket_count):
                        Ticket.objects.create(ticket_booking=self.ticket_booking)
                if old_ticket_count > new_quantity:
                    for ticket in old_tickets[new_quantity:]:
                        ticket.delete()

                if old_ticket_count > 0:
                    ActivityLog.objects.create(
                        log="Ticket quantity updated on booking ref {}".format(
                            self.ticket_booking.booking_reference
                            )
                    )

            tickets = self.ticket_booking.tickets.all()
            context['tickets'] = tickets

            return TemplateResponse(request, self.template_name, context)

        if 'ticket_formset-submit' in request.POST:
            if ticket_formset.is_valid():
                ticket_formset.save()

                # we only create the paypal form if there is a ticket cost and
                # online payments are open
                if self.ticketed_event.ticket_cost and \
                        self.ticketed_event.payment_open:
                    invoice_id = create_ticket_booking_paypal_transaction(
                        self.request.user, self.ticket_booking
                    ).invoice_id
                    host = 'http://{}'.format(self.request.META.get('HTTP_HOST')
                                                      )
                    paypal_form = PayPalPaymentsUpdateForm(
                        initial=context_helpers.get_paypal_dict(
                            host,
                            self.ticketed_event.ticket_cost,
                            self.ticketed_event,
                            invoice_id,
                            '{} {}'.format('ticket_booking', self.ticket_booking.id),
                            paypal_email=self.ticketed_event.paypal_email,
                            quantity=self.ticket_booking.tickets.count()
                        )
                    )
                    context["paypalform"] = paypal_form
                self.ticket_booking.purchase_confirmed = True
                # reset the ticket_booking booked date to the date user confirms
                context['purchase_confirmed'] = True
                self.ticket_booking.date_booked = timezone.now()
                self.ticket_booking.save()
                ActivityLog.objects.create(
                    log="Ticket Purchase confirmed: event {}, user {}, "
                        "booking ref {}".format(
                            self.ticketed_event.name, request.user.username,
                            self.ticket_booking.booking_reference
                        )
                )

                host = 'http://{}'.format(request.META.get('HTTP_HOST'))
                ctx = {
                      'host': host,
                      'ticketed_event': self.ticketed_event,
                      'ticket_booking': self.ticket_booking,
                      'ticket_count': self.ticket_booking.tickets.count(),
                      'user': request.user,
                }

                try:
                    # send notification email to user
                    send_mail('{} Ticket booking confirmed for {}: ref {}'.format(
                            settings.ACCOUNT_EMAIL_SUBJECT_PREFIX,
                            self.ticketed_event,
                            self.ticket_booking.booking_reference,

                        ),
                        get_template(
                            'booking/email/ticket_booking_made.txt'
                        ).render(ctx),
                        settings.DEFAULT_FROM_EMAIL,
                        [request.user.email],
                        html_message=get_template(
                            'booking/email/ticket_booking_made.html'
                            ).render(ctx),
                        fail_silently=False)
                except Exception as e:
                    # send mail to tech support with Exception
                    send_support_email(
                        e, __name__, "ticket booking created - "
                        "send email to user"
                    )

                if self.ticketed_event.email_studio_when_purchased:
                    try:
                        send_mail('{} Ticket booking confirmed for {}: ref {}'.format(
                                settings.ACCOUNT_EMAIL_SUBJECT_PREFIX,
                                self.ticketed_event,
                                self.ticket_booking.booking_reference,
                            ),
                            get_template(
                                'booking/email/to_studio_ticket_booking_made.txt'
                            ).render(ctx),
                            settings.DEFAULT_FROM_EMAIL,
                            [settings.DEFAULT_STUDIO_EMAIL],
                            html_message=get_template(
                                'booking/email/to_studio_ticket_booking_made.html'
                                ).render(ctx),
                            fail_silently=False)
                    except Exception as e:
                        # send mail to tech support with Exception
                        send_support_email(
                            e, __name__, "ticket booking created - "
                            "send email to studio"
                        )
            else:
                messages.error(
                    request, "Please correct errors in the form below"
                )

            tickets = self.ticket_booking.tickets.all()
            context['tickets'] = tickets
            ticket_purchase_form = TicketPurchaseForm(
                prefix='ticket_purchase_form',
                ticketed_event=self.ticketed_event,
                ticket_booking=self.ticket_booking,
                initial={'quantity': self.ticket_booking.tickets.count()}
            )
            context["ticket_purchase_form"] = ticket_purchase_form
            return TemplateResponse(request, self.template_name, context)
示例#16
0
def get_obj(ipn_obj):
    from payments import helpers
    additional_data = {}
    try:
        custom = ipn_obj.custom.split()
        obj_type = custom[0]
        obj_id = int(custom[1])
        voucher_code = custom[2] if len(custom) == 3 and \
            obj_type != 'test' else None
    except IndexError:  # in case custom not included in paypal response
        raise PayPalTransactionError('Unknown object type for payment')

    if obj_type == 'paypal_test':
        # a test payment for paypal email
        # custom in a test payment is in form
        # 'test 0 <invoice_id> <paypal email being tested> <user's email>'
        obj = None
        paypal_trans = None
        additional_data['test_invoice'] = custom[2]
        additional_data['test_paypal_email'] = custom[3]
        additional_data['user_email'] = custom[4]

    elif obj_type == 'booking':
        try:
            obj = Booking.objects.get(id=obj_id)
        except Booking.DoesNotExist:
            raise PayPalTransactionError(
                'Booking with id {} does not exist'.format(obj_id))

        paypal_trans = PaypalBookingTransaction.objects.filter(booking=obj)
        if not paypal_trans:
            paypal_trans = helpers.create_booking_paypal_transaction(
                user=obj.user, booking=obj)
        elif paypal_trans.count() > 1:
            # we may have two ppb transactions created if user changed their
            # username between booking and paying (invoice_id is created and
            # retrieved using username)
            if ipn_obj.invoice:
                paypal_trans = PaypalBookingTransaction.objects.get(
                    booking=obj, invoice_id=ipn_obj.invoice)
            else:
                paypal_trans = paypal_trans.latest('id')
        else:  # we got one paypaltrans, as we should have
            paypal_trans = paypal_trans[0]

    elif obj_type == 'block':
        try:
            obj = Block.objects.get(id=obj_id)
        except Block.DoesNotExist:
            raise PayPalTransactionError(
                'Block with id {} does not exist'.format(obj_id))

        paypal_trans = PaypalBlockTransaction.objects.filter(block=obj)
        if not paypal_trans:
            paypal_trans = helpers.create_block_paypal_transaction(
                user=obj.user, block=obj)
        elif paypal_trans.count() > 1:
            # we may have two ppb transactions created if user changed their
            # username between booking block and paying (invoice_id is created and
            # retrieved using username)
            if ipn_obj.invoice:
                paypal_trans = PaypalBlockTransaction.objects.get(
                    block=obj, invoice_id=ipn_obj.invoice)
            else:
                paypal_trans = paypal_trans.latest('id')
        else:  # we got one paypaltrans, as we should have
            paypal_trans = paypal_trans[0]

    elif obj_type == 'ticket_booking':
        try:
            obj = TicketBooking.objects.get(id=obj_id)
        except TicketBooking.DoesNotExist:
            raise PayPalTransactionError(
                'Ticket Booking with id {} does not exist'.format(obj_id))

        paypal_trans = PaypalTicketBookingTransaction.objects.filter(
            ticket_booking=obj)
        if not paypal_trans:
            paypal_trans = helpers.create_ticket_booking_paypal_transaction(
                user=obj.user, ticket_booking=obj)
        elif paypal_trans.count() > 1:
            # we may have two ppb transactions created if user changed their
            # username between booking and paying (invoice_id is created and
            # retrieved using username)
            if ipn_obj.invoice:
                paypal_trans = PaypalTicketBookingTransaction.objects.get(
                    ticket_booking=obj, invoice_id=ipn_obj.invoice)
            else:
                paypal_trans = paypal_trans.latest('id')
        else:  # we got one paypaltrans, as we should have
            paypal_trans = paypal_trans[0]

    else:
        raise PayPalTransactionError('Unknown object type for payment')

    return {
        'obj_type': obj_type,
        'obj': obj,
        'paypal_trans': paypal_trans,
        'voucher_code': voucher_code,
        'additional_data': additional_data
    }