Skip to content

Commit

Permalink
Change the generic "PaymentProcessor" to "StripeBackend".
Browse files Browse the repository at this point in the history
  • Loading branch information
hjfreyer committed May 28, 2014
1 parent 70db3a3 commit 1a0cb29
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
32 changes: 13 additions & 19 deletions backend/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

'stripe_public_key',

# PaymentProcessor
'payment_processor',
# StripeBackend
'stripe_backend',

# MailingListSubscriber
'mailing_list_subscriber',
Expand All @@ -33,22 +33,13 @@
])


class PaymentProcessor(object):
"""Interface which processes payments."""
class StripeBackend(object):
"""Interface which contacts stripe."""

STRIPE = 'STRIPE'

def CreateCustomer(self, params):
"""Does whatever the payment processor needs to do in order to be able to
charge the customer later.
Args:
params: dict representing the JSON object send to the pledge creation
handler.
Returns: A dict giving the fields that will need to be included in the
Pledge model.
def CreateCustomer(self, email, card_token):
"""Creates a stripe customer so we can charge them later.
Returns: A string customer id.
"""
raise NotImplementedError()

Expand Down Expand Up @@ -114,11 +105,14 @@ def post(self):

# Do any server-side processing the payment processor needs.
stripe_customer_id = None
if PaymentProcessor.STRIPE in data['payment']:
customer = env.payment_processor.CreateCustomer(data)
stripe_customer_id = customer['customer_id']
if 'STRIPE' in data['payment']:
stripe_customer_id = env.stripe_backend.CreateCustomer(
email=data['email'], card_token=data['payment']['STRIPE']['token'])
else:
logging.warning('No payment processor specified: %s', data)
self.error(400)
return

pledge = model.addPledge(email=data['email'],
stripe_customer_id=stripe_customer_id,
amount_cents=data['amountCents'],
Expand Down
28 changes: 12 additions & 16 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,20 @@ def post(self, url_nonce):
return


class ProdPaymentProcessor(handlers.PaymentProcessor):
class ProdStripe(handlers.StripeBackend):
def __init__(self, stripe_private_key):
self.stripe_private_key = stripe_private_key

def CreateCustomer(self, request):
if 'STRIPE' not in request['payment']:
raise Error('STRIPE is the only supported payment platform')
def CreateCustomer(self, email, card_token):
stripe.api_key = self.stripe_private_key
customer = stripe.Customer.create(
card=request['payment']['STRIPE']['token'],
email=request['email'])
return dict(customer_id=customer.id)
customer = stripe.Customer.create(card=card_token, email=email)
return customer.id


class FakePaymentProcessor(handlers.PaymentProcessor):
def CreateCustomer(self, request):
logging.error('USING FAKE PAYMENT PROCESSOR')
return dict(customer_id='fake_1234')
class FakeStripe(handlers.StripeBackend):
def CreateCustomer(self, email, card_token):
logging.error('USING FAKE STRIPE')
return 'fake_1234'


class MailchimpSubscriber(handlers.MailingListSubscriber):
Expand All @@ -306,20 +302,20 @@ def GetEnv():
j = json.load(open('config.json'))
s = model.Secrets.get()

payment_processor = None
stripe_backend = None
mailing_list_subscriber = None
if j['appName'] == 'local':
payment_processor = FakePaymentProcessor()
stripe_backend = FakeStripe()
mailing_list_subscriber = FakeSubscriber()
else:
payment_processor = ProdPaymentProcessor(
stripe_backend = ProdStripe(
model.Config.get().stripe_private_key)
mailing_list_subscriber = MailchimpSubscriber()

return handlers.Environment(
app_name=j['appName'],
stripe_public_key=model.Config.get().stripe_public_key,
payment_processor=payment_processor,
stripe_backend=stripe_backend,
mailing_list_subscriber=mailing_list_subscriber,
mail_sender=ProdMailSender())

Expand Down
34 changes: 17 additions & 17 deletions unittests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def setUp(self):
self.testbed.init_datastore_v3_stub()

self.mockery = mox.Mox()
self.payment_processor = self.mockery.CreateMock(handlers.PaymentProcessor)
self.stripe = self.mockery.CreateMock(handlers.StripeBackend)
self.mailing_list_subscriber = self.mockery.CreateMock(
handlers.MailingListSubscriber)
self.mail_sender = self.mockery.CreateMock(handlers.MailSender)

self.env = handlers.Environment(
app_name='unittest',
stripe_public_key='pubkey1234',
payment_processor=self.payment_processor,
stripe_backend=self.stripe,
mailing_list_subscriber=self.mailing_list_subscriber,
mail_sender=self.mail_sender)
self.wsgi_app = webapp2.WSGIApplication(handlers.HANDLERS,
Expand Down Expand Up @@ -65,9 +65,9 @@ def testNotEnoughJson(self):
self.app.post_json('/r/pledge', dict(email='foo@bar.com'), status=400)

def testCreateAddsPledge(self):
self.payment_processor \
.CreateCustomer(self.samplePledge()) \
.AndReturn(dict(customer_id='cust_4321'))
self.stripe.CreateCustomer(email='pika@pokedex.biz',
card_token='tok_1234') \
.AndReturn('cust_4321')

self.mailing_list_subscriber \
.Subscribe(email='pika@pokedex.biz',
Expand Down Expand Up @@ -103,9 +103,9 @@ def assertEqualsSampleProperty(prop_name, actual):
assert not user.from_import

def testSubscribes(self):
self.payment_processor \
.CreateCustomer(self.samplePledge()) \
.AndReturn(dict(customer_id='cust_4321'))
self.stripe.CreateCustomer(email='pika@pokedex.biz',
card_token='tok_1234') \
.AndReturn('cust_4321')

self.mailing_list_subscriber \
.Subscribe(email='pika@pokedex.biz',
Expand All @@ -126,9 +126,9 @@ def testSubscribeOptOut(self):
sample = self.samplePledge()
sample['subscribe'] = False

self.payment_processor \
.CreateCustomer(sample) \
.AndReturn(dict(customer_id='cust_4321'))
self.stripe.CreateCustomer(email='pika@pokedex.biz',
card_token='tok_1234') \
.AndReturn('cust_4321')

self.mail_sender.Send(to=mox.IsA(str), subject=mox.IsA(str),
text_body=mox.IsA(str),
Expand All @@ -144,9 +144,9 @@ def testNoPhone(self):
sample = self.samplePledge()
sample['phone'] = ''

self.payment_processor \
.CreateCustomer(sample) \
.AndReturn(dict(customer_id='cust_4321'))
self.stripe.CreateCustomer(email='pika@pokedex.biz',
card_token='tok_1234') \
.AndReturn('cust_4321')

self.mailing_list_subscriber \
.Subscribe(email='pika@pokedex.biz',
Expand Down Expand Up @@ -175,9 +175,9 @@ def testMail(self):
sample = self.samplePledge()
sample['subscribe'] = False

self.payment_processor \
.CreateCustomer(sample) \
.AndReturn(dict(customer_id='cust_4321'))
self.stripe.CreateCustomer(email='pika@pokedex.biz',
card_token='tok_1234') \
.AndReturn('cust_4321')

self.mail_sender.Send(to='pika@pokedex.biz', subject='Thank you for your pledge',
text_body="""Dear Pik\xc3\xa1 Chu:
Expand Down

0 comments on commit 1a0cb29

Please sign in to comment.