|
|
@@ -1,467 +0,0 @@
|
|
|
-<?php namespace Seller\Lib;
|
|
|
-use Dever;
|
|
|
-use Dever\Helper\Str;
|
|
|
-use Dever\Helper\Redis;
|
|
|
-class Order
|
|
|
-{
|
|
|
- # 新增订单 收单
|
|
|
- public function add($info, $goods, $sku, $account, $order, $num = 1)
|
|
|
- {
|
|
|
- $data = $this->addAct($info, $goods, $sku, $account, $order, $num);
|
|
|
- if ($data) {
|
|
|
- \Dever\Helper\Redis::push('submit_' . DEVER_PROJECT, $data['id']);
|
|
|
- }
|
|
|
- return $data;
|
|
|
- }
|
|
|
- public function addAct($info, $goods, $sku, $account, $order, $num = 1)
|
|
|
- {
|
|
|
- # 验证规则
|
|
|
- /*
|
|
|
- $cate = Dever::db('cate', 'goods')->find($goods['cate_id']);
|
|
|
- if ($cate && $cate['rule'] && !preg_match($cate['rule'], $account)) {
|
|
|
- Dever::error($account . '不符合规则');
|
|
|
- }*/
|
|
|
- # 验证订单号
|
|
|
- /*
|
|
|
- $check = Dever::db('order', 'seller')->find(array('seller_id' => $info['id'], 'seller_order_num' => $order));
|
|
|
- if ($check) {
|
|
|
- Dever::error('订单重复');
|
|
|
- }*/
|
|
|
-
|
|
|
- $cash = $this->cash($info['id'], $sku['info_id'], $sku['id'], $sku['value'], $num);
|
|
|
-
|
|
|
- # 开始下单
|
|
|
- $data = array();
|
|
|
- $data['status'] = 1;
|
|
|
- $data['finish'] = 2;
|
|
|
- $data['account'] = $account;
|
|
|
- $data['cate_id'] = $goods['cate_id'];
|
|
|
- $data['goods_id'] = $sku['info_id'];
|
|
|
- $data['sku_id'] = $sku['id'];
|
|
|
- $data['num'] = $num;
|
|
|
- $data['cash'] = $sku['value'];
|
|
|
- $data['price'] = $cash;
|
|
|
- $data['seller_id'] = $info['id'];
|
|
|
- $data['seller_order_num'] = $order;
|
|
|
- $data['seller_notify'] = Dever::input('notify');
|
|
|
- $data['id'] = Dever::db('seller/order')->insert($data);
|
|
|
- if (!$data['id']) {
|
|
|
- Dever::error('下单失败');
|
|
|
- }
|
|
|
-
|
|
|
- $seller['order_id'] = $data['id'];
|
|
|
- $seller['request'] = json_encode(Dever::input(), JSON_UNESCAPED_UNICODE);
|
|
|
- Dever::db('seller/order_seller')->insert($seller);
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- # 扣费
|
|
|
- public function cash($seller_id, $goods_id, $sku_id, $value, $num = 1)
|
|
|
- {
|
|
|
- if (!$value || $value <= 0) {
|
|
|
- Dever::error('面值无效');
|
|
|
- }
|
|
|
-
|
|
|
- # 查询折扣 扣费
|
|
|
- $seller_goods = Dever::db('seller/goods')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
- if (!$seller_goods) {
|
|
|
- $seller_goods = Dever::db('seller/goods')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => -1));
|
|
|
- }
|
|
|
-
|
|
|
- $info = Dever::db('seller/info')->find($seller_id);
|
|
|
- if ($seller_goods && $seller_goods['discount']) {
|
|
|
- $info['discount'] = $seller_goods['discount'];
|
|
|
- }
|
|
|
- if (!$info['discount']) {
|
|
|
- $info['discount'] = 1;
|
|
|
- }
|
|
|
- $cash = round($value * $info['discount'], 2) * $num;
|
|
|
-
|
|
|
- # 查询余额是否充足
|
|
|
- $info['yue'] = $info['credit'] + $info['cash'];
|
|
|
- if ($info['yue'] < $cash) {
|
|
|
- Dever::error('余额不足');
|
|
|
- }
|
|
|
- $state = Dever::load(Info::class)->dec($info['id'], $cash, $info['version']);
|
|
|
- if (!$state) {
|
|
|
- Dever::error('余额不足');
|
|
|
- }
|
|
|
- return $cash;
|
|
|
- }
|
|
|
-
|
|
|
- # 向渠道发起请求
|
|
|
- public function handle($info, $selected = array())
|
|
|
- {
|
|
|
- $data = $this->handleAct($info, $selected);
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- public function handleAct($info, $selected = array())
|
|
|
- {
|
|
|
- $lockKey = 'seller_order_lock_' . $info['id'];
|
|
|
- $lockToken = uniqid('order_', true);
|
|
|
- if (!Redis::lock($lockKey, $lockToken, 5)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- $test = Dever::input('test');
|
|
|
- if ($test == 1) {
|
|
|
-
|
|
|
- } elseif ($info['status'] >= 10) {
|
|
|
- return;
|
|
|
- }
|
|
|
- $update['status'] = 2;
|
|
|
- if (!$info['order_num']) {
|
|
|
- $info['order_num'] = $update['order_num'] = $this->createOrder();
|
|
|
- }
|
|
|
- if ($info['seller_callback'] == 1) {
|
|
|
- $info['seller_callback'] = 2;
|
|
|
- $update['seller_callback'] = 2;
|
|
|
- }
|
|
|
- Dever::db('seller/order')->update($info['id'], $update);
|
|
|
-
|
|
|
- $channel_num = 0;
|
|
|
- $channel = $this->channel($info['seller_id'], $info['goods_id'], $info['sku_id'], $channel_num, $selected);
|
|
|
- if (!$channel) {
|
|
|
- return $this->notify($info, '通道未开启', $update);
|
|
|
- }
|
|
|
- $result = array();
|
|
|
- if ($channel['type'] == 1) {
|
|
|
- # 通信
|
|
|
- # 向渠道发起请求 下单
|
|
|
- $param['order_id'] = $info['id'];
|
|
|
- $param['order_num'] = $info['order_num'];
|
|
|
- $param['account'] = $info['account'];
|
|
|
- $param['cash'] = $info['cash'];
|
|
|
- $param['num'] = $info['num'];
|
|
|
- $param['goods_id'] = $info['goods_id'];
|
|
|
- if ($info['other']) {
|
|
|
- $info['other'] = Dever::json_decode($info['other']);
|
|
|
- $param = array_merge($info['other'], $param);
|
|
|
- }
|
|
|
- if (isset($channel['goods']['discount']) && $channel['goods']['discount']) {
|
|
|
- $param['scash'] = $channel['goods']['discount']*$param['cash'];
|
|
|
- }
|
|
|
- if (isset($channel['goods']['code']) && $channel['goods']['code']) {
|
|
|
- $param['code'] = $channel['goods']['code'];
|
|
|
- } else {
|
|
|
- $sku = Dever::db('goods/info_sku')->find($info['sku_id']);
|
|
|
- $param['code'] = $sku['code'];
|
|
|
- }
|
|
|
- $result = Dever::load(\Connect\Lib\Func\Api::class)->run(1, $info['cate_id'], $channel, $param);
|
|
|
- if ($channel_num > 1 && $result && $result['status'] != 1) {
|
|
|
- # 记录渠道错误信息
|
|
|
- $selected[$channel['id']] = true;
|
|
|
- $order_error_data = Dever::db('seller/order_channel_error')->select(array('order_id' => $info['id']));
|
|
|
- if ($order_error_data) {
|
|
|
- foreach ($order_error_data as $k => $v) {
|
|
|
- $selected[$v['channel_id']] = true;
|
|
|
- }
|
|
|
- }
|
|
|
- $num = count($selected);
|
|
|
- $channel_num = $channel_num - $num;
|
|
|
- if ($channel_num > 0) {
|
|
|
- $order_error = $this->channel_record($info, $channel, $result);
|
|
|
- $order_error['order_id'] = $info['id'];
|
|
|
- Dever::db('seller/order_channel_error')->insert($order_error);
|
|
|
- return $this->handleAct($info, $selected);
|
|
|
- }
|
|
|
- }
|
|
|
- } elseif ($channel['type'] == 2) {
|
|
|
- # 需要审核
|
|
|
- # 获取卡密并占用
|
|
|
- $param['seller_id'] = array('in', '-1,' . $info['seller_id']);
|
|
|
- $param['channel_id'] = $channel['id'];
|
|
|
- $param['goods_id'] = $info['goods_id'];
|
|
|
- $param['sku_id'] = $info['sku_id'];
|
|
|
- $param['status'] = 1;
|
|
|
- $param['use_status'] = 1;
|
|
|
- $card = Dever::db('channel/card')->select($param, array('order' => 'seller_id desc','limit' => '0, ' . $info['num']), true);
|
|
|
- $total = count($card);
|
|
|
- if ($total < $info['num']) {
|
|
|
- return $this->notify($info, '卡密剩余数量不足', $update);
|
|
|
- }
|
|
|
- foreach ($card as $k => $v) {
|
|
|
- Dever::db('channel/card')->update($v['id'], array('use_status' => 3, 'order_id' => $info['id']));
|
|
|
- }
|
|
|
- $result['status'] = 1;
|
|
|
- } elseif ($channel['type'] == 11) {
|
|
|
- $result['status'] = 1;
|
|
|
- }
|
|
|
- $update = $this->channel_record($info, $channel, $result);
|
|
|
- if ($result['status'] == 1) {
|
|
|
- # 下单成功
|
|
|
- Dever::db('seller/order')->update($info['id'], $update);
|
|
|
- return 'ok';
|
|
|
- } else {
|
|
|
- # 下单失败
|
|
|
- return $this->notify($info, '下单失败', $update);
|
|
|
- }
|
|
|
- } finally {
|
|
|
- Redis::unlock($lockKey, $lockToken);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 记录渠道信息
|
|
|
- public function channel_record($info, $channel, $result)
|
|
|
- {
|
|
|
- $update = array();
|
|
|
- $update['channel_id'] = $channel['id'];
|
|
|
- $update['order_date'] = time();
|
|
|
- if (isset($result['request'])) {
|
|
|
- $update['request'] = json_encode($result['request'], JSON_UNESCAPED_UNICODE);
|
|
|
- }
|
|
|
- if (isset($result['response'])) {
|
|
|
- $update['response'] = json_encode($result['response'], JSON_UNESCAPED_UNICODE);
|
|
|
- }
|
|
|
- if (isset($channel['goods']['id'])) {
|
|
|
- $update['goods_id'] = $channel['goods']['id'];
|
|
|
- }
|
|
|
- $update['goods_discount'] = $channel['discount'];
|
|
|
- if (isset($channel['goods']['discount']) && $channel['goods']['discount']) {
|
|
|
- $update['goods_discount'] = $channel['goods']['discount'];
|
|
|
- }
|
|
|
- if (!$update['goods_discount']) {
|
|
|
- $update['goods_discount'] = 1;
|
|
|
- }
|
|
|
- $update['buy_price'] = round($info['cash'] * $update['goods_discount'], 2) * $info['num'];
|
|
|
- $update['order_channel_id'] = Dever::db('seller/order_channel')->insert($update);
|
|
|
- return $update;
|
|
|
- }
|
|
|
-
|
|
|
- # 向商户发起回调
|
|
|
- public function notify($info, $msg, $update = array(), $total = 5, $oper = 1, $table = 'order')
|
|
|
- {
|
|
|
- $seller = Dever::db('seller/info')->find($info['seller_id']);
|
|
|
- $status = 11;
|
|
|
- if ($msg == 'ok') {
|
|
|
- $status = 10;
|
|
|
- }
|
|
|
- # 获取商户回调地址,向商户发起回调
|
|
|
- if (isset($info['seller_notify']) && $info['seller_notify']) {
|
|
|
- $notify = $info['seller_notify'];
|
|
|
- } else {
|
|
|
- $notify = $seller['notify'];
|
|
|
- }
|
|
|
- $seller_log = [];
|
|
|
- if ($notify) {
|
|
|
- if ($info['seller_callback'] == 2 && $info['seller_callback_num'] <= $total) {
|
|
|
- $param = array();
|
|
|
- $param['appkey'] = $seller['appkey'];
|
|
|
- $param['order_num'] = $info['seller_order_num'];
|
|
|
- $param['system_order_num'] = $info['order_num'];
|
|
|
- $param['order_status'] = $status;
|
|
|
- $param['order_cash'] = $info['cash'];
|
|
|
- $param['official_order_num'] = $param['official_msg'] = '';
|
|
|
- if (isset($info['official_order_num'])) {
|
|
|
- $param['official_order_num'] = $info['official_order_num'];
|
|
|
- }
|
|
|
- if (isset($info['official_msg'])) {
|
|
|
- $param['official_msg'] = $info['official_msg'];
|
|
|
- }
|
|
|
- if (isset($update['official_order_num'])) {
|
|
|
- $param['official_order_num'] = $update['official_order_num'];
|
|
|
- }
|
|
|
- if (isset($update['official_msg'])) {
|
|
|
- $param['official_msg'] = $update['official_msg'];
|
|
|
- }
|
|
|
-
|
|
|
- # 反正所有榆钱和沧渤的资源进到系统 流水号只要出我们的系统自动就是89 联通就是10010 电信就是10000(不变的)
|
|
|
- # 89+8位随机数字+8位订单日期(比如今天就是20240325)+10位随机
|
|
|
- /*
|
|
|
- $channel_id = array(5,6,8);
|
|
|
- if ($status == 10 && in_array($info['channel_id'], $channel_id)) {
|
|
|
- $official_order_num = '';
|
|
|
- if ($info['goods_id'] == 1) {
|
|
|
- # 移动
|
|
|
- //$str = '8913807663202404289794416983';
|
|
|
- //$str = '8923656675202405029019879800';
|
|
|
- $official_order_num = '89' . rand(10000000, 99999999) . date('Ymd') . rand(1000000000, 9999999999);
|
|
|
- } elseif ($info['goods_id'] == 2) {
|
|
|
- # 联通
|
|
|
- $official_order_num = preg_replace('/^11010/i', '10010', $param['official_order_num']);
|
|
|
- }
|
|
|
- if ($official_order_num) {
|
|
|
- $param['official_order_num'] = $update['official_order_num'] = $official_order_num;
|
|
|
- }
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- $param = \Dever\Helper\Secure::get($param, $seller['appsecret']);
|
|
|
- $notify = urldecode($notify);
|
|
|
- $response = Dever::curl($notify, $param, 'post')->result();
|
|
|
- # ok是成功
|
|
|
- $seller_log['callback'] = $response;
|
|
|
- $seller_log['callback_date'] = time();
|
|
|
- if ($response == 'ok') {
|
|
|
- $update['seller_callback'] = 3;
|
|
|
- } else {
|
|
|
- $update['seller_callback'] = 2;
|
|
|
- }
|
|
|
- $update['seller_callback_num'] = $info['seller_callback_num'] + 1;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $seller_log['callback'] = 'ok';
|
|
|
- $seller_log['callback_date'] = time();
|
|
|
- $update['seller_callback'] = 3;
|
|
|
- $update['seller_callback_num'] = $info['seller_callback_num'] + 1;
|
|
|
- }
|
|
|
- $id = $info['id'];
|
|
|
- if ($info['status'] < 10 && $status >= 10) {
|
|
|
- # 订单完成
|
|
|
- $info['status'] = $update['status'] = $status;
|
|
|
- $update['finish'] = 1;
|
|
|
- $update['finish_date'] = time();
|
|
|
- $info = array_merge($info, $update);
|
|
|
- $this->finish($info, $oper, $table);
|
|
|
- }
|
|
|
- if (!$info['order_num']) {
|
|
|
- $update['order_num'] = $this->createOrder();
|
|
|
- }
|
|
|
- if ($update) {
|
|
|
- Dever::db('seller/' . $table)->update($id, $update);
|
|
|
- }
|
|
|
- if ($seller_log) {
|
|
|
- Dever::db('seller/order_seller')->update(['order_id' => $id], $seller_log);
|
|
|
- }
|
|
|
-
|
|
|
- if (isset($update['channel_callback']) && $update['channel_callback']) {
|
|
|
- Dever::db('seller/order_channel')->update(['id' => $update['order_channel_id']], ['callback' => $update['channel_callback'], 'callback_date' => $update['channel_callback_date']]);
|
|
|
- }
|
|
|
-
|
|
|
- return $msg;
|
|
|
- }
|
|
|
-
|
|
|
- public function finish($info, $oper = 1, $table = 'order')
|
|
|
- {
|
|
|
- if ($info['status'] == 10) {
|
|
|
- if ($info['channel_id']) {
|
|
|
- $channel = Dever::db('channel/info')->find($info['channel_id']);
|
|
|
- if (!$channel) {
|
|
|
- Dever::error('未分配渠道');
|
|
|
- }
|
|
|
- if ($channel['type'] == 2) {
|
|
|
- # 审核通过
|
|
|
- $param['order_id'] = $info['id'];
|
|
|
- //$param['status'] = 1;
|
|
|
- $param['use_status'] = 3;
|
|
|
- Dever::db('channel/card')->update($param, array('use_status' => 2));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if ($info['status'] == 11) {
|
|
|
- if ($info['channel_id']) {
|
|
|
- $channel = Dever::db('channel/info')->find($info['channel_id']);
|
|
|
- if (!$channel) {
|
|
|
- Dever::error('未分配渠道');
|
|
|
- }
|
|
|
- if ($channel['type'] == 2) {
|
|
|
- # 审核失败
|
|
|
- $param['order_id'] = $info['id'];
|
|
|
- //$param['status'] = 1;
|
|
|
- $param['use_status'] = 3;
|
|
|
- Dever::db('channel/card')->update($param, array('use_status' => 1));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if ($oper == 2) {
|
|
|
- $channel_num = Dever::db('seller/channel')->count(array('seller_id' => $info['seller_id'], 'goods_id' => $info['goods_id'], 'status' => 1));
|
|
|
- if ($channel_num > 1 && $info['channel_id']) {
|
|
|
- $info['status'] = 2;
|
|
|
- Dever::db('seller/' . $table)->update($info['id'], array('status' => 2, 'finish' => 2, 'finish_date' => '0'));
|
|
|
- # 记录渠道错误信息
|
|
|
- $selected = array();
|
|
|
- $selected[$info['channel_id']] = true;
|
|
|
- $order_error_data = Dever::db('seller/order_channel_error')->select(array('order_id' => $info['id']));
|
|
|
- if ($order_error_data) {
|
|
|
- foreach ($order_error_data as $k => $v) {
|
|
|
- $selected[$v['channel_id']] = true;
|
|
|
- }
|
|
|
- }
|
|
|
- $num = count($selected);
|
|
|
- $channel_num = $channel_num - $num;
|
|
|
- if ($channel_num > 0) {
|
|
|
- $order_error = array();
|
|
|
- $order_error['order_id'] = $info['id'];
|
|
|
- $order_error['order_channel_id'] = $info['order_channel_id'];
|
|
|
- $order_error['channel_id'] = $info['channel_id'];
|
|
|
- Dever::db('seller/order_channel_error')->insert($order_error);
|
|
|
- return $this->handleAct($info, $selected);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 失败,余额加回来
|
|
|
- Dever::load(Info::class)->inc($info['seller_id'], $info['price'], 1);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 获取渠道
|
|
|
- public function channel($seller_id, $goods_id, $sku_id, &$channel_num, $selected = array())
|
|
|
- {
|
|
|
- # 查找渠道
|
|
|
- $channel_list = Dever::db('seller/channel')->select(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'status' => 1));
|
|
|
- if (!$channel_list) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- $channel_num = count($channel_list);
|
|
|
- $max = 1;
|
|
|
- $channel = $goods = array();
|
|
|
- foreach ($channel_list as $k => $v) {
|
|
|
- if ($selected && isset($selected[$v['channel_id']])) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (!$v['sku_id']) {
|
|
|
- $goods = Dever::db('channel/goods')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
- if ($goods) {
|
|
|
- $channel = $v['channel_id'];
|
|
|
- $max = $v['max'];
|
|
|
- break;
|
|
|
- } else {
|
|
|
- $goods = Dever::db('channel/goods')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => -1));
|
|
|
- if ($goods) {
|
|
|
- $channel = $v['channel_id'];
|
|
|
- $max = $v['max'];
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- $all_sku_id = explode(',', $v['sku_id']);
|
|
|
- if (in_array($sku_id, $all_sku_id)) {
|
|
|
- $channel = $v['channel_id'];
|
|
|
- $max = $v['max'];
|
|
|
- $goods = Dever::db('channel/goods')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
- if (!$goods) {
|
|
|
- $goods = Dever::db('channel/goods')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => -1));
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!$channel) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $channel = Dever::db('channel/info')->find($channel);
|
|
|
- if (!$channel) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if ($channel['status'] == 2) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- $channel['goods'] = $goods;
|
|
|
- $channel['max'] = $max;
|
|
|
- return $channel;
|
|
|
- }
|
|
|
-
|
|
|
- public function createOrder()
|
|
|
- {
|
|
|
- $where['order_num'] = Str::order('C');
|
|
|
- $state = Dever::db('seller/order')->find($where);
|
|
|
- if (!$state) {
|
|
|
- return $where['order_num'];
|
|
|
- } else {
|
|
|
- return $this->createOrder();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|