yii2-easy-wechat
基于 overtrue/wechat 的 yii2 微信 SDK。
此扩展程序可帮助您以简单熟悉的方式访问 overtrue/wechat 应用程序:Yii::$app->wechat。
安装
composer require maxwen/yii2-easy-wechat
配置
在 config/main.php 中将 SDK 添加为 yii2 应用程序组件:
'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
]);
如何加载微信配置?
overrue/wechat 应用程序总是使用 $options 参数构造。 我在 params.php 中将选项作为 yii2 param:
推荐方式:
// 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 ]微信选项配置帮助文档。