PHP code example of yuepay / yuepay-laravel

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

    

yuepay / yuepay-laravel example snippets


'providers' => [
    // ...
    YuePay\YuePayServiceProvider::class,
],

'aliases' => [
    // ...
    'YuePay' => YuePay\YuePay::class,
],

Route::prefix('api')->group(base_path('vendor/yuepay/routes/web.php'));

use YuePay\YuePay;

// 统一下单(微信JSAPI支付)
$result = YuePay::unifiedOrder([
    'mchOrderNo' => 'ORDER' . time(),
    'wayCode'    => 'WX_JSAPI',
    'amount'     => 100,           // 单位:分(1元)
    'subject'    => '测试商品',
    'body'       => '测试商品描述',
    'openid'     => '用户的微信openid',
    'returnUrl'  => 'https://your-domain.com/return',
]);

// 统一下单(支付宝扫码支付)
$result = YuePay::unifiedOrder([
    'mchOrderNo' => 'ORDER' . time(),
    'wayCode'    => 'ALI_QR',
    'amount'     => 100,
    'subject'    => '测试商品',
]);

// 查询支付订单
$result = YuePay::queryOrder(mchOrderNo: 'ORDER123456');

// 关闭订单
$result = YuePay::closeOrder(mchOrderNo: 'ORDER123456');

// 申请退款
$result = YuePay::refund([
    'mchOrderNo'   => 'ORDER123456',
    'mchRefundNo'  => 'REFUND' . time(),
    'refundAmount' => 100,
    'refundReason' => '用户申请退款',
]);

// 查询退款
$result = YuePay::queryRefund(mchRefundNo: 'REFUND123456');

use YuePay\YuePayService;

class PaymentController extends Controller
{
    public function __construct(private YuePayService $yuePay) {}

    public function pay()
    {
        $result = $this->yuePay->unifiedOrder([...]);
    }
}
bash
composer dump-autoload
bash
php artisan vendor:publish --tag=yuepay-config