Mollie webhook not found when trying to get payment status

41 Views Asked by At

I want to simulate a test payment with Mollie developer API. I don't like the Mollie documentation because I have to read a lot of stuff to end up confused.. So my question is: How can I check for my payment status so I can verify if something is payed?

You have to fill out a redirectUrl and a webhook url, but what does those urls..? When I land on my redirect page, I need to make an api call to my webhook so that one can communicate with Mollie. I'll show you some code

// Mollie initialisation //

export async function POST({ request }) {
 try {
 const requestBody = await request.json();
 const userId = requestBody.userId;
 const surveyName = requestBody.surveyTitle;
 const mollieClient = createMollieClient({ apiKey: MOLLIE });

 const payment = await mollieClient.payments.create({
 amount: {
 currency: 'EUR',
 value: '7.99'
          },
 description: `${surveyName} - ` + userId,
 redirectUrl: 'https://bap24.hosted-power.dev/payment-success',
 webhookUrl: 'https://bap24.hosted-power.dev/api/mollie/webhook',
 metadata: {
 userId: PROFILE_ID
          },
 method: ['ideal', 'bancontact', 'belfius', 'creditcard', 'paypal', 'paysafecard', 'sofort'] as PaymentMethod[],
        });
 const checkoutUrl = payment.getCheckoutUrl();
 return json({
 message: 'Checkout URL gegenereerd',
 success: true,
 checkoutUrl: checkoutUrl,
 paymentId: payment.id
    });
} catch (error) {
 console.error(error)
 return json({ message: error, success: true});
    }
}

// Redirect page //

<script lang="ts">
 import Header from "../../components/Header.svelte";
 import { navigateTo } from "../../stores";
 import type { PageData } from "../$types";
 export let data: PageData; 
 import { onMount } from "svelte";
 
 let paymentId;
 $: paymentId

 onMount(async () => {
 paymentId = sessionStorage.getItem('paymentId');
 if (paymentId) {
 await verifyPaymentStatus(paymentId);
        }
    });

 async function verifyPaymentStatus(paymentId) {
 const response = await fetch(`/api/check-payment/${paymentId}`);
 const result = await response.json();
 console.log(result);

 if (result.paymentStatus === 'paid') {
 localStorage.setItem("toast", JSON.stringify({ message: 'De betaling is succesvol!', type: 'success', timeout: 5000 }));
 navigateTo('/dashboard');
    } else {
 localStorage.setItem("toast", JSON.stringify({ message: 'De betaling is mislukt!', type: 'error', timeout: 5000 }));
    }
}
</script>

// Check payment api //

 import type { RequestHandler } from '@sveltejs/kit';
import { createMollieClient } from '@mollie/api-client';
import { MOLLIE } from "$lib/server/config";

const mollieClient = createMollieClient({ apiKey: MOLLIE });

export async function GET ({ params }) {
 const paymentId = params.paymentId;
 try {
 const payment = await mollieClient.payments.get(paymentId);
 return {
 status: 200,
 body: { paymentStatus: payment.status }
        };
    } catch (error) {
 console.error(error);
 return {
 status: 500,
 body: { message: 'Er is een fout opgetreden' }
        };
    }
};

// Webhook Url //

 import type { RequestHandler } from '@sveltejs/kit';
import { createMollieClient } from '@mollie/api-client';
import { MOLLIE } from "$lib/server/config";

const mollieClient = createMollieClient({ apiKey: MOLLIE });

export const POST: RequestHandler = async ({ request }) => {

 try {
 const body = await request.json();
 const paymentId = body.id;

 const payment = await mollieClient.payments.get(paymentId);
 if (payment.status === 'paid') {
 console.log('Payment received');
        }

 return {
 status: 200,
 body: { message: 'Webhook verwerkt' }
        };
    } catch (error) {
 console.error(error);
 return {
 status: 500,
 body: { error: 'Er is een fout opgetreden bij het verwerken van de webhook.' }
        };
    }
};
0

There are 0 best solutions below