This document will guide you through the prerequisites and commands necessary to setup and preview the portal project with complete Stripe integration, locally on your computer.
Within the packages
folder, you will find the three projects that make up this scenario:
portal
- The web app portalapi
- The web app backendstripe
- The Stripe backend
Note: This scenario has been optimised for use with GitHub Codespaces, a development environment hosted in the GitHub cloud, as we prepared predefined environment with everything installed.
The repo is a monorepo, and thus contains all the components from all the scenarios. To support this npm workspaces has been configured, and as a result, all scenarios packages will be installed.
To install all packages required run the following command:
npm install --workspaces
Note: The devcontainer will automatically execute this command on creation, but you can execute it manually if you wish to see the installation happen.
This scenario uses Stripe to handle payments. To use Stripe, you will need to create an account and obtain a set of API keys.
Note: it's possible to run the app without configuring Stripe at all: in that case, the payments will be mocked and accepted directly, so you can still test the flow.
- Create a free Stripe account here: https://dashboard.stripe.com/register
- Once you have created your account, you will be redirected to the Stripe dashboard. Click on the
Developers
menu item, and then click on theAPI keys
tab on the left. - Copy the
Public key
andSecret key
and paste them into a new.stripe.env
file in the root folder, like this:
STRIPE_PUBLIC_KEY=<your public key>
STRIPE_SECRET_KEY=<your secret key>
-
Click on the
Webhooks
tab on the left, and then select theAdd endpoint
button. For the endpoint URL, enterhttp://dummy.com
and choose theSelect events
option. Search forcheckout
, and enable the eventscheckout.session.completed
andcheckout.session.expired
. Then click on theAdd endpoint
button. Note that the endpoint URL will need to be updated later on. -
Copy the
Signing secret key
from your webhook and paste it into the.stripe.env
file, like this:
STRIPE_WEBHOOK_SECRET=<your webhook key>
Once your Stripe keys have been configured, you can provision the Azure resources required for this scenario. To do this, run the following command:
# Export the environment variables from the .stripe.env file
export $(cat .stripe.env | xargs)
# Login to azd (only required once the first time)
azd auth login
# Provision infrastructure and the azd development environment
azd provision
# Package the app using the environment variables in .azure/env + deploy the code on Azure
azd deploy
Except for the first command, the deployment commands are the same as the ones used in the Deploy to Azure guide.
After the deployment is complete, you will need to update the Stripe webhook endpoint URL with the one generated by the deployment.
-
First, run the following command to get the endpoint URL:
# Create a .env file with the environment variables from the azd environment azd env get-values > .env # Get the endpoint URL source .env && echo "Your Stripe webhook endpoint URL is:\n$SERVICE_STRIPE_URI/stripe-api/stripe/webhook"
-
Copy the resulting URL, and then go back to the Stripe dashboard, and select on the
Webhooks
tab on the left. -
Pick on the webhook you created earlier, click on the
...
button then selectUpdate details
. -
Replace the
http://dummy.com
value with the URL you copied earlier, and then click on theUpdate endpoint
button.
You should now have a fully working Stripe test integration.
Note: if you want to test the Stripe integration, you can use the Stripe test cards.
By default, the project is configured to skip the Stripe integration with a warning, to make it easier to run locally without having to configure Stripe.
If you want to run the Stripe integration locally, you need to edit the docker-compose.yml
file to add this line to the stripe service definition:
services:
stripe:
build:
context: .
dockerfile: ./packages/stripe/Dockerfile
environment:
API_URL: http://host.docker.internal:7071
ports:
- "4242:4242"
extra_hosts:
- "host.docker.internal:host-gateway"
# Add this line to enable the Stripe integration
env_file:
- .stripe.env
This will enable the Stripe integration, and use the .stripe.env
file you created to configure the Stripe keys.
If you want to debug the Stripe integration, you can use the Stripe CLI to forward the Stripe webhooks to your local machine. To do this, run the following command:
# Login to stripe (only required once the first time)
stripe login
# Forward the Stripe webhooks to your local machine
stripe listen --forward-to http://localhost:4242/stripe/webhook
At this point, the Stripe CLI will print a webhook secret key that's only valid for local testing.
Copy this key, and paste it into the .stripe.env
file, like this:
STRIPE_WEBHOOK_SECRET=<your local webhook key>
You can now run the project locally using the following command:
npm start
It will start the web app portal, the API, and the Stripe backend, and you can access the portal at http://localhost:4280.
You can now simulate Stripe events by using the Stripe CLI to send test events to your local machine. For example, to simulate a successful payment, you can run the following command:
stripe trigger checkout.session.completed
You should see the event being processed by your Stripe backend, and you can use it to debug your integration.