You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
299 lines
9.2 KiB
299 lines
9.2 KiB
8 months ago
|
<?php
|
||
|
|
||
|
namespace app\api\service\wholesaler;
|
||
|
|
||
|
use app\api\model\Payment as PaymentModel;
|
||
|
use app\api\model\PaymentTrade as PaymentTradeModel;
|
||
|
use app\api\model\wholesaler\Order;
|
||
|
use app\api\service\Order as OrderService;
|
||
|
use app\api\service\User as UserService;
|
||
|
use app\common\enum\Client as ClientEnum;
|
||
|
use app\common\enum\OrderType as OrderTypeEnum;
|
||
|
use app\common\enum\payment\Method as PaymentMethodEnum;
|
||
|
use app\common\library\payment\Facade as PaymentFacade;
|
||
|
use app\common\service\BaseService;
|
||
|
use cores\exception\BaseException;
|
||
|
use think\db\exception\DataNotFoundException;
|
||
|
use think\db\exception\DbException;
|
||
|
use think\db\exception\ModelNotFoundException;
|
||
|
|
||
|
class Payment extends BaseService
|
||
|
{
|
||
|
// 提示信息
|
||
|
private string $message = '';
|
||
|
|
||
|
// 订单信息
|
||
|
private Order $orderInfo;
|
||
|
|
||
|
// 支付方式 (微信支付、支付宝,余额)
|
||
|
private string $method = '';
|
||
|
|
||
|
// 下单的客户端
|
||
|
private string $client = '';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 设置当前支付方式
|
||
|
* @param string $method 支付方式
|
||
|
* @return $this
|
||
|
*/
|
||
|
public function setMethod(string $method): Payment
|
||
|
{
|
||
|
$this->method = $method;
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 设置下单的客户端
|
||
|
* @param string $client 客户端
|
||
|
* @return $this
|
||
|
*/
|
||
|
public function setClient(string $client): Payment
|
||
|
{
|
||
|
$this->client = $client;
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 确认订单支付事件
|
||
|
* @param array $extra 附加数据
|
||
|
* @return array[]
|
||
|
* @throws BaseException
|
||
|
* @throws DataNotFoundException
|
||
|
* @throws DbException
|
||
|
* @throws ModelNotFoundException
|
||
|
*/
|
||
|
public function orderPay(array $data, array $extra = []): array
|
||
|
{
|
||
|
|
||
|
// 创建订单信息
|
||
|
$this->orderInfo = $this->createOrder($data);
|
||
|
// 订单支付事件
|
||
|
$this->orderPayEvent();
|
||
|
// 构建第三方支付请求的参数
|
||
|
$payment = $this->unifiedorder($extra);
|
||
|
$this->recordPaymentTrade($payment);
|
||
|
// 返回结果
|
||
|
return compact('payment');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 记录第三方交易信息
|
||
|
* @param array $payment 第三方支付数据
|
||
|
* @throws BaseException
|
||
|
* @throws DataNotFoundException
|
||
|
* @throws DbException
|
||
|
* @throws ModelNotFoundException
|
||
|
*/
|
||
|
private function recordPaymentTrade(array $payment): void
|
||
|
{
|
||
|
if ($this->method != PaymentMethodEnum::BALANCE) {
|
||
|
PaymentTradeModel::record(
|
||
|
$this->orderInfo,
|
||
|
$this->method,
|
||
|
$this->client,
|
||
|
OrderTypeEnum::WHOLESALER,
|
||
|
$payment
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 返回消息提示
|
||
|
* @return string
|
||
|
*/
|
||
|
public function getMessage(): string
|
||
|
{
|
||
|
return $this->message;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取支付方式的配置信息
|
||
|
* @return mixed
|
||
|
* @throws BaseException
|
||
|
* @throws DataNotFoundException
|
||
|
* @throws DbException
|
||
|
* @throws ModelNotFoundException
|
||
|
*/
|
||
|
private function getPaymentConfig()
|
||
|
{
|
||
|
$PaymentModel = new PaymentModel;
|
||
|
$templateInfo = $PaymentModel->getPaymentInfo($this->method, $this->client, $this->getStoreId());
|
||
|
return $templateInfo['template']['config'][$this->method];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 构建第三方支付请求的参数
|
||
|
* @param array $extra 附加数据
|
||
|
* @return array
|
||
|
* @throws BaseException
|
||
|
* @throws DataNotFoundException
|
||
|
* @throws DbException
|
||
|
* @throws ModelNotFoundException
|
||
|
*/
|
||
|
private function unifiedorder(array $extra = []): array
|
||
|
{
|
||
|
// 判断支付方式是否合法
|
||
|
if (!in_array($this->method, [PaymentMethodEnum::WECHAT, PaymentMethodEnum::ALIPAY])) {
|
||
|
return [];
|
||
|
}
|
||
|
// 生成第三方交易订单号 (并非主订单号)
|
||
|
$outTradeNo = OrderService::createOrderNo();
|
||
|
// 获取支付方式的配置信息
|
||
|
$options = $this->getPaymentConfig();
|
||
|
// 整理下单接口所需的附加数据
|
||
|
$extra = $this->extraAsUnify($extra);
|
||
|
// 构建支付模块
|
||
|
$Payment = PaymentFacade::store($this->method)->setOptions($options, $this->client);
|
||
|
// 执行第三方支付下单API
|
||
|
if (!$Payment->unify($outTradeNo, (string)$this->orderInfo['pay_price'], $extra)) {
|
||
|
throwError('第三方支付下单API调用失败');
|
||
|
}
|
||
|
// 返回客户端需要的支付参数
|
||
|
return $Payment->getUnifyResult();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 整理下单接口所需的附加数据
|
||
|
* @param array $extra
|
||
|
* @return array
|
||
|
* @throws BaseException
|
||
|
*/
|
||
|
private function extraAsUnify(array $extra = []): array
|
||
|
{
|
||
|
// 微信支付时需要的附加数据
|
||
|
if ($this->method === PaymentMethodEnum::WECHAT) {
|
||
|
// 微信小程序端和微信公众号端需要openid
|
||
|
if (in_array($this->client, [ClientEnum::WXOFFICIAL, ClientEnum::MP_WEIXIN])) {
|
||
|
$extra['openid'] = $this->getWechatOpenid();
|
||
|
}
|
||
|
}
|
||
|
// 支付宝支付时需要的附加数据
|
||
|
if ($this->method === PaymentMethodEnum::ALIPAY) {
|
||
|
// 支付宝小程序端需要buyerId
|
||
|
if ($this->client == ClientEnum::MP_ALIPAY) {
|
||
|
$extra['buyerId'] = $this->getAlipayBuyerId();
|
||
|
}
|
||
|
}
|
||
|
return $extra;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取微信端的用户openid(仅微信小程序和微信公众号)
|
||
|
* @return null
|
||
|
* @throws BaseException
|
||
|
*/
|
||
|
private function getWechatOpenid()
|
||
|
{
|
||
|
if (in_array($this->client, [ClientEnum::MP_WEIXIN, ClientEnum::WXOFFICIAL])) {
|
||
|
// 当前登录用户信息
|
||
|
$useInfo = UserService::getCurrentLoginUser(true);
|
||
|
if (!$useInfo['currentOauth'] || empty($useInfo['currentOauth']['oauth_id'])) {
|
||
|
throwError('很抱歉,您当前不存在openid 无法发起微信支付');
|
||
|
}
|
||
|
// 当前第三方用户标识
|
||
|
return $useInfo['currentOauth']['oauth_id'];
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 订单支付事件
|
||
|
* @return void
|
||
|
* @throws BaseException
|
||
|
* @author: wanghousheng
|
||
|
*/
|
||
|
private function orderPayEvent(): void
|
||
|
{
|
||
|
// 余额支付
|
||
|
if ($this->method == PaymentMethodEnum::BALANCE) {
|
||
|
$this->orderPaySuccess($this->orderInfo['order_no']);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 订单支付成功事件
|
||
|
* @param string $orderNo 当前订单号
|
||
|
* @param int|null $tradeId
|
||
|
* @param array $paymentData
|
||
|
* @return void
|
||
|
* @throws BaseException
|
||
|
* @author: wanghousheng
|
||
|
*/
|
||
|
private function orderPaySuccess(string $orderNo, ?int $tradeId = null, array $paymentData = []): void
|
||
|
{
|
||
|
$service = new PaySuccess();
|
||
|
// 订单支付成功业务处理
|
||
|
$service->setOrderNo($orderNo)
|
||
|
->setMethod($this->method)
|
||
|
->setTradeId($tradeId)
|
||
|
->setPaymentData($paymentData);
|
||
|
if (!$service->handle()) {
|
||
|
throwError($service->getError() ?: '申请失败');
|
||
|
}
|
||
|
$this->message = '恭喜您,申请成功';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 查询订单是否支付成功 (仅限第三方支付订单)
|
||
|
* @param string $outTradeNo 商户订单号
|
||
|
* @return bool
|
||
|
* @throws BaseException
|
||
|
* @throws DataNotFoundException
|
||
|
* @throws DbException
|
||
|
* @throws ModelNotFoundException
|
||
|
*/
|
||
|
public function tradeQuery(string $outTradeNo): bool
|
||
|
{
|
||
|
// 获取支付方式的配置信息
|
||
|
$options = $this->getPaymentConfig();
|
||
|
// 构建支付模块
|
||
|
$Payment = PaymentFacade::store($this->method)->setOptions($options, $this->client);
|
||
|
// 执行第三方支付查询API
|
||
|
$result = $Payment->tradeQuery($outTradeNo);
|
||
|
// 订单支付成功事件
|
||
|
if (!empty($result) && $result['paySuccess']) {
|
||
|
// 获取第三方交易记录信息
|
||
|
$tradeInfo = PaymentTradeModel::detailByOutTradeNo($outTradeNo);
|
||
|
// 订单支付成功事件
|
||
|
$this->orderPaySuccess($tradeInfo['order_no'], $tradeInfo['trade_id'], $result);
|
||
|
}
|
||
|
// 返回订单状态
|
||
|
return $result ? $result['paySuccess'] : false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取支付宝端的用户buyerId(仅支付宝小程序端)
|
||
|
* @return null
|
||
|
* @throws BaseException
|
||
|
*/
|
||
|
private function getAlipayBuyerId()
|
||
|
{
|
||
|
if ($this->client == ClientEnum::MP_ALIPAY) {
|
||
|
// 当前登录用户信息
|
||
|
$useInfo = UserService::getCurrentLoginUser(true);
|
||
|
if (!$useInfo['currentOauth'] || empty($useInfo['currentOauth']['oauth_id'])) {
|
||
|
throwError('很抱歉,您当前不存在buyerId 无法发起支付宝支付');
|
||
|
}
|
||
|
// 当前第三方用户标识
|
||
|
return $useInfo['currentOauth']['oauth_id'];
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 创建订单
|
||
|
* @param array $data
|
||
|
* @return Order
|
||
|
* @throws BaseException
|
||
|
*/
|
||
|
private function createOrder(array $data): Order
|
||
|
{
|
||
|
$model = new Order();
|
||
|
if (!$model->createOrder($data)) {
|
||
|
throwError($model->getError() ?: '创建订单失败');
|
||
|
}
|
||
|
$model['order_id'] = (int)$model['order_id'];
|
||
|
return $model;
|
||
|
}
|
||
|
}
|