Skip to content

Instantly share code, notes, and snippets.

@dmitric
Created January 8, 2012 00:42
Show Gist options
  • Save dmitric/1576629 to your computer and use it in GitHub Desktop.
Save dmitric/1576629 to your computer and use it in GitHub Desktop.
WePay Asynchronous Client on top of tornado
{% extends "base.html" %}
{% block page_title %} - Buy Goods {% end %}
{% block head %} <script type="text/javascript" src="https://stage.wepay.com/js/iframe.wepay.js"></script> {% end %}
{% block page_content %}
<div id="checkout"></div>
{% end %}
{% block js %}
WePay.iframe_checkout("checkout", "{{checkout["checkout_uri"]}}");
{% end %}
import tornado.httpclient
import urllib
import logging
import functools
from tornado import escape
from tornado.options import options
from tornado.httputil import url_concat
class WePayClient(object):
def __init__(self, access_token=None, production=True):
if access_token is not None:
self.access_token = access_token
else:
self.access_token = options.wepay_access_token
if production:
self.host = "https://www.wepay.com/v2"
else:
self.host = "https://stage.wepay.com/v2"
def call(self, uri, callback, params=None, access_token=None):
if not params:
params = {}
headers = self._default_headers()
if access_token or self.access_token:
headers = self._add_auth_header(headers,(access_token if access_token else self.access_token))
params = escape.json_encode(params)
http = tornado.httpclient.AsyncHTTPClient()
callback = functools.partial(self._on_wepay_request, callback)
http.fetch(self.host+"/"+uri, method="POST", headers=headers, body=params, callback=callback)
def _get_auth_url(self, redirect_uri, client_id, scope=None, params=None):
if not scope:
scope = "manage_accounts,collect_payments,view_balance,view_user,refund_payments,send_money"
if not params:
params = {}
params['redirect_uri'] = redirect_uri
params['scope'] = scope
params['client_id'] = client_id
return url_concat(self.host+ "/oauth2/authorize", params)
def get_access_token(self, redirect_uri, client_id, client_secret, code, callback):
params = {
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret,
'code': code,
}
return self.call('/oauth2/token', params=params, callback=callback)
def _default_headers(self, custom={}):
headers = {
'Content-Type': 'application/json',
}
headers.update(custom)
return headers
def _add_auth_header(self,headers,access_token):
headers['Authorization'] = 'Bearer ' + access_token
return headers
def _on_wepay_request(self, callback, response):
if response.error:
logging.warning("Error response %s fetching %s", response.error, response.request.url)
callback(None)
return
callback(escape.json_decode(response.body))
class WePayHandler(BaseHandler):
@tornado.web.authenticated
@tornado.web.asynchronous
def get(self):
amount = self.get_argument("amount")
wepay = WePayClient(production=options.wepay_use_production)
params = {
"account_id": options.wepay_account_id,
"amount": amount,
"type": "GOODS",
"short_description": "Buying ${0} worth of goods".format(amount),
"mode": "iframe",
"callback_uri": options.wepay_notification_url
}
wepay.call("checkout/create",params=params,callback=self._on_response)
def _on_response(self, resp):
self.render("base_wepay.html",checkout=resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment