Pay.php 5.9 KB

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