hjp1011/yii2-easy-wechat

基于 overtrue/easywechat 的 yii2 微信 SDK

安装次数: 507

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

1.0.0 2021-12-12 17:38 UTC

This package is not auto-updated.

Last update: 2024-10-01 05:18:23 UTC


README

基于最新的 overtrue/wechat 4.x 微信 SDK for yii2,支持 EasyWechat 4.x。此扩展使您以简单且熟悉的方式访问 overtrue/wechat 应用:Yii::$app->wechat。

基于overtrue/wechat的 Yii2 微信 SDK。
此扩展简化了使用 yii2 调用 EasyWechat 的过程:Yii::$app->wechat
Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

安装

composer require hjp1011/yii2-easy-wechat

配置

将 SDK 添加到 Yii2 'config/main.php' 组件

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

将基本配置信息和微信支付信息设置到 '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' => [],

配置文档

微信配置描述文档。
微信支付配置文档。
微信小程序配置描述文档。
微信开放平台第三方平台
企业微信
企业微信开放平台
小微商户

示例

微信网页授权并获取当前用户信息

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

获取微信 SDK 实例

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

获取微信支付 SDK 实例

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

获取微信小程序实例

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

获取微信开放平台第三方平台实例

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

获取企业微信实例

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

访问微信企业微信开放平台

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

获取微信小微商户

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

微信支付(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 启动支付

<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
    //Array for JSSDK authorization available methods, as needed to add a detailed view of wechat JSSDK methods
    wx.config(<?php echo $jssdk->buildConfig(array('chooseWXPay'), true) ?>);
    // Initiate payment
    wx.chooseWXPay({
        timestamp: <?= $config['timestamp'] ?>,
        nonceStr: '<?= $config['nonceStr'] ?>',
        package: '<?= $config['package'] ?>',
        signType: '<?= $config['signType'] ?>',
        paySign: '<?= $config['paySign'] ?>', // Pay for signature
        success: function (res) {
            // Callback function after successful payment
        }
    });
</script>

智能提示

如果您需要编辑器(PhpStorm 等)智能提醒使用 'Yii::$app->wechat',请将以下内容添加到 'Yii \ Base \ Application'

<?php

namespace yii\base;

use Yii;

/**
 *
 * @property \yiiframe\easywechat\Wechat $wechat Add this line to make the editor smart prompt.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
abstract class Application extends Module
{

}

更多信息

EasyWeChat 文档.

实例

RageFrame

反馈问题

如果在使用过程中有任何问题,请随时给我反馈。您可以通过以下联系方式联系我