Now documenting more of what we need to do next with stripe if we go for a fullblown implementation

pull/1/head
Raymond Yee 2012-08-16 17:24:18 -07:00
parent 0d88a395cb
commit e81a25b1bd
3 changed files with 87 additions and 2 deletions

70
payment/stripelib.py Normal file
View File

@ -0,0 +1,70 @@
import stripe
# should load the keys for Stripe from db -- but for now just hardcode here
try:
from regluit.core.models import Key
STRIPE_PK = Key.objects.get(name="STRIPE_PK").value
STRIPE_SK = Key.objects.get(name="STRIPE_SK").value
logger.info('Successful loading of STRIPE_*_KEYs')
except Exception, e:
STRIPE_PK = 'pk_0AnIkNu4WRiJYzxMKgruiUwxzXP2T'
STRIPE_SK = 'sk_0AnIvBrnrJoFpfD3YmQBVZuTUAbjs'
stripe.api_key = STRIPE_SK
# if you create a Customer object, then you'll be able to charge multiple times. You can create a customer with a token.
# https://stripe.com/docs/tutorials/charges
def create_customer (card=None, description=None, email=None, account_balance=None, plan=None, trial_end=None):
"""card is a dictionary or a token"""
# https://stripe.com/docs/api?lang=python#create_customer
customer = stripe.Customer.create(
card=card,
description=description,
email=email,
account_balance=account_balance,
plan=plan,
trial_end=trial_end
)
# customer.id is useful to save in db
return customer
# if customer.id doesn't exist, create one and then charge the customer
# we probably should ask our users whether they are ok with our creating a customer id account -- or ask for credit
# card info each time....
def create_charge(amount, currency="usd", customer=None, card=None, description=None ):
# https://stripe.com/docs/api?lang=python#create_charge
# customer or card required but not both
# charge the Customer instead of the card
# amount in cents
charge = stripe.Charge.create(
amount=int(100*amount), # in cents
currency=currency,
customer=customer.id,
description=description
)
return charge
def refund_charge(id):
# https://stripe.com/docs/api?lang=python#refund_charge
ch = stripe.Charge.retrieve(id)
ch.refund()
return ch
def list_all_charges(count=None, offset=None, customer=None):
# https://stripe.com/docs/api?lang=python#list_charges
return stripe.Charge.all(count=count, offset=offset, customer=customer)
# key entities: Charge, Customer, Token, Event
# IPNs/webhooks: https://stripe.com/docs/webhooks
# charge object: https://stripe.com/docs/api?lang=python#charge_object
# need to study to figure out db schema
# all events
# https://stripe.com/docs/api?lang=python#list_events

View File

@ -12,7 +12,7 @@
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey('pk_0AnIkNu4WRiJYzxMKgruiUwxzXP2T');
Stripe.setPublishableKey('{{STRIPE_PK}}');
</script>
<script type="application/x-javascript">

View File

@ -3,6 +3,7 @@ from regluit.payment.paypal import IPN
from regluit.payment.models import Transaction
from regluit.core.models import Campaign, Wishlist
from regluit.payment.stripelib import STRIPE_PK
from regluit.payment.forms import StripePledgeForm
from django.conf import settings
@ -332,6 +333,20 @@ class StripeView(FormView):
template_name="stripe.html"
form_class = StripePledgeForm
def get_context_data(self, **kwargs):
context = super(StripeView, self).get_context_data(**kwargs)
context.update({
'STRIPE_PK':STRIPE_PK
})
return context
def form_valid(self, form):
stripeToken = form.cleaned_data["stripeToken"]
return HttpResponse("stripeToken: {0}".format(stripeToken))
# e.g., tok_0C0k4jG5B2Oxox
#
return HttpResponse("stripeToken: {0}".format(stripeToken))