Skip to content

Mollie API client wrapper for Laravel & Mollie Connect provider for Laravel Socialite

License

Notifications You must be signed in to change notification settings

mollie/laravel-mollie

Repository files navigation

Mollie

Laravel-Mollie

Laravel-Mollie incorporates the Mollie API and Mollie Connect into your Laravel or Lumen project.

Accepting iDEAL, Bancontact/Mister Cash, SOFORT Banking, Creditcard, SEPA Bank transfer, SEPA Direct debit, Bitcoin, PayPal, Belfius Direct Net, KBC/CBC, paysafecard, ING Home'Pay, Giftcards, Giropay and EPS online payments without fixed monthly costs or any punishing registration procedures. Just use the Mollie API to receive payments directly on your website or easily refund transactions to your customers.

Build Status Latest Stable Version Total Downloads

Requirements

  • Get yourself a free Mollie account. No sign up costs.
  • Now you're ready to use the Mollie API client in test mode.
  • Follow a few steps to enable payment methods in live mode, and let us handle the rest.
  • Up-to-date OpenSSL (or other SSL/TLS toolkit)
  • PHP >= 7.0
  • Laravel (or Lumen) >= 5.5
  • Laravel Socialite >= 3.0 (if you intend on using Mollie Connect)

Upgrading from v1.x?

To support the enhanced Mollie API, some breaking changes were introduced. Make sure to follow the instructions in the package migration guide.

Not ready to upgrade? The Laravel-Mollie client v1 will remain supported for now.

Fresh install? Continue with the installation guide below.

Installation

Add Laravel-Mollie to your composer file via the composer require command:

$ composer require mollie/laravel-mollie:2.0.*

Or add it to composer.json manually:

"require": {
    "mollie/laravel-mollie": "^2.0"
}

Laravel-Mollie's service providers will be automatically registered using Laravel's auto-discovery feature.

Configuration

You'll only need to add the MOLLIE_KEY variable to your .env file.

MOLLIE_KEY=test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Mollie Connect with Laravel Socialite

If you intend on using Mollie Connect, update config/services.php by adding this to the array:

'mollie' => [
    'client_id' => env('MOLLIE_CLIENT_ID', 'app_xxx'),
    'client_secret' => env('MOLLIE_CLIENT_SECRET'),
    'redirect' => env('MOLLIE_REDIRECT_URI'),
],

Then add the corresponding credentials (MOLLIE_CLIENT_ID, MOLLIE_CLIENT_SECRET, MOLLIE_REDIRECT_URI) to your .env file.

To make sure Laravel Socialite can actually find the Mollie driver, use the following code snippet and paste it in the boot() method of your AppServiceProvider.php.

Socialite::extend('mollie', function ($app) {
    $config = $app['config']['services.mollie'];

    return Socialite::buildProvider('Mollie\Laravel\MollieConnectProvider', $config);
});

Usage

Here you can see an example of just how simple this package is to use.

Mollie API

$payment = Mollie::api()->payments()->create([
    'amount' => [
        'currency' => 'EUR',
        'value' => '10.00', // You must send the correct number of decimals, thus we enforce the use of strings
    ],
    "description" => "My first API payment",
    "redirectUrl" => "https://webshop.example.org/order/12345/",
]);

$payment = Mollie::api()->payments()->get($payment->id);

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

Mollie Connect with Laravel Socialite

Route::get('login', function () {
    return Socialite::with('mollie')
        ->scopes(['profiles.read']) // Additional permission: profiles.read
        ->redirect();
});

Route::get('login_callback', function () {
    $user = Socialite::with('mollie')->user();

    Mollie::api()->setAccessToken($user->token);

    return Mollie::api()->profiles()->all(); // Retrieve all payment profiles available on the obtained Mollie account
});

Mollie Recurring

Here you can see an example of how easy it is to use Mollie recurring payments.

Create a customer

First of all you need to create a new customer (step 1), this is pretty straight forward

$customer = Mollie::api()->customers()->create([
    "name"  => "John Doe",
    "email" => "john@doe.com",
]);

Initial Payment

After creating the user, you can start a payment (step 3), it's important to set sequenceType to first, this will generate a mandate on Mollie's end that can be used to do direct charges. Without setting the method the payment screen of Mollie will display your methods that support recurring payments.

$payment = Mollie::api()->payments()->create([
    'amount' => [
        'currency' => 'EUR',
        'value' => '25.00', // You must send the correct number of decimals, thus we enforce the use of strings
    ],
    'customerId'    => $customer->id,
    'sequenceType' => 'first',
    'description'   => 'My Initial Payment',
    'redirectUrl'   => 'https://domain.com/return',
]);

Direct Charge

After doing the initial payment, you may charge the users card/account directly. Make sure there's a valid mandate connected to the customer. In case there are multiple mandates at least one should have status set to valid. Checking mandates is easy:

$mandates = Mollie::api()->mandates()->listFor($customer);

If any of the mandates is valid, charging the user is a piece of cake. Make sure sequenceType is set to recurring.

 $payment = Mollie::api()->payments()->create([
    'amount' => [
        'currency' => 'EUR',
        'value' => '25.00', // You must send the correct number of decimals, thus we enforce the use of strings
    ],
    'customerId'    => $customer->id,
    'sequenceType' => 'recurring',
    'description'   => 'Direct Charge',
]);

Like any other payment, Mollie will call your webhook to register the payment status so don't forget to save the transaction id to your database.

Possible problems

Webhook cannot be reached, because of CSRF protection

The VerifyCsrfToken middleware, which is included in the web middleware group by default, is the troublemaker if your webhook route is in the same middleware group in the app/Http/routes.php file.

Route::post('webhooks/mollie', function ($paymentId) { /** Your logic... */ });

You can exclude URIs from the CSRF protection in the app/Http/Middleware/VerifyCsrfToken.php file:

/**
 * The URIs that should be excluded from CSRF verification.
 *
 * @var array
 */
protected $except = [
    'webhooks/mollie'
];

If this solution does not work, open an issue so we can assist you.

Roadmap

You can find the latest development roadmap for this package here. Feel free to open an issue if you have a feature request.

Want to help us make our Laravel module even better?

Want to help us make our Laravel module even better? We take pull requests, sure. But how would you like to contribute to a technology oriented organization? Mollie is hiring developers and system engineers. Check out our vacancies or get in touch.

License

BSD (Berkeley Software Distribution) License. Copyright (c) 2016, Mollie B.V.

Support

Contact: www.mollie.cominfo@mollie.com — +31 20-612 88 55

About

Mollie API client wrapper for Laravel & Mollie Connect provider for Laravel Socialite

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages