Django

Charge Stripe in Actual Dollars, not Cents

Stripe is a great payments solutions for developers and merchants alike. With rich API documentation in varied programming languages, it is undoubtedly one of the best in the system.

I’m currently working on a project where Stripe payments are integrated. I’m using Python-cum-Django for the project.

Unfortunately (or maybe fortunately), Stripe charges only in cents. Thus, if you’ve come across a similar situation, you’ll realize users enter, say 5 dollars into your form, but it ends up showing in your Stripe dashboard as 0.50 dollars.

This is because Stripe charges in cents. It is great having enough time to test using the Stripe testing sandbox, to iron out kinda weirdness.

I’m using a simple workaround, you might find useful. Some code will do now, enough of the talking:

    # Snippet below enough to explain

    # from our form
    if request.method == 'POST':
        token = request.POST['stripeToken']
        amount = request.POST['donate']
        # Create the charge on Stripe's servers - this will charge the user's card (we're in testing sandbox, so no harm)
        try:
            customer = stripe.Customer.retrieve(customer_id)
            customer.sources.create(card=token)
            charge = stripe.Charge.create(
                # convert amount var above into integer,
                # multiply by 100, then make it a string again
                # Stripe servers expects the amount param to be a string
                # Done. 
                amount=str(int(amount)*100),
                currency="usd",
                customer=customer,
                description="Donated an amount of "
                            + amount + "to support the project"
                            + project.title + " by " + project.org.title,
                )
        ....
        except:
            ...

100 Cents make a dollar, thus if the user wants an amount of 5 dollars to be charged to the card, 5 dollars is equivalent to 500 cents. Stripe servers will understand 500 cents the way it should be understood.

No need for any complex conversions. Same should work for other currencies too.

Related Articles

Back to top button