Skip to content

Instantly share code, notes, and snippets.

@robrocker7
Created August 11, 2011 20:51
Show Gist options
  • Save robrocker7/1140733 to your computer and use it in GitHub Desktop.
Save robrocker7/1140733 to your computer and use it in GitHub Desktop.
import pycurl
import base64
import StringIO
import datetime
from xml.dom import minidom
from dateutil import relativedelta
from django.http import HttpResponse, HttpResponseRedirect
class GoogleResponse:
def __init__(self):
self.response = ''
def body_callback(self, buff):
self.response = self.response + buff
def test(request):
# GOOGLE Sandbox info
MERCHANT_ID = '43sdfasdf50'
MERCHANT_KEY = 'Rasdfasdfw'
MERCHANT_URL = 'https://%s:%s@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/%s' % (MERCHANT_ID, MERCHANT_KEY, MERCHANT_ID)
doc = minidom.Document()
# cart object world
cart = doc.createElement('checkout-shopping-cart')
cart.setAttribute("xmlns","http://checkout.google.com/schema/2")
item_cart = doc.createElement('shopping-cart')
cart.appendChild(item_cart)
items = doc.createElement('items')
item_cart.appendChild(items)
item = doc.createElement('item')
items.appendChild(item)
# name info
name = doc.createElement('item-name')
name_text = doc.createTextNode('WEREWOLF GAME PRO Account')
name.appendChild(name_text)
item.appendChild(name)
# description
desc = doc.createElement('item-description')
desc_text = doc.createTextNode('Subscription to werewolf-game.com.')
desc.appendChild(desc_text)
item.appendChild(desc)
# price
price = doc.createElement('unit-price')
price.setAttribute('currency', 'USD')
price_text = doc.createTextNode('2.00')
price.appendChild(price_text)
item.appendChild(price)
# quantity
quantity = doc.createElement('quantity')
quantity_text = doc.createTextNode('1')
quantity.appendChild(quantity_text)
item.appendChild(quantity)
# subscription info
sub = doc.createElement('subscription')
sub.setAttribute('period', 'MONTHLY')
sub.setAttribute('type', 'google')
now = datetime.datetime.now()
sub.setAttribute('start-date', now.isoformat())
item.appendChild(sub)
# payments
payments = doc.createElement('payments')
sub.appendChild(payments)
#subscription payment
sub_payment = doc.createElement('subscription-payment')
payments.appendChild(sub_payment)
# max-charge
max_charge = doc.createElement('maximum-charge')
max_charge.setAttribute('currency', 'USD')
max_charge_text = doc.createTextNode('2.00')
max_charge.appendChild(max_charge_text)
sub_payment.appendChild(max_charge)
# recurring item
re_item = doc.createElement('recurrent-item')
sub.appendChild(re_item)
# item name
re_item_name = doc.createElement('item-name')
re_item_name_text = doc.createTextNote('Werewolf Game subscription for ONE month')
re_item_name.appendChild(re_item_name_text)
re_item.appendChild(re_item_name)
# item description
re_item_desc = doc.createElement('item-description')
re_item_desc_text = doc.createTextNote('Your flat fee for having a pro account on www.werewolf-game.com')
re_item_desc.appendChild(re_item_desc_text)
re_item.appendChild(re_item_desc)
# item quantity
re_item_quantity = doc.createElement('item-quantity')
re_item_quantity_text = doc.createTextNote('1')
re_item_quantity.appendChild(re_item_quantity_text)
re_item.appendChild(re_item_quantity)
# append the cart
doc.appendChild(cart)
data = doc.toxml(encoding="utf-8")
#return HttpResponse(data)
res = GoogleResponse()
# send to google
ch = pycurl.Curl()
ch.setopt(pycurl.URL, MERCHANT_URL)
ch.setopt(pycurl.POST, 1)
ch.setopt(pycurl.POSTFIELDS, data)
ch.setopt(pycurl.WRITEFUNCTION, res.body_callback)
ch.perform()
ch.close()
#return HttpResponse(res.response)
google_xml = minidom.parseString(res.response)
redirect = google_xml.childNodes[0].childNodes[1].childNodes[0].nodeValue
return HttpResponseRedirect(redirect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment