1. Go to this page and download the library: Download lahirulhr/laravel-payhere library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
return [
/*
PayHere action url. usually,
for production https://www.payhere.lk
for testing https://sandbox.payhere.lk
remember to update api when production
* */
'api_endpoint' => env('PAYHERE_API'),
/*
PayHere merchant ID can be found in their dashboard
https://www.payhere.lk/account/settings/domain-credentials
* */
'merchant_id' => env('PAYHERE_MERCHANT_ID'),
/*
Merchant Secret is specific to each App/Domain. it can be generated for your domain/app as follows
https://www.payhere.lk/account/settings/domain-credentials
*Click 'Add Domain/App' > Fill details > Click 'Request to Allow'
*Wait for the approval for your domain
*Copy the Merchant Secret for your domain/app to .env file
* */
'merchant_secret' => env('PAYHERE_MERCHANT_SECRET'),
/*
Follow PayHere official instructions to obtain 'app_id' and 'app_secret'.
NOTE: you dont need to generate "Authorization code". it will be automatically generate by this package
*Sign in to your PayHere account & go to Settings > Business Apps section
*Click 'Create App' button & enter an app name & comma seperated domains to whilelist
*Tick the permission 'Payment Retrieval API'
*Click 'Add Business App' button to create the app
*Once the app is created click 'View Credential' button in front of the created app
*Copy the 'App ID' & 'App Secret' values
* */
'app_id' => env('PAYHERE_APP_ID'),
'app_secret' => env('PAYHERE_APP_SECRET'),
// in your controller
use Lahirulhr\PayHere\PayHere;
// prepair posting data
$data = [
'first_name' => 'Lahiru',
'last_name' => 'Tharaka',
'email' => '[email protected]',
'phone' => '+94761234567',
'address' => 'Main Rd',
'city' => 'Anuradhapura',
'country' => 'Sri lanka',
'order_id' => '45552525005',
'items' => 'Smart band MI 4 - BLACK',
'currency' => 'LKR',
'amount' => 4960.00,
];
// creating checkout page & ridirect the user
return PayHere::checkOut()
->data($data)
->setOptionalData() // Set optional data. see PayHere documantaion for available values
->successUrl('www.visanduma.com/success')
->failUrl('www.visanduma.com/fail')
->renderView();
// define listners in your EventServiceProvider.php
class EventServiceProvider extends ServiceProvider
{
use Lahirulhr\PayHere\Events\CheckoutCallbackEvent;
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
CheckoutCallbackEvent::class => [
// register listeners to do something with callback
SomeListener::class
],
];
}
class SomeListener{
//....
public function handle($event)
{
// you can access payhere callback data using $event->payload
Log::info($event->payload);
}
}
return PayHere::recurring()
->data($data)
->setOptionalData() // Set optional data. see PayHere documantaion for available values
->successUrl('www.visanduma.com/success')
->failUrl('www.visanduma.com/fail')
->chargeMonthly(2)
->forYears()
->renderView();
// Charging interval (Recurrence)
PayHere::recurring()->chargeWeekly(2) // charge per specific period of weeks .the default value is one week
PayHere::recurring()->chargeMonthly(3) // charge per specific period of months .the default value is one month
PayHere::recurring()->chargeAnnually() // charge per specific period of years .the default value is one year
// Duration to charge
PayHere::recurring()->forWeeks(6) // set duratoin by weeks .the default value is one week
PayHere::recurring()->forMonths(3) // set duratoin by months .the default value is one month
PayHere::recurring()->forYears() // set duratoin by years .the default value is one year
PayHere::recurring()->forForever() // set charging period to infinity.
// use this event to recieve server callback. see above example on Checkout API
RecurringCallbackEvent::class
return PayHere::preapproval()
->data($data)
->setOptionalData() // Set optional data. see PayHere documantaion for available values
->successUrl('www.visanduma.com/payment-success')
->failUrl('www.visanduma.com/payment-fail')
->renderView();
// use this event to recieve server callback. see above example on Checkout API
PreapprovalCallbackEvent::class
$info = PayHere::retrieve()
->orderId("od-43784658374534") // order number that you use to charge from customer
->submit();
// get all subscriptions
$subscriptions = PayHere::subscription()->getAll();
// get payment details of specific subscription
$paymentInfo = PayHere::subscription()
->getPaymentsOfSubscription("420075032251"); // subscription ID
// retry on failed supscription payments
PayHere::subscription()
->retry("420075032251"); // subscription ID
// cancel a subscription
PayHere::subscription()
->cancel("420075032251"); // subscription ID
PayHere::refund()
->makePaymentRefund('320027150501') // payment_id
->note("Out of stock") // note for refund
->submit();
// use same $data as Checkout method
return PayHere::authorize()
->data($data)
->successUrl('www.visanduma.com/success')
->failUrl('www.visanduma.com/fail')
->renderView();
// use this event to recieve server callback. see above example on Checkout API
AuthorizeCallbackEvent::class