BasePayServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * 支付公用类
  4. */
  5. namespace app\api\services;
  6. use app\common\model\ShopGoodsModel;
  7. use app\common\model\ShopOrderGoodsModel as OrderGoods;
  8. use app\common\model\ShopOrderModel;
  9. use app\common\model\ShopOrderModel as Order;
  10. use app\common\model\UserModel;
  11. use app\common\model\UserMoneyModel;
  12. use app\common\model\UserShouxufeilogModel;
  13. use app\common\model\VipOrderModel;
  14. use jobs\VipOrderJob;
  15. use services\CacheServices;
  16. use think\Exception;
  17. use think\facade\Db;
  18. use think\facade\Env;
  19. abstract class BasePayServices
  20. {
  21. /**
  22. * 支付状态
  23. * @var bool
  24. */
  25. protected $pay_status = false;
  26. /**
  27. * 支付回调信息
  28. * @var string
  29. */
  30. protected $notify_info = '';
  31. /**
  32. * 支付通道 1 支付宝 2 微信
  33. * @var int
  34. */
  35. protected $pay_way = 1;
  36. /**
  37. * 费用配置
  38. * @param int $uid
  39. * @param array $data
  40. * @param int $type
  41. * @return array
  42. */
  43. public function _payConf(int $uid, array $data, int $type = 1)
  44. {
  45. $body = $total_amount = $remarks = '';
  46. switch ($data['order_type']) {
  47. case 1: // 充值
  48. $body = '充值';
  49. $total_amount = $data['money'];
  50. break;
  51. case 2: // 实名认证
  52. $total_amount = Db::name('user_data')->where('uid', $uid)->value('rz_money');
  53. // if (!$total_amount || $total_amount < 1.5)
  54. // throw new Exception('认证费用不正确');
  55. $body = '实名认证费用';
  56. break;
  57. case 3: // 开通会员
  58. $vip_money = Env::get('app.VIP_MONEY', 38);
  59. if (!$vip_money > 0) {
  60. throw new Exception('会员配置不存在');
  61. }
  62. $body = '开通会员';
  63. $total_amount = $vip_money;
  64. $remarks = $data['order_id'];
  65. break;
  66. case 4: //购买商品
  67. $order_sn = $data['order_id'];
  68. // $checkingOrder = Order::checkingOrder($order_sn, $data['money'], $uid);
  69. // new
  70. $checkingOrder = Order::checkingOrder($order_sn, 0, $uid);
  71. if (!$checkingOrder) {
  72. throw new Exception('校验订单失败');
  73. }
  74. if ($checkingOrder['flag'] !== 200) {
  75. throw new Exception($checkingOrder['msg']);
  76. }
  77. $body = '购买商品';
  78. $total_amount = $checkingOrder['orderPayment'];
  79. $remarks = $order_sn;
  80. //更改订单支付状态为线下支付待审核状态
  81. if ($this->data['channel'] == 4 && $this->data['order_type'] == 4) {
  82. Order::where('order_sn', 'in', $order_sn)->save(['pay_type' => 4, 'status' => 4]);
  83. }
  84. break;
  85. }
  86. if (!$body || !$total_amount || $total_amount <= 0)
  87. throw new Exception('配置错误');
  88. // 这是测试 线要干掉
  89. // $total_amount = floatval('0.'.mt_rand(1, 3).mt_rand(1, 9));;
  90. return [$body, $type == 1 ? $total_amount : bcmul($total_amount, 100, 2), $data['order_type'], $remarks, $data['trade_type'], $data['channel'], isset($data['voucher_img']) ? $data['voucher_img'] : ''];
  91. }
  92. /**
  93. * 保存订单信息
  94. * @param array $param
  95. * @param int $pay_way
  96. */
  97. public function setPaymentOrder(array $param, int $pay_way = 1)
  98. {
  99. $insert = [
  100. 'total_fee' => $param['total_amount'],
  101. 'trade_type' => $param['trade_type'],
  102. 'body' => $param['body'],
  103. 'state' => 7,
  104. 'out_trade_no' => $param['out_trade_no'],
  105. 'pay_way' => $param['pay_way'],
  106. 'remarks' => $param['remarks'],
  107. 'order_type' => $param['order_type'],
  108. 'uid' => $param['uid'],
  109. 'voucher_img' => $param['voucher_img'],
  110. 'out_trade_no1'=>(isset($param['out_trade_no1'])?$param['out_trade_no1']:(isset($param[''])))
  111. ];
  112. Db::name('payment')->insert($insert);
  113. }
  114. /**
  115. * 更改订单状态
  116. * @return mixed
  117. */
  118. public function afterPay()
  119. {
  120. if ($this->pay_status && !empty($this->notify_info)) {
  121. $pay_info = Db::name('payment')->where('out_trade_no', $this->notify_info['out_trade_no'])->where('state', 7)->where('pay_way', 2)->findOrEmpty();
  122. if (!empty($pay_info)) {
  123. Db::startTrans();
  124. try {
  125. Db::name('payment')->where('id', $pay_info['id'])->update(['state' => 6, 'pay_at' => date('Y-m-d H:i:s')]); // 更改订单状态
  126. switch ($pay_info['order_type']) {
  127. case 1: // 充值
  128. // $pay_num = Db::name('payment')->where(['uid' => $pay_info['uid'], 'state' => 6, 'order_type' => 1])->count('id');
  129. // set_user_money($pay_info['uid'], $pay_info['total_fee'], 1);
  130. // if ($pay_num == 1 && $pay_info['total_fee'] >= 100) { // 首次充值并且金额大于100,送月会员
  131. // $user = Db::name('user')->where('id', $pay_info['uid'])->field('vip,vip_expire_time')->find();
  132. // $date = new \DateTime($user['vip_expire_time']);
  133. // $date->add(new \DateInterval(get_vip_datetime_string('monthly_card')['time']));
  134. // $vip_expire_time = $date->format('Y-m-d H:i:s'); // vip到期时间
  135. // $user_update['vip_expire_time'] = $vip_expire_time;
  136. // $user_update['vip_type'] = 'monthly_card';
  137. // $user['vip'] < 1 && $user_update['vip'] = get_vip_datetime_string('monthly_card')['vip_type'];
  138. // Db::name('user')->where('id', $pay_info['uid'])->update($user_update);
  139. // Db::name('vip_log')->insert(['uid' => $pay_info['uid'], 'type' => 2, 'pay_price' => $pay_info['total_fee'], 'vip_expire_time' => $vip_expire_time, 'vip_type' => 'monthly_card']);
  140. // }
  141. break;
  142. case 3:
  143. // 充值会员
  144. // CacheServices::set('is_lock_vipdata', 2);
  145. // 更改订单状态
  146. // $order = VipOrderModel::where('order_id', $pay_info['remarks'])->find();
  147. //
  148. // // 更新viporder为支付状态
  149. // VipOrderModel::where('order_id', $pay_info['remarks'])->save(['status' => 2]);
  150. //
  151. // $buy_info = Db::name('user')->where('id', $order['user_id'])->find();
  152. // Db::name('user')->where('id', $order['user_id'])->inc('vip_count', 1)->update();
  153. // Db::name('user')->where('id', $order['user_id'])->save(['is_vip' => 1]);
  154. // Db::name('user')->where('id', $order['user_id'])->inc('target_inc', 3069)->update();
  155. // $curpid = $buy_info['pid'];
  156. // if ($curpid > 0) {
  157. // while (1) {
  158. // if ($curpid == 0 || empty($curpid)) {
  159. // break;
  160. // }
  161. // $cur_user = Db::name('user')->where('id', $curpid)->find();
  162. // Db::name('user')->where('id', $curpid)->inc('group_vip_count', 1)->update();
  163. // $curpid = $cur_user['pid'];
  164. // }
  165. // }
  166. //
  167. //// $val = CacheServices::get('is_lock_vipdata', 0);
  168. //// if ($val == 1){
  169. //// return api_error_return('请稍后再试1');
  170. //// }
  171. //
  172. //// CacheServices::set('is_lock_vipdata', 1);
  173. //// Db::startTrans();
  174. //// try {
  175. // $level = Db::name('vip_relation_data')->lock(true)->max('level');
  176. // if (empty($level)) {
  177. // $level = 1;
  178. // } else {
  179. // $maxid = Db::name('vip_relation_data')->lock(true)->max('id');
  180. // if ($maxid >= (pow(2, $level) - 1)) {
  181. // ++$level;
  182. // };
  183. // }
  184. // if ($level > 1) {
  185. // $curarr = CacheServices::get('vip_level_data' . $level);
  186. // if (!$curarr) {
  187. // $info = Db::name('vip_level_data')->where(['level' => $level])->find();
  188. // $curarr = explode(',', $info['value']);
  189. // CacheServices::set('vip_level_data' . $level, $curarr);
  190. // }
  191. // // 上一层最大的id
  192. // $max_level_info_id = Db::name('vip_relation_data')->where(['level' => $level - 1])->field('id')->lock(true)->max('id');
  193. // $pid = $curarr[$maxid - $max_level_info_id];
  194. // } else {
  195. // $pid = 0;
  196. // }
  197. //
  198. // $insertData = [
  199. // 'create_time' => getCurTimeStamp(),
  200. // 'pid' => $pid,
  201. // 'level' => $level,
  202. // 'uid' => $buy_info['id']
  203. // ];
  204. //
  205. // Db::name('vip_relation_data')->lock(true)->insertGetId($insertData);
  206. //// \utils\Queue::instance()->log('会员VIP订单执行成功' . $buy_info['id'])->do('VipOrderBack')->job(VipOrderJob::class)->push([$level, $buy_info['id'], $pid]);
  207. //
  208. // break;
  209. case 4: //购买商品
  210. // $order = Order::where(['order_sn' => explode(',', $pay_info['remarks'])])->select()->toArray();
  211. // Order::payDoneOrderCallback($order, 3, $pay_info['uid']);
  212. $m_goods = new ShopGoodsModel();
  213. $m_user = new UserModel();
  214. $m_shoporder = new ShopOrderModel();
  215. // 更改订单状态
  216. $order = ShopOrderModel::where('order_sn', $pay_info['remarks'])->find();
  217. $nowTime = date('Y-m-d H:i:s', time());
  218. Db::name('shop_order')->where(['order_sn' => $pay_info['remarks']])
  219. ->update(['pay_type' => 2, 'status' => 1, 'updated_time' => $nowTime]);
  220. // 增加销量
  221. $orderGoods = OrderGoods::where(['order_id' => $order['order_id']])->select()->toArray();
  222. foreach ($orderGoods as $k => $v) {
  223. ShopGoodsModel::where(['goods_id' => $v['goods_id']])->inc('sales_volume', $v['num'])->inc('real_sales_volume', $v['num'])->update();
  224. }
  225. // 送积分
  226. edit_user_score(5, $order['user_id'], $order['rebate_score']);
  227. edit_user_redscore(1, $order['user_id'], $order['rebate_lock_score']);
  228. break;
  229. }
  230. Db::commit();
  231. echo 'success';
  232. die;
  233. } catch (\Exception $e) {
  234. Db::rollback();
  235. echo $e->getMessage();
  236. die;
  237. }
  238. }
  239. echo 'success';
  240. die;
  241. }
  242. echo 'failure';
  243. die;
  244. }
  245. }