def processPointOfSalePayment(request):
print('<STR_LIT>' % request.GET)<EOL>data = json.loads(request.GET.get('<STR_LIT:data>','<STR_LIT:{}>'))<EOL>if data:<EOL><INDENT>status = data.get('<STR_LIT:status>')<EOL>errorCode = data.get('<STR_LIT>')<EOL>errorDescription = errorCode<EOL>try:<EOL><INDENT>stateData = data.get('<STR_LIT:state>','<STR_LIT>')<EOL>if stateData:<EOL><INDENT>metadata = json.loads(b64decode(unquote(stateData).encode()).decode())<EOL><DEDENT>else:<EOL><INDENT>metadata = {}<EOL><DEDENT><DEDENT>except (TypeError, ValueError, binascii.Error):<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(reverse('<STR_LIT>'))<EOL><DEDENT>serverTransId = data.get('<STR_LIT>')<EOL>clientTransId = data.get('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>errorCode = request.GET.get('<STR_LIT>')<EOL>errorDescription = request.GET.get('<STR_LIT>')<EOL>status = '<STR_LIT>' if not errorCode else '<STR_LIT:error>'<EOL>serverTransId = request.GET.get('<STR_LIT>')<EOL>clientTransId = request.GET.get('<STR_LIT>')<EOL>try:<EOL><INDENT>stateData = request.GET.get('<STR_LIT>','<STR_LIT>')<EOL>if stateData:<EOL><INDENT>metadata = json.loads(b64decode(unquote(stateData).encode()).decode())<EOL><DEDENT>else:<EOL><INDENT>metadata = {}<EOL><DEDENT><DEDENT>except (TypeError, ValueError, binascii.Error):<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(reverse('<STR_LIT>'))<EOL><DEDENT><DEDENT>sourceUrl = metadata.get('<STR_LIT>',reverse('<STR_LIT>'))<EOL>successUrl = metadata.get('<STR_LIT>',reverse('<STR_LIT>'))<EOL>submissionUserId = metadata.get('<STR_LIT>', getattr(getattr(request,'<STR_LIT:user>',None),'<STR_LIT:id>',None))<EOL>transactionType = metadata.get('<STR_LIT>')<EOL>taxable = metadata.get('<STR_LIT>', False)<EOL>addSessionInfo = metadata.get('<STR_LIT>',False)<EOL>customerEmail = metadata.get('<STR_LIT>')<EOL>if errorCode or status != '<STR_LIT>':<EOL><INDENT>logger.error('<STR_LIT>' % (errorCode, errorDescription))<EOL>messages.error(<EOL>request,<EOL>format_html(<EOL>'<STR_LIT>',<EOL>str(_('<STR_LIT>')), errorCode, errorDescription<EOL>)<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>api_instance = TransactionsApi()<EOL>api_instance.api_client.configuration.access_token = getattr(settings,'<STR_LIT>','<STR_LIT>')<EOL>location_id = getattr(settings,'<STR_LIT>','<STR_LIT>')<EOL>if serverTransId:<EOL><INDENT>try:<EOL><INDENT>api_response = api_instance.retrieve_transaction(transaction_id=serverTransId,location_id=location_id)<EOL><DEDENT>except ApiException:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>messages.error(request,_('<STR_LIT>'))<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>if api_response.errors:<EOL><INDENT>logger.error('<STR_LIT>' % api_response.errors)<EOL>messages.error(request,str(_('<STR_LIT>')) + api_response.errors)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>transaction = api_response.transaction<EOL><DEDENT>elif clientTransId:<EOL><INDENT>try:<EOL><INDENT>api_response = api_instance.list_transactions(location_id=location_id)<EOL><DEDENT>except ApiException:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>messages.error(request,_('<STR_LIT>'))<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>if api_response.errors:<EOL><INDENT>logger.error('<STR_LIT>' % api_response.errors)<EOL>messages.error(request,str(_('<STR_LIT>')) + api_response.errors)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>transactions_list = [x for x in api_response.transactions if x.client_id == clientTransId]<EOL>if len(transactions_list) == <NUM_LIT:1>:<EOL><INDENT>transaction = transactions_list[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>messages.error(request,_('<STR_LIT>'))<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>messages.error(request,_('<STR_LIT>'))<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>this_total = sum([x.amount_money.amount / <NUM_LIT:100> for x in transaction.tenders or []]) -sum([x.amount_money.amount / <NUM_LIT:100> for x in transaction.refunds or []])<EOL>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>if '<STR_LIT>' in metadata.keys():<EOL><INDENT>try:<EOL><INDENT>tr_id = int(metadata.get('<STR_LIT>'))<EOL>tr = TemporaryRegistration.objects.get(id=tr_id)<EOL><DEDENT>except (ValueError, TypeError, ObjectDoesNotExist):<EOL><INDENT>logger.error('<STR_LIT>' % metadata.get('<STR_LIT>'))<EOL>messages.error(<EOL>request,<EOL>str(_('<STR_LIT>')) + '<STR_LIT>' % metadata.get('<STR_LIT>')<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT>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><DEDENT>elif '<STR_LIT>' in metadata.keys():<EOL><INDENT>try:<EOL><INDENT>this_invoice = Invoice.objects.get(id=int(metadata.get('<STR_LIT>')))<EOL>this_description = _('<STR_LIT>' % this_invoice.id)<EOL><DEDENT>except (ValueError, TypeError, ObjectDoesNotExist):<EOL><INDENT>logger.error('<STR_LIT>' % metadata.get('<STR_LIT>'))<EOL>messages.error(<EOL>request,<EOL>str(_('<STR_LIT>')) + '<STR_LIT>' % metadata.get('<STR_LIT>')<EOL>)<EOL>return HttpResponseRedirect(sourceUrl)<EOL><DEDENT><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>this_total,<EOL>this_description,<EOL>submissionUser=submissionUser,<EOL>calculate_taxes=(taxable is not False),<EOL>transactionType=transactionType,<EOL>)<EOL><DEDENT>paymentRecord, created = SquarePaymentRecord.objects.get_or_create(<EOL>transactionId=transaction.id,<EOL>locationId=transaction.location_id,<EOL>defaults={'<STR_LIT>': this_invoice,}<EOL>)<EOL>if created:<EOL><INDENT>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><DEDENT>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 callbacks from point-of-sale transactions.
Please note that this will only work if you have set up your callback
URL in Square to point to this view.