PHP code example of hjp1011 / yii2-easy-wechat

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

    

hjp1011 / yii2-easy-wechat example snippets



'components' => [
	// ...
	'wechat' => [
		'class' => 'yiiframe\easywechat\Wechat',
		'userOptions' => [],  // user identity class parameters
		'sessionParam' => 'wechatUser', // wechatUser information will be stored in the session in this key
		'returnUrlParam' => '_wechatReturnUrl', //returnUrl is stored in the session
		'rebinds' => [ // Customizable service module 
		    // 'cache' => 'common\components\Cache',
		]
	],
	// ...
]

// Wechat configuration details can refer to EasyWechat
'wechatConfig' => [],

// Wechat Pay configuration
'wechatPaymentConfig' => [],

// Micro channel applets configuration
'wechatMiniProgramConfig' => [],

// Wechat Open Platform Third-party platform configuration
'wechatOpenPlatformConfig' => [],

// Wechat enterprise wechat configuration
'wechatWorkConfig' => [],

// Wechat enterprise wechat open platform
'wechatOpenWorkConfig' => [],

// Wechat small and micro merchants
'wechatMicroMerchantConfig' => [],

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

// Method 1 to obtain the current user information of wechat
Yii::$app->session->get('wechatUser')

// Method 2 to obtain the current user information of wechat
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;

// Pay parameters
$orderData = [ 
    'openid' => '.. '
    // ... etc. 
];

// Generate payment configuratio
$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('Wechat payment is abnormal, please try again later');
}  

return $this->render('wxpay', [
    'jssdk' => $payment->jssdk, // $app is retrieved from the above fetching instance
    'config' => $config
]);