Pay.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. # 支付
  3. namespace Goods\Lib;
  4. use Dever;
  5. class Pay
  6. {
  7. # 发起支付
  8. public function action($parent_uid, $user, $id, $sku, $num, $mode, $store_id, $system_source, $type = false, $type_id = false, $refer = '')
  9. {
  10. if (!$user) {
  11. Dever::alert('错误的用户信息');
  12. }
  13. $uid = $user['id'];
  14. $goods = Dever::load('goods/lib/info')->getPayInfo($id, $sku, $num, $user);
  15. if (!$goods) {
  16. Dever::alert('错误的商品信息');
  17. }
  18. # 验证库存
  19. if ($goods['num'] < $num) {
  20. Dever::alert('已售空');
  21. }
  22. $shop = Dever::db('goods/shop')->one($goods['shop_id']);
  23. if (!$shop) {
  24. Dever::alert('店铺不存在');
  25. }
  26. if (!$parent_uid && Dever::project('invite')) {
  27. $parent = Dever::load('invite/api')->getParent($uid, 1);
  28. if ($parent) {
  29. $parent_uid = $parent['uid'];
  30. }
  31. }
  32. if ($parent_uid) {
  33. $order_data['parent_uid'] = $parent_uid;
  34. }
  35. $order_data['type'] = $type;
  36. $order_data['type_id'] = $type_id;
  37. $order_data['name'] = $goods['name'];
  38. $order_data['uid'] = $uid;
  39. $order_data['status'] = 1;
  40. $order_data['info_id'] = $goods['id'];
  41. $order_data['sku_id'] = $sku;
  42. $order_data['mode'] = $mode;
  43. $order_data['platform'] = $goods['platform'];
  44. $order_data['shape'] = $goods['shape'];
  45. $order_data['area_id'] = $user['area_id'];
  46. $order_data['address'] = $user['address'];
  47. $order_data['username'] = $user['address_contact'];
  48. $order_data['mobile'] = $user['address_mobile'];
  49. $order_data['store_id'] = $store_id;
  50. $order_data['cash'] = $goods['price'];
  51. $order_data['freight_id'] = $goods['freight_id'];
  52. $order_data['freight_price'] = $goods['freight_price'];
  53. $order_data['num'] = $num;
  54. $price = $order_data['cash'] * $order_data['num'];
  55. if ($mode == 1) {
  56. $price += $order_data['freight_price'];
  57. }
  58. $order_data['price'] = $price;
  59. $order_data['reward'] = isset($goods['reward']) && $goods['reward'] ? $goods['reward'] : 0;
  60. $order_data['system_source'] = $system_source;
  61. $order_data['order_id'] = $this->getOrderId();
  62. $id = Dever::db('goods/order')->insert($order_data);
  63. if (!$id) {
  64. Dever::alert('支付失败');
  65. }
  66. $param = array
  67. (
  68. 'project_id' => 1,
  69. 'channel_id' => $shop['pay_channel'],
  70. 'system_source' => $system_source,
  71. 'uid' => $uid,
  72. 'name' => $order_data['name'],
  73. 'cash' => $price,
  74. 'product_id' => $goods['id'],
  75. 'order_id' => $order_data['order_id'],
  76. 'refer' => $refer,
  77. );
  78. $receipt = Dever::input('receipt');
  79. if ($receipt) {
  80. $param['receipt'] = $receipt;
  81. }
  82. $result = Dever::load('pay/api.pay', $param);
  83. return $result;
  84. }
  85. # 支付成功回调 安全加密 设置token
  86. public function success_secure_api_token()
  87. {
  88. $project_id = Dever::input('pay_project_id');
  89. $info = Dever::db('pay/project')->one($project_id);
  90. if ($info) {
  91. return $info['key'];
  92. }
  93. return 'goods_dever_2020';
  94. }
  95. # 支付成功回调 安全加密
  96. public function success_secure_api($param = array())
  97. {
  98. $this->success($param);
  99. }
  100. # 支付成功回调
  101. public function success($param = array())
  102. {
  103. $send = $param ? $param : Dever::preInput('pay_');
  104. $order_id = $send['pay_order_id'];
  105. $status = $send['pay_status'];
  106. $msg = $send['pay_msg'];
  107. $order = Dever::db('goods/order')->one(array('order_id' => $order_id, 'time' => time()));
  108. if ($order && $order['pay_status'] == 1) {
  109. if ($status == 2) {
  110. # 减少库存 增加销量
  111. $update['where_id'] = $order['info_id'];
  112. $update['sell_num'] = $order['num'];
  113. Dever::db('goods/info')->updateSell($update);
  114. if ($order['sku_id'] > 0) {
  115. $update['where_id'] = $order['sku_id'];
  116. $update['sell_num'] = $order['num'];
  117. Dever::db('goods/info_sku')->updateSell($update);
  118. }
  119. # 增加积分
  120. if ($order['parent_uid'] > 0 && $order['parent_uid'] != $order['uid']) {
  121. $uid = $order['parent_uid'] . '_' . $order['uid'];
  122. # 给上级发佣金
  123. Dever::score($order['parent_uid'], 'sell_my_goods', '销售自营商品', false, $order['reward'], false, $order['type'], $order['type_id']);
  124. } else {
  125. $uid = $order['uid'];
  126. }
  127. # 榜单更新
  128. if (Dever::project('collection')) {
  129. Dever::load('collection/lib/ranking')->up($order['uid'], $order['type_id'], 2, $order['num']);
  130. }
  131. # 给自己发佣金
  132. Dever::score($uid, 'buy_my_goods', '购买自营商品', false, $order['reward'], false, $order['type'], $order['type_id']);
  133. # 发消息
  134. if (Dever::project('message')) {
  135. Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功', 1);
  136. }
  137. }
  138. $update['pay_status'] = $status;
  139. $update['status'] = 2;
  140. $update['where_id'] = $order['id'];
  141. $update['pay_time'] = time();
  142. Dever::db('goods/order')->update($update);
  143. }
  144. return 'ok';
  145. }
  146. # 生成订单号
  147. public function getOrderId()
  148. {
  149. $where['order_id'] = Dever::order('G');
  150. $state = Dever::db('goods/order')->one($where);
  151. if (!$state) {
  152. return $where['order_id'];
  153. } else {
  154. return $this->getOrderId();
  155. }
  156. }
  157. }