mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 03:31:23 +00:00
Add ordercloud provider
This commit is contained in:
62
framework/ordercloud/api/endpoints/customer/card/add-item.ts
Normal file
62
framework/ordercloud/api/endpoints/customer/card/add-item.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { CustomerCardEndpoint } from '.'
|
||||
import type { OredercloudCreditCard } from '../../../../types/customer/card'
|
||||
|
||||
import Stripe from 'stripe'
|
||||
|
||||
const stripe = new Stripe(process.env.STRIPE_SECRET as string, {
|
||||
apiVersion: '2020-08-27',
|
||||
})
|
||||
|
||||
const addItem: CustomerCardEndpoint['handlers']['addItem'] = async ({
|
||||
res,
|
||||
body: { item, cartId },
|
||||
config: { restFetch },
|
||||
}) => {
|
||||
// Return an error if no item is present
|
||||
if (!item) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Missing item' }],
|
||||
})
|
||||
}
|
||||
|
||||
// Return an error if no item is present
|
||||
if (!cartId) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Cookie not found' }],
|
||||
})
|
||||
}
|
||||
|
||||
// Get token
|
||||
const token = await stripe.tokens
|
||||
.create({
|
||||
card: {
|
||||
number: item.cardNumber,
|
||||
exp_month: item.cardExpireDate.split('/')[0],
|
||||
exp_year: item.cardExpireDate.split('/')[1],
|
||||
cvc: item.cardCvc,
|
||||
},
|
||||
})
|
||||
.then((res: { id: string }) => res.id)
|
||||
|
||||
// Register credit card
|
||||
const creditCard = await restFetch('POST', `/me/creditcards`, {
|
||||
Token: token,
|
||||
CardType: 'credit',
|
||||
PartialAccountNumber: item.cardNumber.slice(-4),
|
||||
CardholderName: item.cardHolder,
|
||||
ExpirationDate: item.cardExpireDate,
|
||||
}).then((response: OredercloudCreditCard) => response.ID)
|
||||
|
||||
// Assign payment to order
|
||||
await restFetch('POST', `/orders/Outgoing/${cartId}/payments`, {
|
||||
Accepted: true,
|
||||
Type: 'CreditCard',
|
||||
CreditCardID: creditCard,
|
||||
})
|
||||
|
||||
return res.status(200).json({ data: null, errors: [] })
|
||||
}
|
||||
|
||||
export default addItem
|
@@ -0,0 +1,9 @@
|
||||
import type { CustomerCardEndpoint } from '.'
|
||||
|
||||
const getCards: CustomerCardEndpoint['handlers']['getCards'] = async ({
|
||||
res,
|
||||
}) => {
|
||||
return res.status(200).json({ data: null, errors: [] })
|
||||
}
|
||||
|
||||
export default getCards
|
27
framework/ordercloud/api/endpoints/customer/card/index.ts
Normal file
27
framework/ordercloud/api/endpoints/customer/card/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { CustomerCardSchema } from '../../../../types/customer/card'
|
||||
import type { OrdercloudAPI } from '../../..'
|
||||
|
||||
import { GetAPISchema, createEndpoint } from '@commerce/api'
|
||||
import customerCardEndpoint from '@commerce/api/endpoints/customer/card'
|
||||
|
||||
import getCards from './get-cards'
|
||||
import addItem from './add-item'
|
||||
import updateItem from './update-item'
|
||||
import removeItem from './remove-item'
|
||||
|
||||
export type CustomerCardAPI = GetAPISchema<OrdercloudAPI, CustomerCardSchema>
|
||||
export type CustomerCardEndpoint = CustomerCardAPI['endpoint']
|
||||
|
||||
export const handlers: CustomerCardEndpoint['handlers'] = {
|
||||
getCards,
|
||||
addItem,
|
||||
updateItem,
|
||||
removeItem,
|
||||
}
|
||||
|
||||
const customerCardApi = createEndpoint<CustomerCardAPI>({
|
||||
handler: customerCardEndpoint,
|
||||
handlers,
|
||||
})
|
||||
|
||||
export default customerCardApi
|
@@ -0,0 +1,9 @@
|
||||
import type { CustomerCardEndpoint } from '.'
|
||||
|
||||
const removeItem: CustomerCardEndpoint['handlers']['removeItem'] = async ({
|
||||
res,
|
||||
}) => {
|
||||
return res.status(200).json({ data: null, errors: [] })
|
||||
}
|
||||
|
||||
export default removeItem
|
@@ -0,0 +1,9 @@
|
||||
import type { CustomerCardEndpoint } from '.'
|
||||
|
||||
const updateItem: CustomerCardEndpoint['handlers']['updateItem'] = async ({
|
||||
res,
|
||||
}) => {
|
||||
return res.status(200).json({ data: null, errors: [] })
|
||||
}
|
||||
|
||||
export default updateItem
|
Reference in New Issue
Block a user