Created
January 8, 2012 00:42
-
-
Save dmitric/1576629 to your computer and use it in GitHub Desktop.
WePay Asynchronous Client on top of tornado
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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