Advertisement
alwerdani

Untitled

May 23rd, 2022
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import json
  2. import re
  3. from datetime import datetime
  4. from decimal import Decimal
  5.  
  6. from django.http import HttpResponse
  7. from moneyouts.models import Moneyout
  8. from payment import do_payout
  9.  
  10. DEFAULT_CURRENCY = "EUR"
  11.  
  12.  
  13. def int_to_decimal(value):
  14.     try:
  15.         if len(str(value)) == 2:
  16.             return Decimal("0" + "." + str(value)[-2:])
  17.         return Decimal(str(value)[:-2] + "." + str(value)[-2:])
  18.     except:
  19.         return Decimal(0)
  20.  
  21.  
  22. def moneyout_view(request):
  23.     client = request.user.client
  24.     wallet = client.wallet
  25.  
  26.     wallet_info = {"amount": int_to_decimal(wallet.amount)}
  27.  
  28.     success = ""
  29.     errors = ""
  30.  
  31.     if request.POST:
  32.         if client.wallet and client.wallet.bic and not request.POST.get("bic"):
  33.             bic = client.wallet.bic.replace(" ", "").upper()
  34.         else:
  35.             bic = request.POST.get("bic", "").replace(" ", "").upper()
  36.        
  37.         if client.wallet and client.wallet.iban and not request.POST.get("iban"):
  38.             iban = client.wallet.iban.replace(" ", "").upper()
  39.         else:
  40.             iban = request.POST.get("iban", "").replace(" ", "").upper()
  41.  
  42.         try:
  43.             amount = int_to_decimal(int(request.POST.get("amount", "")))
  44.         except Exception:
  45.             amount = None
  46.             errors = "Call us at 0 890 215 315".encode("utf8")
  47.             amount = 0
  48.  
  49.         if not amount or amount > wallet_info.get("amount"):
  50.             errors = "Amount error".encode("utf8")
  51.             success = ""
  52.  
  53.         elif bic and iban and amount:
  54.             try:
  55.                 wallet.bic = bic
  56.                 wallet.iban = iban
  57.                 wallet.save()
  58.  
  59.                 now = datetime.now()
  60.                 m_o = Moneyout(
  61.                     client=client,
  62.                     bic=bic,
  63.                     iban=iban,
  64.                     amount=amount,
  65.                     date_creation=now,
  66.                     state=0,
  67.                 )
  68.                 m_o.save()
  69.  
  70.                 response = do_payout(
  71.                     "MONEYOUT-%s" % m_o.pk,
  72.                     amount,
  73.                     DEFAULT_CURRENCY,
  74.                     iban,
  75.                     bic,
  76.                 )
  77.  
  78.                 m_o.response = "{}".format(response)
  79.                 m_o.psp_reference = response.get("pspReference", "")
  80.                
  81.                 if response.get("resultCode", "") == "[payout-success]":
  82.                     m_o.state = 1
  83.                     errors = ""
  84.                     success = (
  85.                         "Your payment request has been successfully recorded. "
  86.                         "Go to your wallet page to follow the payment process."
  87.                     )
  88.                 else:
  89.                     m_o.state = 6
  90.                     errors = response.get("message", "Please contact Back Market")
  91.                     success = ""
  92.  
  93.             except Exception as moneyout_error:
  94.                 errors = "Ko %s" % moneyout_error
  95.  
  96.             return HttpResponse(
  97.                 json.dumps({"success": success, "errors": errors})
  98.             )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement