PHP code example of mollie / mollie-api-php

1. Go to this page and download the library: Download mollie/mollie-api-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mollie / mollie-api-php example snippets


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

$order = $mollie->orders->create([
    "amount" => [
        "value" => "1027.99",
        "currency" => "EUR",
    ],
    "billingAddress" => [
        "streetAndNumber" => "Keizersgracht 313",
        "postalCode" => "1016 EE",
        "city" => "Amsterdam",
        "country" => "nl",
        "givenName" => "Luke",
        "familyName" => "Skywalker",
        "email" => "[email protected]",
    ],
    "shippingAddress" => [
        "streetAndNumber" => "Keizersgracht 313",
        "postalCode" => "1016 EE",
        "city" => "Amsterdam",
        "country" => "nl",
        "givenName" => "Luke",
        "familyName" => "Skywalker",
        "email" => "[email protected]",
    ],
    "metadata" => [
        "some" => "data",
    ],
    "consumerDateOfBirth" => "1958-01-31",
    "locale" => "en_US",
    "orderNumber" => "1234",
    "redirectUrl" => "https://your_domain.com/return?some_other_info=foo",
    "webhookUrl" => "https://your_domain.com/webhook",
    "method" => "ideal",
    "lines" => [
        [
            "sku" => "5702016116977",
            "name" => "LEGO 42083 Bugatti Chiron",
            "productUrl" => "https://shop.lego.com/nl-NL/Bugatti-Chiron-42083",
            "imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image//LEGO/42083_alt1?$main$',
            "quantity" => 2,
            "vatRate" => "21.00",
            "unitPrice" => [
                "currency" => "EUR",
                "value" => "399.00",
            ],
            "totalAmount" => [
                "currency" => "EUR",
                "value" => "698.00",
            ],
            "discountAmount" => [
                "currency" => "EUR",
                "value" => "100.00",
            ],
            "vatAmount" => [
                "currency" => "EUR",
                "value" => "121.14",
            ],
        ],
        // more order line items
    ],
]);

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

$order = $mollie->orders->get("ord_kEn1PlbGa");
$order->billingAddress->organizationName = "Mollie B.V.";
$order->billingAddress->streetAndNumber = "Keizersgracht 126";
$order->billingAddress->city = "Amsterdam";
$order->billingAddress->region = "Noord-Holland";
$order->billingAddress->postalCode = "1234AB";
$order->billingAddress->country = "NL";
$order->billingAddress->title = "Dhr";
$order->billingAddress->givenName = "Piet";
$order->billingAddress->familyName = "Mondriaan";
$order->billingAddress->email = "[email protected]";
$order->billingAddress->phone = "+31208202070";
$order->update();

$order = $mollie->orders->get('ord_8wmqcHMN4U');
$refund = $order->refundAll();

echo 'Refund ' . $refund->id . ' was created for order ' . $order->id;

$order = $mollie->orders->get('ord_8wmqcHMN4U');
$refund = $order->refund([
    'lines' => [
        [
            'id' => 'odl_dgtxyl',
            'quantity' => 1,
        ],
    ],
    "description" => "Required quantity not in stock, refunding one photo book.",
]);

$order = $mollie->orders->get("ord_pbjz8x");

if ($order->isCancelable) {
    $canceledOrder = $order->cancel();
    echo "Your order " . $order->id . " has been canceled.";
} else {
    echo "Unable to cancel your order " . $order->id . ".";
}

$payment = $mollie->payments->create([
    "amount" => [
        "currency" => "EUR",
        "value" => "10.00"
    ],
    "description" => "My first API payment",
    "redirectUrl" => "https://webshop.example.org/order/12345/",
    "webhookUrl"  => "https://webshop.example.org/mollie-webhook/",
]);

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

$payment = $mollie->payments->create([
    "amount" => [
        "currency" => "USD",
        "value" => "10.00"
    ],
    //...
]);

$method = $mollie->methods->get(\Mollie\Api\Types\PaymentMethod::IDEAL, ["

$payment = $mollie->payments->create([
    "amount" => [
        "currency" => "EUR",
        "value" => "10.00"
    ],
    "description" => "My first API payment",
    "redirectUrl" => "https://webshop.example.org/order/12345/",
    "webhookUrl"  => "https://webshop.example.org/mollie-webhook/",
    "method"      => \Mollie\Api\Types\PaymentMethod::IDEAL,
    "issuer"      => $selectedIssuerId, // e.g. "ideal_INGBNL2A"
]);

$payment = $mollie->payments->get($payment->id);

if ($payment->isPaid())
{
    echo "Payment received.";
}

$payments = $mollie->payments->page();

$payment = $mollie->payments->get($payment->id);

// Refund € 2 of this payment
$refund = $payment->refund([
    "amount" => [
        "currency" => "EUR",
        "value" => "2.00"
    ]
]);

/** @var $mollie \Mollie\Api\MollieApiClient */
$mollie->enableDebugging();

try {
    $mollie->payments->get('tr_12345678');
} catch (\Mollie\Api\Exceptions\ApiException $exception) {
    $request = $exception->getRequest();
}

/** @var $mollie \Mollie\Api\MollieApiClient */
$mollie->disableDebugging();
bash
composer