lahirulhr/laravel-payhere

斯里兰卡PayHere支付网关的API集成

v1.0.4 2024-08-01 06:51 UTC

This package is auto-updated.

Last update: 2024-09-01 07:03:14 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads


Laravel - PayHere旨在轻松管理您的Laravel应用程序上的PayHere支付网关。目前,此包支持官方PayHere文档中提供的所有可用方法。

阅读官方文档获取更多信息

可用的API方法

✔️ Checkout API
✔️ 定期API
✔️ 预批准API
✔️ 充值API
✔️ 检索API
✔️ 订阅管理API
✔️ 退款API
✔️ 授权API
✔️ 捕获API

基本用法
// adding payment details
$data = [
            'first_name' => 'Lahiru',
            'last_name' => 'Tharaka',
            'email' => 'lahirulhr@gmail.com',
            '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 & redirect user
        
return PayHere::checkOut()
            ->data($data)
            ->successUrl('www.visanduma.com/payment-success')
            ->failUrl('www.visanduma.com/payment-fail')
            ->renderView();

安装

您可以通过composer安装此包

composer require lahirulhr/laravel-payhere

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Lahirulhr\PayHere\PayHereServiceProvider" --tag="laravel-payhere-config"

这是已发布配置文件的内容

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'),

用法

Checkout API

Checkout API允许您在代码级别将PayHere集成到您的网站、Web应用程序或其他应用程序中。它提供了一个简单的HTML表单来启动支付请求,并将您的客户重定向到PayHere支付网关以安全地处理支付。

// in your controller

use Lahirulhr\PayHere\PayHere;

// prepair posting data

$data = [
            'first_name' => 'Lahiru',
            'last_name' => 'Tharaka',
            'email' => 'lahirulhr@gmail.com',
            '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();

处理服务器回调

PayHere将通过使用公共URL POST请求回调通知您的应用程序响应数据。然后,此包将使用回调数据触发一个新事件。您只需要监听一个事件,并用有效载荷数据执行您想要的任何操作。

可用的事件

  • AuthorizeCallbackEvent
  • CheckoutCallbackEvent
  • PreapprovalCallbackEvent
  • RecurringCallbackEvent

示例

            
// 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);
    }

}
定期API

定期API接受与Checkout API相同的数据数组。您只需要将checkOut()方法更改为recurring()。

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
预批准API

使用与Checkout方法相同的方式

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
充值API

充值API允许您使用加密令牌按需以编程方式对预批准客户进行充值。它将在成功时返回响应数据数组或返回PayHereException(如果有错误)。

$data = [
        "type" => "PAYMENT",
        "order_id" => "Order12345",
        "items" => "Taxi Hire 123",
        "currency" => "LKR",
        "amount" => 345.67,
    ];

$response =  PayHere::charge()
        ->byToken("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") // customer token
        ->withData($data)
        ->submit();
检索API

检索API允许您检索通过PayHere处理的成功支付的详细信息

$info = PayHere::retrieve()
        ->orderId("od-43784658374534") // order number that you use to charge from customer
        ->submit();
订阅管理

订阅管理API允许您以编程方式查看、重试和取消您通过Recurring API订阅的客户。

// 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
退款API

退款API允许您以编程方式对现有支付进行退款。

PayHere::refund()
        ->makePaymentRefund('320027150501') // payment_id
        ->note("Out of stock") // note for refund
        ->submit();
授权API

授权API允许您获取您的客户对持卡支付授权的授权。此方法将用户重定向到支付页面

// 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
捕获API

捕获API允许您按需使用从支付授权API检索的授权令牌以编程方式捕获授权的持卡支付。

$response =  PayHere::capture()
        ->usingToken('e34f3059-7b7d-4b62-a57c-784beaa169f4') // authorization token
        ->amount(100) // charging amount
        ->reason("reason for capture")
        ->submit();

待办事项

  • 服务器回调事件
  • 自定义支付重定向页面
  • 自定义错误类型
  • 响应数据对象
  • 可选数据处理
  • 内置订阅数据库
  • 服务器回调日志

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请参阅我们关于如何报告安全漏洞的安全策略

致谢

许可协议

MIT许可协议(MIT)。更多信息请参阅许可文件