项目作者: imaxwen

项目描述 :
WeChat SDK for yii2 , based on overtrue/wechat.
高级语言: PHP
项目地址: git://github.com/imaxwen/yii2-easy-wechat.git
创建时间: 2016-10-25T06:14:54Z
项目社区:https://github.com/imaxwen/yii2-easy-wechat

开源协议:MIT License

下载


yii2-easy-wechat

WeChat SDK for yii2 , based on overtrue/wechat.
This extension helps you access overtrue/wechat application in a simple & familiar way: Yii::$app->wechat.

Latest Stable Version
Total Downloads
License

Installation

  1. composer require maxwen/yii2-easy-wechat

Configuration

Add the SDK as a yii2 application component in the config/main.php:

  1. 'components' => [
  2. // ...
  3. 'wechat' => [
  4. 'class' => 'maxwen\easywechat\Wechat',
  5. // 'userOptions' => [] # user identity class params
  6. // 'sessionParam' => '' # wechat user info will be stored in session under this key
  7. // 'returnUrlParam' => '' # returnUrl param stored in session
  8. ],
  9. // ...
  10. ]

Usage

  1. // here are two representative examples that will help you:
  2. // 微信网页授权:
  3. if(Yii::$app->wechat->isWechat && !Yii::$app->wechat->isAuthorized()) {
  4. return Yii::$app->wechat->authorizeRequired()->send();
  5. }
  6. // 微信支付(JsApi):
  7. $orderData = [
  8. 'openid' => '.. '
  9. // ... etc.
  10. ];
  11. $order = new WechatOrder($orderData);
  12. $payment = Yii::$app->wechat->payment;
  13. $prepayRequest = $payment->prepare($order);
  14. if($prepayRequest->return_code = 'SUCCESS' && $prepayRequest->result_code == 'SUCCESS') {
  15. $prepayId = $prepayRequest->prepay_id;
  16. }else{
  17. throw new yii\base\ErrorException('微信支付异常, 请稍后再试');
  18. }
  19. $jsApiConfig = $payment->configForPayment($prepayId);
  20. return $this->render('wxpay', [
  21. 'jsApiConfig' => $jsApiConfig,
  22. 'orderData' => $orderData
  23. ]);

How to load Wechat configures?

the overtrue/wechat application always constructs with a $options parameter.
I made the options as a yii2 param in the params.php:

recomended way:

  1. // in this way you need to create a wechat.php in the same directory of params.php
  2. // put contents in the wechat.php like:
  3. // return [
  4. // // wechat options here
  5. // ];
  6. 'WECHAT' => require(__DIR__.'/wechat.php'),

OR

  1. 'WECHAT' => [ // wechat options here ]

Wechat options configure help docs.

More documentation

see EasyWeChat Docs.

Thanks to overtrue/wechat , realy a easy way to play with wechat SDK 😁.

More repos for Yii2:

yii2-ckeditor-widget
yii2-adminlte-gii
yii2-curl