CRMEB_PRO_M/app/services/pay/RechargeServices.php

89 lines
3.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\pay;
use app\services\user\UserRechargeServices;
use app\services\wechat\WechatUserServices;
use think\exception\ValidateException;
/**
*
* Class RechargeServices
* @package app\services\pay
*/
class RechargeServices
{
protected $pay;
/**
* RechargeServices constructor.
* @param PayServices $pay
*/
public function __construct(PayServices $pay)
{
$this->pay = $pay;
}
/**
* 充值订单支付
* @param string $order_id
* @param string $authCode
* @param string $payType
* @return array|string
*/
public function recharge(string $order_id, string $authCode = '', string $payType = '')
{
/** @var UserRechargeServices $rechargeServices */
$rechargeServices = app()->make(UserRechargeServices::class);
$recharge = $rechargeServices->getOne(['order_id' => $order_id]);
if (!$recharge) {
throw new ValidateException('订单失效或者不存在');
}
if ($recharge['paid'] == 1) {
throw new ValidateException('订单已支付');
}
if ($payType && $payType != $recharge['recharge_type']) {
$res = $rechargeServices->update(['order_id' => $order_id], ['recharge_type' => $payType]);
if (!$res) {
throw new ValidateException('订单支付方式修改失败');
}
}
$openid = '';
//没有付款码不是微信H5支付门店支付PC支付不再APP端需要判断用户openid
if (!$authCode && !in_array($recharge['recharge_type'], ['weixinh5', 'store', 'pc', 'alipay']) && !request()->isApp()) {
$userType = '';
switch ($recharge['recharge_type']) {
case 'weixin':
case 'weixinh5':
$userType = 'wechat';
break;
case 'routine':
$userType = 'routine';
break;
}
if (!$userType) {
throw new ValidateException('不支持该类型方式');
}
/** @var WechatUserServices $wechatUser */
$wechatUser = app()->make(WechatUserServices::class);
$openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
if (!$openid) {
throw new ValidateException('获取用户openid失败,无法支付');
}
}
return $this->pay->setAuthCode($authCode)->pay($recharge['recharge_type'], $openid, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
}
}