ttrig / laravel-billmate
Laravel package for working with Billmate API
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.0.1
- illuminate/support: ^10.0
Requires (Dev)
- graham-campbell/testbench: ^6.0
- laravel/pint: ^1.10
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^10.0
README
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
- https://developer.billmate.se/checkout-documentation
- https://developer.billmate.se/api-integration/getpaymentinfo
- https://developer.billmate.se/api-integration/initcheckout
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.
- Fork the Project
- Create your Feature Branch (
git checkout -b amazing-feature
) - Commit your Changes (
git commit -m 'Add some amazing feature
) - Push to the Branch (
git push origin amazing-feature
) - Open a Pull Request
License
laravel-billmate is open-sourced software licensed under the MIT license.