PHP code example of hanson / laravel-admin-wechat

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

    

hanson / laravel-admin-wechat example snippets




return [
    'guards' => [
        // ...
        'mini' => [
            'driver' => 'jwt',
            'provider' => 'wechat_user',
        ]
    ],
    'providers' => [
        // ...
        'wechat_user' => [
            'driver' => 'eloquent',
            'model' => Hanson\LaravelAdminWechat\Models\WechatUser::class, // 你也可以自己继承此 model 后修改为自己的 model
        ],
    ]
];


use \Hanson\LaravelAdminWechat\Facades\ConfigService;
use \Hanson\LaravelAdminWechat\Facades\MerchantService;
use \Hanson\LaravelAdminWechat\Facades\OrderService;

// ConfigService 可获取 公众号/小程序 实例
ConfigService::getCurrent(); // 获取后台操作中的 WechatConfig 对象
ConfigService::getAdminCurrentApp(); // 获取后台操作中的微信实例
ConfigService::getInstanceByAppId('app id'); // 根据 appid 获取微信实例

// MerchantService 可获取 微信支付实例
MerchantService::getInstanceByMchId('mch id'); // 根据 mch id 获取微信支付实例

// OrderService 订单相关服务
OrderService::unify('mch id', 'JSAPI', array $data); // 统一下单并创建微信订单 data 为统一下单参数,与微信支付文档一致
OrderService::jsConfig('mch id', 'JSAPI', array $data); // 返回 js sdk 所需参数(其中包括统一下单,创建订单)



protected $listen = [
    \Hanson\LaravelAdminWechat\Events\DecryptUserInfo::class => [
        'App\Listeners\AfterSaveUserInfo',
    ],
    \Hanson\LaravelAdminWechat\Events\DecryptMobile::class => [
        'App\Listeners\SaveMobile',        
    ],
    \Hanson\LaravelAdminWechat\Events\OrderPaid::class => [
        'App\Listeners\ChangeOrderStatus',
    ]
];


use \Hanson\LaravelAdminWechat\Events\DecryptMobile;

class SaveMobile 
{
    public function handle(DecryptMobile $event)
    {
        $event->wechatUser->user()->update([
            'phone' => $event->decryptedData['purePhoneNumber'],
            'country_code' => $event->decryptedData['countryCode'],
        ]);
    }
}


use \Hanson\LaravelAdminWechat\Events\DecryptUserInfo;

class AfterSaveUserInfo 
{
    public function handle(DecryptUserInfo $event)
    {
        // 你的业务
        $event->decryptedData['nickname'];
        $event->wechatUser;
    }
}


use \Hanson\LaravelAdminWechat\Events\OrderPaid;

class AfterSaveUserInfo 
{
    public function handle(OrderPaid $event)
    {
        // 你的业务
        $wechatOrder = $event->order;
        
        $order = $wechatOrder->order()->update(['status' => 'paid']);
        
        $openId = $wechatOrder->openid;
    }
}

// 支付接口示例


class OrderController extends Controller
{
    public function pay()
    {
        // some validate
        
        $data = [
            'body' => '商品标题', 
            'total_fee' => 100,
            'openid' => auth('mini')->user()->openid,
            // 'out_trade_no' => 'xxx', 选填,如不填写时会自动创建一个订单号
        ];
        
        /**
        * $result['config'] jssdk 所需参数
        * $result['order'] WechatOrder 的 model 对象
        * $result['unify'] unify 接口返回的结果
        */
        $result = \Hanson\LaravelAdminWechat\Facades\OrderService::jsConfig('mch id', 'JSAPI', $data);
        
        App\Models\Order::create([
            'wechat_order_id' => $result['order']->id,
            'status' => 'not paid',
            'goods_id' => '...',
            'address_id' => '...',
        ]);
        
        return $result;
    }
}

// app/Exceptions/Handler.php


public function render($request, Exception $exception)
{
    if ($request->acceptsJson()) {
        if ($exception instanceof AuthenticationException) {
            return Response::json(['err_code' => 401, 'err_msg' => 'Unauthenticated'], 401);
        }
        return fail($exception->getMessage());
    }
    return parent::render($request, $exception);
}