2016-12-07 64 views
0

所以,如果代碼有一些縮進問題,這是由於我如何將它粘貼到堆棧溢出中,但基本上我有下面列出的這個服務,當Django web服務被擊中時運行。 95%的時間一切正常,但如果你看看.save()被調用的數量,那麼有時即使運行了account.save(),付款對象也不會被保存。我不熟悉調用django commit,但是我知道由於過去的項目,最好不要在一個web服務中列出太多的.save()。任何人都知道我可以做些什麼來確保所有的物體都被保存或根本沒有?如果信用卡付款經過,賬戶對象被更新,然後即使卡被收費並且賬戶顯示新付款,也不會創建付款對象,這會造成巨大的問題。確保在Django服務中保存多個對象

import gateway 
import datetime 
import smtplib 
merchant = {'merchantKey': '', 
      'processorId': '', 
      } 
data = dict(merchant) 
cardNumber = request.GET.get("cardNumber", "") 
cardExpYear = request.GET.get("cardExpYear", "") 
cardExpMonth = request.GET.get("cardExpMonth", "") 
fullName = request.GET.get("fullName", "") 
accountIDOriginal = request.GET.get("accountID", "") 
accountID = accountIDOriginal.split('-')[0] 
if Payments.objects.filter(accountID=accountID,paymentDate=str(datetime.date.today())).count() > 0: 
    return HttpResponse('You Have already ran a payment today.') 
else: 
    theAccount = Account.objects.get(acctno=accountID) 
    collectorID = theAccount.collector_id 
    collectorEmail = Collector.objects.get(pk=collectorID).email 
    ownerZip = request.GET.get("ownerZip", "") 
    ownerState = request.GET.get("ownerState", "") 
    ownerStreet = request.GET.get("ownerStreet", "") 
    ownerCity = request.GET.get("ownerCity", "") 
    cVV = request.GET.get("cVV", "") 
    transactionAmountString = request.GET.get("transactionAmount", "") 
    transactionAmount = Decimal(transactionAmountString) 
    if ownerState == "CO" or ownerState == "co" or ownerState == "Co": 
     creditCardCharge = round(transactionAmount * Decimal(1.00),2) 
    else: 
     creditCardCharge = round(transactionAmount * Decimal(1.03),2) 
    data['cardNumber'] = cardNumber 
    data['cardExpMonth'] = cardExpMonth 
    data['cardExpYear'] = cardExpYear 
    data['ownerState'] = ownerState 
    data['ownerCity'] = ownerCity 
    data['ownerName'] = fullName 
    data['ownerStreet'] = ownerStreet 
    data['ownerState'] = ownerState 
    data['ownerZip'] = ownerZip 
    data['cVV'] = cVV 
    data['transactionAmount'] = creditCardCharge 
    sale = gateway.RestGateway(data) 
    resultsCC = str(sale.createSale()) 
    if sale.status == 'Success': 
     fromaddr = '[email protected]' 
     subject = 'New CC Payment' 
     actualaccountid = Account.objects.get(acctno=accountID).accountid 
     email = str(fullName) + str(' has made a payment for the amount of ') + str(transactionAmount) + str(
     '. Account ID is ') + str(accountID) + str(""" 
     https://web.domain.com/admin/web/account/""") + str(actualaccountid) 
     message = 'Subject: %s\n\n%s' % (subject, email) 
     username = 'emailaddress' 
     password = 'password' 
     server = smtplib.SMTP('smtp.gmail.com:587') 
     server.ehlo() 
     server.starttls() 
     server.login(username, password) 
     server.sendmail(fromaddr, collectorEmail, message) 
     server.quit() 
     previousBalance = theAccount.balance 
     clientNumber = Account.objects.get(acctno=accountID).cl_no 
     client = Client.objects.get(cl_no=clientNumber) 
     clRates = Decimal(client.cl_rates) 
     clientsRateCorrected = clRates/Decimal(100.00) 
     agFee = Decimal(clientsRateCorrected) * transactionAmount 
     # calculate taxes 
     if client.cl_taxable == "Y": 
      tax = transactionAmount * Decimal(client.cl_taxrate) 
      tax = transactionAmount * Decimal(client.cl_taxrate) 
     else: 
      tax = '0.0' 
     # calculate new balance 
     newBalance = Decimal(theAccount.balance) - transactionAmount 
     # save Account with new Balance 
     theAccount.balance = Decimal(newBalance) 
     # set new DLP 
     theAccount.lastamt = transactionAmount 
     theAccount.doctor_id = 3 
     theAccount.lastpay = str(datetime.date.today()) 
     theAccount.save() 
     newPayment = Payments(paymentDate=str(datetime.date.today()), 
          # claimDate='', 
          accountID_id=theAccount.acctno, 
          firstName=theAccount.first, 
          lastName=theAccount.last, 
          clientID=clientNumber, 
          pAgency=transactionAmount, 
          pClient=0.00, 
          rate=client.cl_rates, 
          agFee=agFee, 
          taxable=client.cl_taxable, 
          tax=tax, 
          # desc='', 
          fee=0.00, 
          # expense='', 
          # adjust='', 
          # salesman='', 
          balance=newBalance, 
          # intDue='', 
          status='$', 
          # area='', 
          ref_no='', 
          collector=theAccount.collector, 
          # cref_date='', 
          clName=client.cl_name, 
          # referred='', 
          # contact='', 
          isCC=True, 
          isProccessed=False, 
          creditCardChargedDate=str(datetime.date.today()), 
         ) 
     newPayment.save() 
     paymentID = newPayment.paymentID 
     handlingFee = Decimal(creditCardCharge) - Decimal(transactionAmount) 
     clientName = Client.objects.get(cl_no=theAccount.cl_no).cl_name 
     colelctorEmail = Collector.objects.get(name=theAccount.collector).email 
     newReceipt = Invoice(paymentID=paymentID, 
         clientName=clientName, 
         accountNumber=theAccount.acctno, 
         claimNumber=theAccount.ref_no, 
         handlingFee=Decimal(handlingFee), 
         paymentAmount=Decimal(transactionAmount), 
         paymentAmountWithHandling=Decimal(creditCardCharge), 
         ccCharge=True, 
         newBalance=Decimal(newBalance), 
         previousBalance=Decimal(previousBalance), 
         originalBalance=Decimal(theAccount.referred), 
         paymentDate=str(datetime.date.today()), 
         collector=theAccount.collector, 
         fullName=fullName, 
         lastFourofCard=1234, 
         collectorEmailAddress = colelctorEmail 
         ) 
     newReceipt.save() 
     return HttpResponse(str('https://domain.com/invoices/') + str(newReceipt.pk) + str('/') + str(theAccount.acctno) + str('/')) 
    #return HttpResponse(final) 
    else: 
     fromaddr = 'email' 
     subject = 'Error CC Payment' 
     actualaccountid = Account.objects.get(acctno=accountID).accountid 
     email = str(fullName) + str(' has attempted to make a payment for the amount of ') + str(transactionAmount) + str(
     '. Account ID is ') + str(accountID) + str(""" 

     Attached are the error results from the card being ran: 
     """) + str(resultsCC) 
     message = 'Subject: %s\n\n%s' % (subject, email) 
     username = 'email' 
     password = 'Password' 
     server = smtplib.SMTP('smtp.gmail.com:587') 
     server.ehlo() 
     server.starttls() 
     server.login(username, password) 
     server.sendmail(fromaddr, collectorEmail, message) 
     server.quit() 
     response = "An Error Has Occured. Please Contact ____ for Help." 
     return HttpResponse(response) 
+0

「如果代碼有一些縮進問題,這是由於我如何將它粘貼到堆棧溢出」=>我們實際上並不關心 - 如果您的代碼嚴重縮進,我們無法分辨它的真正作用,所以它是__你的責任去解決它。 –

回答

0

任何人都知道我能做些什麼,以確保所有的對象都保存或者根本沒有?

是的,這叫做交易,它是fully documented here

+0

即使沒有交易,有沒有一種方法可以確保所有人都能正確保存,就像你會認爲預期的行爲一樣? –