vr-shopxo-source/application/plugins/wallet/Service.php

252 lines
8.5 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\plugins\wallet;
use think\Db;
use app\service\PluginsService;
use app\service\ResourcesService;
use app\service\PaymentService;
/**
* 会员等级服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class Service
{
// 基础数据附件字段
public static $base_config_attachment_field = [
'default_level_images'
];
// 等级规则
public static $members_level_rules_list = [
0 => ['value' => 0, 'name' => '积分(可用积分)', 'checked' => true],
1 => ['value' => 1, 'name' => '消费总额(已完成订单)'],
];
/**
* 充值订单创建
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-04-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function RechargeCreate($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'user',
'error_msg' => '用户信息有误',
],
[
'checked_type' => 'fun',
'key_name' => 'money',
'checked_data' => 'CheckPrice',
'error_msg' => '请输入有效的充值金额',
],
[
'checked_type' => 'min',
'key_name' => 'money',
'checked_data' => 0.01,
'error_msg' => '请输入大于0的充值金额',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 添加
$data = [
'recharge_no' => date('YmdHis').GetNumberCode(6),
'user_id' => $params['user']['id'],
'money' => PriceNumberFormat($params['money']),
'status' => 0,
'add_time' => time(),
];
$params['recharge_id'] = Db::name('PluginsWalletRecharge')->insertGetId($data);
if($params['recharge_id'] > 0)
{
return self::Pay($params);
}
return DataReturn('添加失败', -100);
}
/**
* 支付
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-04-29
* @desc description
* @param array $params [description]
*/
public static function Pay($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'user',
'error_msg' => '用户信息有误',
],
[
'checked_type' => 'empty',
'key_name' => 'recharge_id',
'error_msg' => '充值日志id不能为空',
],
[
'checked_type' => 'empty',
'key_name' => 'payment_id',
'error_msg' => '请选择支付方式',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 支付方式;
$payment = PaymentService::PaymentList(['where'=>['id'=>intval($params['payment_id']), 'is_enable'=>1, 'is_open_user'=>1]]);
if(empty($payment[0]))
{
return DataReturn('支付方式有误', -1);
}
// 支付入口文件检查
$pay_checked = PaymentService::EntranceFileChecked($payment[0]['payment'], 'wallet');
if($pay_checked['code'] != 0)
{
// 入口文件不存在则创建
$payment_params = [
'payment' => $payment[0]['payment'],
'business' => [
['name' => 'Wallet', 'desc' => '钱包'],
],
'respond' => '/index/plugins/index/pluginsname/wallet/pluginscontrol/wallet/pluginsaction/respond',
'notify' => '/index/plugins/index/pluginsname/wallet/pluginscontrol/wallet/pluginsaction/notify',
];
$ret = PaymentService::PaymentEntranceCreated($payment_params);
if($ret['code'] != 0)
{
return $ret;
}
}
// 非线上支付方式不可用
if(in_array($payment[0]['payment'], config('shopxo.under_line_list')))
{
return DataReturn('不能使用非线上支付方式进行充值', -10);
}
// 获取充值数据
$recharge = Db::name('PluginsWalletRecharge')->where(['id'=>intval($params['recharge_id'])])->find();
if(empty($recharge))
{
return DataReturn('充值数据不存在', -1);
}
if($recharge['status'] == 1)
{
return DataReturn('该数据已充值,请重新创建充值订单', -2);
}
// 回调地址
$url = __MY_URL__.'payment_wallet_'.strtolower($payment[0]['payment']);
// url模式, pathinfo模式下采用自带url生成url, 避免非index.php多余
if(MyC('home_seo_url_model', 0) == 0)
{
$call_back_url = $url.'_respond.php';
} else {
$call_back_url = PluginsHomeUrl('wallet', 'wallet', 'respond', ['paymentname'=>$payment[0]['payment']]);
if(stripos($call_back_url, '?') !== false)
{
$call_back_url = $url.'_respond.php';
}
}
// 发起支付
$pay_data = array(
'user' => $params['user'],
'out_user' => md5($params['user']['id']),
'order_id' => $recharge['id'],
'order_no' => $recharge['recharge_no'],
'name' => '钱包充值',
'total_price' => $recharge['money'],
'notify_url' => $url.'_notify.php',
'call_back_url' => $call_back_url,
'site_name' => MyC('home_site_name', 'ShopXO', true),
'ajax_url' => PluginsHomeUrl('wallet', 'wallet', 'paycheck')
);
$pay_name = 'payment\\'.$payment[0]['payment'];
$ret = (new $pay_name($payment[0]['config']))->Pay($pay_data);
if(isset($ret['code']) && $ret['code'] == 0)
{
return $ret;
}
return DataReturn(empty($ret['msg']) ? '支付接口异常' : $ret['msg'], -1);
}
/**
* 支付状态校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-01-08
* @desc description
* @param [array] $params [输入参数]
*/
public static function RechargePayCheck($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_no',
'error_msg' => '充值单号有误',
],
[
'checked_type' => 'empty',
'key_name' => 'user',
'error_msg' => '用户信息有误',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 获取订单状态
$where = ['recharge_no'=>$params['order_no'], 'user_id'=>$params['user']['id']];
$recharge = Db::name('PluginsWalletRecharge')->where($where)->field('id,status')->find();
if(empty($recharge))
{
return DataReturn('充值数据不存在', -400, ['url'=>__MY_URL__]);
}
if($recharge['status'] == 1)
{
return DataReturn('支付成功', 0, ['url'=>PluginsHomeUrl('wallet', 'wallet', 'recharge')]);
}
return DataReturn('支付中', -300);
}
}
?>