Setting up Stripe

The project you cloned in the previous lesson is missing the appsettings.json file in the API project as this file is used to contain secret information so is excluded from the public git repository.

In the root of your API project folder create a new file called appsettings.json and paste in the following JSON code:

1{
2    "Logging": {
3      "LogLevel": {
4        "Default": "Information",
5        "Microsoft.AspNetCore": "Warning"
6      }
7    },
8    "StripeSettings": {
9      "PublishableKey": "pk_test_REPLACE_ME",
10      "SecretKey": "sk_test_REPLACE_ME",
11      "WhSecret": "whsec_REPLACE_ME"
12    },
13    "AllowedHosts": "*"
14  }

You will need to get the PublishableKey and the SecretKey from the dashboard after you have signed in:

Image

Copy both of these keys into the appsettings.json file.

For the WhSecret key, this is the one that is used for the Webhook that stripe will use to send the API an event when payment has been received. To get this and test the checkout system you will need to download and install the Stripe CLI.

Click on Developers → Webhooks tab from the Stripe dashboard.

Image

Click the button to “Test in a Local Environment”

Follow the instructions to download the CLI from here for your operating system and then login to stripe one you have this installed by running the following command (use a separate terminal tab for this so you can keep it running):

1stripe login

Once you are logged in we can start listening for Stripe events and forwarding them to the API server when they are received. There are 2 events we are interested in here:

  • payment_intent.succeeded
  • payment_intent.payment_failed

Run the following command to do this:

1stripe listen -f https://localhost:5001/api/payments/webhook -e payment_intent.succeeded,payment_intent.payment_failed

You should see the following output:

Image

You can now copy the key beginning with ‘whsec…’ and paste this into the appsettings.json file.

Restart the API server so it picks up the new configuration settings and then we can use the app. Add some products into the shopping cart, register a new user and follow the checkout process all the way through to the order. You can use a Stripe test card to pay for this:

  • Card Number: 4242 4242 4242 4242
  • Expiry date: Any date in the future
  • CVC: Any 3 digit number

Once you have completed the payment you should be taken to the checkout success page.

Image

If you are seeing this then congratulations, you have successfully set up and configured the completed application created on this course.