Browse Source
feat(pay): 集成二维码生成工具并优化支付逻辑
feat(pay): 集成二维码生成工具并优化支付逻辑
- 添加 endroid/qrcode 依赖用于生成二维码 - 创建 PayUtils 工具类封装二维码生成功能 - 更新微信支付逻辑以使用新工具生成二维码 - 调整支付服务返回结构支持多平台支付码 - 移除支付接口中不再使用的 type 参数 - 更新 composer.json 和 composer.lock 文件引入新依赖master
7 changed files with 247 additions and 11 deletions
-
3composer.json
-
180composer.lock
-
1plugin/piadmin/app/controller/v1/pay/PayController.php
-
3plugin/piadmin/app/route/v1/pay.php
-
20plugin/piadmin/app/service/pay/PayService.php
-
48plugin/piadmin/app/utils/pay/PayUtils.php
-
3plugin/piadmin/app/utils/pay/wechat/WechatPay.php
@ -0,0 +1,48 @@ |
|||
<?php |
|||
|
|||
namespace plugin\piadmin\app\utils\pay; |
|||
|
|||
use Endroid\QrCode\Builder\Builder; |
|||
use Endroid\QrCode\Encoding\Encoding; |
|||
use Endroid\QrCode\ErrorCorrectionLevel; |
|||
use Endroid\QrCode\RoundBlockSizeMode; |
|||
use Endroid\QrCode\Writer\PngWriter; |
|||
use Exception; |
|||
|
|||
/** |
|||
* 支付工具类 |
|||
*/ |
|||
class PayUtils |
|||
{ |
|||
/** |
|||
* 生成二维码 |
|||
*/ |
|||
static function generateQrCode($url, $size = 300) |
|||
{ |
|||
try { |
|||
// 确保数据编码正确
|
|||
if (!mb_check_encoding($url, 'UTF-8')) { |
|||
$url = mb_convert_encoding($url, 'UTF-8', 'auto'); |
|||
} |
|||
|
|||
$result = new Builder( |
|||
writer: new PngWriter(), |
|||
data: $url, |
|||
encoding: new Encoding('UTF-8'), |
|||
errorCorrectionLevel: ErrorCorrectionLevel::High, |
|||
size: $size, |
|||
margin: 10, |
|||
roundBlockSizeMode: RoundBlockSizeMode::Margin |
|||
); |
|||
|
|||
$qrCode = $result->build(); |
|||
header('Content-Type: '.$qrCode->getMimeType()); |
|||
return $qrCode->getDataUri(); |
|||
} catch (Exception $e) { |
|||
// 记录错误日志
|
|||
error_log('QR Code generation error: ' . $e->getMessage()); |
|||
throw $e; |
|||
} |
|||
} |
|||
|
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue