def process_transaction(self, card, amount: int = None): if amount is None: amount = card.amount Sender = self.balances.filter(Balance.account == card.sender) if Sender.first() is None: return if Sender.first().value >= amount: Receiver = self.balances.filter(Balance.account == card.receiver) if card.ctype == "CardBurn": self.process_burn(card) return elif Receiver.first() is None: B = Balance(card.receiver, amount, self.short_id, card.blockhash) session.add(B) commit() else: Receiver = self.balances.filter( Balance.account == card.receiver) Receiver.update({'value': Receiver.first().value + amount}, synchronize_session='fetch') Sender = self.balances.filter(Balance.account == card.sender) Sender.update({'value': Sender.first().value - amount}, synchronize_session='fetch') commit() return
def ONCE(amount: int = amount): ''' Set Issuer object to a query of all accounts containing the deck issuing address ''' Issuer = self.balances.filter( Balance.account.contains(self.deck.issuer)) if Issuer.first() is not None: ''' There is already an entry containing the deck issuing address ''' if self.mode == 'ONCE': ''' If issue mode of the deck is ONCE then only first occurence of CardIssuance transaction is allowed ''' if Issuer.filter(Balance.account.contains( c_short_id)).first() is not None: ''' Here we are checking to see if this card is a part of the first issuance transaction (There can be multiple with same txid)''' process_sender(amount, card) process_receiver(amount, card) _card = session.query(Card).filter( Card.txid == card.txid).filter( Card.blockseq == card.blockseq).filter( Card.cardseq == card.cardseq).first() _card.valid = True commit() return else: return else: ''' Continue processing issuance since it does not contain ONCE issue mode''' process_sender(amount, card) process_receiver(amount, card) _card = session.query(Card).filter( Card.txid == card.txid).filter( Card.blockseq == card.blockseq).filter( Card.cardseq == card.cardseq).first() _card.valid = True commit() return elif Issuer.first() is None: ''' Create a genesis CardIssue account then process receiver ''' B = Balance(self.deck.issuer + c_short_id, -abs(amount), self.short_id, card.blockhash) try: session.add(B) commit() process_receiver(amount, card) _card = session.query(Card).filter( Card.txid == card.txid).filter( Card.blockseq == card.blockseq).filter( Card.cardseq == card.cardseq).first() _card.valid = True commit() except Exception as e: print(e + '\r') pass
def __balance_for_user(session, currency, user_id): balance = session.query(Balance).\ filter(Balance.currency == currency).\ filter(Balance.user_id == user_id).with_for_update().first() if balance is None: return Balance(currency=currency, amount=Decimal(0), user_id=user_id) return balance
def counter(self): ''' Get the block number that the balances for this account were last updated''' Blocknum = self.balances.filter(Balance.account == 'blocknum') if Blocknum.first() is None: ''' If this deck does not have a 'blocknum' account in database then create one ''' B = Balance('blocknum', 0, self.short_id, '') session.add(B) commit() Blocknum = self.balances.filter(Balance.account == 'blocknum') if Blocknum.first() is not None: ''' Only concerned with cards that are before the last update to balances of this deck ''' self.cards = self.cards.filter( Card.blocknum > Blocknum.first().value)
def process_receiver(amount, card): ''' Add receiver to db if the account doesn't exist and update receiver balance ''' Receiver = self.balances.filter(Balance.account == card.receiver) if Receiver.first() is not None: Receiver.update( { "value": Receiver.first().value + amount, "checkpoint": card.blockhash }, synchronize_session='fetch') if Receiver.first() is None: B = Balance(card.receiver, amount, self.short_id, card.blockhash) session.add(B) commit()
def process_burn(self, card, amount: int = None): if amount is None: amount = card.amount Receiver = self.balances.filter(Balance.account == 'CardBurn') Sender = self.balances.filter(Balance.account == card.sender) if Receiver.first() is not None: Receiver.update({'value': Receiver.first().value + amount}, synchronize_session='fetch') else: B = Balance('CardBurn', amount, self.short_id, card.blockhash) session.add(B) commit() Sender.update({'value': Sender.first().value - amount}, synchronize_session='fetch') return
def process_sender(amount, card): ''' Add sender to db if the account doesn't exist and update sender balance ''' Sender = self.balances.filter( Balance.account.contains(card.sender + c_short_id)) if Sender.first() is not None: ''' If there is already an existing address for the sender ''' Sender.update( { "value": Sender.first().value - abs(amount), "checkpoint": card.blockhash }, synchronize_session='fetch') elif Sender.first() is None: B = Balance(card.sender + c_short_id, -abs(amount), self.short_id, card.blockhash) session.add(B) commit()
def process_order(request): if request.method == 'POST': #Pull all data out of POST (some of it mightn't be there) if request.POST.get('tickets'): tickets = int(request.POST.get('tickets', '')) if request.POST.get('deposits', ''): deposits = int(request.POST.get('deposits', '')) first_name = request.POST['first_name'] last_name = request.POST['last_name'] email = request.POST['email'] phone = request.POST['phone'] referring_ambassador_code = request.POST.get('referring_ambassador_code', '') deposit_code_one = request.POST.get('deposit_code_one', '') deposit_code_two = request.POST.get('deposit_code_two', '') deposit_code_three = request.POST.get('deposit_code_three', '') deposit_code_four = request.POST.get('deposit_code_four', '') deposit_code_five = request.POST.get('deposit_code_five', '') #First build the order order = Order(fname=first_name, lname=last_name, email=email, phone=phone) order.save() #Initialise the lists to hold the possible ordered items ordered_tickets = [] ordered_deposits = [] ordered_balances = [] if referring_ambassador_code: #Purchasing tickets and/or deposits referring_ambassador = Ambassador.objects.get(code=referring_ambassador_code) #Then generate tickets and deposits while tickets > 0: ticket = Ticket(order=order, ambassador=referring_ambassador) ticket.save() ordered_tickets.append(ticket) tickets -= 1 while deposits > 0: deposit = Deposit(order=order, ambassador=referring_ambassador) deposit.save() ordered_deposits.append(deposit) deposits -= 1 else: #Purchasing balances for paid deposits if deposit_code_one != 'None': balance = Balance(order=order, deposit=Deposit.objects.get(code=deposit_code_one)) balance.save() ordered_balances.append(balance) if deposit_code_two != 'None': balance = Balance(order=order, deposit=Deposit.objects.get(code=deposit_code_two)) balance.save() ordered_balances.append(balance) if deposit_code_three != 'None': balance = Balance(order=order, deposit=Deposit.objects.get(code=deposit_code_three)) balance.save() ordered_balances.append(balance) if deposit_code_four != 'None': balance = Balance(order=order, deposit=Deposit.objects.get(code=deposit_code_four)) balance.save() ordered_balances.append(balance) if deposit_code_five != 'None': balance = Balance(order=order, deposit=Deposit.objects.get(code=deposit_code_five)) balance.save() ordered_balances.append(balance) #Build the XML request for Google Checkout data = {"_type": "checkout-shopping-cart", "_charset_":""} itemcount = 0 for ticket in ordered_tickets: itemcount += 1 data['item_name_'+ str(itemcount)] = "1 x Summer Saturnalia 2011 Ticket" data['item_description_'+str(itemcount)] = 'One ticket including £2.50 booking fee. Unique ticket code: ' + str(ticket.code) data['item_price_'+str(itemcount)] = "57.50" data['item_currency_'+str(itemcount)] = "GBP" data['item_quantity_'+str(itemcount)] = "1" data['item_merchant_id_'+str(itemcount)] = ticket.code data['shopping-cart.items.item-'+str(itemcount)+'.digital-content.display-disposition'] = "OPTIMISTIC" data['shopping-cart.items.item-'+str(itemcount)+'.digital-content.description'] = "After your payment has been processed your printable ticket will be emailed to you; this normally takes less than a day." for deposit in ordered_deposits: itemcount += 1 data['item_name_'+str(itemcount)] = "1 x Summer Saturnalia 2011 Deposit" data['item_description_'+str(itemcount)] = "One deposit including £2.50 booking fee. Unique deposit code: " + str(deposit.code) data['item_price_'+str(itemcount)] = "8" data['item_currency_'+str(itemcount)] = "GBP" data['item_quantity_'+str(itemcount)] = "1" data['item_merchant_id_'+str(itemcount)] = deposit.code data['shopping-cart.items.item-'+str(itemcount)+'.digital-content.display-disposition'] = "OPTIMISTIC" data['shopping-cart.items.item-'+str(itemcount)+'.digital-content.description'] = "Your deposit code is " + str(deposit.code) +". You can use this code to pay the balance and obtain your ticket when payment has been processed. This normally takes less than a day, after which a copy of this code will be emailed to you. " for balance in ordered_balances: itemcount += 1 data['item_name_'+str(itemcount)] = "1 x Summer Saturnalia 2011 Balance Payment (Ticket)" data['item_description_'+str(itemcount)] = "The final payment for deposit number " + str(balance.deposit.code) + " - Booking already included in deposit." data['item_price_'+str(itemcount)] = "49.50" data['item_currency_'+str(itemcount)] = "GBP" data['item_quantity_'+str(itemcount)] = "1" data['item_merchant_id_'+str(itemcount)] = balance.deposit.code data['shopping-cart.items.item-'+str(itemcount)+'.digital-content.display-disposition'] = "OPTIMISTIC" data['shopping-cart.items.item-'+str(itemcount)+'.digital-content.description'] = "After your payment has been processed your printable ticket will be emailed to you; this normally takes less than a day." #Build the rest of the request and send to Google headers = {"Content-Type": 'application/xml; charset=UTF-8', "Accpt": 'application/xml; charset=UTF-8'} h = Http() h.add_credentials('210280845590798', 'qqnL2K9V76aNWEVVXAoLtQ') resp, content = h.request("https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/210280845590798", "POST", urlencode(data), headers=headers) #If everything worked, we can redirect the user to Google Checkout to complete payment location = resp['location'] return redirect(location)