Use token at checkout

This commit is contained in:
goncy
2021-09-30 15:59:59 -03:00
parent 19a5e22302
commit 8c2212a580
2 changed files with 22 additions and 5 deletions

View File

@@ -1,9 +1,10 @@
import type { CheckoutEndpoint } from '.' import type { CheckoutEndpoint } from '.'
const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
req,
res, res,
body: { cartId }, body: { cartId },
config: { restBuyerFetch }, config: { restBuyerFetch, tokenCookie },
}) => { }) => {
// Return an error if no item is present // Return an error if no item is present
if (!cartId) { if (!cartId) {
@@ -13,15 +14,22 @@ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
}) })
} }
// Get token from cookies
const token = req.cookies[tokenCookie]
// Register credit card // Register credit card
const payments = await restBuyerFetch( const payments = await restBuyerFetch(
'GET', 'GET',
`/orders/Outgoing/${cartId}/payments` `/orders/Outgoing/${cartId}/payments`,
null,
{ token }
).then((response: { Items: unknown[] }) => response.Items) ).then((response: { Items: unknown[] }) => response.Items)
const address = await restBuyerFetch( const address = await restBuyerFetch(
'GET', 'GET',
`/orders/Outgoing/${cartId}` `/orders/Outgoing/${cartId}`,
null,
{ token }
).then( ).then(
(response: { ShippingAddressID: string }) => response.ShippingAddressID (response: { ShippingAddressID: string }) => response.ShippingAddressID
) )

View File

@@ -1,9 +1,10 @@
import type { CheckoutEndpoint } from '.' import type { CheckoutEndpoint } from '.'
const submitCheckout: CheckoutEndpoint['handlers']['submitCheckout'] = async ({ const submitCheckout: CheckoutEndpoint['handlers']['submitCheckout'] = async ({
req,
res, res,
body: { cartId }, body: { cartId },
config: { restBuyerFetch }, config: { restBuyerFetch, tokenCookie },
}) => { }) => {
// Return an error if no item is present // Return an error if no item is present
if (!cartId) { if (!cartId) {
@@ -13,8 +14,16 @@ const submitCheckout: CheckoutEndpoint['handlers']['submitCheckout'] = async ({
}) })
} }
// Get token from cookies
const token = req.cookies[tokenCookie]
// Submit order // Submit order
await restBuyerFetch('POST', `/orders/Outgoing/${cartId}/submit`, {}) await restBuyerFetch(
'POST',
`/orders/Outgoing/${cartId}/submit`,
{},
{ token }
)
// Return cart and errors // Return cart and errors
res.status(200).json({ data: null, errors: [] }) res.status(200).json({ data: null, errors: [] })