Stripe is a popular software platform for online payments. Since Open Event allows the event organizers to sell tickets, an option to accept payments through Stripe is extremely beneficial to the organizer. Stripe allows accepting payments on other’s behalf using Connect. Connect is the Stripe’s full stack solution for platforms that need to process payments and process to multiple parties. This blog post goes over how Event organizers are able to link their Stripe accounts in order to accept payments later.
Registering the platform
The Admin of the Open Event Server will create an account on Stripe and register the platform. Upon creating the account he/she will get a secret_key and publishable_key. Moreover on registering the platform a client_id will be provided. These keys are saved in the application settings and only the Admin is authorized to view or change them.
Connecting the Organiser’s account
The Open Event Frontend has a wizard for creating an Event. It provides the organiser an option to connect his/her Stripe account in order to accept payments.
Upon clicking the following button, the organiser is directed to Stripe where he/she can fill the required details.
The button directs the organizer to the following URL:
The above URL has the following parameters:
- client_id – The client ID acquired when registering your platform.required.
- response_type – Response type. The value is always code. required.
- redirect_uri – The URL to redirect the customer to after authorization.
- scope – We need it to be read_write in order to be able to charge on behalf of the customer later.
After successfully entering the required details, the organizer is redirected to the redirect_url as specified in the above URL with a query parameter named as authorization_code. The Frontend sends this code to the Server using the Stripe Authorization endpoint which will be discussed in detail below.
Fetching Tokens from Stripe
The Server accepts the authorization_code by exposing the Stripe Authorization endpoint. It then uses it to fetch organizer’s details and token from Stripe and stores it for future use.
The schema for Stripe Authorization is extremely simple. We require the client to send an authorization_code which will be used to fetch the details. Stripe_publishable_key of the event organizer is exposed via the endpoint and will be used by the Frontend later.
We use the Requests library in order to fetch the results. First we fetch the client_id that we had stored in the application settings using a helper method called get_credentials. We then use it along with the authorization_code in order to make a POST request to Stripe Connect API. The full method is given below for reference.
We call the above method before creating the object using the before_create_object method of Marshmallow which allows us to do data preprocessing and validations.
If the request was a success, the response from Stripe connect API includes all the details necessary to accept payments on their behalf. We add these fields to the data and save it in the database.
In case there was an error, an error_description would be returned. This error_description is sent back to the frontend and shown to the event organizer.
After successfully fetching the results, we save it inside the database and return the stripe_publishable_key which will be used by the Frontend when charging the ticket buyers later.
Lastly we can go over the Stripe Authorization model as well. The stripe_secret_key will be used to charge the customers later.
- Flask: http://flask.pocoo.org/
- Stripe Connect: https://stripe.com/docs/connect/quickstart
- Marshmallow: http://marshmallow.readthedocs.io/en/latest/index.html