You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
11 KiB
250 lines
11 KiB
10 months ago
|
<?php
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | CRMEB [ CRMEB赋能开发者,助力企业发展aaaa ]
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
|
||
|
// +----------------------------------------------------------------------
|
||
|
|
||
|
namespace app\web\model\material;
|
||
|
|
||
|
use traits\ModelTrait;
|
||
|
use basic\ModelBasic;
|
||
|
use app\web\model\user\User;
|
||
|
use app\web\model\user\UserBill;
|
||
|
use app\wap\model\user\WechatUser;
|
||
|
use service\HookService;
|
||
|
use service\SystemConfigService;
|
||
|
use service\WechatService;
|
||
|
use service\WechatTemplateService;
|
||
|
use service\AlipayTradeWapService;
|
||
|
use app\wap\model\wap\SmsTemplate;
|
||
|
use app\web\model\merchant\MerchantFlowingWater;
|
||
|
use app\wap\model\institution\InstitutionFlowingWater;
|
||
|
use think\Cache;
|
||
|
use think\Url;
|
||
|
|
||
|
/**
|
||
|
* 资料订单 model
|
||
|
* Class DataDownloadOrder
|
||
|
*/
|
||
|
class DataDownloadOrder extends ModelBasic
|
||
|
{
|
||
|
use ModelTrait;
|
||
|
|
||
|
protected $insert = ['add_time'];
|
||
|
|
||
|
protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'zhifubao' => '支付宝','toutiao' => '抖音支付','kuaishou' => '快手支付'];
|
||
|
|
||
|
protected function setAddTimeAttr()
|
||
|
{
|
||
|
return time();
|
||
|
}
|
||
|
|
||
|
public static function getNewOrderId()
|
||
|
{
|
||
|
return getNewOrderId('wx');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 创建订单资料订单
|
||
|
* @param $special
|
||
|
* @param $pinkId
|
||
|
* @param $pay_type
|
||
|
* @param $uid
|
||
|
* @param $payType
|
||
|
* @param int $link_pay_uid
|
||
|
* @param int $total_num
|
||
|
* @return bool|object
|
||
|
*/
|
||
|
public static function createDataDownloadOrder($data, $uid, $payType, $total_num = 1)
|
||
|
{
|
||
|
if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
|
||
|
$userInfo = User::getUserData($uid);
|
||
|
if (!$userInfo) return self::setErrorInfo('用户不存在!');
|
||
|
$total_price = $data->money;
|
||
|
if (isset($userInfo['level']) && $userInfo['level'] > 0 && $data->member_pay_type == 1 && $data->member_money > 0) {
|
||
|
$total_price = $data->member_money;
|
||
|
} else if (isset($userInfo['level']) && $userInfo['level'] > 0 && $data->member_pay_type == 0) {
|
||
|
return self::setErrorInfo('会员免费,该资料您不需要支付!');
|
||
|
}
|
||
|
$res = DataDownloadBuy::PayDataDownload($data->id, $uid);
|
||
|
if ($res) return self::setErrorInfo('您已购买资料,无需再次购买!');
|
||
|
$orderInfo = [
|
||
|
'uid' => $uid,
|
||
|
'mer_id' => $data->mer_id,
|
||
|
'order_id' => self::getNewOrderId(),
|
||
|
'data_id' => $data->id,
|
||
|
'total_num' => $total_num,
|
||
|
'total_price' => $total_price,
|
||
|
'pay_price' => $total_price,
|
||
|
'pay_type' => $payType,
|
||
|
'paid' => 0,
|
||
|
'unique' => md5(time() . '' . $uid . $data->id),
|
||
|
'is_del' => 0
|
||
|
];
|
||
|
$order = self::set($orderInfo);
|
||
|
if (!$order) return self::setErrorInfo('订单生成失败!');
|
||
|
return $order;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 微信支付 为 0元时 资料
|
||
|
* @param $order_id
|
||
|
* @param $uid
|
||
|
* @return bool
|
||
|
*/
|
||
|
public static function jsPayDataDownloadPrice($order_id, $uid)
|
||
|
{
|
||
|
$orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
|
||
|
if (!$orderInfo) return self::setErrorInfo('订单不存在!');
|
||
|
if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
|
||
|
$userInfo = User::getUserData($uid);
|
||
|
if (!$userInfo) return self::setErrorInfo('用户不存在!');
|
||
|
self::beginTrans();
|
||
|
$res1 = UserBill::expend('购买资料', $uid, 'now_money', 'pay_data_download', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '支付' . floatval($orderInfo['pay_price']) . '元购买资料');
|
||
|
$res2 = self::payDataDownloadSuccess($order_id);
|
||
|
$res = $res1 && $res2;
|
||
|
self::checkTrans($res);
|
||
|
return $res;
|
||
|
}
|
||
|
|
||
|
/**资料微信支付
|
||
|
* @param $orderId
|
||
|
* @param string $field
|
||
|
* @return array|string
|
||
|
* @throws \think\db\exception\DataNotFoundException
|
||
|
* @throws \think\db\exception\ModelNotFoundException
|
||
|
* @throws \think\exception\DbException
|
||
|
*/
|
||
|
public static function jsDataDownloadPay($orderId, $field = 'order_id')
|
||
|
{
|
||
|
if (is_string($orderId))
|
||
|
$orderInfo = self::where($field, $orderId)->where('is_del', 0)->find();
|
||
|
else
|
||
|
$orderInfo = $orderId;
|
||
|
if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
|
||
|
if ($orderInfo['paid']) exception('支付已支付!');
|
||
|
if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
|
||
|
$openid = WechatUser::uidToOpenid($orderInfo['uid']);
|
||
|
return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'datadownload', SystemConfigService::get('site_name'));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 微信扫码支付
|
||
|
* @param $orderId
|
||
|
* @param string $field
|
||
|
* @return array|string
|
||
|
* @throws \think\db\exception\DataNotFoundException
|
||
|
* @throws \think\db\exception\ModelNotFoundException
|
||
|
* @throws \think\exception\DbException
|
||
|
*/
|
||
|
public static function nativeDataDownloadPay($orderId, $field = 'order_id')
|
||
|
{
|
||
|
if (is_string($orderId))
|
||
|
$orderInfo = self::where($field, $orderId)->find();
|
||
|
else
|
||
|
$orderInfo = $orderId;
|
||
|
if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
|
||
|
if ($orderInfo['paid']) exception('支付已支付!');
|
||
|
if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
|
||
|
$site_name = SystemConfigService::get('site_name');
|
||
|
if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
|
||
|
return WechatService::nativePay(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'datadownload', self::getSubstrUTf8($site_name . '-资料购买', 30), '', 'NATIVE');
|
||
|
}
|
||
|
|
||
|
/**资料购买支付宝扫码支付
|
||
|
* @param $orderId
|
||
|
* @param string $field
|
||
|
* @return mixed|\SimpleXMLElement|string|\提交表单HTML文本
|
||
|
* @throws \think\db\exception\DataNotFoundException
|
||
|
* @throws \think\db\exception\ModelNotFoundException
|
||
|
* @throws \think\exception\DbException
|
||
|
*/
|
||
|
public static function alipayAativeDataDownloadPay($orderId, $field = 'order_id')
|
||
|
{
|
||
|
if (is_string($orderId))
|
||
|
$orderInfo = self::where($field, $orderId)->find();
|
||
|
else
|
||
|
$orderInfo = $orderId;
|
||
|
if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
|
||
|
if ($orderInfo['paid']) exception('支付已支付!');
|
||
|
if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
|
||
|
return AlipayTradeWapService::init()->AliPayNative($orderInfo['order_id'], $orderInfo['pay_price'], '资料购买', 'datadownload');
|
||
|
}
|
||
|
|
||
|
/**资料余额支付
|
||
|
* @param $order_id
|
||
|
* @param $uid
|
||
|
* @return bool
|
||
|
* @throws \think\db\exception\DataNotFoundException
|
||
|
* @throws \think\db\exception\ModelNotFoundException
|
||
|
* @throws \think\exception\DbException
|
||
|
*/
|
||
|
public static function yueDataDownloadPay($order_id, $uid)
|
||
|
{
|
||
|
$orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
|
||
|
if (!$orderInfo) return self::setErrorInfo('订单不存在!');
|
||
|
if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
|
||
|
if ($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
|
||
|
$userInfo = User::getUserData($uid);
|
||
|
if ($userInfo['now_money'] < $orderInfo['pay_price']) return self::setErrorInfo('余额不足' . floatval($orderInfo['pay_price']));
|
||
|
self::beginTrans();
|
||
|
$res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
|
||
|
$res2 = UserBill::expend('购买资料', $uid, 'now_money', 'pay_data_download', $orderInfo['pay_price'], $orderInfo['id'], bcsub($userInfo['now_money'], $orderInfo['pay_price'], 2), '余额支付' . floatval($orderInfo['pay_price']) . '元购买资料');
|
||
|
$res3 = self::payDataDownloadSuccess($order_id);
|
||
|
$res = $res1 && $res2 && $res3;
|
||
|
self::checkTrans($res);
|
||
|
return $res;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* //TODO 资料支付成功后
|
||
|
* @param $orderId
|
||
|
*/
|
||
|
public static function payDataDownloadSuccess($orderId)
|
||
|
{
|
||
|
$order = self::where('order_id', $orderId)->where('is_del', 0)->find();
|
||
|
if (!$order) return false;
|
||
|
$res1 = self::where('order_id', $orderId)->where('is_del', 0)->update(['paid' => 1, 'pay_time' => time()]);
|
||
|
$res2 = true;
|
||
|
$res3 = true;
|
||
|
$res4 = true;
|
||
|
$res5 = true;
|
||
|
if ($res1) {
|
||
|
try {
|
||
|
if ($order['pay_type'] != 'yue') {
|
||
|
$res2 = UserBill::expend('购买资料', $order['uid'], $order['pay_type'], 'pay_data_download', $order['pay_price'], $order['id'], 0, '支付' . floatval($order['pay_price']) . '元购买资料');
|
||
|
}
|
||
|
$res3 = MerchantFlowingWater::setMerchantFlowingWater($order, 3);
|
||
|
$res4 = InstitutionFlowingWater::setInstitutionFlowingWater($order, 3); // 机构
|
||
|
$res5 = MerchantFlowingWater::setAgentFlowingWater($order, 3); // 代理
|
||
|
DataDownloadBuy::setUserDataDownload($orderId, $order['data_id'], $order['uid'], 0);
|
||
|
} catch (\Throwable $e) {
|
||
|
}
|
||
|
}
|
||
|
$site_url = SystemConfigService::get('site_url');
|
||
|
try {
|
||
|
WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
|
||
|
'first' => '亲,您购买的资料已支付成功',
|
||
|
'keyword1' => $orderId,
|
||
|
'keyword2' => $order['pay_price'],
|
||
|
'remark' => '点击查看订单详情'
|
||
|
], $site_url . Url::build('wap/material/my_material'));
|
||
|
WechatTemplateService::sendAdminNoticeTemplate($order['mer_id'],[
|
||
|
'first' => "亲,您有一个新的资料购买订单",
|
||
|
'keyword1' => $orderId,
|
||
|
'keyword2' => $order['pay_price'],
|
||
|
'remark' => '请及时处理'
|
||
|
]);
|
||
|
} catch (\Throwable $e) {
|
||
|
}
|
||
|
$res = $res1 && $res2 && $res3 && $res4 && $res5;
|
||
|
return false !== $res;
|
||
|
}
|
||
|
|
||
|
}
|