maxwen / yii2-easy-wechat
基于 overtrue/easywechat 的 yii2 微信 SDK
1.0.3
2017-08-11 05:09 UTC
Requires
- overtrue/wechat: ~3.0
- yiisoft/yii2: ~2.0.6
README
yii2 微信 SDK,基于 overtrue/wechat。
本扩展帮助您以简单且熟悉的方式访问 overtrue/wechat
应用: Yii::$app->wechat
。
安装
composer require maxwen/yii2-easy-wechat
配置
在 config/main.php
中将 SDK 添加为 yii2 应用的 component
'components' => [ // ... 'wechat' => [ 'class' => 'maxwen\easywechat\Wechat', // 'userOptions' => [] # user identity class params // 'sessionParam' => '' # wechat user info will be stored in session under this key // 'returnUrlParam' => '' # returnUrl param stored in session ], // ... ]
用法
// here are two representative examples that will help you: // 微信网页授权: if(Yii::$app->wechat->isWechat && !Yii::$app->wechat->isAuthorized()) { return Yii::$app->wechat->authorizeRequired()->send(); } // 微信支付(JsApi): $orderData = [ 'openid' => '.. ' // ... etc. ]; $order = new WechatOrder($orderData); $payment = Yii::$app->wechat->payment; $prepayRequest = $payment->prepare($order); if($prepayRequest->return_code = 'SUCCESS' && $prepayRequest->result_code == 'SUCCESS') { $prepayId = $prepayRequest->prepay_id; }else{ throw new yii\base\ErrorException('微信支付异常, 请稍后再试'); } $jsApiConfig = $payment->configForPayment($prepayId); return $this->render('wxpay', [ 'jsApiConfig' => $jsApiConfig, 'orderData' => $orderData ]);
如何加载微信配置?
overtrue/wechat
应用始终使用一个 $options
参数构建。我已将这些选项作为 yii2 参数添加到 params.php
推荐方法
// in this way you need to create a wechat.php in the same directory of params.php // put contents in the wechat.php like: // return [ // // wechat options here // ]; 'WECHAT' => require(__DIR__.'/wechat.php'),
或者
'WECHAT' => [ // wechat options here ]
更多文档
请参阅 EasyWeChat 文档。
感谢 overtrue/wechat
,确实是一个轻松玩转微信 SDK 的方式 😁。