Ejemplo n.º 1
0
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)