From the course: Next.js Ecommerce: Build a Shopping Platform from Scratch

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Create order and capture API requests

Create order and capture API requests

So now that we have a way to generate the access token and we've tested it, we're going to now add the ability to create an order. And we want to be able to capture payment. So those are going to go in our PayPal API file as well. Now to do the create order, we need to send a request to the slash v2 slash checkout slash orders. And what we want to send with it is an authorization header with a bearer and then the access token from the function that we created. Then in the body, we want to send a stringified object with the intent to capture. And then the purchase units, which would be an array of the product prices. So we're going to do this in our PayPal.ts, and we want to put it in this PayPal object. So let's go into the object here, and let's call this key is going to be create order. And then we're going to set that to an async function called createOrder. And then that's going to take in the price, and that'll be a number. Now, we need to get the token, so let's create a…

Contents