tbclla / laravel-revolut-merchant
An unoffial Laravel wrapper for Revolut's Merchant API
Requires
- guzzlehttp/guzzle: ^6.5
Requires (Dev)
- orchestra/testbench: ^5.1
This package is auto-updated.
Last update: 2024-11-19 20:19:44 UTC
README
Laravel-Revolut (Merchant)
An unofficial Laravel wrapper for Revolut's Merchant API.
A sister package for Revolut's Open API for Business can be found here.
Requirements
- Laravel >=5.8
- PHP >=7.2
Getting Started
Follow this guide on how to install the package, and read Revolut's Merchant API documentation to learn more about the functionalities offered by the API.
⚠️ Please use a sandbox account when setting up this package, and only switch to your real-world account once you're happy that everything is working correclty.
Installing
Pull the package in through Composer
composer require tbclla/laravel-revolut-merchant
Service Provider & Facade
If you have disabled auto-discovery, add the service provider and facade to your config/app.php
.
'providers' => [ // ... tbclla\RevolutMerchant\Providers\RevolutMerchantServiceProvider::class, ], 'aliases' => [ // ... 'Merchant' => tbclla\RevolutMerchant\Facades\Merchant::class, ]
Publish the configuration (optional)
If you would like to publish this package's configuration to your own config directory, use the below artisan command.
php artisan vendor:publish --provider "tbclla\RevolutMerchant\Providers\RevolutMerchantServiceProvider"
Set environment variables
Add the following keys to your project's .env
file, as all of the configuration values are read from there.
❗Complete the REVOLUT_MERCHANT_API_KEY
with the API key from your merchant account.
REVOLUT_MERCHANT_SANDBOX=true
REVOLUT_MERCHANT_API_KEY=
That's it, you're all done.
How to use this package
To use the client, you can either instantiate a new tbclla\RevolutMerchant\Client
which accepts your API key, and whether or not to run in sandbox mode:
use tbclla\RevolutMerchant\Client; // sandbox $merchant = new Client('your_api_key', true); // production $merchant = new Client('your_api_key'); $merchant->order()->get($orderId);
Or you can use the facade, which will inject your environment values.
For brevity, all of the examples in this documentation are using the facade.
use tbclla\RevolutMerchant\Facades\Merchant; Merchant::order()->get($id);
Orders
Please refer to Revolut's official documentation on orders for additional information on how to use the orders endpoint.
Create an order
Merchant::order()->create([ "amount" => 200, "capture_mode" => "MANUAL", "merchant_order_id" => "00122", "customer_email" => "sally.gibson@gmail.com", "description" => "description", "currency" => "GBP", "settlement_currency" => "USD", "merchant_customer_id" => "sally01" ]);
Retrieve an order
$orderId = 'd41c46db-5f82-4dd7-8a22-a43ac517b6df'; Merchant::order()->get($orderId);
Capture an order
Merchant::order()->capture($orderId);
Cancel an order
Merchant::order()->cancel($orderId);
Refund an order
Merchant::order()->refund($orderId, [ "amount" => 100, "currency" => "GBP", "merchant_order_id" => "00122", "description" => null ]);
Web-Hooks
Read Revolut's official documentation about web-hooks to learn more.
Create the web-hook
Merchant::webhook()->create('https://myapp.com/webhook');
Revoke the web-hook
Merchant::webhook()->revoke();
Retrieve web-hooks
Merchant::webhook()->retrieve();
Revolut Checkout Widget API
To use the checkout widget, you have to embed a script from Revolut on any page that will use the checkout widget.
This package includes a @revolutMerchantScript
blade directive which will embed this script for you and set the correct source depending on your configured environment.
<head> <title>Checkout Page</title> @revolutMerchantScript </head> <body> ... </body>
Brief example
Below is a quick and dirty example to illustrate how to create a new payment order, and subsequently display a payment pop-up.
For this example, the logged in user sends an AJAX request to a /purchase
route, where a new payment order is created.
The payment order's public_id
is then returned and passed to RevolutCheckout()
.
PHP
Route::post('/purchase', function() { // Get the logged in user $user = Auth::user(); // Fetch the item being purchased $item = Item::find(request('item_id')); // Create a new payment order $paymentOrder = Merchant::order()->create([ "currency" => "GBP", "description" => $item->name, "amount" => $item->price, "customer_email" => $user->email, "merchant_customer_id" => $user->id, ]); // return the payment order's 'public_id' return $paymentOrder['public_id']; });
JavaScript
❗Don't forget that any page which intends to use RevolutCheckout()
must include the above mentioned checkout widget script before calling RevolutCheckout()
.
// the user triggers a request to the above route axios.post('/purchase', {"item_id": 123456}).then((response) => { // get the 'public_id' from the response let publicId = response.data; // pass the 'public_id' to RevolutCheckout() RevolutCheckout(publicId).then(function(instance) { // Launch the pop-up instance.payWithPopup({ onSuccess() { ... }, onError(message) { ... } }); }); });
License
This project is licensed under the MIT License - see the LICENSE.md file for details