summaryrefslogtreecommitdiff
path: root/postgresqleu/confreg/invoicehandler.py
blob: aefd479e321e9d72725a0429b14c795e78d6adda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from django.conf import settings

from postgresqleu.mailqueue.util import send_template_mail, send_simple_mail
from models import ConferenceRegistration, BulkPayment, PendingAdditionalOrder
from models import RegistrationWaitlistHistory, PrepaidVoucher
from util import notify_reg_confirmed, expire_additional_options, cancel_registration

from datetime import datetime

class InvoiceProcessor(object):
    # Process invoices once they're getting paid
    #
    # In the case of conference registration, this means that we
    # flag the conference registration as confirmed.
    #
    # Since we lock the registration when the invoice is generated,
    # we don't actually need to verify that nothing has changed.
    #
    # All modifications are already wrapped in a django transaction
    def process_invoice_payment(self, invoice):
        # The processorid field contains our registration id
        try:
            reg = ConferenceRegistration.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find conference registration %s" % invoice.processorid)

        if reg.payconfirmedat:
            raise Exception("Registration already paid")

        reg.payconfirmedat = datetime.now()
        reg.payconfirmedby = "Invoice paid"
        reg.save()
        notify_reg_confirmed(reg)

    # Process an invoice being canceled. This means we need to unlink
    # it from the registration. We don't actually remove the registration,
    # but it will automatically become "unlocked" for further edits.
    def process_invoice_cancellation(self, invoice):
        try:
            reg = ConferenceRegistration.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find conference registration %s" % invoice.processorid)

        if reg.payconfirmedat:
            raise Exception("Registration already paid")

        # Unlink this invoice from the registration. This will automatically
        # "unlock" the registration
        reg.invoice = None
        reg.save()

        # If this registration holds any additional options that are about to expire, release
        # them for others to use at this point. (This will send an additional email to the
        # attendee automatically)
        expire_additional_options(reg)

        # If the registration was on the waitlist, put it back in the
        # queue.
        if hasattr(reg, 'registrationwaitlistentry'):
            wl = reg.registrationwaitlistentry
            RegistrationWaitlistHistory(waitlist=wl,
                                        text="Invoice was cancelled, moving back to waitlist").save()
            wl.offeredon = None
            wl.offerexpires = None
            wl.enteredon = datetime.now()
            wl.save()

        # If the registration was attached to a discount code, remove it so that it is no
        # longer counted against it. Also clear out the field, in case others want to use
        # that discount code.
        if reg.discountcode_set.exists():
            reg.discountcode_set.clear()
            reg.save()
        if reg.vouchercode:
            reg.vouchercode = ''
            reg.save()

    # Process an invoice being refunded. This means we need to unlink
    # it from the registration, and also unconfirm the registration.
    def process_invoice_refund(self, invoice):
        try:
            reg = ConferenceRegistration.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find conference registration %s" % invoice.processorid)

        cancel_registration(reg)

    # Return the user to a page showing what happened as a result
    # of their payment. In our case, we just return the user directly
    # to the registration page.
    def get_return_url(self, invoice):
        # The processorid field contains our registration id
        try:
            reg = ConferenceRegistration.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find conference registration %s" % invoice.processorid)
        return "%s/events/%s/register/" % (settings.SITEBASE, reg.conference.urlname)




class BulkInvoiceProcessor(object):
    # Process invoices once they're getting paid
    #
    # In the case of conference bulk registrations, this means that we
    # flag all the related conference registrations as confirmed.
    #
    # Since we lock the registration when the invoice is generated,
    # we don't actually need to verify that nothing has changed.
    #
    # All modifications are already wrapped in a django transaction
    def process_invoice_payment(self, invoice):
        # The processorid field contains our bulkpayment id
        try:
            bp = BulkPayment.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find bulk payment %s" % invoice.processorid)

        if bp.paidat:
            raise Exception("Bulk payment already paid")

        bp.paidat = datetime.today()

        # Confirm all related ones
        for r in bp.conferenceregistration_set.all():
            r.payconfirmedat = datetime.now()
            r.payconfirmedby = "Bulk paid"
            r.save()
            notify_reg_confirmed(r)

        bp.save()

    # Process an invoice being canceled. This means we need to unlink
    # it from the registration. We don't actually remove the registration,
    # but it will automatically become "unlocked" for further edits.
    def process_invoice_cancellation(self, invoice):
        try:
            bp = BulkPayment.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find bulk payment %s" % invoice.processor)
        if bp.paidat:
            raise Exception("Bulk registration already paid")

        # Unlink this bulk payment from all registrations. This will
        # automatically unlock the registrations. Also notify the
        # attendees that this happened.
        for r in bp.conferenceregistration_set.all():
            r.bulkpayment = None
            r.save()

            if r.attendee:
                # Only notify if this attendee actually knows about the
                # registration.
                send_template_mail(bp.conference.contactaddr,
                                   r.email,
                                   "Your registration for {0} bulk payment canceled".format(bp.conference.conferencename),
                                   'confreg/mail/bulkpay_canceled.txt',
                                   {
                                       'conference': bp.conference,
                                       'reg': r,
                                       'bulk': bp,
                                   },
                                   sendername=bp.conference.conferencename,
                                   receivername=r.fullname,
                )

            # If this registration holds any additional options that are about to expire, release
            # them for others to use at this point. (This will send an additional email to the
            # attendee automatically)
            expire_additional_options(r)

            # If the registration was attached to a discount code, remove it so that it is no
            # longer counted against it. Also clear out the field, in case others want to use
            # that discount code.
            if r.discountcode_set.exists():
                r.discountcode_set.clear()
                r.save()
            if r.vouchercode:
                # Also mark the voucher code as not used anymore
                vc = PrepaidVoucher.objects.get(vouchervalue=r.vouchercode)
                vc.usedate = None
                vc.user = None
                vc.save()

                r.vouchercode = ''
                r.save()

        # Now actually *remove* the bulk payment record completely,
        # since it no longer contains anything interesting.
        bp.delete()

    # Process an invoice being refunded. Since this is often just a part
    # of a bulk payment, we cannot just cancel all registrations. Instead
    # we'll just generate a notification...
    def process_invoice_refund(self, invoice):
        try:
            bp = BulkPayment.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find bulk payment %s" % invoice.processor)
        if not bp.paidat:
            raise Exception("Bulk registration not paid - things are out of sync")

        send_simple_mail(bp.conference.contactaddr,
                         bp.conference.contactaddr,
                         'Bulk invoice refunded',
                         u"The bulk payment with id {0} has been refunded.\nNote that the registrations on this bulk invoice has\nNOT been canceled!!!\n\nThis needs to be processed manually since it may be a partial refund.\n\nThe following registrations are attached:\n\n{1}\n".format(
                             bp.id,
                             u"\n".join([u'* {0}'.format(r.fullname) for r in bp.conferenceregistration_set.all()]),
                         ),
                         sendername=bp.conference.conferencename,
                         )

    # Return the user to a page showing what happened as a result
    # of their payment. In our case, we just return the user directly
    # to the bulk payment page.
    def get_return_url(self, invoice):
        try:
            bp = BulkPayment.objects.get(pk=invoice.processorid)
        except ConferenceRegistration.DoesNotExist:
            raise Exception("Could not find bulk payment %s" % invoice.processor)
        return "%s/events/%s/bulkpay/%s/" % (settings.SITEBASE, bp.conference.urlname, invoice.processorid)



class AddonInvoiceProcessor(object):
    can_refund = False
    # Process invoices for additional options added to an existing
    # registration.
    #
    # Since we lock the registration when the invoice is generated,
    # we don't actually need to verify that nothing has changed.
    #
    # All modifications are already wrapped in a django transaction
    def process_invoice_payment(self, invoice):
        try:
            order = PendingAdditionalOrder.objects.get(pk=invoice.processorid)
        except PendingAdditionalOrder.DoesNotExist:
            raise Exception("Could not find additional options order %s!" % invoice.processorid)

        if order.payconfirmedat:
            raise Exception("Additional options already paid")

        order.payconfirmedat = datetime.now()
        if order.newregtype:
            order.reg.regtype = order.newregtype

        for o in order.options.all():
            order.reg.additionaloptions.add(o)

        order.reg.save()
        order.save()

    def process_invoice_cancellation(self, invoice):
        try:
            order = PendingAdditionalOrder.objects.get(pk=invoice.processorid)
        except PendingAdditionalOrder.DoesNotExist:
            raise Exception("Could not find additional options order %s!" % invoice.processorid)

        # We just remove the entry completely, as there is no "unlocking"
        # here.
        order.delete()

    # Return the user to their dashboard
    def get_return_url(self, invoice):
        try:
            order = PendingAdditionalOrder.objects.get(pk=invoice.processorid)
        except PendingAdditionalOrder.DoesNotExist:
            raise Exception("Could not find additional options order %s!" % invoice.processorid)

        return "%s/events/%s/register/" % (settings.SITEBASE, order.reg.conference.urlname)