Skip to content

Instantly share code, notes, and snippets.

@fijiaaron
Last active December 30, 2015 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fijiaaron/7835438 to your computer and use it in GitHub Desktop.
Save fijiaaron/7835438 to your computer and use it in GitHub Desktop.
Using a mock payment processor
# this is really just an interface, but python doesn't really have interfaces
class PaymentProcessor():
def make_payment(self, payment_info, amount):
raise NotImplementedError()
#any other helper methods & properties
# this technically implements payment processor, but doesn't do anything -- used for testing
class MockPaymentProcessor(PaymentProcessor):
def make_payment(self, payment_info, amount):
payment = Payment()
payment.status = self.status
return payment
# this is one way to fake the response
def always_return_status(self, status)
self.status = "APPROVED"
# you can implement this if you figure out how to work with paypal
class PaypalPaymentProcessor(PaymentProcessor):
def make_payment(self, payment_info, amount):
payment = Payment()
# TODO: implement whatever you need to do to actually call paypal and parse the response
return payment
class CloudTestEnvironmentApp():
#...
def purchase(self, payment_info, order):
#validate
#log
payment = pp.make_payment(payment_info, order.amount)
db.session.save(payment)
if (payment.status == "APPROVED"):
db.session.save(payment)
if (payment.status == "DECLINED"):
raise PaymentException()
# etc
return payment
db = Whatever()
pp = MockPaymentProcessor()
pp.always_return_status("APPROVED")
app = CloudTestEnviromentApp(config=None, db=db, payment_processor=pp)
customer = app.register(customer_info)
order = app.order(order_info, customer)
payment = app.purchase(payment_info, order)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment