0

I am making payments using mollie API in php. But its giving me some errors . I have posted my code and screenshot of the errors.

THIS IS MY CODE:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

try {
    require_once "vendor/autoload.php";

    $mollie = new \Mollie\Api\MollieApiClient();
    $mollie->setApiKey("test_XXXX");

    $orderId = time();

    $amount = $_POST['amount'];
    $payment = $mollie->payments->create([
        "amount" => [
            "currency" => "EUR",
            "value" => "$amount", // You must send the correct number of decimals, thus we enforce the use of strings
        ],
        "description" => "Order #{$orderId}",
        "redirectUrl" => "https://example.com/Mollie/return/".$orderId."/",
        "webhookUrl" => "https://example.com/Mollie/webhook/",
        "metadata" => [
            "order_id" => $orderId,
        ],
    ]);
    print_r($payment);

    $payment_data = array(
            'payment_id' => $payment->id,
            'user_id' => $_POST['user_id'],
            'order_id' => $orderId,
            'mode' => $payment->mode,
            'description' => $payment->description,
            'status' => $payment->status,
            'added_date' => date('Y-m-d'),
            'added_time' => date('H:i:s')
    );
    print_r($payment_data);

    header("Location: " . $payment->getCheckoutUrl(), true, 303);

} catch (\Mollie\Api\Exceptions\ApiException $e) {
    echo "API call failed: ". htmlspecialchars($e->getMessage());
}

THE SCREENSHOT:

  1. The first image shows "Uncaught SyntaxError: Unexpected token M in JSON at position 0" in RED color.
  2. The second image is the warning I am getting

First image

Second Image

Sayali
  • 11
  • 2
  • 1
    This is probably caused by a `print_r();` on line 31. You should not output before sending headers, it's a HTML thing, it wants the headers before the content. – KIKO Software Apr 14 '21 at 07:49
  • Thank you so much for your response. I will try removing it and will get back here if its working. :) – Sayali Apr 14 '21 at 08:02
  • KIKO Software- I have removed the print_r line and now that error has gone. But I don't know why $payment->getCheckoutUrl() is throwing 404 error. – Sayali Apr 14 '21 at 12:05
  • A 404 error means that the URL doesn't exist. There's no harm in checking which URL your code returns, before you actually implement the redirection. Another method to the check the URL is to use the developer tools of your browser. – KIKO Software Apr 14 '21 at 12:15

0 Answers0