Creating Stripe Payment Service

In this lesson, we're going to set up Stripe payment service

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:17] Now that we have set up Stripe, let's create a payment service, which will be responsible for interacting with the Stripe server. So inside the infrastructure project and inside services, let's create a new service and let's call it payment service.

    [00:18 - 00:28] To start off, let's create a constructor. After creating the constructor, we need to access the Stripe keys, which are inside the app settings.development.json file.

    [00:29 - 00:40] So we can use i configuration as a dependency. So let me mention i configuration and the name can be simply configuration.

    [00:41 - 00:49] And let's import it using Microsoft extensions configuration. And also let's initialize field from parameter.

    [00:50 - 01:02] Now below this, we will create an asynchronous function, which will return the payment intent. So what we can do is below this, let's write public async.

    [01:03 - 01:17] And since we are returning payment intent, so we can say task of type payment intent. And let's call this function payment intent async.

    [01:18 - 01:28] So payment intent async. And as a parameter, this will take a basket.

    [01:29 - 01:35] So let me write basket and the name can be basket. Let's import task using system threading tasks.

    [01:36 - 01:42] Let's import payment intent using Stripe. Let's import basket using entity.

    [01:43 - 01:53] Inside this function, we will write stripe configuration. So let's write stripe configuration.api key.

    [01:54 - 01:59] And this takes the client secret. So we can use configuration.

    [02:00 - 02:14] And then we have stripe property, which has secret key. Let's make sure we are doing it correctly.

    [02:15 - 02:30] So inside app settings, we have it inside stripe and client secret. And we have stripe and it should be client secret.

    [02:31 - 02:46] Below this, let's create a new service, which will be equal to new payment intent service. So new payment intent service.

    [02:47 - 02:51] And this is provided by Stripe as well. So we need intent.

    [02:52 - 03:08] So below this, let's write intent, which is equal to new payment intent. Now let's write the total cost of the basket.

    [03:09 - 03:23] So let me write var total and I can write basket. items.some. We can use the some method from system.link.

    [03:24 - 03:37] This will simply check for the items that are inside the basket. And from the item, it will go to the costs and from the cost to the price.

    [03:38 - 03:46] And let's import some using system.link. The some function is basically accumulating all the items and their price.

    [03:47 - 03:55] And it will give us total of all the products that are there inside our basket. Now we want to check if we have the payment intent ID already.

    [03:56 - 04:03] If we have it, then it means that we are updating the previous payment intent. Otherwise we'll have to create a new payment intent.

    [04:04 - 04:07] So that's two one thing. Let's check it.

    [04:08 - 04:23] So if string dot is empty, is not all empty. And basket dot payment intent ID.

    [04:24 - 04:29] So this condition is checking if it is empty. In this case, we will simply create a new payment intent.

    [04:30 - 04:48] So first of all, let's write some options, which will be equal to new payment intent create options. So new payment intent create options.

    [04:49 - 04:59] And it takes some properties such as amount and our amount is equal to total. And we see some error.

    [05:00 - 05:05] It says cannot implicitly convert type float to long. All right.

    [05:06 - 05:18] Stripe accepts payment in long format and as per the database, we are using float for storing the price. So what we can do is we can create a new total and that will be of type long.

    [05:19 - 05:23] Let's call it updated total. Now we have to type cast float into long.

    [05:24 - 05:42] So what we can do is type long and here you want to write total into hundred. Doing this are updated total has total converted into a long format.

    [05:43 - 05:51] And now instead of using total, we can use updated total and we don't see any error. This also takes currency.

    [05:52 - 06:03] So currency and let's keep it to USD and also the payment options. So let's write payment method types.

    [06:04 - 06:15] And for now we are not doing anything fancy. So just mention new list of string and for now let's only use card.

    [06:16 - 06:23] Let's import list using system collections generic. We are simply using card that does not mean we don't have other options.

    [06:24 - 06:32] So we do have a lot of other options, but let's keep it simple and use card for payments. Below this we will update our intent.

    [06:33 - 06:53] So here we can write intent is equal to await and we can use service dot create async and we will simply pass these options. Now this was the case when we did not have the payment intent ID.

    [06:54 - 07:02] But if we already have one, we can write an else condition. So after if we can write else.

    [07:03 - 07:09] In this case we will write the options again. So let me write options and is equal to new.

    [07:10 - 07:18] Here we will use payment intent update options. So let's write payment intent update options.

    [07:19 - 07:32] We need to pass the amount and let's call it updated total. Because if we already have a payment intent ID, which means we already have the currency and the payment methods configured.

    [07:33 - 07:42] And below this we just want to update it. So let's use await service dot update async.

    [07:43 - 07:57] Inside it just needs payment intent ID. So we can write basket dot payment intent ID and the options which simply has the amount.

    [07:58 - 08:10] And finally from this function we can return the intent. Now that we have created a service, we need to register it inside the startup class.

    [08:11 - 08:26] So let's go to the startup class. And below the token service, I can register services dot at scoped and will simply use the payment service.

    [08:27 - 08:29] And the next lesson let's start working on the payments controller.