A Laravel package for integrating Opn Payments (formerly Omise) using the omise-php SDK. This package provides a simple way to implement payments in your Laravel project with a ready-made payment form and automatic payment handling.
- Install Package
composer require necessarylion/opn-payments-laravel
- Generate vendor
php artisan vendor:publish --provider="OpnPayments\OpnPaymentsServiceProvider" --force
- Run Migration
php artisan migrate
- Register Event Listener (optional)
- Register
OpnPaymentCompleted
class inapp/Providers/EventServiceProvider.php
to handle your order, sending email etc...
public function boot()
{
...
Event::listen(
\OpnPayments\Events\OpnPaymentCompleted::class,
[\App\Listeners\OpnPaymentHandler::class, 'handle']
);
}
- Add credentials in .env file
OPN_MODE=test # test or live
OPN_TEST_PUBLIC_KEY=pkey_***
OPN_TEST_SECRET_KEY=skey_***
OPN_LIVE_PUBLIC_KEY=pkey_***
OPN_LIVE_SECRET_KEY=skey_***
- Make sure that
APP_URL
include port if you are running on port - Example
APP_URL=http://localhost:8000
Create charge using redirect url function
$payload = new OpnPaymentsRedirectPayload();
$payload->amount = 1000;
$payload->currency = OpnPaymentsCurrency::THAILAND_BAHT;
$payload->cancelUri = 'http://localhost:8000';
$payload->redirectUri = 'http://localhost:8000';
$payload->orderId = Str::uuid();
$payload->locale = OpnPaymentsLocale::ENGLISH;
$payload->paymentMethods = OpnPayments::paymentMethods();
return redirect(OpnPayments::getRedirectUrl($payload)->authorized_uri);
Fields | Type | Description |
---|---|---|
amount |
int |
Amount to charge |
currency |
string |
Currency of the amount eg. THB, SGD, RGN. You can use OpnPaymentsCurrency helper class for this field |
cancelUri |
string |
Url to redirect back if the user cancel payment |
redirectUri |
string |
Url to redirect back if the payment completed |
orderId |
string |
Unique order Id. |
paymentMethods |
array |
Array of payment methods. You can see list of supported methods here |
locale |
string |
Language, such ash , en, th, ja. You can use OpnPaymentsLocale helper class for this field |
metaData |
array |
Extra meta data to append. |
If you want to show list of products in payment page, you can do as below.
$payload->metaData = [
'product' => [
'image' => 'https://placehold.jp/75767a/ffffff/150x150.png'
'name' => 'I Phone',
'quantity' => '1',
'price' => '320000',
]
];
OR
$payload->metaData = [
'products' => [
[
'image' => 'https://placehold.jp/75767a/ffffff/150x150.png'
'name' => 'I Phone',
'quantity' => '1',
'price' => '320000',
]
]
];
Handle Completed Payment using OpnPaymentHandler
Listener
in app/Listeners/OpnPaymentHandler.php
, you can check the payment_successful
status of payment attempt.
to handle order, sending email etc..
public function handle(OpnPaymentCompleted $event) {
$attempt = $event->attempt;
$charge = $event->charge;
if ($attempt->payment_successful) {
// handle payment success here
} else {
// handle payment failed here
}
}
Register scheduler for pending charges.
This scheduler will get all pending charge from records withing 24 hours. Then it will fetch status from Opn API and update if success or failed.
In app/Console/Kernel.php
inside schedule()
function add below line.
$schedule->command('opn-payments-scheduler')->everyFiveMinutes();
Name | Description |
---|---|
OpnPayments::charge() |
To create/retrieve charge |
OpnPayments::account() |
To retrieve account information |
OpnPayments::capability() |
To retrieve capability data |
OpnPayments::card() |
To create/retrieve card |
OpnPayments::token() |
To create/retrieve token |
OpnPayments::refund() |
To create/retrieve refund |
OpnPayments::event() |
To create/retrieve event |
OpnPayments::source() |
To create/retrieve source |
OpnPayments::customer() |
To create/retrieve customer |
OpnPayments::receipt() |
To create/retrieve receipt |
OpnPayments::transfer() |
To create/retrieve transfer |
OpnPayments::balance() |
To create/retrieve balance |
Want to contribute? Great! Fork the repo and create PR to us.
- In your Laravel project
- Create packages folder
mkdir packages
- And clone our package
git clone git@github.com:necessarylion/opn-payments-laravel.git
{
...
"repositories": {
"opn-payments-laravel" : {
"type": "path",
"url" : "packages/opn-payments-laravel",
"options": {
"symlink": true
}
}
},
}
"require": {
...
"necessarylion/opn-payments-laravel" : "@dev"
},
composer update