def success_response(request): hash_value = check_hash(request.POST) if check_hash(request.POST): transaction = get_transaction(request.POST) transaction.order.order() return HttpResponse("Order Placed") else : return HttpResponse("Transaction Failed!")
def cancel(request): if request.method == 'POST': try: email = request.POST["email"] user = Account.objects.get(email=email) order = Order.objects.get(user=user, ordered=False) transaction = Transaction.objects.get( transaction_id=request.POST["txnid"]) transaction_date = request.POST["addedon"] data = request.POST.copy() data.update({ "discount": 0, }) context = {"status": data["status"], "txnid": data["txnid"]} if not check_hash(data): # Response data for order (txnid: %s) has been tampered. Confirm payment with PayU # Invalid transaction. Please try again! return redirect('app:home') else: order.transaction = transaction order.ordered_date = transaction_date order.save() # Payment for order (txnid: %s) cancelled at PayU messages.warning(request, "Transaction Cancelled!") return render(request, 'app/cancel.html', context) except ObjectDoesNotExist: return render(request, "app/error.html") else: return render(request, "app/error.html")
def test_check_hash(self): data = {'additionalCharges': 220, 'txnid': 123456, 'amount': 500, 'discount': 50} response = check_hash(data) self.assertFalse(response)
def test_check_hash(self): data = { 'additionalCharges': 220, 'txnid': 123456, 'amount': 500, 'discount': 50 } response = check_hash(data) self.assertFalse(response)
def failure(request): if request.method == 'POST': try: email = request.POST["email"] user = Account.objects.get(email=email) order = Order.objects.get(user=user, ordered=False) order_items = order.items.all() transaction = Transaction.objects.get(transaction_id=request.POST["txnid"]) transaction_date = request.POST["addedon"] data = request.POST.copy() data.update({ "discount": 0, }) context = { "status": data["status"], "txnid": data["txnid"], "contact_url": request.build_absolute_uri(reverse('app:contact')), "cart_url": request.build_absolute_uri(reverse('app:order-summary', )) } email_data = { "transaction_id": transaction.transaction_id, "transaction_date": transaction_date, "order_items": order_items, "username": user.first_name + ' ' + user.last_name, "contact_url": request.build_absolute_uri(reverse('app:contact')), "cart_url": request.build_absolute_uri(reverse('app:order-summary')) } if not check_hash(data): # Response data for order (txnid: %s) has been tampered. Confirm payment with PayU # Invalid transaction. Please try again! return redirect('app:home') else: order.transaction = transaction order.ordered_date = transaction_date order.save() # Payment for order (txnid: %s) failed at PayU # Sending Email for failed transaction subject = "Transaction Failed" html_message = render_to_string('app/transaction_failed_email.html', email_data) plain_message = strip_tags(html_message) from_email = None # This will have no effect is you have set DEFAULT_FROM_EMAIL in settings.py to = email # This is a string, will be sent as a list fail_silently = False send_mail(subject, plain_message, from_email, [to], fail_silently, html_message=html_message) messages.warning(request, "Transaction Failed!") return render(request, 'app/failure.html', context) except ObjectDoesNotExist: return render(request, "app/error.html") else: return render(request, "app/error.html")
def success(request): if request.method == 'POST': try: email = request.POST["email"] user = Account.objects.get(email=email) order = Order.objects.get(user=user, ordered=False) # print(order.get_total()) transaction = Transaction.objects.get( transaction_id=request.POST["txnid"]) # print(request.POST) c = {} c.update(csrf(request)) status = request.POST["status"] amount = request.POST["amount"] # amount paid to payu txnid = request.POST["txnid"] transaction_date = request.POST["addedon"] context = {"status": status, "txnid": txnid, "amount": amount} if not check_hash(request.POST): # Response data for order (txnid: %s) has been tampered. Confirm payment with PayU # Invalid transaction. Please try again! return redirect('app:order.failure') else: # Payment for order (txnid: %s) succeeded at PayU if float(order.get_total()) == float(request.POST["amount"]): # checking if the amount paid is equal to total amount of items in cart # Marking the order items and order as ordered and updating transaction id order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.transaction = transaction order.ordered_date = transaction_date user.transactions.add(transaction) user.save() order.ordered = True order.save() context["order_items"] = order_items # Generating the invoice invoice_num = Order.objects.filter(ordered=True).count() amt_no_tax = round((order.get_total() * 100 / 118), 2) igst = round((amt_no_tax * 0.18), 2) data = { "transaction_id": txnid, "transaction_date": transaction_date, "order_items": order_items, "username": user.first_name + ' ' + user.last_name, "user_email": user.email, "user_phone": user.phone, "invoice_num": invoice_num, "total_amt": amount, "amt_no_tax": amt_no_tax, "igst": igst, "contact_url": request.build_absolute_uri(reverse('app:contact')), "invoice_url": request.build_absolute_uri( reverse('app:pdf_view', args=(txnid, ))), } pdf = render_to_pdf('app/invoice.html', data) filename = "Invoice{}-{}.pdf".format(invoice_num, txnid) order.invoice.save(filename, File(BytesIO(pdf.content))) # Sending Email for successful transaction subject = "Transaction Successful" html_message = render_to_string( 'app/transaction_email.html', data) plain_message = strip_tags(html_message) from_email = None # This will have no effect is you have set DEFAULT_FROM_EMAIL in settings.py to = email # This is a string, will be sent as a list fail_silently = False send_mail(subject, plain_message, from_email, [to], fail_silently, html_message=html_message) else: pass ######################## # Test this option, i.e, if courses are added during payment process from diff device. ######################## messages.success(request, "Transaction Successful!") return render(request, 'app/success.html', context) except ObjectDoesNotExist: return render(request, "app/error.html") else: return render(request, "app/error.html")
def post(self, request): if gateway.check_hash(request.data): return Response({"Transaction has been Successful."}) else: return Response({"Transaction has been Failed."})