Pay.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. # 支付
  3. namespace Goods\Lib;
  4. use Dever;
  5. class Pay
  6. {
  7. # 发起支付
  8. public function action($uid, $id, $sku, $num, $source)
  9. {
  10. if (!$uid) {
  11. Dever::alert('错误的用户信息');
  12. }
  13. $source = 'android';
  14. $goods = Dever::load('goods/lib/info')->getPayInfo($id, $sku);
  15. $user = Dever::db('passport/user')->one($uid);
  16. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  17. if ($source == 'ios') {
  18. $method = 'apple';
  19. $account_id = 3;
  20. # 使用苹果内购支付
  21. $receipt = Dever::input('receipt');
  22. if (!$receipt) {
  23. Dever::alert('苹果内购支付失败,没有receipt参数');
  24. }
  25. } elseif ($source == 'android') {
  26. $method = 'app';
  27. $account_id = 3;
  28. } elseif ($source == 'applet') {
  29. # 小程序支付
  30. $method = 'applet';
  31. $account_id = 2;
  32. } else {
  33. # 默认是网页支付
  34. $method = 'page';
  35. $account_id = 1;
  36. }
  37. $order_data['uid'] = $uid;
  38. $order_data['status'] = 1;
  39. $order_data['info_id'] = $goods['id'];
  40. $order_data['sku_id'] = $sku;
  41. $order_data['name'] = $goods['name'];
  42. $order_data['cash'] = $goods['price'];
  43. $order_data['num'] = $num;
  44. $order_data['source'] = $source;
  45. $order_data['order_id'] = $this->getOrderId();
  46. $id = Dever::db('goods/order')->insert($order_data);
  47. if (!$id) {
  48. Dever::alert('支付失败');
  49. }
  50. $refer = 'test';
  51. //$param参数
  52. $param = array
  53. (
  54. 'account_id' => $account_id,
  55. 'project_id' => 1,
  56. 'uid' => $uid,
  57. 'username' => $user['username'],
  58. 'name' => $order_data['name'],
  59. 'cash' => $order_data['cash'] * $order_data['num'],
  60. //'cash' => '0.01',
  61. 'openid' => isset($wechat['openid']) ? $wechat['openid'] : '',
  62. 'product_id' => $goods['id'],
  63. 'order_id' => $order_data['order_id'],
  64. 'refer' => $refer
  65. );
  66. if ($method == 'apple') {
  67. $param['other'] = $receipt;
  68. }
  69. $result['pay'] = Dever::load('pay/api.' . $method, $param);
  70. $result['order_id'] = $order_data['order_id'];
  71. return $result;
  72. }
  73. # 支付成功回调 安全加密 设置token
  74. public function success_secure_api_token()
  75. {
  76. $project_id = Dever::input('pay_project_id');
  77. $info = Dever::db('pay/project')->one($project_id);
  78. if ($info) {
  79. return $info['key'];
  80. }
  81. return 'goods_dever_2020';
  82. }
  83. # 支付成功回调 安全加密
  84. public function success_secure_api($param = array())
  85. {
  86. $this->success($param);
  87. }
  88. # 支付成功回调
  89. public function success($param = array())
  90. {
  91. $send = $param ? $param : Dever::preInput('pay_');
  92. $order_id = $send['pay_order_id'];
  93. $status = $send['pay_status'];
  94. $msg = $send['pay_msg'];
  95. $order = Dever::db('goods/order')->one(array('order_id' => $order_id));
  96. if ($order && $order['status'] == 1) {
  97. if ($status == 2) {
  98. # 减少库存 增加销量
  99. $update['where_id'] = $order['info_id'];
  100. $update['sell_num'] = $order['num'];
  101. Dever::db('goods/info')->updateSell($update);
  102. if ($order['sku_id'] > 0) {
  103. $update['where_id'] = $order['sku_id'];
  104. $update['sell_num'] = $order['num'];
  105. Dever::db('goods/info_sku')->updateSell($update);
  106. }
  107. # 增加积分
  108. Dever::score($order['uid'], 'buy_goods', '购买商品');
  109. # 发消息
  110. if (Dever::project('message')) {
  111. Dever::load('message/lib/data')->push(-1, $order['uid'], '购买提醒', '购买成功', 11);
  112. }
  113. # 增加上级佣金
  114. $cash = $order['cash'] * $order['num'];
  115. $this->rebate($order['uid'], $order['info_id'], $cash);
  116. }
  117. $update['status'] = $status;
  118. $update['where_id'] = $order['id'];
  119. Dever::db('goods/order')->update($update);
  120. }
  121. return 'ok';
  122. }
  123. # 增加上级佣金
  124. public function rebate($uid, $goods_id, $cash)
  125. {
  126. $goods = Dever::db('goods/info')->one($goods_id);
  127. $rebate_id = $goods['rebate_id'];
  128. if ($rebate_id > 0) {
  129. $rebate = Dever::db('goods/rebate')->one($rebate_id);
  130. if ($rebate) {
  131. if ($rebate['type'] == 1) {
  132. } else {
  133. }
  134. }
  135. }
  136. }
  137. # 生成订单号
  138. public function getOrderId()
  139. {
  140. $where['order_id'] = $this->createOrderId();
  141. $state = Dever::db('goods/order')->one($where);
  142. if (!$state) {
  143. return $where['order_id'];
  144. } else {
  145. return $this->getOrderId();
  146. }
  147. }
  148. # 生成订单号
  149. public function createOrderId()
  150. {
  151. if (function_exists('session_create_id')) {
  152. return strtoupper(session_create_id());
  153. } else {
  154. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  155. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  156. }
  157. }
  158. # 临时订单号 无用
  159. public function createTmpOrderId($prefix = '')
  160. {
  161. return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  162. }
  163. }