def manual_payment(request, invoice_id): ''' Allows staff to make manual payments or refunds on an invoice. This form requires a login, and the logged in user needs to be staff. Arguments: invoice_id (castable to int): The invoice ID to be paid Returns: render: Renders ``registrasion/manual_payment.html`` with the following data:: { "invoice": models.commerce.Invoice(), "form": form, # A form that saves a ``ManualPayment`` # object. } ''' FORM_PREFIX = "manual_payment" current_invoice = InvoiceController.for_id_or_404(invoice_id) form = forms.ManualPaymentForm( request.POST or None, prefix=FORM_PREFIX, ) if request.POST and form.is_valid(): form.instance.invoice = current_invoice.invoice form.instance.entered_by = request.user form.save() current_invoice.update_status() form = forms.ManualPaymentForm(prefix=FORM_PREFIX) data = { "invoice": current_invoice.invoice, "form": form, } return render(request, "registrasion/manual_payment.html", data)
def manual_payment(request, invoice_id): ''' Allows staff to make manual payments or refunds on an invoice. This form requires a login, and the logged in user needs to be staff. Arguments: invoice_id (castable to int): The invoice ID to be paid Returns: render: Renders ``registrasion/manual_payment.html`` with the following data:: { "invoice": models.commerce.Invoice(), "form": form, # A form that saves a ``ManualPayment`` # object. } ''' FORM_PREFIX = "manual_payment" current_invoice = InvoiceController.for_id_or_404(invoice_id) form = forms.ManualPaymentForm( request.POST or None, prefix=FORM_PREFIX, ) if request.POST and form.is_valid(): form.instance.invoice = current_invoice.invoice form.instance.entered_by = request.user form.save() current_invoice.update_status() form = forms.ManualPaymentForm(prefix=FORM_PREFIX) data = { "invoice": current_invoice.invoice, "form": form, } return render(request, "registrasion/manual_payment.html", data)
def invoice(request, invoice_id, access_code=None): ''' Displays an invoice. This view is not authenticated, but it will only allow access to either: the user the invoice belongs to; staff; or a request made with the correct access code. Arguments: invoice_id (castable to int): The invoice_id for the invoice you want to view. access_code (Optional[str]): The access code for the user who owns this invoice. Returns: render: Renders ``registrasion/invoice.html``, with the following data:: { "invoice": models.commerce.Invoice(), } Raises: Http404: if the current user cannot view this invoice and the correct access_code is not provided. ''' current_invoice = InvoiceController.for_id_or_404(invoice_id) if not current_invoice.can_view( user=request.user, access_code=access_code, ): raise Http404() data = { "invoice": current_invoice.invoice, } return render(request, "registrasion/invoice.html", data)
def invoice(request, invoice_id, access_code=None): ''' Displays an invoice. This view is not authenticated, but it will only allow access to either: the user the invoice belongs to; staff; or a request made with the correct access code. Arguments: invoice_id (castable to int): The invoice_id for the invoice you want to view. access_code (Optional[str]): The access code for the user who owns this invoice. Returns: render: Renders ``registrasion/invoice.html``, with the following data:: { "invoice": models.commerce.Invoice(), } Raises: Http404: if the current user cannot view this invoice and the correct access_code is not provided. ''' current_invoice = InvoiceController.for_id_or_404(invoice_id) if not current_invoice.can_view( user=request.user, access_code=access_code, ): raise Http404() data = { "invoice": current_invoice.invoice, } return render(request, "registrasion/invoice.html", data)
def tuokcehc_finalise(request, invoice_id, access_code): ''' View that takes an actual django form and uses it to finalise a payment. ''' inv = InvoiceController.for_id_or_404(str(invoice_id)) to_invoice = get_to_invoice(inv, access_code) form = forms.TuokcehcForm(request.POST) if request.POST and form.is_valid(): try: inv.validate_allowed_to_pay() # Verify that we're allowed to do this. process_card(request, form, inv) except StripeError as e: body = e.json_body err = body.get('error', {}) message = err.get('message') messages.error(request, "There was an issue processing your card: " + message) return to_invoice
def card(request, invoice_id, access_code=None): ''' View that shows and processes a Stripe CreditCardForm to pay the given invoice. Redirects back to the invoice once the invoice is fully paid. Arguments: invoice_id (castable to str): The invoice id for the invoice to pay. access_code (str): The optional access code for the invoice (for unauthenticated payment) ''' form = forms.CreditCardForm(request.POST or None) inv = InvoiceController.for_id_or_404(str(invoice_id)) if not inv.can_view(user=request.user, access_code=access_code): raise Http404() to_invoice = get_to_invoice(inv, access_code) if inv.invoice.balance_due() <= 0: return to_invoice if request.POST and form.is_valid(): try: inv.validate_allowed_to_pay() # Verify that we're allowed to do this. process_card(request, form, inv) return to_invoice except StripeError as e: form.add_error(None, ValidationError(e)) except ValidationError as ve: form.add_error(None, ve) data = { "invoice": inv.invoice, "form": form, } return render( request, "registrasion/stripe/credit_card_payment.html", data )
def refund(request, invoice_id): ''' Marks an invoice as refunded and requests a credit note for the full amount paid against the invoice. This view requires a login, and the logged in user must be staff. Arguments: invoice_id (castable to int): The ID of the invoice to refund. Returns: redirect: Redirects to ``invoice``. ''' current_invoice = InvoiceController.for_id_or_404(invoice_id) try: current_invoice.refund() messages.success(request, "This invoice has been refunded.") except ValidationError as ve: messages.error(request, ve) return redirect("invoice", invoice_id)
def refund(request, invoice_id): ''' Marks an invoice as refunded and requests a credit note for the full amount paid against the invoice. This view requires a login, and the logged in user must be staff. Arguments: invoice_id (castable to int): The ID of the invoice to refund. Returns: redirect: Redirects to ``invoice``. ''' current_invoice = InvoiceController.for_id_or_404(invoice_id) try: current_invoice.refund() messages.success(request, "This invoice has been refunded.") except ValidationError as ve: messages.error(request, ve) return redirect("invoice", invoice_id)
def tuokcehc_entry_point(request, invoice_id, access_code): ''' View that takes a POST token from tuokcehc.js and uses it to load a form that may charge a card. ''' inv = InvoiceController.for_id_or_404(str(invoice_id)) to_invoice = get_to_invoice(inv, access_code) if not inv.can_view(user=request.user, access_code=access_code): raise Http404() if request.POST and "stripe_token" not in request.POST: return to_invoice form = forms.TuokcehcForm(request.POST) data = { "form": form, "access_code": access_code, "invoice": inv.invoice, } return render( request, "registrasion/stripe/tuokcehc.html", data )