shopxo/app/service/PayLogService.php

274 lines
10 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2021-07-18 23:42:10 +08:00
use think\facade\Db;
2018-12-28 18:58:37 +08:00
/**
* 支付日志服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class PayLogService
{
2019-05-07 17:39:55 +08:00
/**
* 支付日志添加
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-05-07T00:57:36+0800
* @param [array] $params [输入参数]
* @param [int] $user_id [用户id]
2020-07-28 21:15:47 +08:00
* @param [int] $business_ids [业务订单id]
2019-05-07 17:39:55 +08:00
* @param [float] $total_price [业务订单实际金额]
2020-07-28 21:15:47 +08:00
* @param [string] $subject [业务订单名称]
2020-07-29 19:55:53 +08:00
* @param [string] $business_type [业务类型,字符串(如:订单、钱包充值、会员购买、等...]
2020-07-28 21:15:47 +08:00
* @return [boolean] [成功true, 失败false]
*/
public static function PayLogInsert($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'business_ids',
'error_msg' => '业务id为空',
],
[
'checked_type' => 'is_array',
'key_name' => 'business_ids',
'error_msg' => '业务id数据类型有误',
],
[
'checked_type' => 'empty',
'key_name' => 'user_id',
'error_msg' => '用户id为空',
],
[
'checked_type' => 'empty',
'key_name' => 'business_type',
'error_msg' => '业务类型为空',
],
[
'checked_type' => 'isset',
2020-07-28 21:15:47 +08:00
'key_name' => 'total_price',
'error_msg' => '业务金额为空',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 业务id
if(empty($params['business_ids']))
{
return DataReturn('业务id为空', -1);
}
// 日志主数据
$data = [
'log_no' => date('YmdHis').GetNumberCode(6),
'user_id' => intval($params['user_id']),
'total_price' => PriceNumberFormat($params['total_price']),
'business_type' => trim($params['business_type']),
'subject' => isset($params['subject']) ? $params['subject'] : '',
'payment' => isset($params['payment']) ? $params['payment'] : '',
'payment_name' => isset($params['payment_name']) ? $params['payment_name'] : '',
'add_time' => time(),
];
$pay_log_id = Db::name('PayLog')->insertGetId($data);
if($pay_log_id > 0)
{
$business_nos = isset($params['business_nos']) && is_array($params['business_nos']) ? $params['business_nos'] : [];
$value_data = [];
foreach($params['business_ids'] as $bk=>$bv)
{
$value_data[] = [
'pay_log_id' => $pay_log_id,
'business_id' => $bv,
'business_no' => isset($business_nos[$bk]) ? trim($business_nos[$bk]) : '',
'add_time' => time(),
];
}
$res = Db::name('PayLogValue')->insertAll($value_data);
if($res >= count($params['business_ids']))
{
2021-06-03 14:15:24 +08:00
// 日志 id 加入数组中
2020-07-28 21:15:47 +08:00
$data['id'] = $pay_log_id;
2021-06-03 14:15:24 +08:00
// 支付日志添加成功钩子
$hook_name = 'plugins_service_paylog_insert_success';
2021-07-18 23:42:10 +08:00
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
2021-06-03 14:15:24 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'params' => $params,
'data' => $data,
]));
if(isset($ret['code']) && $ret['code'] != 0)
{
return $ret;
}
// 返回成功
return DataReturn(MyLang('common.insert_success'), 0, $data);
2020-07-28 21:15:47 +08:00
}
}
return DataReturn('支付订单添加失败', -100);
}
/**
2022-07-23 22:24:53 +08:00
* 支付日志更新
2020-07-28 21:15:47 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-27
* @desc description
* @param [int] $log_id [支付日志id]
2019-05-07 17:39:55 +08:00
* @param [string] $trade_no [支付平台交易号]
* @param [string] $buyer_user [支付平台用户帐号]
* @param [float] $pay_price [支付金额]
* @param [string] $payment [支付方式标记]
* @param [string] $payment_name [支付方式名称]
*/
2020-07-28 21:15:47 +08:00
public static function PayLogSuccess($params = [])
2019-05-07 17:39:55 +08:00
{
2020-07-28 21:15:47 +08:00
// 参数
if(empty($params['log_id']))
{
return DataReturn('日志id有误', -100);
}
// 更新数据
2019-05-07 17:39:55 +08:00
$data = [
'trade_no' => isset($params['trade_no']) ? $params['trade_no'] : '',
2019-05-07 18:42:57 +08:00
'buyer_user' => isset($params['buyer_user']) ? $params['buyer_user'] : '',
2019-05-07 17:39:55 +08:00
'pay_price' => isset($params['pay_price']) ? PriceNumberFormat($params['pay_price']) : 0.00,
2019-05-07 18:42:57 +08:00
'subject' => isset($params['subject']) ? $params['subject'] : '',
'payment' => isset($params['payment']) ? $params['payment'] : '',
'payment_name' => isset($params['payment_name']) ? $params['payment_name'] : '',
2020-07-28 21:15:47 +08:00
'status' => 1,
'pay_time' => time(),
2019-05-07 17:39:55 +08:00
];
2020-07-28 21:15:47 +08:00
if(Db::name('PayLog')->where(['id'=>intval($params['log_id']), 'status'=>0])->update($data))
{
return DataReturn('日志订单更新成功', 0);
}
return DataReturn('日志订单更新失败', -100);
2019-05-07 17:39:55 +08:00
}
2018-12-28 18:58:37 +08:00
/**
* 获取支付日志类型
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-23T02:22:03+0800
* @param [array] $params [输入参数]
*/
public static function PayLogTypeList($params = [])
2018-12-28 18:58:37 +08:00
{
2021-07-18 23:42:10 +08:00
$data = Db::name('PayLog')->field('payment as id, payment_name as name')->group('payment,payment_name')->select()->toArray();
return DataReturn(MyLang('common.handle_success'), 0, $data);
2018-12-28 18:58:37 +08:00
}
2022-08-02 17:23:10 +08:00
2018-12-28 18:58:37 +08:00
/**
2022-08-02 17:23:10 +08:00
* 列表数据处理
* @author Devil
2018-12-28 18:58:37 +08:00
* @blog http://gong.gg/
* @version 1.0.0
2022-08-02 17:23:10 +08:00
* @date 2022-08-01
2018-12-28 18:58:37 +08:00
* @desc description
2022-08-02 17:23:10 +08:00
* @param [array] $data [数据列表]
2018-12-28 18:58:37 +08:00
* @param [array] $params [输入参数]
*/
2022-08-02 17:23:10 +08:00
public static function PayLogListHandle($data, $params = [])
2018-12-28 18:58:37 +08:00
{
if(!empty($data))
{
2020-12-09 11:54:55 +08:00
// 字段列表
$keys = ArrayKeys($data);
2020-07-26 23:49:08 +08:00
// 获取支付业务关联数据
$log_value_list = [];
2021-07-18 23:42:10 +08:00
$log_value = Db::name('PayLogValue')->field('pay_log_id,business_id,business_no')->where(['pay_log_id'=>array_column($data, 'id')])->select()->toArray();
2020-07-26 23:49:08 +08:00
if(!empty($log_value))
{
foreach($log_value as $lv)
{
$log_value_list[$lv['pay_log_id']][] = $lv;
}
2020-12-09 14:48:33 +08:00
}
2020-12-09 11:54:55 +08:00
2020-12-09 14:48:33 +08:00
// 用户列表
2020-12-09 11:54:55 +08:00
if(in_array('user_id', $keys) && isset($params['is_public']) && $params['is_public'] == 0)
{
$user_list = UserService::GetUserViewInfo(array_column($data, 'user_id'));
}
2020-07-26 23:49:08 +08:00
// 循环处理数据
2018-12-28 18:58:37 +08:00
foreach($data as &$v)
{
2020-06-27 22:48:16 +08:00
// 用户信息
if(isset($v['user_id']))
{
if(isset($params['is_public']) && $params['is_public'] == 0)
{
2020-12-09 11:54:55 +08:00
$v['user'] = (!empty($user_list) && is_array($user_list) && array_key_exists($v['user_id'], $user_list)) ? $user_list[$v['user_id']] : [];
2020-06-27 22:48:16 +08:00
}
}
2018-12-28 18:58:37 +08:00
2020-07-26 23:49:08 +08:00
// 关联业务数据
$v['business_list'] = isset($log_value_list[$v['id']]) ? $log_value_list[$v['id']] : [];
2018-12-28 18:58:37 +08:00
// 时间
2020-07-26 23:49:08 +08:00
$v['add_time'] = empty($v['add_time']) ? '' : date('Y-m-d H:i:s', $v['add_time']);
$v['pay_time'] = empty($v['pay_time']) ? '' : date('Y-m-d H:i:s', $v['pay_time']);
$v['close_time'] = empty($v['close_time']) ? '' : date('Y-m-d H:i:s', $v['close_time']);
2018-12-28 18:58:37 +08:00
}
}
2022-08-02 17:23:10 +08:00
return $data;
2018-12-28 18:58:37 +08:00
}
2020-07-28 23:28:06 +08:00
/**
* 关闭
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-18
* @desc description
* @param [array] $params [输入参数]
*/
public static function PayLogClose($params = [])
{
// 参数是否有误
if(empty($params['ids']))
{
return DataReturn('操作id有误', -1);
}
// 是否数组
if(!is_array($params['ids']))
{
$params['ids'] = explode(',', $params['ids']);
}
// 关闭操作
2020-08-13 23:18:13 +08:00
if(Db::name('PayLog')->where(['id'=>$params['ids'], 'status'=>0])->update(['status'=>2, 'close_time'=>time()]))
2020-07-28 23:28:06 +08:00
{
return DataReturn(MyLang('common.close_success'), 0);
2020-07-28 23:28:06 +08:00
}
return DataReturn(MyLang('common.close_fail'), -100);
2020-07-28 23:28:06 +08:00
}
2018-12-28 18:58:37 +08:00
}
?>