def createPaypalPayment(request):
logger.info('<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>if amount:<EOL><INDENT>try:<EOL><INDENT>amount = float(amount)<EOL><DEDENT>except ValueError:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>return HttpResponseBadRequest()<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>raise ValueError<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>logger.error(e)<EOL>return HttpResponseBadRequest()<EOL><DEDENT>this_currency = getConstant('<STR_LIT>')<EOL>this_total = min(this_invoice.outstandingBalance, amount)<EOL>this_subtotal = this_total - this_invoice.taxes<EOL>this_transaction = {<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': round(this_total,<NUM_LIT:2>),<EOL>'<STR_LIT>': this_currency,<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': round(this_subtotal,<NUM_LIT:2>),<EOL>'<STR_LIT>': round(this_invoice.taxes,<NUM_LIT:2>),<EOL>},<EOL>},<EOL>'<STR_LIT:description>': str(this_description),<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': []<EOL>}<EOL>}<EOL>for item in this_invoice.invoiceitem_set.all():<EOL><INDENT>if not getConstant('<STR_LIT>'):<EOL><INDENT>this_item_price = item.grossTotal - item.taxes<EOL><DEDENT>else:<EOL><INDENT>this_item_price = item.grossTotal<EOL><DEDENT>this_transaction['<STR_LIT>']['<STR_LIT>'].append({<EOL>'<STR_LIT:name>': str(item.name),<EOL>'<STR_LIT>': round(this_item_price,<NUM_LIT:2>),<EOL>'<STR_LIT>': round(item.taxes,<NUM_LIT:2>),<EOL>'<STR_LIT>': this_currency,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>})<EOL><DEDENT>if this_invoice.grossTotal != this_invoice.total:<EOL><INDENT>this_transaction['<STR_LIT>']['<STR_LIT>'].append({<EOL>'<STR_LIT:name>': str(_('<STR_LIT>')),<EOL>'<STR_LIT>': round(this_invoice.total,<NUM_LIT:2>) - round(this_invoice.grossTotal,<NUM_LIT:2>),<EOL>'<STR_LIT>': this_currency,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>})<EOL><DEDENT>if this_invoice.amountPaid > <NUM_LIT:0>:<EOL><INDENT>this_transaction['<STR_LIT>']['<STR_LIT>'].append({<EOL>'<STR_LIT:name>': str(_('<STR_LIT>')),<EOL>'<STR_LIT>': -<NUM_LIT:1> * round(this_invoice.amountPaid,<NUM_LIT:2>),<EOL>'<STR_LIT>': this_currency,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>})<EOL><DEDENT>if amount != this_invoice.outstandingBalance:<EOL><INDENT>this_transaction['<STR_LIT>']['<STR_LIT>'].append({<EOL>'<STR_LIT:name>': str(_('<STR_LIT>')),<EOL>'<STR_LIT>': round(amount,<NUM_LIT:2>) - round(this_invoice.outstandingBalance,<NUM_LIT:2>),<EOL>'<STR_LIT>': this_currency,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>})<EOL><DEDENT>site = SimpleLazyObject(lambda: get_current_site(request))<EOL>protocol = '<STR_LIT>' if request.is_secure() else '<STR_LIT:http>'<EOL>base_url = SimpleLazyObject(lambda: "<STR_LIT>".format(protocol, site.domain))<EOL>payment = Payment({<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>},<EOL>'<STR_LIT>': [this_transaction],<EOL>'<STR_LIT>': {<EOL>'<STR_LIT>': str(base_url),<EOL>'<STR_LIT>': str(base_url),<EOL>}<EOL>})<EOL>if payment.create():<EOL><INDENT>logger.info('<STR_LIT>')<EOL>if this_invoice:<EOL><INDENT>this_invoice.status = Invoice.PaymentStatus.authorized<EOL>this_invoice.save()<EOL>PaypalPaymentRecord.objects.create(<EOL>paymentId=payment.id,<EOL>invoice=this_invoice,<EOL>status=payment.state,<EOL>)<EOL><DEDENT>return JsonResponse(payment.to_dict())<EOL><DEDENT>else:<EOL><INDENT>logger.error('<STR_LIT>')<EOL>logger.error(payment)<EOL>logger.error(payment.error)<EOL>if this_invoice:<EOL><INDENT>this_invoice.status = Invoice.PaymentStatus.error<EOL>this_invoice.save()<EOL><DEDENT>return HttpResponseBadRequest()<EOL><DEDENT>
This view handles the creation of Paypal Express Checkout Payment objects.

All Express 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).