PHP code example of ledc / easywechat

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

    

ledc / easywechat example snippets


use Ledc\EasyWechat\PayConfigManager;
use Ledc\EasyWechat\Enums\TerminalEnum;

// 可在Bootstrap,全局注入配置
PayConfigManager::set(function (TerminalEnum $terminalEnum) {
    // TODO... 从数据库读取配置,然后返回
    return [];
});

use Ledc\EasyWechat\Enums\TerminalEnum;
use Ledc\EasyWechat\PayService;

// 用户支付终端
$terminal = $request->post('terminal');
$terminalEnum = TerminalEnum::from((int)$terminal);

$payService = new PayService($terminalEnum);

$attach = '业务附加数据(微信支付成功后,原样返回)';
// 待支付的订单数据(可以看微信官方文档或跟踪进pay方法查看参数,按实际需要的传)
$order = [
    'description' => '购买会员',
    'out_trade_no' => uniqid(),
    'amount' => 1,
];

// 统一支付
$result = $payService->pay($attach, $order);

use Ledc\EasyWechat\Enums\EventEnum;

return [
    // 微信支付回调通知:所有事件消息
    EventEnum::wechat_pay_any->value => [],
    // 微信支付回调通知:支付成功事件
    EventEnum::wechat_pay_success->value => [
        [WechatPaySuccessListener::class, 'handle'],
    ],
    // 微信支付回调通知:退款成功事件
    EventEnum::wechat_pay_refunded->value => [],
];