This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python implementation of the server (#14)
- Loading branch information
1 parent
4058c96
commit e5d9288
Showing
19 changed files
with
801 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Stripe Payments Demo — Node Server | ||
|
||
This directory contains the main Node implementation of the payments server. | ||
|
||
## Requirements | ||
|
||
You’ll need the following: | ||
|
||
* [Node.js](http://nodejs.org) >= 8.x. | ||
* Modern browser that supports ES6 (Chrome to see the Payment Request, and Safari to see Apple Pay). | ||
* Stripe account to accept payments ([sign up](https://dashboard.stripe.com/register) for free). | ||
|
||
In your Stripe Dashboard, you can [enable the payment methods](https://dashboard.stripe.com/payments/settings) you’d like to test with one click. | ||
|
||
Some payment methods require receiving a real-time webhook notification to complete a charge. This demo is bundled with [ngrok](https://ngrok.com/), which allows us to securely receive webhooks and serve the app locally via HTTPS, which is also required to complete transactions in the browser with Elements or the Payment Request API. | ||
|
||
## Getting Started | ||
|
||
Copy the environment variables file from the root of the repository: | ||
|
||
cp .env.example .env | ||
|
||
Update `.env` with your own [Stripe API keys](https://dashboard.stripe.com/account/apikeys) and any other configuration details. These environment variables are loaded and used in [`server/node/config.js`](/server/node/config.js), where you can review and edit other options such as the app currency and your Stripe account country. | ||
|
||
Install dependencies using npm: | ||
|
||
npm install | ||
|
||
This demo uses the Stripe API as a datastore for products and orders, but you can always choose to use your own datastore instead. When starting the app for the first time, the initial loading can take a couple of seconds as it will automatically set up the products within Stripe. | ||
|
||
Run the app: | ||
|
||
npm run start | ||
|
||
Two public ngrok URLs will be provided when the app starts. The first URL should be used to [setup webhooks](https://dashboard.stripe.com/account/webhooks) in your Stripe Dashboard. For example: | ||
|
||
https://<example>.ngrok.io/webhook | ||
|
||
The second URL will serve our app via HTTPS. For example: | ||
|
||
https://<example>.ngrok.io | ||
|
||
Use this second URL in your browser to start the demo. | ||
|
||
**Don’t want to use ngrok?** As long as you serve the app over HTTPS and that Stripe can reach the webhook endpoint via a public URL, all the payment flows will work. | ||
|
||
**Want to test a hosted version of this app with your own Stripe account?** You can deploy an instance of this app on Heroku and set up your own API keys: | ||
|
||
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) | ||
|
||
## Credits | ||
|
||
* Code: [Romain Huet](https://twitter.com/romainhuet) and [Thorsten Schaeff](https://twitter.com/schaeff_t) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Python environment files | ||
venv | ||
env | ||
|
||
# Python compiled resources + JetBrains | ||
__pycache__/ | ||
.pytest_cache/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web gunicorn app:app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Stripe Payments Demo — Python Server | ||
|
||
This demo uses a simple [Flask](http://flask.pocoo.org/) application as the server. | ||
|
||
## Payments Integration | ||
|
||
* [`app.py`](app.py) contains the routes that interface with Stripe to create charges and receive webhook events. | ||
* [`setup.py`](setup.py) a simple setup script to make some fake Products and SKUs for our Stripe store. | ||
* [`tests/tests.py`](tests/tests.py) some unit tests that test the logic of our heavier APIs like `orders/<string:id>/pay` and `/webhook`. | ||
* [`test_data.py`](tests/tests.py) contains some hardcoded mocked responses to test with. | ||
* [`inventory_manager.py`](stripe_lib.py) a minimal wrapper over the Stripe Python SDK that handles creating/fetching orders and products. You can override this class with your own order management system code. | ||
|
||
## Requirements | ||
|
||
You’ll need the following: | ||
|
||
* [Python 3.6.5](https://www.python.org/downloads/release/python-365/) | ||
* Modern browser that supports ES6 (Chrome to see the Payment Request, and Safari to see Apple Pay). | ||
* Stripe account to accept payments ([sign up](https://dashboard.stripe.com/register) 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](http://docs.python-guide.org/en/latest/starting/install3/osx/), [Windows](http://docs.python-guide.org/en/latest/starting/install3/win/), or [Linux](http://docs.python-guide.org/en/latest/starting/install3/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](https://dashboard.stripe.com/account/apikeys) and any other configuration details you might want to add. The env variables are managed via the [python-dotenv](https://github.com/theskumar/python-dotenv) package. | ||
|
||
Create a [virtual environment](https://docs.python.org/3/tutorial/venv.html) 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/`](http://127.0.0.1:5000/) | ||
|
||
### Testing Webhooks | ||
|
||
If you want to test [receiving webhooks](https://stripe.com/docs/webhooks), we recommend using ngrok to expose your local server. | ||
|
||
First [download ngrok](https://ngrok.com) and start your Flask application. | ||
|
||
[Run ngrok](https://ngrok.com/docs). Assuming your Flask application is running on the default port 5000, you can simply run ngrok in your Terminal in the directory where you downloaded ngrok: | ||
|
||
``` | ||
./ngrok http 5000 | ||
``` | ||
|
||
ngrok will display a UI in your terminal telling you the new forwarding address for your Flask app. Use this URL as the URL to be called in your developer [webhooks panel.](https://dashboard.stripe.com/account/webhooks) | ||
|
||
Don't forget to append `/webhook` when you set up your Stripe webhook URL in the Dashboard. Example URL to be called: `http://75795038.ngrok.io/webhook`. | ||
|
||
## 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. | ||
|
||
``` | ||
souce env/bin/activate | ||
cd tests | ||
python tests.py | ||
``` | ||
|
||
## Credits | ||
|
||
* Code: [Adrienne Dreyfus](http://twitter.com/adrind) |
Oops, something went wrong.