Using Stripe to Implement Payment handling on a website

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • capstone-project

  • Copy the link after Forwarding and head back over to your Stripe dashboard. From the developers tab you should find a tab for webhooks. Once there click on add endpoint. Then paste the link from the terminal into endpoint URL and add /webhook to the end. Now click on select events then checkout. Now toggle the event called checkout.session.completed, and select add endpoint. Now click on the endpoint you just created; under signing secret click reveal and copy the key. Same as before my key is stored as an environment variable, but you can just paste it in here as a string. Now we'll write the method to use the webhook. The only really important thing here is lines 67-75. This is where we retrieve the event object and add the relevant information to the receipts table. You can get a lot of information back from this depending on what you specify when you initially create the checkout session. Now you can test it out and see how it all works! Stripe provides you with a test card number you can use for the transactions so you don't unload your bank account trying to test an app. Feel free to leave any questions in the comments I'll be sure to get back to you, and checkout the repo used in the examples here!

  • Stripe

    PHP library for the Stripe API.

  • First you'll want to go to Stripe's website and setup an account. Then install Stripe with pip install stripe. In this example I'm using Flask for the backend and React for the frontend. The website is an e-commerce page for buying and selling skateboards. Now create some models for the product to sell and the receipts then run flask db init, flask db migrate, and flask db upgrade to create the database. My models look like so:

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • ngrok

    Expose your localhost to the web. Node wrapper for ngrok. (by bubenshchykov)

  • Here we defined a route for the session, and got the product from our database. Now that we have said product we can use the price id stripe provides to determined the amount the user will be charged. We also specified the client_reference_id so when we get the information back we'll know which user made the purchase. You can add a lot here from custom fields to shipping addresses, but for the purposes of this example we won't be needing all the extra fields. Now you'll need to setup a webhook to retrieve the data from stripe after the transaction. I used Ngrok, you can use any service you like. Stripe even offers its own CLI you can use, but I prefer ngrok. If you go with ngrok you'll need to create an account and download ngrok. Once you've done that unzip the file. Sometimes you'll run into issues if it isn't set as a windows environment variable. To set it as such copy the path for the ngrok application. Then in the windows search bar search edit environment variables. You should see something like this:

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts