Stripe - Create a customer.

104 views
Skip to first unread message

UG

unread,
Jan 16, 2015, 9:13:25 PM1/16/15
to web...@googlegroups.com
Hi,

i am a beginner in python and web2py.

i am using the stripe form in web2py to process credit cards. It is simple and all works when i make a test.

what i want to do is make what stripe calls a "customer" so that i can make a charge at a later time.
i tried to read through the stripe file in gluon but can not figure it out , Is there something i pass to the form instead of "amount" to get it to generate the customer token instead of a charge?

Or is it something else?

i would appreciate any direction on this.

Thanks


Massimo Di Pierro

unread,
Jan 17, 2015, 1:32:50 AM1/17/15
to web...@googlegroups.com
The StripeForm does not support it. Sorry. You have to use their API.

UG

unread,
Jan 18, 2015, 9:03:56 PM1/18/15
to web...@googlegroups.com

Thanks for the pointer.

It was quite easy but as a beginner it took me a while to figure this out, so i thought i would write what i did in the hope it helps someone else.

I followed the instructions in the documentation. https://stripe.com/docs

You need to install the API as described then you need to remove or rename the stripe.py found in gluon/contrib

in db.py i
import stripe
and added
stripe.api_key = "Replace_with_your_key"

I used the Custom form from the Stripe documents in my view.

Then in the controller i put the code from the example just a couple of small changes. You change the request.POST to request.post_vars. You also need to put this in an if loop.

def createcustomer():
     # Get the credit card details submitted by the form
    if request.post_vars:
        token = request.post_vars['stripeToken']

        # Create the charge on Stripe's servers - this will charge the user's card
        try:

            # Create a Customer
            customer = stripe.Customer.create(
                card=token,
                description="payin...@example.com"
            )
            
           # Charge the Customer instead of the card
               stripe.Charge.create(
               amount=1000, # in cents
               currency="usd",
               customer=customer.id
           )

            #Save Customer ID in your DB
            db.customer.insert(customer_id = customer.id )
            db.commit()

            # Redirect to view post
            session.flash = T("You Payed!")
            return redirect(URL('default', 'view'))

        except stripe.CardError, e:
          # The card has been declined
          response.flash = "Card Error"
          pass
   


I am a beginner, so there is probably a cleaner and better way, but the above worked for me. Hope it helps

Massimo Di Pierro

unread,
Jan 19, 2015, 5:51:51 PM1/19/15
to web...@googlegroups.com
this is nice and clean! :-)


On Sunday, 18 January 2015 20:03:56 UTC-6, UG wrote:

Thanks for the pointer.

It was quite easy but as a beginner it took me a while to figure this out, so i thought i would write what i did in the hope it helps someone else.

I followed the instructions in the documentation. https://stripe.com/docs

You need to install the API as described then you need to remove or rename the stripe.py found in gluon/contrib

in db.py i
import stripe
and added
stripe.api_key = "Replace_with_your_key"

I used the Custom form from the Stripe documents in my view.

Then in the controller i put the code from the example just a couple of small changes. You change the request.POST to request.post_vars. You also need to put this in an if loop.

def createcustomer():
     # Get the credit card details submitted by the form
    if request.post_vars:
        token = request.post_vars['stripeToken']

        # Create the charge on Stripe's servers - this will charge the user's card
        try:

            # Create a Customer
            customer = stripe.Customer.create(
                card=token,
                description="payinguser@example.com"
Reply all
Reply to author
Forward
0 new messages