106 lines
3.2 KiB
PHP
106 lines
3.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace app\common\logic\order;
|
|
|
|
use app\common\enum\OrderEnum;
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\order\Cart;
|
|
use app\common\model\order\FinancialRecord;
|
|
use app\common\model\user\User;
|
|
use Exception;
|
|
use support\Log;
|
|
use taoser\exception\ValidateException;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 零售订单逻辑处理
|
|
*/
|
|
class RetailOrderLogic extends BaseLogic
|
|
{
|
|
|
|
/**
|
|
* 余额订单支付
|
|
* @param User $user
|
|
* @param $order
|
|
* @return bool
|
|
* @throws Exception
|
|
* @throws ValidateException
|
|
*/
|
|
static public function payBalance(User $user, $order)
|
|
{
|
|
if ($user['user_money'] < $order['actual']){
|
|
self::setError('余额不足,请更换支付方式');
|
|
return false;
|
|
}
|
|
Db::startTrans();
|
|
try {
|
|
$user->user_money = bcsub($user->user_money, $order['actual'], 2);
|
|
$user->save();
|
|
//用户支出流水
|
|
$record[] = [
|
|
'financial_record_sn' => time(),
|
|
'order_id' => $order['id'],
|
|
'number_sn' => $order['number'],
|
|
'user_id' => $order['customer'],
|
|
'financial_type' => OrderEnum::USER_ORDER_PAY,
|
|
'financial_pm' => OrderEnum::EXPENDITURE,
|
|
'number' => $order['actual'],
|
|
'status' => 1,
|
|
'type' => OrderEnum::USER,
|
|
'mer_id' => $order['merchant'],
|
|
];
|
|
//商户获得流水
|
|
$record[] = [
|
|
'financial_record_sn' => time(),
|
|
'order_id' => $order['id'],
|
|
'number_sn' => $order['number'],
|
|
'user_id' => $order['customer'],
|
|
'financial_type' => OrderEnum::MERCHANT_ORDER_OBTAINS,
|
|
'financial_pm' => OrderEnum::INCOME,
|
|
'number' => $order['actual'],
|
|
'status' => 0,
|
|
'type' => OrderEnum::MERCHANT,
|
|
'mer_id' => $order['merchant'],
|
|
];
|
|
(new FinancialRecord())->saveAll($record);
|
|
$CallbackData = ['money' => $order['actual']];
|
|
self::paySuccess($order, $CallbackData);
|
|
Db::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
Log::error('余额支付失败' . $e->getMessage() . '。line:' . $e->getLine() . '。file:' . $e->getFile());
|
|
self::setError('余额支付失败' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 订单支付成功
|
|
* @param $order 订单
|
|
* @param $CallbackData 回调数据
|
|
* @date 2021/7/8 00:40
|
|
*/
|
|
static function paySuccess($order, $CallbackData = [])
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
$order->money = $CallbackData['money'];
|
|
$order->paid = 1;
|
|
$order->save();
|
|
if($order['cart_id']){
|
|
$cart_arr=implode(',',$order['cart_id']);
|
|
Cart::whereIn('id',$cart_arr)->update(['is_pay'=>1]);
|
|
}
|
|
// 提交事务
|
|
Db::commit();
|
|
} catch (\Exception $e) {
|
|
// 回滚事务
|
|
Db::rollback();
|
|
}
|
|
// TODO: Implement paySuccess() method.
|
|
|
|
}
|
|
}
|