Skip to content

Commit

Permalink
Fixes order state not updating in the callback page
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomaskontolacr committed Sep 15, 2023
1 parent d43c913 commit 059cff0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/[locale]/callback/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import paytrailService from '@/utils/paytrail';

import { serverFetchAPI } from '@/lib/serverApi';
import { CallbackPageFields } from '@/utils/models';
import { updateOrderState } from '@/app/api/createPayment/route';

import Result from './Result';

Expand All @@ -18,6 +19,8 @@ const getContent = async (locale: string) => {
}
}



type Props = {
params: {
locale: string
Expand All @@ -28,6 +31,13 @@ type Props = {
const CallbackPage = async ({params: {locale}, searchParams}: Props) => {
const params = searchParams;
const isValid = paytrailService.verifyPayment(params);
if(isValid) {
await updateOrderState(
Number(params['checkout-reference']),
params['checkout-status'] || 'fail',
params['checkout-transaction-id'] || undefined,
);
}
const paymentStatus = params['checkout-status'] as CheckoutStatus;
const content = await getContent(locale);

Expand Down
8 changes: 4 additions & 4 deletions app/[locale]/signups/Component.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import Link from 'next/link';

import { fetchAPI } from '@/lib/api';
Expand Down Expand Up @@ -85,10 +87,8 @@ const Signups = ({content, categories, locale}: Props) => {
</div>
</div>)}
<div className='mt-4'>
<Link href="">
<a onClick={goBack} className='underline text-primary-900 dark:text-primary-100'>
{translation.back}
</a>
<Link onClick={goBack} className='underline text-primary-900 dark:text-primary-100' href="">
{translation.back}
</Link>
</div>

Expand Down
9 changes: 5 additions & 4 deletions app/api/verifyPaymentCallback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { NextRequest, NextResponse } from "next/server";
import paytrailService from "@/utils/paytrail";
import { updateOrderState } from "../createPayment/route";


// This is called by paytrail directly
export const GET = async (request: NextRequest) => {
if (request.method !== 'GET') return NextResponse.json({}, {status: 405});
const query = request.nextUrl.searchParams;
const queryObject = Object.fromEntries(query.entries())
try {
const result = paytrailService.verifyPayment(queryObject);
if (!result) return NextResponse.json({}, {status: 400});
await updateOrderState(
Number(query.get('checkout-reference')),
query.get('checkout-status') || 'fail',
query.get('checkout-transaction-id') || undefined,
Number(queryObject['checkout-reference']),
queryObject['checkout-status'] || 'fail',
queryObject['checkout-transaction-id'] || undefined,
);
return NextResponse.json({}, {status: 200});
} catch(error) {
Expand Down

0 comments on commit 059cff0

Please sign in to comment.