Laravel-Mollie incorporates Mollie Connect and the Mollie API into your Laravel or Lumen project.
- Laravel Socialite (if you intend on using Mollie Connect)
Add Laravel-Mollie to your composer file via the composer require
command:
$ composer require mollie/laravel-mollie
Or add it to composer.json
manually:
"require": {
"mollie/laravel-mollie": "~1.0"
}
Register the service provider by adding it to the providers
key in config/app.php
. Also register the facade by adding it to the aliases
key in config/app.php
.
'providers' => [
...
Mollie\Laravel\MollieServiceProvider::class,
],
'aliases' => [
...
'Mollie' => Mollie\Laravel\Facades\Mollie::class,
]
Make sure that Laravel Socialite service provider and facade are also registered in your configuration files, if you intend on using Mollie Connect.
To get started, you'll need to publish all vendor assets:
$ php artisan vendor:publish --provider="Mollie\Laravel\MollieServiceProvider"
This will create a config/mollie.php
file in your app that you can modify to set your configuration.
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'),
],
To make sure Laravel Socialite can actually find the Mollie driver, use the following code snippet and paste it in the boot()
method from your AppServiceProvider.php
.
Socialite::extend('mollie', function ($app) {
$config = $app['config']['services.mollie'];
return Socialite::buildProvider('Mollie\Laravel\MollieConnectProvider', $config);
});
Here you can see an example of just how simple this package is to use.
$payment = Mollie::api()->payments()->create([
"amount" => 10.00,
"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.";
}
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
});
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('mollie/webhook', 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 = [
'mollie/webhook'
];
If this solution does not work, open an issue so we can assist you.
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.
BSD (Berkeley Software Distribution) License. Copyright (c) 2016, Mollie B.V.
Contact: www.mollie.com — [email protected] — +31 20-612 88 55