85 lines
1.9 KiB
PHP
85 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace app\common\library\pay;
|
|
|
|
use fast\Http;
|
|
use think\Cache;
|
|
use think\Session;
|
|
use app\common\model\order\Order;
|
|
use app\common\library\Auth;
|
|
use think\Db;
|
|
use app\common\model\User;
|
|
use app\common\exception\Exception;
|
|
/**
|
|
* 余额
|
|
*
|
|
*/
|
|
class Balance
|
|
{
|
|
protected $auth;
|
|
protected $order;
|
|
public function __construct()
|
|
{
|
|
$this->auth = new Auth();
|
|
$this->order = new Order();
|
|
}
|
|
|
|
/**
|
|
* 余额支付
|
|
* @param $params 订单信息
|
|
* @return bool
|
|
*/
|
|
public function pay($order){
|
|
|
|
$userInfo = $this->getOrderUser($order['order_no']);
|
|
|
|
if(!$userInfo){
|
|
throw new Exception('获取用户信息失败');
|
|
}
|
|
|
|
//余额不足
|
|
if($userInfo['money'] < $order['amount']){
|
|
throw new Exception('余额不足');
|
|
}
|
|
|
|
$order = Db::transaction(function () use ($order,$userInfo) {
|
|
//扣减余额
|
|
User::money(($order['amount'] * -1), $userInfo['id'], "余额支付");
|
|
// 支付后流程
|
|
$notify = [
|
|
'order_no' => $order['order_no'],
|
|
'transaction_id' => '',
|
|
'notify_time' => date('Y-m-d H:i:s'),
|
|
'buyer_email' => $userInfo['id'],
|
|
'pay_fee' => $order['amount'],
|
|
'pay_type' => 'balance' // 支付方式
|
|
];
|
|
$notify['payment_json'] = json_encode($notify);
|
|
|
|
return Order::paySuccess($order, $notify);
|
|
|
|
});
|
|
|
|
|
|
return $order;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取订单用户信息
|
|
* @param $orderNo
|
|
* @return false
|
|
*/
|
|
public function getOrderUser($orderNo)
|
|
{
|
|
$orderInfo = Order::orderNoGetOrder($orderNo);
|
|
|
|
if(!$orderInfo){
|
|
return false;
|
|
}
|
|
|
|
$userInfo = $this->auth->getUserinfo($orderInfo['user_id']);
|
|
|
|
return $userInfo;
|
|
}
|
|
} |