Sell.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. # 销售支付
  3. namespace Shop\Lib;
  4. use Dever;
  5. class Sell
  6. {
  7. # 发起支付
  8. 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 = '')
  9. {
  10. if (!$user) {
  11. Dever::alert('错误的用户信息');
  12. }
  13. $uid = $user['id'];
  14. if (!$parent_uid && Dever::project('invite')) {
  15. $parent = Dever::load('invite/api')->getParent($uid, 1);
  16. if ($parent) {
  17. $parent_uid = $parent['uid'];
  18. }
  19. }
  20. if ($parent_uid) {
  21. $order_data['parent_uid'] = $parent_uid;
  22. }
  23. $order_data['shop_id'] = $shop['id'];
  24. $order_data['uid'] = $uid;
  25. $order_data['mobile'] = $user['mobile'];
  26. $order_data['address_id'] = $address_id;
  27. $order_data['invoice_id'] = $invoice_id;
  28. $order_data['name'] = $name;
  29. $order_data['num'] = $num;
  30. $order_data['info'] = $info;
  31. $order_data['price'] = $price;
  32. $order_data['area'] = $shop['area'];
  33. $order_data['province'] = $shop['province'];
  34. $order_data['city'] = $shop['city'];
  35. $order_data['county'] = $shop['county'];
  36. $order_data['town'] = $shop['town'];
  37. $order_data['method'] = $method;
  38. $order_data['pay_method'] = $pay_method;
  39. if ($card) {
  40. $order_data['card_code_id'] = $card['id'];
  41. }
  42. if ($coupon_id) {
  43. $order_data['coupon_id'] = $coupon_id;
  44. }
  45. if ($user_coupon_id) {
  46. $order_data['user_coupon_id'] = $user_coupon_id;
  47. }
  48. $order_data['order_num'] = $this->getOrderId();
  49. $order_data['pay_id'] = $order_data['order_num'];
  50. $order_data['pay_status'] = 1;
  51. $order_data['pay_price'] = 0;
  52. $id = Dever::db('shop/sell_order')->insert($order_data);
  53. if (!$id) {
  54. Dever::alert('支付失败');
  55. }
  56. foreach($goods as $k => $v) {
  57. if ($v['ku_state'] == 1) {
  58. $data['uid'] = $uid;
  59. $data['shop_id'] = $shop['id'];
  60. $data['order_id'] = $id;
  61. $data['goods_id'] = $v['id'];
  62. $data['sku_id'] = $v['sku_id'];
  63. $data['price'] = $v['price'];
  64. $data['num'] = $v['buy_num'];
  65. Dever::db('shop/sell_order_goods')->insert($data);
  66. }
  67. }
  68. if ($card || $price <= 0) {
  69. $param['pay_product_id'] = $id;
  70. $param['pay_order_id'] = $order_data['pay_id'];
  71. $param['pay_status'] = 2;
  72. $param['pay_cash'] = $price;
  73. $param['pay_msg'] = '';
  74. $this->success($param);
  75. if ($cart == 1) {
  76. # 来自购物车,要清空一下
  77. $this->dropCart($uid, $goods, $shop['id']);
  78. }
  79. # 使用优惠券
  80. $this->useCoupon($coupon_id, $user_coupon_id);
  81. return 'ok';
  82. }
  83. $param = array
  84. (
  85. 'project_id' => 1,
  86. 'channel_id' => 1,
  87. 'system_source' => 5,
  88. 'uid' => $uid,
  89. 'name' => $order_data['name'],
  90. 'cash' => $price,
  91. 'product_id' => $id,
  92. 'order_id' => $order_data['pay_id'],
  93. 'refer' => $refer,
  94. );
  95. $receipt = Dever::input('receipt');
  96. if ($receipt) {
  97. $param['receipt'] = $receipt;
  98. }
  99. $result = Dever::load('pay/api.pay', $param);
  100. if ($cart == 1) {
  101. # 来自购物车,要清空一下
  102. $this->dropCart($uid, $goods, $shop['id']);
  103. }
  104. # 使用优惠券
  105. $this->useCoupon($coupon_id, $user_coupon_id);
  106. return $result;
  107. }
  108. public function dropCart($uid, $goods, $shop_id)
  109. {
  110. $where['uid'] = $uid;
  111. $where['shop_id'] = $shop_id;
  112. foreach ($goods as $k => $v) {
  113. $where['goods_id'] = $v['id'];
  114. Dever::db('shop/cart')->delete($where);
  115. }
  116. }
  117. public function useCoupon($coupon_id, $user_coupon_id)
  118. {
  119. if ($user_coupon_id) {
  120. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  121. }
  122. }
  123. # 再次发起支付
  124. public function raction($id, $refer = '')
  125. {
  126. $order = Dever::db('shop/sell_order')->find($id);
  127. if (!$order) {
  128. Dever::alert('订单信息错误');
  129. }
  130. if ($order['status'] > 1) {
  131. Dever::alert('订单已支付或已取消');
  132. }
  133. $param = array
  134. (
  135. 'project_id' => 1,
  136. 'account_id' => 5,
  137. 'uid' => $order['uid'],
  138. 'name' => $order['name'],
  139. 'cash' => $order['price'],
  140. 'product_id' => $id,
  141. 'order_id' => $order['pay_id'],
  142. 'refer' => $refer,
  143. );
  144. $receipt = Dever::input('receipt');
  145. if ($receipt) {
  146. $param['receipt'] = $receipt;
  147. }
  148. $result = Dever::load('pay/api.pay', $param);
  149. return $result;
  150. }
  151. # 支付成功回调 安全加密 设置token
  152. public function success_secure_api_token()
  153. {
  154. $project_id = Dever::input('pay_project_id');
  155. $info = Dever::db('pay/project')->one($project_id);
  156. if ($info) {
  157. return $info['key'];
  158. }
  159. return 'shop_dever_2020';
  160. }
  161. # 支付成功回调 安全加密
  162. public function success_secure_api($param = array())
  163. {
  164. $this->success($param);
  165. }
  166. # 支付成功回调
  167. public function success($param = array())
  168. {
  169. $send = $param ? $param : Dever::preInput('pay_');
  170. $product_id = $send['pay_product_id'];
  171. $order_id = $send['pay_order_id'];
  172. $status = $send['pay_status'];
  173. $cash = $send['pay_cash'];
  174. $msg = $send['pay_msg'];
  175. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  176. if ($order && $order['pay_status'] == 1) {
  177. if ($status == 2) {
  178. # 减少库存 增加销量
  179. $this->updateSell($order);
  180. # 发消息
  181. if (Dever::project('message')) {
  182. Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', '购买成功', 1);
  183. }
  184. # 如果是自提订单,生成自提码
  185. if ($order['method'] == 1) {
  186. $update['code_status'] = 2;
  187. $update['code'] = $this->getCode();
  188. } else {
  189. $update['ps_status'] = 2;
  190. }
  191. }
  192. $update['pay_status'] = $status;
  193. $update['status'] = 2;
  194. $update['where_id'] = $order['id'];
  195. $update['pay_time'] = time();
  196. $update['pay_price'] = $cash;
  197. Dever::db('shop/sell_order')->update($update);
  198. }
  199. return 'ok';
  200. }
  201. # 减少库存,增加销量
  202. public function updateSell($order)
  203. {
  204. $where['order_id'] = $order['id'];
  205. $data = Dever::db('shop/sell_order_goods')->select($where);
  206. if ($data) {
  207. foreach ($data as $k => $v) {
  208. $up = array();
  209. $up['where_shop_id'] = $v['shop_id'];
  210. $up['where_goods_id'] = $v['goods_id'];
  211. $up['sell_num'] = $v['num'];
  212. Dever::db('shop/goods')->updateSell($up);
  213. if ($v['sku_id'] > 0) {
  214. } else {
  215. $v['sku_id'] = -1;
  216. }
  217. $upSku = $up;
  218. $upSku['where_sku_id'] = $v['sku_id'];
  219. Dever::db('shop/goods_sku')->updateSell($upSku);
  220. }
  221. }
  222. }
  223. # 生成自提码
  224. public function getCode()
  225. {
  226. $where['code'] = Dever::rand(6, 0);
  227. $state = Dever::db('shop/sell_order')->one($where);
  228. if (!$state) {
  229. return $where['code'];
  230. } else {
  231. return $this->getCode();
  232. }
  233. }
  234. # 生成订单号
  235. public function getOrderId()
  236. {
  237. $where['order_num'] = Dever::order('G');
  238. $state = Dever::db('shop/sell_order')->one($where);
  239. if (!$state) {
  240. return $where['order_num'];
  241. } else {
  242. return $this->getOrderId();
  243. }
  244. }
  245. # 展示订单详情
  246. public function show()
  247. {
  248. $id = Dever::input('order_id');
  249. $config = Dever::db('shop/sell_order')->config;
  250. $info = Dever::db('shop/sell_order')->one($id);
  251. $shop = Dever::db('shop/info')->find($info['shop_id']);
  252. $user = Dever::db('passport/user')->find($info['uid']);
  253. if (!$user['truename']) {
  254. $user['truename'] = $user['username'];
  255. }
  256. $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
  257. $html .= '<tr>
  258. <td>订单号</td>
  259. <td>'.$this->table(false, array(array($info['order_num']))).'</td>
  260. </tr>';
  261. $html .= '<tr>
  262. <td>门店信息</td>
  263. <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>
  264. </tr>';
  265. $html .= '<tr>
  266. <td>用户信息</td>
  267. <td>'.$this->table(false, array(array('昵称:' . $user['username'], '联系电话:' . $user['mobile']))).'</td>
  268. </tr>';
  269. $html .= '<tr>
  270. <td>订单信息</td>
  271. <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>
  272. </tr>';
  273. if ($info['address_id'] > 0) {
  274. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  275. $html .= '<tr>
  276. <td>收货地址</td>
  277. <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>
  278. </tr>';
  279. }
  280. if ($info['invoice_id'] > 0) {
  281. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  282. $html .= '<tr>
  283. <td>发票信息</td>
  284. <td>'.$this->table(array('发票类型', '抬头', '税号','手机号', '邮箱'), array(array($invoice['title_type_name'], $invoice['title'], $invoice['number'], $invoice['mobile'], $invoice['email']))).'</td>
  285. </tr>';
  286. }
  287. $html .= '</tbody></table>';
  288. $html .= '[商品清单]:';
  289. $head = array('商品名称', '商品属性', '商品价格', '商品数量');
  290. $body = array();
  291. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  292. foreach ($goods as $k => $v) {
  293. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  294. $body[$k] = array
  295. (
  296. $goods_info['name'],
  297. $v['price'],
  298. $v['price'],
  299. $v['num'],
  300. );
  301. }
  302. $html .= $this->table($head, $body);
  303. return $html;
  304. }
  305. private function table($head, $data)
  306. {
  307. $html = '';
  308. if ($head) {
  309. $html = '<table class="layui-table">';
  310. $html .= '<thead><tr>';
  311. foreach ($head as $k => $v) {
  312. $html .= '<th>'.$v.'</th>';
  313. }
  314. $html .= '</tr></thead>';
  315. $html .= '<tbody>';
  316. foreach ($data as $k => $v) {
  317. $html .= '<tr>';
  318. foreach ($v as $k1 => $v1) {
  319. $html .= '<td>'.$v1.'</td>';
  320. }
  321. $html .= '</tr>';
  322. }
  323. $html .= '</tbody>';
  324. $html .= '</table>';
  325. } else {
  326. foreach ($data as $k => $v) {
  327. $html .= '';
  328. foreach ($v as $k1 => $v1) {
  329. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  330. }
  331. $html .= '';
  332. }
  333. }
  334. return $html;
  335. }
  336. }