mollie-api-php icon indicating copy to clipboard operation
mollie-api-php copied to clipboard

Mollie not returning right status via webhook

Open bosmanrja opened this issue 8 months ago • 4 comments

Ik installed Mollie and finally got the payment part working. I only don't get the selected returnstatus via Webhook. I assume I made some misstakes. Can anybody advise?

  • With payment I can make a payment and it works.
  • After payment in the testmode I can choose a return status but it always stays 'open'.
  • I write values in a SQL database i.s.o. text file.

Thanks in advance.

payment:

try {
	/*
	 * Initialize the Mollie API library with your API key.
	 *
	 * See: https://www.mollie.com/beheer/account/profielen/
	 *
	 * Testen met: include/initialize_test.php
	 * Live      : include/initialize.php
	 */
    require "./include/initialize_test.php";

	/*
	 * Generate a unique order id for this example. It is important to include this unique attribute
	 * in the redirectUrl (below) so a proper return page can be shown to the customer.
	 */
	$orderId = $iBetID;

	/*
	 * Determine the url parts to these example files.
	 */
	$protocol = isset($_SERVER['HTTPS']) && strcasecmp('off', $_SERVER['HTTPS']) !== 0 ? "https" : "http";
 	$hostname = $_SERVER['HTTP_HOST'];
 	$path = dirname($_SERVER['REQUEST_URI'] ?? $_SERVER['PHP_SELF']);

    /*
     * Payment parameters:
     *   amount        Amount in EUROs. This example creates a € 10,- payment.
     *   description   Description of the payment.
     *   redirectUrl   Redirect location. The customer will be redirected there after the payment.
     *   webhookUrl    Webhook location, used to report when the payment changes state.
     *   metadata      Custom metadata that is stored with the payment.
     */
    $payment = $mollie->payments->create([
        "amount" => [
            "currency" => "EUR",
            "value" => $bedrag_m // You must send the correct number of decimals, thus we enforce the use of strings
        ],
        "description" => $omschrijving,
        "redirectUrl" => "{$protocol}://{$hostname}{$path}/../stap4.asp?iBetID={$orderId}",
        "webhookUrl" => "{$protocol}://{$hostname}{$path}/webhook.php",
        "metadata" => [
            "order_id" => $orderId,
        ],
    ]);

    /*
     * In this example we store the order with its payment status in a database.
     */
    database_write($orderId, $payment->status);

    /*
     * Send the customer off to complete the payment.
     * This request should always be a GET, thus we enforce 303 http response code
     */
    header("Location: " . $payment->getCheckoutUrl(), true, 303);
} catch (\Mollie\Api\Exceptions\ApiException $e) {
    echo "API call failed: " . htmlspecialchars($e->getMessage());

Webhook:

<?php
/*
 * How to verify Mollie API Payments in a webhook.
 *
 * See: https://docs.mollie.com/guides/webhooks
 */

try {
    /*
     * Initialize the Mollie API library with your API key.
     *
     * See: https://www.mollie.com/dashboard/developers/api-keys
	 * Testen met: include/initialize_test.php
	 * Live      : include/initialize.php
	 */
	require "../initialize_test.php";

	/*
	 * Retrieve the payment's current state.
	 */
	$payment = $mollie->payments->get($_POST["id"]);
	$orderId = $payment->metadata->order_id;

	/*
	 * Update the order in the database.
	 */
	database_write($orderId, $payment->status);

    if ($payment->isPaid() && ! $payment->hasRefunds() && ! $payment->hasChargebacks()) {
        /*
         * The payment is paid and isn't refunded or charged back.
         * At this point you'd probably want to start the process of delivering the product to the customer.
         */
    } elseif ($payment->isOpen()) {
        /*
         * The payment is open.
         */
    } elseif ($payment->isPending()) {
        /*
         * The payment is pending.
         */
    } elseif ($payment->isFailed()) {
        /*
         * The payment has failed.
         */
    } elseif ($payment->isExpired()) {
        /*
         * The payment is expired.
         */
    } elseif ($payment->isCanceled()) {
        /*
         * The payment has been canceled.
         */
    } elseif ($payment->hasRefunds()) {
        /*
         * The payment has been (partially) refunded.
         * The status of the payment is still "paid"
         */
    } elseif ($payment->hasChargebacks()) {
        /*
         * The payment has been (partially) charged back.
         * The status of the payment is still "paid"
         */
    }
} catch (\Mollie\Api\Exceptions\ApiException $e) {
    echo "API call failed: " . htmlspecialchars($e->getMessage());

Function database_write:

function database_write($orderId, $status)
{
	/* Initialisatie database gegevens */
	include "include/init_db.php";
	
	$conn = new mysqli($servername, $username, $password, $dbname);
	// Check connection
	if ($conn->connect_error){
		die("Connection failed: " . $conn->connect_error);
	}
	else {
		$sql = "SELECT * FROM Betaling WHERE Bet_ID = " . $orderId;

		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			// Update
			$sql = "UPDATE Betaling SET Bet_Status = '" . $status . "' WHERE Bet_ID = " . $orderId;
			if(mysqli_query($conn, $sql)) {
				echo "Record was updated successfully."; 
			}
			else {
				echo "ERROR: Could not able to execute $sql. "  
										. mysqli_error($conn); 
			}  
		} 
		else {
			echo "Geen record gevonden."; 
		}
	}
	$conn->close();
}

bosmanrja avatar Feb 13 '25 07:02 bosmanrja