PHPackages                             hjp1011/yii2-easy-wechat - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. hjp1011/yii2-easy-wechat

ActiveYii2-extension[API Development](/categories/api)

hjp1011/yii2-easy-wechat
========================

WeChat SDK for yii2, 基于 overtrue/easywechat

1.0.0(4y ago)0543MITPHPPHP &gt;=7.0

Since Dec 12Pushed 4y ago1 watchersCompare

[ Source](https://github.com/hjp1011/yii2-easy-wechat)[ Packagist](https://packagist.org/packages/hjp1011/yii2-easy-wechat)[ RSS](/packages/hjp1011-yii2-easy-wechat/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

yii2-easy-wechat
================

[](#yii2-easy-wechat)

> Based on the latest overtrue/wechat 4.x WeChat SDK for yii2 , based on overtrue/wechat.support EasyWechat 4.x This extension helps you access overtrue/wechat application in a simple &amp; familiar way: Yii::$app-&gt;wechat.

WeChat SDK for Yii2 , Based on [overtrue/wechat](https://github.com/overtrue/wechat).
This extension makes it easy to use yii2 to call EasyWechat: `Yii::$app->wechat`.
[![Latest Stable Version](https://camo.githubusercontent.com/2cc9d3b248b784cb20efd4ecf064023a5ffc9c65b78384fabaae2eff29b6d78e/687474703a2f2f706f7365722e707567782e6f72672f686a70313031312f796969322d656173792d7765636861742f76)](https://packagist.org/packages/hjp1011/yii2-easy-wechat) [![Total Downloads](https://camo.githubusercontent.com/0673ab649cc65246ee40bd98e868163d894ab4703dd190784b2d93ab8eb8ecf5/687474703a2f2f706f7365722e707567782e6f72672f686a70313031312f796969322d656173792d7765636861742f646f776e6c6f616473)](https://packagist.org/packages/hjp1011/yii2-easy-wechat) [![Latest Unstable Version](https://camo.githubusercontent.com/40eb30ad505ed02ae48a484fa857e9206efd41c25498ca04a0e243f12221c029/687474703a2f2f706f7365722e707567782e6f72672f686a70313031312f796969322d656173792d7765636861742f762f756e737461626c65)](https://packagist.org/packages/hjp1011/yii2-easy-wechat) [![License](https://camo.githubusercontent.com/cd12fd7b7ca86807d2656b5052b9a181bdf29291562f8e4f8bc66ad4bd9b1536/687474703a2f2f706f7365722e707567782e6f72672f686a70313031312f796969322d656173792d7765636861742f6c6963656e7365)](https://packagist.org/packages/hjp1011/yii2-easy-wechat) [![PHP Version Require](https://camo.githubusercontent.com/3838aafea98f2dd6a80f7092f6083a93cc5de6d52bf817437bc321744ffbfb36/687474703a2f2f706f7365722e707567782e6f72672f686a70313031312f796969322d656173792d7765636861742f726571756972652f706870)](https://packagist.org/packages/hjp1011/yii2-easy-wechat)

Install
-------

[](#install)

```
composer require hjp1011/yii2-easy-wechat

```

Config
------

[](#config)

Add SDK to Yii2 'config/main.php' component:

```
'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',
		]
	],
	// ...
]
```

Set basic configuration information and wechat payment information to 'config/params.php' :

```
// 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' => [],
```

The configuration document

[Wechat configuration description document.](https://www.easywechat.com/docs/master/official-account/configuration)
[Wechat Pay configuration documentation.](https://www.easywechat.com/docs/master/payment/jssdk)
[Wechat applets configuration description document.](https://www.easywechat.com/docs/master/mini-program/index)
[Wechat open Platform Third-party platform](https://www.easywechat.com/docs/master/open-platform/index)
[Enterprise WeChat](https://www.easywechat.com/docs/master/wework/index)
[Enterprise wechat open platform](https://www.easywechat.com/docs/master/open-work/index)
[Small businesses](https://www.easywechat.com/docs/master/micro-merchant/index)

Eample
------

[](#eample)

Wechat web page authorization adn Get the current user information

```
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
```

Obtain the wechat SDK instance

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

Obtain the wechat Pay SDK instance

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

Get the instance of wechat applets

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

Obtain the third-party platform instance of wechat open Platform

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

Obtain the enterprise wechat instance

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

Access to wechat enterprise wechat open platform

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

Get wechat small and micro merchants

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

WeChat pay(JsApi):

```
// 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
]);
```

JSSDK initiates payment

```

    //Array for JSSDK authorization available methods, as needed to add a detailed view of wechat JSSDK methods
    wx.config();
    // Initiate payment
    wx.chooseWXPay({
        timestamp: ,
        nonceStr: '',
        package: '',
        signType: '',
        paySign: '', // Pay for signature
        success: function (res) {
            // Callback function after successful payment
        }
    });

```

### Smart tips

[](#smart-tips)

If you need an intelligent reminder from your editor (PhpStorm etc.) to use 'Yii::$app-&gt;wechat', add the following to 'Yii \\ Base \\Application' :

```
