diff --git a/VERSION b/VERSION index c74f875b..e3e33a3f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.6.4-stable+timestamp.2013.10.01.15.24.15 +Version 2.6.4-stable+timestamp.2013.10.02.11.05.35 diff --git a/gluon/contrib/stripe.py b/gluon/contrib/stripe.py index 6383f6b3..136405fc 100644 --- a/gluon/contrib/stripe.py +++ b/gluon/contrib/stripe.py @@ -1,13 +1,13 @@ import urllib import simplejson - +from hashlib import sha1 class Stripe: """ Usage: key='' d = Stripe(key).charge( - amount=100, + amount=100, # 1 dollar!!!! currency='usd', card_number='4242424242424242', card_exp_month='5', @@ -17,10 +17,27 @@ class Stripe: print d print Stripe(key).check(d['id']) print Stripe(key).refund(d['id']) + Sample output (python dict): {u'fee': 0, u'description': u'test charge', u'created': 1321242072, u'refunded': False, u'livemode': False, u'object': u'charge', u'currency': u'usd', u'amount': 100, u'paid': True, u'id': u'ch_sdjasgfga83asf', u'card': {u'exp_month': 5, u'country': u'US', u'object': u'card', u'last4': u'4242', u'exp_year': 2012, u'type': u'Visa'}} - if paid is True than transaction was processed + + Use in WEB2PY (guaranteed PCI compliant) + +def pay(): + from gluon.contrib.stripe import StripeForm + form = StripeForm( + pk=STRIPE_PUBLISHABLE_KEY, + sk=STRIPE_SECRET_KEY, + amount=150, # $1.5 (amount is in cents) + description="Nothing").process() + if form.accepted: + payment_id = form.response['id'] + redirect(URL('thank_you')) + elif form.errors: + redirect(URL('pay_error')) + return dict(form=form) + """ URL_CHARGE = 'https://%s:@api.stripe.com/v1/charges' @@ -31,7 +48,7 @@ class Stripe: self.key = key def charge(self, - amount, + amount, # in cents currency='usd', card_number='4242424242424242', card_exp_month='5', @@ -69,6 +86,163 @@ class Stripe: params) return simplejson.loads(u.read()) +class StripeForm(object): + def __init__(self, + pk, sk, + amount, # in cents + description, + currency = 'usd', + currency_symbol = '$', + security_notice = True, + disclosure_notice = True, + template = None): + from gluon import current + if not current.request.is_local or current.request.is_https: + redirect(URL(args=current.request.args,scheme='https')) + self.pk = pk + self.sk = sk + self.amount = amount + self.description = description + self.currency = currency + self.currency_symbol = currency_symbol + self.security_notice = security_notice + self.disclosure_notice = disclosure_notice + self.template = template or TEMPLATE + self.accepted = None + self.errors = None + self.signature = sha1(repr((self.amount,self.description))).hexdigest() + + def process(self): + from gluon import current + request = current.request + if request.post_vars: + if self.signature == request.post_vars.signature: + self.response = Stripe(self.sk).charge( + token=request.post_vars.stripeToken, + amount=self.amount, + description=self.description, + currency=self.currency) + if self.response.get('paid',False): + self.accepted = True + return self + self.errors = True + return self + + def xml(self): + from gluon.template import render + if self.accepted: + return "Your payment was processed successfully" + elif self.errors: + return "There was an processing error" + else: + context = dict(amount=self.amount, + signature=self.signature, pk=self.pk, + currency_symbol=self.currency_symbol, + security_notice=self.security_notice, + disclosure_notice=self.disclosure_notice) + return render(content=self.template, context=context) + + +TEMPLATE = """ + + + +

Secure Payment with Stripe.com

+

Payment Amount: {{=currency_symbol}} {{="%.2f" % amount}}

+ +
+ +
+ +
+ +
+
+ +
+ + +
+ +
+ +
+ (MM) + / + (YYYY) +
+
+ + +
+
+ + +
+
+ +
+ +{{if security_notice or disclosure_notice:}} +
+ {{if security_notice:}} +

Security Notice

+

For your security we process all payments using a service called Stripe. Thanks to Stripe your credit card information is communicated directly between your Web Browser and the payment processor, Stripe, without going through our server. Since we never see your card information nobody can steal it through us. Stripe is PCI compliant and so are we.

+ {{pass}} + {{if disclosure_notice:}} +

Disclosure Notice

+ +

We do store other information about your purchase including your name, a description of the purchase, the time when it was processed, and the amount paid. This information is necessary to provide our services and for accounting purposes. We do not disclose this information to third parties unless required to operate our services or accounting purposes.

+ {{pass}} +
+{{pass}} +""" + if __name__ == '__main__': key = raw_input('user>') d = Stripe(key).charge(100)