PHP code example of bestyii / yii2-easywechat

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

    

bestyii / yii2-easywechat example snippets



'components' => [
	// ...
	'wechat' => [
		'class' => 'bestyii\easywechat\Wechat',
		'userOptions' => [],  // 用户身份类参数
		'sessionParam' => 'wechatUser', // 微信用户信息将存储在会话在这个密钥
		'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存储在会话中
		'rebinds' => [ // 自定义服务模块 
		    // 'cache' => 'common\components\Cache',
		]
	],
	// ...
]

// 微信配置 具体可参考EasyWechat 
'wechatConfig' => [],

// 微信支付配置 具体可参考EasyWechat
'wechatPaymentConfig' => [],

// 微信小程序配置 具体可参考EasyWechat
'wechatMiniProgramConfig' => [],

// 微信开放平台第三方平台配置 具体可参考EasyWechat
'wechatOpenPlatformConfig' => [],

// 微信企业微信配置 具体可参考EasyWechat
'wechatWorkConfig' => [],

// 微信企业微信开放平台 具体可参考EasyWechat
'wechatOpenWorkConfig' => [],

// 微信小微商户 具体可参考EasyWechat
'wechatMicroMerchantConfig' => [],

if (Yii::$app->wechat->isWechat && !Yii::$app->wechat->isAuthorized()) {
    return Yii::$app->wechat->authorizeRequired()->send();
}

// 获取微信当前用户信息方法一
Yii::$app->session->get('wechatUser')

// 获取微信当前用户信息方法二
Yii::$app->wechat->user

$app = Yii::$app->wechat->app;

$payment = Yii::$app->wechat->payment;

$miniProgram = Yii::$app->wechat->miniProgram;

$openPlatform = Yii::$app->wechat->openPlatform;

$work = Yii::$app->wechat->work;

$work = Yii::$app->wechat->openWork;

$microMerchant = Yii::$app->wechat->microMerchant;

// 支付参数
$orderData = [ 
    'openid' => '.. '
    // ... etc. 
];

// 生成支付配置
$payment = Yii::$app->wechat->payment;
$result = $payment->order->unify($orderData);
if ($result['return_code'] == 'SUCCESS') {
    $prepayId = $result['prepay_id'];
    $config = $payment->jssdk->sdkConfig($prepayId);
} else {
    throw new yii\base\ErrorException('微信支付异常, 请稍后再试');
}  

return $this->render('wxpay', [
    'jssdk' => $payment->jssdk, // $app通过上面的获取实例来获取
    'config' => $config
]);