Skip to content

Commit

Permalink
Fix middleware, paytrail locales
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomaskontolacr committed Sep 18, 2023
1 parent 7cc7271 commit ce88d98
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MERCHANT_ID=375917
SECRET_KEY=SAIPPUAKAUPPIAS
STRAPI_TOKEN=get-from-strapi
REDIRECT_SUCCESS=http://127.0.0.1:3000/fi/callback
REDIRECT_CANCEL=http://127.0.0.1:3000/fi/callback
URL=http://127.0.0.1:3000
REDIRECT_SUCCESS=/callback
REDIRECT_CANCEL=/callback
NEXT_PUBLIC_STRAPI_API_URL=http://localhost:1337
4 changes: 2 additions & 2 deletions app/[locale]/callback/Result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const CallbackResult = ({ locale, isValid, paymentStatus, content}: Props) => {
</Link>
</div>
else if (paymentStatus !== 'ok') result = <div>
{content.attributes.onCancel} <Link href={`/${locale}/summary`}>
<a className='text-primary-900 dark:text-primary-100 underline'>{translation.backToOrder}</a>
{content.attributes.onCancel} <Link className='text-primary-900 dark:text-primary-100 underline' href={`/${locale}/summary`}>
{translation.backToOrder}
</Link>
</div>
return (
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/contact/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Props = {
}
}

const ContactPage = ({params: {locale}}:Props) => {
const ContactPage = async ({params: {locale}}:Props) => {
return <Form locale={locale}/>
}

Expand Down
25 changes: 15 additions & 10 deletions app/[locale]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ type FrontPageFields = StrapiBaseType<{
}>;

export const getFrontpageFields = async (locale: string) => {
const content = await fetchAPI<FrontPageFields>('/front-page',{},{
try {
const content = await fetchAPI<FrontPageFields>('/front-page',{},{
locale,
});
return content;
return content;
}catch(error) {
return undefined;
}
}

type Props = {
Expand All @@ -28,18 +32,19 @@ type Props = {
const Home = async ({params: {locale}}: Props) => {
const translation = await getTranslation(locale);
const response = await getFrontpageFields(locale);
const {bodyText, showSignups} = response.attributes;
const bodyText = response?.attributes.bodyText || "";
const showSignups = response?.attributes.showSignups || false;
return (
<div className="container max-w-3xl bg-secondary-50 dark:bg-secondary-800 mx-auto rounded shadow-md mt-4 p-1 sm:p-8">
<main className='container mx-auto px-4'>
<ReactMarkdown
components={{
img: image => {
if(!image.src) return null;
return <Image src={getStrapiURL(image.src)} width={400} height={800} alt={image.alt || ""}></Image>
}
}}
className="prose dark:prose-invert prose-li:my-0.5 prose-ul:my-0.5 prose-secondary mt-0 mb-4">
components={{
img: image => {
if(!image.src) return null;
return <Image src={getStrapiURL(image.src)} width={400} height={800} alt={image.alt || ""}></Image>
}
}}
className="prose dark:prose-invert prose-li:my-0.5 prose-ul:my-0.5 prose-secondary mt-0 mb-4">
{bodyText}
</ReactMarkdown>
{showSignups &&
Expand Down
2 changes: 1 addition & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const middleware = async (request: NextRequest) => {
export const config = {
matcher: [
// Skip all internal paths (_next)
"/((?!api|_next|favicon.ico|[^.]+).*)",
"/((?!api|_next|favicon.ico).*)",
// Optional: only run on root (/) URL
// '/'
],
Expand Down
9 changes: 6 additions & 3 deletions utils/paytrail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type RefundBody = {
}

const PAYTRAIL_ENDPOINT = 'https://services.paytrail.com';
const URL = process.env.URL || '127.0.0.1:3000';
const MERCHANT_ID = process.env.MERCHANT_ID || '';
const SECRET_KEY = process.env.SECRET_KEY || '';
const REDIRECT_SUCCESS = process.env.REDIRECT_SUCCESS || '';
Expand All @@ -72,6 +73,8 @@ const calculateHmac = (
}

const generatePaymentBody = (order: Order, translation: Record<string,string>) => {
const locale = order.attributes.customer.data.attributes.locale;
const callbackUrl = `${URL}/${locale}/callback`;
const mappedCart = mappedItems(order.attributes.items.data,translation);
const items: PaymentBodyItem[] = mappedCart.map(item => {
return {
Expand All @@ -86,16 +89,16 @@ const generatePaymentBody = (order: Order, translation: Record<string,string>) =
reference: String(order.id),
amount: items.reduce((a,b)=> a + b.units * b.unitPrice,0),
currency: 'EUR',
language: 'FI',
language: locale.toLocaleUpperCase(),
items,
customer: {
firstName: order.attributes.customer.data.attributes.firstName,
lastName: order.attributes.customer.data.attributes.lastName,
email: order.attributes.customer.data.attributes.email,
},
redirectUrls: {
success: REDIRECT_SUCCESS,
cancel: REDIRECT_CANCEL,
success: callbackUrl,
cancel: callbackUrl,
},
}
if (CALLBACK_URL) {
Expand Down

0 comments on commit ce88d98

Please sign in to comment.