Pay.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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)
  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. $shop = Dever::db('goods/shop')->one($goods['shop_id']);
  19. if ($parent_uid) {
  20. $order_data['parent_uid'] = $parent_uid;
  21. }
  22. $order_data['type'] = $type;
  23. $order_data['type_id'] = $type_id;
  24. $order_data['name'] = $goods['name'];
  25. $order_data['uid'] = $uid;
  26. $order_data['status'] = 1;
  27. $order_data['info_id'] = $goods['id'];
  28. $order_data['sku_id'] = $sku;
  29. $order_data['mode'] = $mode;
  30. $order_data['platform'] = $goods['platform'];
  31. $order_data['shape'] = $goods['shape'];
  32. $order_data['area_id'] = $user['area_id'];
  33. $order_data['address'] = $user['address'];
  34. $order_data['username'] = $user['address_contact'];
  35. $order_data['mobile'] = $user['address_mobile'];
  36. $order_data['store_id'] = $store_id;
  37. $order_data['cash'] = $goods['price'];
  38. $order_data['freight_id'] = $goods['freight_id'];
  39. $order_data['freight_price'] = $goods['freight_price'];
  40. $order_data['num'] = $num;
  41. $price = $order_data['cash'] * $order_data['num'];
  42. if ($mode == 1) {
  43. $price += $order_data['freight_price'];
  44. }
  45. $order_data['price'] = $price;
  46. $order_data['system_source'] = $system_source;
  47. $order_data['order_id'] = $this->getOrderId();
  48. print_r($order_data);die;
  49. $id = Dever::db('goods/order')->insert($order_data);
  50. if (!$id) {
  51. Dever::alert('支付失败');
  52. }
  53. $param = array
  54. (
  55. 'project_id' => 1,
  56. 'channel_id' => $shop['channel_id'],
  57. 'system_source' => $system_source,
  58. 'uid' => $uid,
  59. 'name' => $order_data['name'],
  60. 'cash' => $price,
  61. 'product_id' => $goods['id'],
  62. 'order_id' => $order_data['order_id'],
  63. );
  64. $receipt = Dever::input('receipt');
  65. if ($receipt) {
  66. $param['receipt'] = $receipt;
  67. }
  68. $result = Dever::load('pay/api.pay', $param);
  69. return $result;
  70. }
  71. # 支付成功回调 安全加密 设置token
  72. public function success_secure_api_token()
  73. {
  74. $project_id = Dever::input('pay_project_id');
  75. $info = Dever::db('pay/project')->one($project_id);
  76. if ($info) {
  77. return $info['key'];
  78. }
  79. return 'goods_dever_2020';
  80. }
  81. # 支付成功回调 安全加密
  82. public function success_secure_api($param = array())
  83. {
  84. $this->success($param);
  85. }
  86. # 支付成功回调
  87. public function success($param = array())
  88. {
  89. $send = $param ? $param : Dever::preInput('pay_');
  90. $order_id = $send['pay_order_id'];
  91. $status = $send['pay_status'];
  92. $msg = $send['pay_msg'];
  93. $order = Dever::db('goods/order')->one(array('order_id' => $order_id));
  94. if ($order && $order['status'] == 1) {
  95. if ($status == 2) {
  96. # 减少库存 增加销量
  97. $update['where_id'] = $order['info_id'];
  98. $update['sell_num'] = $order['num'];
  99. Dever::db('goods/info')->updateSell($update);
  100. if ($order['sku_id'] > 0) {
  101. $update['where_id'] = $order['sku_id'];
  102. $update['sell_num'] = $order['num'];
  103. Dever::db('goods/info_sku')->updateSell($update);
  104. }
  105. # 增加积分
  106. if ($order['parent_uid'] > 0) {
  107. $uid = $order['parent_uid'] . '_' . $order['uid'];
  108. } else {
  109. $uid = $order['uid'];
  110. }
  111. Dever::score($uid, 'buy_my_goods', '购买自营商品', false, false, false, $order['type'], $order['type_id']);
  112. # 发消息
  113. if (Dever::project('message')) {
  114. Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功', 11);
  115. }
  116. }
  117. $update['status'] = $status;
  118. $update['where_id'] = $order['id'];
  119. $update['pay_time'] = time();
  120. Dever::db('goods/order')->update($update);
  121. }
  122. return 'ok';
  123. }
  124. # 生成订单号
  125. public function getOrderId()
  126. {
  127. $where['order_id'] = Dever::order('G');
  128. $state = Dever::db('goods/order')->one($where);
  129. if (!$state) {
  130. return $where['order_id'];
  131. } else {
  132. return $this->getOrderId();
  133. }
  134. }
  135. }