Skip to content

Laravel package for interacting with Billmate API

License

Notifications You must be signed in to change notification settings

ttrig/laravel-billmate

Repository files navigation

Build Status Codecov Packagist Version License

Laravel Billmate

Laravel package for interacting with Billmate API.

Installation

composer require ttrig/laravel-billmate

Configuration

Publish the configuration file using this command:

php artisan vendor:publish --provider="Ttrig\Billmate\ServiceProvider"

Add a Billmate ID and key in .env.

BILLMATE_ID=123
BILLMATE_KEY=abc
BILLMATE_TEST=true

Update config/billmate.php to use your own controller(s).

'accept_action' => 'App\Http\Controllers\BillmateController@accept',
'cancel_action' => 'App\Http\Controllers\BillmateController@cancel',
'callback_action' => \Ttrig\Billmate\Controllers\CallbackController::class,

General payment flow

Usage example

Checkout

use Ttrig\Billmate\Article as BillmateArticle;
use Ttrig\Billmate\Service as BillmateService;

class CheckoutController extends Controller
{
    public function index(BillmateService $billmate)
    {
        $articles = collect()->push(new BillmateArticle([
            'title' => '1kg potatoes',
            'price' => 30,
        ]));

        $checkout = $billmate->initCheckout($articles);

        return view('checkout', compact('checkout'));
    }
}

You can view or update the data to be sent to Billmate by passing a callback as second argument to initCheckout.

$billmate->initCheckout($articles, function (&$data) {
    data_set($data, 'PaymentData.autoactivate', '1');
});

View

To render the Billmate Checkout iframe you can use $checkout->iframe() in your blade template or write your own iframe and pass $checkout->url to its src attribute.

JavaScript

To update height of the Checkout when it updates, we need this JavaScript.

window.addEventListener('message', function (event) {
    if (event.origin !== 'https://checkout.billmate.se') {
        return
    }

    try {
        var json = JSON.parse(event.data)
    } catch (e) {
        return
    }

    if (json.event === 'content_height') {
        $('#checkout').height(json.data)
    }
})

Redirect controller

You need your own controller(s) for handling the accept and cancel redirections.

use Ttrig\Billmate\Order as BillmateOrder;
use Ttrig\Billmate\Service as BillmateService;

class YourRedirectController extends Controller
{
    public function accept(BillmateService $billmate)
    {
        $order = new BillmateOrder(request()->data);

        $paymentInfo = $billmate->getPaymentInfo($order);

        return view('payment.accept');
    }

    public function cancel()
    {
        return view('payment.cancel');
    }
}

Callback controller

If you use Ttrig\Billmate\Controllers\CallbackController::class as your "callback_action" in config/billmate.php, you need to listen to the Ttrig\Billmate\Events\OrderCreated event in your EventServiceProvider to handle the order.

protected $listen = [
    \Ttrig\Billmate\Events\OrderCreated::class => [
        \App\Listeners\DoSomething::class,
    ],
];

Read more about events at https://laravel.com/docs/10.x/events.

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b amazing-feature)
  3. Commit your Changes (git commit -m 'Add some amazing feature)
  4. Push to the Branch (git push origin amazing-feature)
  5. Open a Pull Request

License

laravel-billmate is open-sourced software licensed under the MIT license.