PHP code example of ethanfly / businesspay-laravel

1. Go to this page and download the library: Download ethanfly/businesspay-laravel 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/ */

    

ethanfly / businesspay-laravel example snippets


// config/businesspay.php
'accounts' => [
    'default'    => [ 'env' => 'sandbox', ... ],
    'production' => [ 'env' => 'production', ... ],
    // SAAS / 企业证书认证(可选)
    'saas' => [ ..., 'ent_id' => ..., 'enterprise_serial_number' => ..., 'ent_private_key' => ... ],
],

use Ethanfly\BusinessPay\Facades\BusinessPay;

// 发起支付(数组自动规范化为 PaymentParam)
$payment = BusinessPay::payment()->createPay([
    'out_payment_id' => 'ORDER_20240101_001',
    'amount'         => 100,
    'currency'       => 'CNY',
    'payee'          => [...],
    // ...
]);

// 查询
BusinessPay::payment()->retrieve('PAYMENT_ID');
BusinessPay::payment()->retrieveByOutPaymentId('ORDER_20240101_001');

// 退款 / 提现 / 分账
BusinessPay::refund()->create([...]);
BusinessPay::withdraw()->create([...]);
BusinessPay::profitAllocation()->create([...]);

// 切换账户
BusinessPay::account('production')->payment()->retrieve('PAYMENT_ID');

use Ethanfly\BusinessPay\BusinessPayManager;

class OrderController extends Controller
{
    public function store(BusinessPayManager $businessPay)
    {
        return $businessPay->payment()->createPay([...]);
    }
}

use Ethanfly\BusinessPay\Notify\NotifyHandler;
use Entpay\Mse\Client\Model\PaymentNotifyModel;

$notify = app(NotifyHandler::class)
    ->handleRequest($request, PaymentNotifyModel::class);

$notify->getEventType();          // PAYMENT.SUCCEEDED
$notify->getEventId();            // 幂等键
$payment = $notify->getEventContent();  // Payment 模型

use Ethanfly\BusinessPay\Exceptions\BusinessPayException;

try {
    BusinessPay::payment()->createPay([...]);
} catch (BusinessPayException $e) {
    $e->getStatusCode();   // HTTP 状态码
    $e->getTraceId();      // 链路 ID
    $e->getUri();          // 请求 URI
    $e->getApiException(); // 原始 SDK 异常
}
bash
php artisan vendor:publish --tag=businesspay-config