2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
We use here EasyWeChat
EasyWeCha official website https://www.easywechat.com/
Installation addresshttps://github.com/easywechat/docs
Related Documentshttps://www.easywechat.com/docs/4.1/payment/index
$ composer require overtrue/wechat:~4.0 -vvv
<?php
namespace appcommonservice;
use EasyWeChatFactory;
use EasyWeChatMiniProgramApplication;
use thinkHook;
/**
* 微信服务
* @package appcommonservice
*/
class WeChatService
{
//微信公众号配置
private $officeConfig = [
'app_id' => 'wx727ac3b3f4439a25',
'secret' => '23471aaeb7d0ab3679da9f9a7d58bb25',
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
//...
];
//微信支付
private $payConfig = [
// 必要配置
'app_id' => 'xxxx',
'mch_id' => 'your-mch-id',
'key' => 'key-for-signature', // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!!
'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它
];
//微信小程序配置
private $miniConfig = [
'app_id' => 'wx3cf0f39249eb0exx',
'secret' => 'f1c242f4f28f735d4687abb469072axx',
// 下面为可选项
// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => PUBLIC_PATH . '/logs/wechat.log',
],
];
//开放平台配置
private $openConfig = [
'app_id' => '开放平台第三方平台 APPID',
'secret' => '开放平台第三方平台 Secret',
'token' => '开放平台第三方平台 Token',
'aes_key' => '开放平台第三方平台 AES Key'
];
private $token = ''; //获取小程序的ACCESS_TOKEN
private $isContract = false; //是否开启支付中签约
/**
* @ApiTitle (实例化)
* @param int $type
* @return bool|Application|EasyWeChatOfficialAccountApplication|EasyWeChatOpenPlatformApplication|EasyWeChatPaymentApplication
* @throws EasyWeChatKernelExceptionsHttpException
* @throws EasyWeChatKernelExceptionsInvalidArgumentException
* @throws EasyWeChatKernelExceptionsInvalidConfigException
* @throws EasyWeChatKernelExceptionsRuntimeException
* @throws PsrSimpleCacheInvalidArgumentException
*/
public function connect($type = 0)
{
//实例化对象
if ($type == 0) $app = Factory::miniProgram($this->miniConfig); //微信小程序
if ($type == 1) $app = Factory::officialAccount($this->officeConfig); //微信公众号
if ($type == 2) $app = Factory::payment($this->payConfig); //微信支付
if ($type == 3) $app = Factory::openPlatform($this->payConfig); //微信开放平台
//获取token
$accessToken = $app->access_token;
$this->token = $accessToken->getToken()['access_token']; // token 数组 token['access_token'] 字符串
return $app ?? false;
}
/**
* @ApiTitle (生成小程序二维码)
* @return bool|int
* @throws EasyWeChatKernelExceptionsHttpException
* @throws EasyWeChatKernelExceptionsInvalidArgumentException
* @throws EasyWeChatKernelExceptionsInvalidConfigException
* @throws EasyWeChatKernelExceptionsRuntimeException
* @throws PsrSimpleCacheInvalidArgumentException
*/
public function createCode()
{
$app = $this->connect(0);
$response = $app->app_code->getUnlimit('scene-value', [
'page' => 'path/to/page',
'width' => 600,
]);
if ($response instanceof EasyWeChatKernelHttpStreamResponse) {
$dir = 'qrCode/' . date('Ymd', time()) . '/';
if (!file_exists($dir)) mkdir($dir, 0777, true);
$url = PUBLIC_PATH . $dir . 'app_mini_code.png';
$response->save($url);
//保存到OSS
$urlOss = Hook::listen("upload_oss", $url, null, true);
//删除本地图片和文件夹
if (file_exists($url)) {
unlink($url);
rmdir($dir);
}
}
return $urlOss ?? false;
}
}
The configurations of WeChat mini-programs, official accounts, WeChat payments, and open platforms are all written here. If you need configurations such as enterprise WeChat, you can modify the code according to your own business.
public function createWeChatCode()
{
$code = (new WeChatService)->createCode();
$this->success('生成小程序码成功', $code);
}
$app = (new WeChatService())->connect(1);
Thank you for watching. If you have any questions, please leave a message in the comment section.
If you like it, please remember to like, collect and follow it!!!