Giter VIP home page Giter VIP logo

laravel-mollie's Introduction

Mollie

Laravel-Mollie Build Status

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

Requirements

Installation

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.

Configuration

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.

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'),
],

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);
});

Usage

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

Mollie API

$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.";
}

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" => "[email protected]",
]);

Initial Payment

After creating the user, you can start a payment (step 3), it's important to set recurringType 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'        => 25.00,
    'customerId'    => $customer->id,
    'recurringType' => '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()->customersMandates()->withParentId($customer->id)->all();

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

 $payment = mollie::api()->payments()->create([
    'amount'        => 25.00,
    'customerId'    => $customer->id,
    'recurringType' => 'recurring',
    'description'   => 'Direct Charge',
]);

Like any other payments, 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('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?

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.com โ€” [email protected] โ€” +31 20-612 88 55

laravel-mollie's People

Contributors

faapz avatar joshuadegier avatar ptondereau avatar ricardodevries avatar juukie avatar adriaanmol avatar freekmurze avatar

Watchers

James Cloos avatar Mostafa Mohammad Reyad Rahman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.