Mollie Payment Integration in Laravel 7
![](https://blog.codehunger.in/wp-content/uploads/2022/08/mollie-payment-integration-in-laravel-7_974.jpg)
Before moving towards the topic I want to tell you what is a payment gateway and how it is beneficial for our website.
What is the Payment Gateway?
A payment gateway is a merchant service provided by an e-commerce application service provider that authorizes credit card or direct payments processing for e-businesses, online retailers. Payment gateway like Stripe, Razorpay, Mollie, Authorize are very popular nowadays.
Benefits of Payment Gateway
- Secure transactions
- Expanded customer base
- Faster transaction processing
Requirements
- Mollie account
- The Basic knowledge of PHP
- The basic idea of Laravel Framework
Create a Mollie account by clicking on this link.
![mollie account creation](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-dashboard-signup-1584280767658-1024x493.png)
Login to Mollie dashboard in the sidebar you will see developers, click on developers to get your API Key.
![mollie api key](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-dashboard-org_7698331-developers-api-keys-1584280995582-1024x462.png)
Don’t forget to activate your payment method, as it gives errors during the payment. To activate the payment method you have to go settings -> website profile, then under the website profile, you can see the payment methods. You have to activate them.
![mollie payment method](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-dashboard-org_7698331-settings-profiles-1584281407839-1024x524.png)
![mollie payment method list](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-dashboard-org_7698331-settings-profiles-pfl_GfUFyE8VPn-payment-methods-1584281485653-618x1024.png)
Installation
Add Laravel-Mollie to your composer file via the composer require
command:
composer require mollie/laravel-mollie:^2.0
You can set the mollie API key in two way either by putting API key in you .env file like below
MOLLIE_KEY=test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Or by calling it in your constructor function like below.
public function __construct() {
Mollie::api()->setApiKey('test_FbVACj7UbsdkHtAUWnCnmSNUFHKuuA');
}
Add routes in your web.php file
Route::get('/mollie-payment','MollieController@preparePayment')->name('mollie.payment');
Route::get('/payment-success','MollieController@paymentSuccess')->name('payment.success');
Create a controller by using the following command
php artisan make:controller MollieController
Under your controller put the following code
<?php
namespace App\Http\Controllers;
use Mollie\Laravel\Facades\Mollie;
class MollieController extends Controller
{
public function __construct() {
Mollie::api()->setApiKey('test_FbVACj7UbsdkHtAUWnCnmSNGFWMuuA'); // your mollie test api key
}
/**
* Redirect the user to the Payment Gateway.
*
* @return Response
*/
public function preparePayment()
{
$payment = Mollie::api()->payments()->create([
'amount' => [
'currency' => 'EUR', // Type of currency you want to send
'value' => '10.00', // You must send the correct number of decimals, thus we enforce the use of strings
],
'description' => 'Payment By codehunger',
'redirectUrl' => route('payment.success'), // after the payment completion where you to redirect
]);
$payment = Mollie::api()->payments()->get($payment->id);
// redirect customer to Mollie checkout page
return redirect($payment->getCheckoutUrl(), 303);
}
/**
* Page redirection after the successfull payment
*
* @return Response
*/
public function paymentSuccess() {
echo 'payment has been received';
}
}
Visit the below URL
http://localhost/mollie/public/mollie-payment
After visiting on the URL, you are redirected to the Mollie payment page
![mollie payment page](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-payscreen-select-method-6q3a5Mgn69-1584282473696-1024x524.png)
Here all your payment methods will be shown, which you activated in your profile. You can choose any payment method to make the payment.
![mollie payment](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-paymentscreen-testmode-1584282799737-1024x770.png)
Now you can click on paid for your successful payment. You can see your all payment details under your transaction details.
![mollie payment dashboard](https://blog.codehunger.in/wp-content/uploads/2020/03/screencapture-www-mollie-com-dashboard-org_7698331-payments-1584283065848-1024x158.png)
Still have questions in mind feel free to ask, we are here to help you.
Still having confusion feel free to ask, we are here to help you
How can I check the payment status is paid or not. I have to insert all the details in database once I will get the status.
for that you have to add webhook url during the payment request like below
$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” => “Order #12345”,
“redirectUrl” => route(‘order.success’),
“webhookUrl” => route(‘webhooks.mollie’),
“metadata” => [
“order_id” => “12345”,
],
]);
the webhook url must be https in order to listen,in the webhook function you can write the code as below
public function handleWebhookNotification(Request $request) {
$paymentId = $request->input(‘id’);
$payment = Mollie::api()->payments->get($paymentId);
}