def processSquarePayment(request):
logger.info('<STR_LIT>')<EOL>nonce_id = request.POST.get('<STR_LIT>')<EOL>invoice_id = request.POST.get('<STR_LIT>')<EOL>tr_id = request.POST.get('<STR_LIT>')<EOL>amount = request.POST.get('<STR_LIT>')<EOL>submissionUserId = request.POST.get('<STR_LIT>')<EOL>transactionType = request.POST.get('<STR_LIT>')<EOL>taxable = request.POST.get('<STR_LIT>', False)<EOL>sourceUrl = request.POST.get('<STR_LIT>', reverse('<STR_LIT>'))<EOL>addSessionInfo = request.POST.get('<STR_LIT>',False)<EOL>successUrl = request.POST.get('<STR_LIT>',reverse('<STR_LIT>'))<EOL>customerEmail = request.POST.get('<STR_LIT>')<EOL>if amount:<EOL><INDENT>try:<EOL><INDENT>amount = float(amount)<EOL><DEDENT>except ValueError:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>messages.error(<EOL>request,<EOL>format_html(<EOL>'<STR_LIT>',<EOL>str(_('<STR_LIT>')),<EOL>str(_('<STR_LIT>'))<EOL>)<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT><DEDENT>submissionUser = None<EOL>if submissionUserId:<EOL><INDENT>try:<EOL><INDENT>submissionUser = User.objects.get(id=int(submissionUserId))<EOL><DEDENT>except (ValueError, ObjectDoesNotExist):<EOL><INDENT>logger.warning('<STR_LIT>')<EOL><DEDENT><DEDENT>try:<EOL><INDENT>if invoice_id:<EOL><INDENT>this_invoice = Invoice.objects.get(id=invoice_id)<EOL>this_description = _('<STR_LIT>' % this_invoice.id)<EOL>if not amount:<EOL><INDENT>amount = this_invoice.outstandingBalance<EOL><DEDENT><DEDENT>elif tr_id:<EOL><INDENT>tr = TemporaryRegistration.objects.get(id=int(tr_id))<EOL>tr.expirationDate = timezone.now() + timedelta(minutes=getConstant('<STR_LIT>'))<EOL>tr.save()<EOL>this_invoice = Invoice.get_or_create_from_registration(tr, submissionUser=submissionUser)<EOL>this_description = _('<STR_LIT>' % tr_id)<EOL>if not amount:<EOL><INDENT>amount = this_invoice.outstandingBalance<EOL><DEDENT><DEDENT>elif not transactionType or not amount:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>messages.error(<EOL>request,<EOL>format_html(<EOL>'<STR_LIT>',<EOL>str(_('<STR_LIT>')),<EOL>str(_('<STR_LIT>'))<EOL>)<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>else:<EOL><INDENT>if transactionType == '<STR_LIT>':<EOL><INDENT>this_description = _('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>this_description = transactionType<EOL><DEDENT>this_invoice = Invoice.create_from_item(<EOL>float(amount),<EOL>this_description,<EOL>submissionUser=submissionUser,<EOL>calculate_taxes=(taxable is not False),<EOL>transactionType=transactionType,<EOL>)<EOL><DEDENT><DEDENT>except (ValueError, ObjectDoesNotExist) as e:<EOL><INDENT>logger.error('<STR_LIT>' % (invoice_id, tr_id, amount))<EOL>messages.error(<EOL>request,<EOL>format_html(<EOL>'<STR_LIT>',<EOL>str(_('<STR_LIT>')),<EOL>str(_('<STR_LIT>' % (invoice_id, tr_id, amount)))<EOL>)<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>this_currency = getConstant('<STR_LIT>')<EOL>this_total = min(this_invoice.outstandingBalance, amount)<EOL>api_instance = TransactionsApi()<EOL>api_instance.api_client.configuration.access_token = getattr(settings,'<STR_LIT>','<STR_LIT>')<EOL>idempotency_key = str(uuid.uuid1())<EOL>location_id = getattr(settings,'<STR_LIT>','<STR_LIT>')<EOL>amount = {'<STR_LIT>': int(<NUM_LIT:100> * this_total), '<STR_LIT>': this_currency}<EOL>body = {'<STR_LIT>': idempotency_key, '<STR_LIT>': nonce_id, '<STR_LIT>': amount}<EOL>errors_list = []<EOL>try:<EOL><INDENT>api_response = api_instance.charge(location_id, body)<EOL>if api_response.errors:<EOL><INDENT>logger.error('<STR_LIT>' % api_response.errors)<EOL>errors_list = api_response.errors<EOL><DEDENT><DEDENT>except ApiException as e:<EOL><INDENT>logger.error('<STR_LIT>' % e)<EOL>errors_list = json.loads(e.body).get('<STR_LIT>',[])<EOL><DEDENT>if errors_list:<EOL><INDENT>this_invoice.status = Invoice.PaymentStatus.error<EOL>this_invoice.save()<EOL>errors_string = '<STR_LIT>'<EOL>for err in errors_list:<EOL><INDENT>errors_string += '<STR_LIT>' % (<EOL>err.get('<STR_LIT:code>',str(_('<STR_LIT>'))), err.get('<STR_LIT>',str(_('<STR_LIT>')))<EOL>)<EOL><DEDENT>messages.error(<EOL>request,<EOL>format_html(<EOL>'<STR_LIT>',<EOL>str(_('<STR_LIT>')),<EOL>mark_safe(errors_list),<EOL>)<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>else:<EOL><INDENT>logger.info('<STR_LIT>')<EOL><DEDENT>transaction = api_response.transaction<EOL>paymentRecord = SquarePaymentRecord.objects.create(<EOL>invoice=this_invoice,<EOL>transactionId=transaction.id,<EOL>locationId=transaction.location_id,<EOL>)<EOL>this_invoice.processPayment(<EOL>amount=this_total,<EOL>fees=<NUM_LIT:0>,<EOL>paidOnline=True,<EOL>methodName='<STR_LIT>',<EOL>methodTxn=transaction.id,<EOL>notify=customerEmail,<EOL>)<EOL>updateSquareFees.schedule(args=(paymentRecord,), delay=<NUM_LIT>)<EOL>if addSessionInfo:<EOL><INDENT>paymentSession = request.session.get(INVOICE_VALIDATION_STR, {})<EOL>paymentSession.update({<EOL>'<STR_LIT>': str(this_invoice.id),<EOL>'<STR_LIT>': this_total,<EOL>'<STR_LIT>': successUrl,<EOL>})<EOL>request.session[INVOICE_VALIDATION_STR] = paymentSession<EOL><DEDENT>return HttpResponseRedirect(successUrl)<EOL>
This view handles the charging of approved Square Checkout payments.

All Checkout payments must either be associated with a pre-existing Invoice
or a registration, or they must have an amount and type passed in the post data
(such as gift certificate payment requests).