-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gift certificates page with purchase, check, and redeem tabs
- Loading branch information
1 parent
031d44b
commit f5cfa2c
Showing
13 changed files
with
791 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
core/app/[locale]/(default)/gift-certificates/_actions/add-to-cart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use server'; | ||
|
||
import { revalidateTag } from 'next/cache'; | ||
import { cookies } from 'next/headers'; | ||
import { getFormatter, getTranslations } from 'next-intl/server'; | ||
|
||
import { addCartLineItem } from '~/client/mutations/add-cart-line-item'; | ||
import { createCartWithGiftCertificate } from '../_mutations/create-cart-with-gift-certificate'; | ||
import { getCart } from '~/client/queries/get-cart'; | ||
import { TAGS } from '~/client/tags'; | ||
|
||
const GIFT_CERTIFICATE_THEMES = ['GENERAL', 'BIRTHDAY', 'BOY', 'CELEBRATION', 'CHRISTMAS', 'GIRL', 'NONE']; | ||
type giftCertificateTheme = "GENERAL" | "BIRTHDAY" | "BOY" | "CELEBRATION" | "CHRISTMAS" | "GIRL" | "NONE"; | ||
|
||
export const addGiftCertificateToCart = async (data: FormData) => { | ||
const format = await getFormatter(); | ||
const t = await getTranslations('GiftCertificate.Actions.AddToCart'); | ||
|
||
let theme = String(data.get('theme')) as giftCertificateTheme; | ||
const amount = Number(data.get('amount')); | ||
const senderEmail = String(data.get('senderEmail')); | ||
const senderName = String(data.get('senderName')); | ||
const recipientEmail = String(data.get('recipientEmail')); | ||
const recipientName = String(data.get('recipientName')); | ||
const message = data.get('message') ? String(data.get('message')) : null; | ||
|
||
if (!GIFT_CERTIFICATE_THEMES.includes(theme)) { | ||
theme = 'GENERAL' | ||
} | ||
|
||
const giftCertificate = { | ||
name: t('certificateName', { | ||
amount: format.number(amount, { | ||
style: 'currency', | ||
currency: 'USD', // TODO: Determine this from the selected currency | ||
}) | ||
}), | ||
theme, | ||
amount, | ||
"quantity": 1, | ||
"sender": { | ||
"email": senderEmail, | ||
"name": senderName, | ||
}, | ||
"recipient": { | ||
"email": recipientEmail, | ||
"name": recipientName, | ||
}, | ||
message, | ||
} | ||
|
||
const cartId = cookies().get('cartId')?.value; | ||
let cart; | ||
|
||
try { | ||
cart = await getCart(cartId); | ||
|
||
if (cart) { | ||
cart = await addCartLineItem(cart.entityId, { | ||
giftCertificates: [ | ||
giftCertificate | ||
], | ||
}); | ||
|
||
if (!cart?.entityId) { | ||
return { status: 'error', error: t('error') }; | ||
} | ||
|
||
revalidateTag(TAGS.cart); | ||
|
||
return { status: 'success', data: cart }; | ||
} | ||
|
||
cart = await createCartWithGiftCertificate([giftCertificate]); | ||
|
||
if (!cart?.entityId) { | ||
return { status: 'error', error: t('error') }; | ||
} | ||
|
||
cookies().set({ | ||
name: 'cartId', | ||
value: cart.entityId, | ||
httpOnly: true, | ||
sameSite: 'lax', | ||
secure: true, | ||
path: '/', | ||
}); | ||
|
||
revalidateTag(TAGS.cart); | ||
|
||
return { status: 'success', data: cart }; | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
return { status: 'error', error: error.message }; | ||
} | ||
|
||
return { status: 'error', error: t('error') }; | ||
} | ||
}; |
55 changes: 55 additions & 0 deletions
55
core/app/[locale]/(default)/gift-certificates/_actions/lookup-balance.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use server' | ||
|
||
import { getTranslations } from 'next-intl/server'; | ||
|
||
export async function lookupGiftCertificateBalance(code: string) { | ||
const t = await getTranslations('GiftCertificate.Actions.Lookup'); | ||
|
||
if (!code) { | ||
return { error: t('noCode') } | ||
} | ||
|
||
const apiUrl = `https://api.bigcommerce.com/stores/${process.env.BIGCOMMERCE_STORE_HASH}/v2/gift_certificates` | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'X-Auth-Token': process.env.GIFT_CERTIFICATE_V3_API_TOKEN ?? '', | ||
'Accept': 'application/json' | ||
} | ||
|
||
try { | ||
const response = await fetch(`${apiUrl}?limit=1&code=${encodeURIComponent(code)}`, { | ||
method: 'GET', | ||
headers: headers | ||
}) | ||
|
||
if (response.status === 404 || response.status === 204) { | ||
return { error: t('notFound') } | ||
} | ||
|
||
if (!response.ok) { | ||
console.error(`v2 Gift Certificate API responded with status ${response.status}: ${response.statusText}`) | ||
return { error: t('error') } | ||
} | ||
|
||
const data = await response.json() | ||
|
||
if (Array.isArray(data) && data.length > 0 && typeof data[0].balance !== 'undefined') { | ||
// There isn't a way to query the exact code in the v2 Gift Certificate API, | ||
// so we'll loop through the results to make sure it's not a partial match | ||
for (const certificate of data) { | ||
if (certificate.code === code) { | ||
return { balance: parseFloat(data[0].balance), currencyCode: data[0].currency_code } | ||
} | ||
} | ||
|
||
// No exact match, so consider it not found | ||
return { error: t('notFound') } | ||
} else { | ||
console.error('Unexpected v2 Gift Certificate API response structure') | ||
return { error: t('error') } | ||
} | ||
} catch (error) { | ||
console.error('Error checking gift certificate balance:', error) | ||
return { error: t('error') } | ||
} | ||
} |
Oops, something went wrong.