maxwen/yii2-easy-wechat

基于 overtrue/easywechat 的 yii2 微信 SDK

1.0.3 2017-08-11 05:09 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:30 UTC


README

yii2 微信 SDK,基于 overtrue/wechat
本扩展帮助您以简单且熟悉的方式访问 overtrue/wechat 应用: Yii::$app->wechat

Latest Stable Version Total Downloads License

安装

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 的方式 😁。

更多 Yii2 仓库

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