GithubHelp home page GithubHelp logo

helloflaskstripe's Introduction

Stripe Payments Demo — Python Server

Deploy to DO

This demo uses a simple Flask application as the server.

Payments Integration

  • app.py contains the routes that interface with Stripe to create PaymentIntents and receive webhook events.
  • setup.py a simple setup script to make some fake Products and SKUs for our Stripe store.
  • tests/tests.py some unit tests that test the logic of our heavier APIs like /webhook.
  • test_data.py contains some hardcoded mocked responses to test with.
  • inventory.py a minimal wrapper over the Stripe Python SDK that handles creating/fetching products and caluclating payment amounts. You can override this class with your own product and order management system code.

Requirements

You’ll need the following:

  • Python 3.6.5
  • Modern browser that supports ES6 (Chrome to see the Payment Request, and Safari to see Apple Pay).
  • Stripe account to accept payments (sign up for free!)

Getting Started

Before getting started, check to see that you have the right version of Python installed (3.6.5).

python3 --version

If your machine is running Python 2 as the default then you can follow these great guides on installing Python 3 for macOS, Windows, or Linux.

Once you have Homebrew and Python 3 set up, copy the example environment variables file .env.example from the root of the repo into your own environment file called .env:

cp .env.example .env

Update your .env file with your own Stripe API keys and any other configuration details you might want to add. The env variables are managed via the python-dotenv package.

Create a virtual environment to manage the packages and state our application needs:

cd server/python
python3 -m venv env
source env/bin/activate

Run pip install to fetch the Python packages we use:

pip install -r requirements.txt

Export our Flask app and run!

export FLASK_APP=./app.py
flask run

You should now see it running on http://127.0.0.1:5000/

Testing Webhooks

We can use the Stripe CLI to forward webhook events to our local development server:

  • Install the Stripe CLI.
  • Follow the login steps to connect the CLI with your Stripe account.
  • Run the listen command to forward the webhooks to localhost:
stripe listen --forward-to http://localhost:5000/webhook

Note: You do not need to configure any webhook endpoints in your Dashboard to receive webhooks with the CLI.

The Stripe CLI will let you know that webhook forwarding is ready and output your webhook signing secret:

> Ready! Your webhook signing secret is whsec_xxx

Please copy the webhook signing secret (whsec_xxx) to your .env file.

Tests

You can find tests for the API located in the tests directory. Make sure you're running in the virtual environment you created earlier.

source env/bin/activate
cd tests
python tests.py

Credits

helloflaskstripe's People

Contributors

robhagopian avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.