Python and Stripe: Implementing an online payment system in e-shop.

Maciej Januszewski
Inside the Tech by SoftServe
4 min readApr 26, 2021

Are you wondering about your own business? Already have a website? If so, you’ve come to the right place! In this article I’ll tell you how to implement Stripe — one of the most popular online payment platforms.

Pssst… #python on the board!

At the beginning, let me introduce you one of the most popular online payment platforms — Stripe. It operates in over 44 countries and is constantly expanding its activities to new countries. It allows you to accept transfers, settlements, payments, and also provides a ready interface for user management. You, as the owner, do not store users card / bank account details — Stripe provides full support and relieves you of any responsibility for sensitive data.

www.stripe.com
www.stripe.com

It is also worth saying that Stripe operates on the basis of a commission model — depending on the country, the appropriate amount is charged for a successful debiting of the card (in Poland it is 1.40% + PLN 1 for each transaction).

For you, as a seller, using online payments means charging commissions on transactions: transfers, card payments etc. In a word, you earn.

And most of all, Stripe provides an API, written in #python. Let’s get down to the implementation!

Stripe implementation

Let’s assume we keep basic information about the users in database (we’ll create tables in Django). The user model (User), in addition to the profile (UserProfile), will also have a stripe_id field in which we will store the unique user ID generated by Stripe.

Having previously defined models, we can create test users:

We have all the information we need, we’ve created two users and we can start working with Stripe.

Creating Accounts

Everything really depends on what type of account we would like to associate our user with. If our user orders services (Employer), we create a Customer account:

On the other hand, when our user will provide services (Employee — contractor), we create a Connected Account:

In both cases, Stripe’s API returns an unique ID (id or stripe_id), thanks to whom we can identify our users.

If we create a Customer account, the ID is preceded by the prefix “cus_xxx” and in the case of Connected Account the prefix is “acct_xxx”.

While the Customer account does not require additional steps in the context of registration, in order to have a fully verified Connected Account, you must:

  • accept the Stripe policy:

We provide here the current time (date) and the user’s IP address (ip) in the form of a timestamp.

  • attach the documents confirming identity (passport, ID card or driving license):

In this example, we submit a passport for verification — in addition to the front photo of the document, the same object must also be submitted as an additional document. Otherwise, the verification will fail…

These are all of the required steps when it comes to customer registration. It’s time to add your card to your Customer account:

First of all, we need to add a new payment source to the Customer (token which contains encoded information about the card). Then we need to set the previously created payment method as the default (SetupIntent).

Now we can try to withdraw the funds from Customer’s account:

In this example, the Customer’s card is debited by the value given in the amount variable. Our commission is application_fee_amount — here we have to enter an already calculated amount of commission that should go to our account.

transfer_data is responsible for the target account (where the funds for the order are to go) — in destination we enter the client’s Stripe ID (Connected Account). Then we need to confirm that the Customer card is charged (payment_intent_confirm) — as a result there will be a temporary amount blockage on the Customer’s card.

When we want to withdraw funds from the Customer card, we do:

After calling this method, funds should appear on the account (Connected Account).

To check its balance, enter:

In response we’ll receive information about the funds currently available on the account:

  • pending — in progress, which means that the funds are not yet available for withdrawal.
  • available — available, which means the funds can be withdrawn now.

Sometimes there may be a situation in which we will have to charge one of the users, e.g. for the lack of an order, and we have already charged the Customer’s card:

In this way, the contractor (Connected Account) can be charged with value specified in the amount. In a result we’ll reduce its current account balance.

And now we have to return the funds (Customer):

Summary

Stripe provides an API that allows us to quickly and easily build an online payment support system. Depending on our “business model”, we’re able to easily select properly implemented methods.

The API documentation is so transparent that, after proper reading, we can start writing a multi-line code that does all the work for us — of course, we need to properly test the logic we wrote to make sure that every transaction will be processed according to our expectations. This is can be achieved by test tokens, as well as test bank and card numbers, thanks to which we can perform real transactions.

After properly performed tests, we can switch from development to production mode. The only difference will be the API key that will be used for every request to Stripe.

--

--