123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- # 销售支付
- namespace Shop\Lib;
- use Dever;
- class Sell
- {
- # 发起支付
- public function action($method, $pay_method, $user, $shop, $name, $num, $goods, $price, $address_id, $invoice_id, $info, $card, $coupon_id, $user_coupon_id, $parent_uid = false, $cart = false, $refer = '')
- {
- if (!$user) {
- Dever::alert('错误的用户信息');
- }
- $uid = $user['id'];
- if (!$parent_uid && Dever::project('invite')) {
- $parent = Dever::load('invite/api')->getParent($uid, 1);
- if ($parent) {
- $parent_uid = $parent['uid'];
- }
- }
-
- if ($parent_uid) {
- $order_data['parent_uid'] = $parent_uid;
- }
-
- $order_data['shop_id'] = $shop['id'];
- $order_data['uid'] = $uid;
- $order_data['mobile'] = $user['mobile'];
- $order_data['address_id'] = $address_id;
- $order_data['invoice_id'] = $invoice_id;
- $order_data['name'] = $name;
- $order_data['num'] = $num;
- $order_data['info'] = $info;
- $order_data['price'] = $price;
- $order_data['area'] = $shop['area'];
- $order_data['province'] = $shop['province'];
- $order_data['city'] = $shop['city'];
- $order_data['county'] = $shop['county'];
- $order_data['town'] = $shop['town'];
- $order_data['method'] = $method;
- $order_data['pay_method'] = $pay_method;
- if ($card) {
- $order_data['card_code_id'] = $card['id'];
- }
- if ($coupon_id) {
- $order_data['coupon_id'] = $coupon_id;
- }
- if ($user_coupon_id) {
- $order_data['user_coupon_id'] = $user_coupon_id;
- }
- $order_data['order_num'] = $this->getOrderId();
- $order_data['pay_id'] = $order_data['order_num'];
- $order_data['pay_status'] = 1;
- $order_data['pay_price'] = 0;
- $id = Dever::db('shop/sell_order')->insert($order_data);
- if (!$id) {
- Dever::alert('支付失败');
- }
- foreach($goods as $k => $v) {
- if ($v['ku_state'] == 1) {
- $data['uid'] = $uid;
- $data['shop_id'] = $shop['id'];
- $data['order_id'] = $id;
- $data['goods_id'] = $v['id'];
- $data['sku_id'] = $v['sku_id'];
- $data['price'] = $v['price'];
- $data['num'] = $v['buy_num'];
- Dever::db('shop/sell_order_goods')->insert($data);
- }
- }
- if ($card || $price <= 0) {
- $param['pay_product_id'] = $id;
- $param['pay_order_id'] = $order_data['pay_id'];
- $param['pay_status'] = 2;
- $param['pay_cash'] = $price;
- $param['pay_msg'] = '';
- $this->success($param);
- if ($cart == 1) {
- # 来自购物车,要清空一下
- $this->dropCart($uid, $goods, $shop['id']);
- }
- # 使用优惠券
- $this->useCoupon($coupon_id, $user_coupon_id);
- return 'ok';
- }
- $param = array
- (
- 'project_id' => 1,
- 'channel_id' => 1,
- 'system_source' => 5,
- 'uid' => $uid,
- 'name' => $order_data['name'],
- 'cash' => $price,
- 'product_id' => $id,
- 'order_id' => $order_data['pay_id'],
- 'refer' => $refer,
- );
- $receipt = Dever::input('receipt');
- if ($receipt) {
- $param['receipt'] = $receipt;
- }
- $result = Dever::load('pay/api.pay', $param);
- if ($cart == 1) {
- # 来自购物车,要清空一下
- $this->dropCart($uid, $goods, $shop['id']);
- }
- # 使用优惠券
- $this->useCoupon($coupon_id, $user_coupon_id);
- return $result;
- }
- public function dropCart($uid, $goods, $shop_id)
- {
- $where['uid'] = $uid;
- $where['shop_id'] = $shop_id;
- foreach ($goods as $k => $v) {
- $where['goods_id'] = $v['id'];
- Dever::db('shop/cart')->delete($where);
- }
- }
- public function useCoupon($coupon_id, $user_coupon_id)
- {
- if ($user_coupon_id) {
- Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
- }
- }
- # 再次发起支付
- public function raction($id, $refer = '')
- {
- $order = Dever::db('shop/sell_order')->find($id);
- if (!$order) {
- Dever::alert('订单信息错误');
- }
- if ($order['status'] > 1) {
- Dever::alert('订单已支付或已取消');
- }
- $param = array
- (
- 'project_id' => 1,
- 'account_id' => 5,
- 'uid' => $order['uid'],
- 'name' => $order['name'],
- 'cash' => $order['price'],
- 'product_id' => $id,
- 'order_id' => $order['pay_id'],
- 'refer' => $refer,
- );
- $receipt = Dever::input('receipt');
- if ($receipt) {
- $param['receipt'] = $receipt;
- }
- $result = Dever::load('pay/api.pay', $param);
- return $result;
- }
- # 支付成功回调 安全加密 设置token
- public function success_secure_api_token()
- {
- $project_id = Dever::input('pay_project_id');
- $info = Dever::db('pay/project')->one($project_id);
- if ($info) {
- return $info['key'];
- }
- return 'shop_dever_2020';
- }
- # 支付成功回调 安全加密
- public function success_secure_api($param = array())
- {
- $this->success($param);
- }
- # 支付成功回调
- public function success($param = array())
- {
- $send = $param ? $param : Dever::preInput('pay_');
- $product_id = $send['pay_product_id'];
- $order_id = $send['pay_order_id'];
- $status = $send['pay_status'];
- $cash = $send['pay_cash'];
- $msg = $send['pay_msg'];
- $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
- if ($order && $order['pay_status'] == 1) {
- if ($status == 2) {
- # 减少库存 增加销量
- $this->updateSell($order);
- # 发消息
- if (Dever::project('message')) {
- Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', '购买成功', 1);
- }
- # 如果是自提订单,生成自提码
- if ($order['method'] == 1) {
- $update['code_status'] = 2;
- $update['code'] = $this->getCode();
- } else {
- $update['ps_status'] = 2;
- }
- }
- $update['pay_status'] = $status;
- $update['status'] = 2;
- $update['where_id'] = $order['id'];
- $update['pay_time'] = time();
- $update['pay_price'] = $cash;
- Dever::db('shop/sell_order')->update($update);
- }
- return 'ok';
- }
- # 减少库存,增加销量
- public function updateSell($order)
- {
- $where['order_id'] = $order['id'];
- $data = Dever::db('shop/sell_order_goods')->select($where);
- if ($data) {
- foreach ($data as $k => $v) {
- $up = array();
- $up['where_shop_id'] = $v['shop_id'];
- $up['where_goods_id'] = $v['goods_id'];
- $up['sell_num'] = $v['num'];
- Dever::db('shop/goods')->updateSell($up);
- if ($v['sku_id'] > 0) {
-
- } else {
- $v['sku_id'] = -1;
- }
- $upSku = $up;
- $upSku['where_sku_id'] = $v['sku_id'];
- Dever::db('shop/goods_sku')->updateSell($upSku);
- }
- }
- }
- # 生成自提码
- public function getCode()
- {
- $where['code'] = Dever::rand(6, 0);
- $state = Dever::db('shop/sell_order')->one($where);
- if (!$state) {
- return $where['code'];
- } else {
- return $this->getCode();
- }
- }
- # 生成订单号
- public function getOrderId()
- {
- $where['order_num'] = Dever::order('G');
- $state = Dever::db('shop/sell_order')->one($where);
- if (!$state) {
- return $where['order_num'];
- } else {
- return $this->getOrderId();
- }
- }
- # 展示订单详情
- public function show()
- {
- $id = Dever::input('order_id');
- $config = Dever::db('shop/sell_order')->config;
- $info = Dever::db('shop/sell_order')->one($id);
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- $user = Dever::db('passport/user')->find($info['uid']);
- if (!$user['truename']) {
- $user['truename'] = $user['username'];
- }
-
- $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
- $html .= '<tr>
- <td>订单号</td>
- <td>'.$this->table(false, array(array($info['order_num']))).'</td>
- </tr>';
- $html .= '<tr>
- <td>门店信息</td>
- <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>
- </tr>';
- $html .= '<tr>
- <td>用户信息</td>
- <td>'.$this->table(false, array(array('昵称:' . $user['username'], '联系电话:' . $user['mobile']))).'</td>
- </tr>';
- $html .= '<tr>
- <td>订单信息</td>
- <td>'.$this->table(array('订单状态', '金额', '数量','支付方式', '配送方式'), array(array($config['status'][$info['status']], $info['price'], $info['num'],$config['pay_method'][$info['pay_method']], $config['method'][$info['method']]))).'</td>
- </tr>';
- if ($info['address_id'] > 0) {
- $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
- $html .= '<tr>
- <td>收货地址</td>
- <td>'.$this->table(array('省份', '城市', '地区','地址', '门牌号', '姓名', '手机号'), array(array($address['province_name'], $address['city_name'], $address['county_name'], $address['address'], $address['house_number'], $address['contact'], $address['mobile']))).'</td>
- </tr>';
- }
- if ($info['invoice_id'] > 0) {
- $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
- $html .= '<tr>
- <td>发票信息</td>
- <td>'.$this->table(array('发票类型', '抬头', '税号','手机号', '邮箱'), array(array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email']))).'</td>
- </tr>';
- }
-
- $html .= '</tbody></table>';
- $html .= '[商品清单]:';
- $head = array('商品名称', '商品属性', '商品价格', '商品数量');
- $body = array();
- $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
- foreach ($goods as $k => $v) {
- $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
- $body[$k] = array
- (
- $goods_info['name'],
- $v['price'],
- $v['price'],
- $v['num'],
- );
- }
- $html .= $this->table($head, $body);
- return $html;
- }
- private function table($head, $data)
- {
- $html = '';
- if ($head) {
- $html = '<table class="layui-table">';
- $html .= '<thead><tr>';
- foreach ($head as $k => $v) {
- $html .= '<th>'.$v.'</th>';
- }
- $html .= '</tr></thead>';
- $html .= '<tbody>';
- foreach ($data as $k => $v) {
- $html .= '<tr>';
- foreach ($v as $k1 => $v1) {
- $html .= '<td>'.$v1.'</td>';
- }
- $html .= '</tr>';
- }
- $html .= '</tbody>';
- $html .= '</table>';
- } else {
- foreach ($data as $k => $v) {
- $html .= '';
- foreach ($v as $k1 => $v1) {
- $html .= $v1 . ' ';
- }
- $html .= '';
- }
- }
- return $html;
- }
- }
|