Files
huangjingfen/pro_v3.5.1/app/services/pay/OrderPayServices.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

114 lines
4.0 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace app\services\pay;
use app\services\order\StoreOrderCartInfoServices;
use app\services\wechat\WechatUserServices;
use think\annotation\Inject;
use think\exception\ValidateException;
/**
* 订单发起支付
* Class OrderPayServices
* @package app\services\pay
*/
class OrderPayServices
{
/**
* 支付
* @var PayServices
*/
#[Inject]
protected PayServices $payServices;
/**
* 订单发起支付
* @param array $orderInfo
* @param string $payType
* @return array|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function orderPay(array $orderInfo, string $payType)
{
if ($orderInfo['paid']) {
throw new ValidateException('订单已支付!');
}
if ($orderInfo['pay_price'] <= 0) {
throw new ValidateException('该支付无需支付!');
}
$openid = '';
if (!in_array($payType, ['weixinh5', 'pc', 'store']) && !request()->isApp()) {
if ($payType === 'weixin') {
$userType = 'wechat';
} else {
$userType = $payType;
}
/** @var WechatUserServices $services */
$services = app()->make(WechatUserServices::class);
$openid = $services->uidToOpenid($orderInfo['uid'], $userType);
if (!$openid) {
throw new ValidateException('获取用户openid失败,无法支付');
}
}
if (isset($orderInfo['recharge_type'])) {
$body = '用户充值';
$successAction = "user_recharge";
} elseif (isset($orderInfo['member_type'])) {
$body = substrUTf8('线下收银支付:' . $orderInfo['member_type'], 30);
$successAction = "member";
} else {
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
$body = substrUTf8($body, 30);
$successAction = "product";
}
if (!$body) {
throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
}
return $this->payServices->pay($payType, $openid, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body);
}
/**
* 支付宝支付
* @param array $orderInfo
* @param string $quitUrl
* @return array|string
*/
public function alipayOrder(array $orderInfo, string $quitUrl, bool $isCode = false)
{
if ($orderInfo['paid']) {
throw new ValidateException('订单已支付!');
}
if ($orderInfo['pay_price'] <= 0) {
throw new ValidateException('该支付无需支付!');
}
if (isset($orderInfo['recharge_type'])) {
$body = '用户充值';
$successAction = "user_recharge";
} elseif (isset($orderInfo['member_type'])) {
$body = substrUTf8($orderInfo['member_type'], 30);
$successAction = "member";
} else {
/** @var StoreOrderCartInfoServices $orderInfoServices */
$orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
$body = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
$body = substrUTf8($body, 30);
$successAction = "product";
}
if (!$body) {
throw new ValidateException('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
}
return $this->payServices->pay('alipay', $quitUrl, $orderInfo['order_id'], $orderInfo['pay_price'], $successAction, $body, $isCode);
}
}