Open verico1 opened 3 years ago
from django.http import HttpResponse, request from django.shortcuts import redirect, get_object_or_404, render from zeep import Client from django.conf import settings from accounts.models import Profile from products.models import Product from pay.models import Order, OrderItem import datetime from django.core.mail import BadHeaderError, send_mail from django.template.loader import render_to_string from pay.views import get_user_pending_order MERCHANT = 'xxx...xxx' client = Client('https://www.zarinpal.com/pg/services/WebGate/wsdl') amount = "" description = "" # Required email = "" # Optional mobile = "" # Optional CallbackURL = 'https://mysite/payment/verify/' # Important: need to edit for realy server. def send_request(request): username = request.user amount = get_user_pending_order(request).get_total() + get_user_pending_order(request).transport.transport_price email = username.email # Optional mobile = username.profile.phone_number # Optional result = client.service.PaymentRequest(MERCHANT, amount, description, email, mobile, CallbackURL) order_to_purchase = get_user_pending_order(request) order_to_purchase.save() if result.Status == 100: return redirect('https://www.zarinpal.com/pg/StartPay/' + str(result.Authority)) else: return HttpResponse('Error code: ' + str(result.Status)) def verify(request): amount = get_user_pending_order(request).get_total() + get_user_pending_order(request).transport.transport_price if request.GET.get('Status') == 'OK': result = client.service.PaymentVerification(MERCHANT, request.GET['Authority'], amount) if result.Status == 100: user = request.user order_to_purchase = get_user_pending_order(request) order_to_purchase.is_ordered = True order_to_purchase.save() order_to_purchase.date_ordered=datetime.datetime.now() order_to_purchase.created_on_time=datetime.datetime.now() order_to_purchase.save() order_items = order_to_purchase.items.all() for order_item in order_items: order_item.product.quantity = order_item.product.quantity - 1 order_item.product.save() order_items.update(is_ordered=True, date_ordered=datetime.datetime.now()) subject = 'پرداخت با موفقیت انجام شد' c = { "refid":str(result.RefID), "ref_code":order_to_purchase.ref_code, "owner":order_to_purchase.owner, } email_template_name = "pay/after_pay_confirm_email.html" email_html = render_to_string(email_template_name, c) email_from = settings.EMAIL_HOST_USER send_mail(subject, email_html, email_from, [user.email], html_message=email_html) ctx = {'message_good':'پرداخت با موفقیت انجام شد. \n کد پیگیری تراکنش : ' + str(result.RefID)} elif result.Status == 101: return HttpResponse('Transaction submitted : ' + str(result.Status)) else: return HttpResponse('Transaction failed.\nStatus: ' + str(result.Status)) else: ctx = {'message_bad':'پرداخت با مشکل مواجه شده است یا توسط کاربر لغو شده'} return render(request, 'pay/verify.html', ctx)
additional codes :
`def get_user_pending_order(request): user_profile = get_object_or_404(Profile, user=request.user) order = Order.objects.filter(owner=user_profile, is_ordered=False) if order.exists(): return order[0] return 0`
and
class Order(models.Model): ... owner = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True) is_ordered = models.BooleanField(default=False) transport = models.ForeignKey(Transport, on_delete=models.SET_NULL, null=True) ... def get_cart_items(self): return self.items.all() def __str__(self): return '{0} - {1}'.format(self.owner, self.ref_code) def get_total(self): total = 0 for order_item in self.items.all(): total += order_item.get_final_price() return total
in the beginning of verify get_user_pending_order(request).get_total() + get_user_pending_order(request).transport.transport_price about requests
get_user_pending_order(request).get_total() + get_user_pending_order(request).transport.transport_price
sometimes with different users give me "anonymoususer" error, not always and its after success payment
additional codes :
and
in the beginning of verify
get_user_pending_order(request).get_total() + get_user_pending_order(request).transport.transport_price
about requestssometimes with different users give me "anonymoususer" error, not always and its after success payment