From a4e0aa022fdd32b42606987101da49bcaf78d870 Mon Sep 17 00:00:00 2001 From: bsimpson63 Date: Wed, 20 Feb 2013 18:16:14 -0500 Subject: [PATCH] RedditGifts gold controller. --- r2/example.ini | 2 ++ r2/r2/config/routing.py | 2 ++ r2/r2/controllers/__init__.py | 1 + r2/r2/controllers/ipn.py | 58 ++++++++++++++++++++++++++++++++++- 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/r2/example.ini b/r2/example.ini index 1692fc6b0..552f38a52 100644 --- a/r2/example.ini +++ b/r2/example.ini @@ -93,6 +93,8 @@ COINBASE_BUTTONID_ONETIME_1YR = COINBASE_BUTTONID_ONETIME_2YR = COINBASE_BUTTONID_ONETIME_3YR = +RG_SECRET = + # -- feature toggles -- disable_ads = false disable_captcha = false diff --git a/r2/r2/config/routing.py b/r2/r2/config/routing.py index b69edeafd..fbba14434 100644 --- a/r2/r2/config/routing.py +++ b/r2/r2/config/routing.py @@ -266,6 +266,8 @@ def make_map(): action='goldwebhook') mc('/api/coinbasewebhook/gold/:secret', controller='coinbase', action='goldwebhook') + mc('/api/rgwebhook/gold/:secret', controller='redditgifts', + action='goldwebhook') mc('/api/ipn/:secret', controller='ipn', action='ipn') mc('/ipn/:secret', controller='ipn', action='ipn') mc('/api/:action/:url_user', controller='api', diff --git a/r2/r2/controllers/__init__.py b/r2/r2/controllers/__init__.py index 8ba31ab9e..b2173f808 100644 --- a/r2/r2/controllers/__init__.py +++ b/r2/r2/controllers/__init__.py @@ -84,5 +84,6 @@ def load_controllers(): from ipn import IpnController from ipn import StripeController from ipn import CoinbaseController + from ipn import RedditGiftsController _reddit_controllers.update((name.lower(), obj) for name, obj in locals().iteritems()) diff --git a/r2/r2/controllers/ipn.py b/r2/r2/controllers/ipn.py index 23748803d..4d82a6c1b 100644 --- a/r2/r2/controllers/ipn.py +++ b/r2/r2/controllers/ipn.py @@ -35,7 +35,7 @@ from sqlalchemy.exc import IntegrityError import stripe from r2.controllers.reddit_base import RedditController -from r2.lib.filters import _force_unicode +from r2.lib.filters import _force_unicode, _force_utf8 from r2.lib.log import log_text from r2.lib.strings import strings from r2.lib.utils import randstr, tup @@ -788,6 +788,62 @@ class CoinbaseController(GoldPaymentController): return status, passthrough, transaction_id, pennies, months +class RedditGiftsController(GoldPaymentController): + """Handle notifications of gold purchases from reddit gifts. + + Payment is handled by reddit gifts. Once an order is complete they can hit + this route to apply gold to a user's account. + + The post should include data in the form: + { + 'transaction_id', transaction_id, + 'goldtype': goldtype, + 'buyer': buyer name, + 'pennies': pennies, + 'months': months, + ['recipient': recipient name,] + ['giftmessage': message,] + ['signed': bool,] + } + + """ + + name = 'redditgifts' + webhook_secret = g.RG_SECRET + event_type_mappings = {'succeeded': 'succeeded'} + + def process_response(self): + data = request.POST + + transaction_id = 'RG%s' % data['transaction_id'] + pennies = int(data['pennies']) + months = int(data['months']) + status = 'succeeded' + + buyer_name = data['buyer'] + goldtype = data['goldtype'] + + buyer = Account._by_name(buyer_name) + + blob = { + 'goldtype': goldtype, + 'account_id': buyer._id, + 'account_name': buyer.name, + 'status': 'initialized', + } + + if goldtype == 'gift': + blob['recipient'] = data['recipient'] + giftmessage = data.get('giftmessage', None) + blob['giftmessage'] = _force_utf8(giftmessage) + signed = data.get('signed') + blob['signed'] = True if signed == 'True' else False + + passthrough = generate_blob(blob) + + return status, passthrough, transaction_id, pennies, months + + class GoldException(Exception): pass