UserRecharge.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\user;
  12. use app\wap\model\user\WechatUser;
  13. use basic\ModelBasic;
  14. use service\SystemConfigService;
  15. use service\WechatService;
  16. use think\Log;
  17. use traits\ModelTrait;
  18. use app\wap\model\user\User;
  19. use service\HookService;
  20. /**虚拟币充值表
  21. * Class UserRecharge
  22. * @package app\wap\model\user
  23. */
  24. class UserRecharge extends ModelBasic
  25. {
  26. use ModelTrait;
  27. protected $insert = ['add_time'];
  28. protected function setAddTimeAttr()
  29. {
  30. return time();
  31. }
  32. public static function addRecharge($uid, $price, $recharge_type = 'weixin', $paid = 0)
  33. {
  34. $goldNum = money_rate_num($price, 'gold');
  35. $orderInfo = [
  36. 'uid' => $uid,
  37. 'order_id' => self::getNewOrderId(),
  38. 'price' => $price,
  39. 'recharge_type' => $recharge_type,
  40. 'paid' => $paid,
  41. 'gold_num' => $goldNum
  42. ];
  43. return self::set($orderInfo);
  44. }
  45. public static function getNewOrderId()
  46. {
  47. $count = (int)self::where('add_time', ['>=', strtotime(date("Y-m-d"))], ['<', strtotime(date("Y-m-d", strtotime('+1 day')))])->count();
  48. return 'wx1' . date('YmdHis', time()) . (10000 + $count + 1);
  49. }
  50. /**微信js支付
  51. * @param $orderId
  52. * @param string $field
  53. * @return array|string
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @throws \think\exception\DbException
  57. */
  58. public static function jsRechargePay($orderId, $field = 'order_id')
  59. {
  60. if (is_string($orderId))
  61. $orderInfo = self::where($field, $orderId)->find();
  62. else
  63. $orderInfo = $orderId;
  64. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  65. if ($orderInfo['paid']) exception('支付已支付!');
  66. if ($orderInfo['price'] <= 0) exception('该订单无需支付!');
  67. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  68. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['price'], 'recharge', SystemConfigService::get('site_name'));
  69. }
  70. /**
  71. * 微信h5支付
  72. * @param $orderId
  73. * @param string $field
  74. * @return array|string
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. */
  79. public static function h5RechargePay($orderId, $field = 'order_id')
  80. {
  81. if (is_string($orderId))
  82. $orderInfo = self::where($field, $orderId)->find();
  83. else
  84. $orderInfo = $orderId;
  85. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  86. if ($orderInfo['paid']) exception('支付已支付!');
  87. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  88. $site_name = SystemConfigService::get('site_name');
  89. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  90. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'recharge', self::getSubstrUTf8($site_name . '-金币充值', 30), '', 'MWEB');
  91. }
  92. /**余额支付
  93. * @param $order_id
  94. * @param $user_info
  95. * @return bool
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public static function yuePay($order_id, $user_info)
  101. {
  102. $uid = $user_info['uid'];
  103. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
  104. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  105. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  106. if ($orderInfo['recharge_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  107. if ($user_info['now_money'] < $orderInfo['price'])
  108. return self::setErrorInfo('余额不足' . floatval($orderInfo['price']));
  109. self::beginTrans();
  110. $res1 = self::where('order_id', $order_id)->update(['paid' => 1, 'pay_time' => time()]);
  111. $goldNum = money_rate_num($orderInfo['price'], 'gold');
  112. $goldName = SystemConfigService::get("gold_name");
  113. $res2 = false !== UserBill::income('用户充值' . $goldName, $orderInfo['uid'], 'gold_num', 'recharge', $goldNum, 0, bcadd($user_info['gold_num'], $goldNum, 0), '用户充值' . $orderInfo['price'] . '元人民币获得' . $goldNum . '个' . $goldName);
  114. $res3 = User::bcInc($orderInfo['uid'], 'gold_num', $goldNum, 'uid');
  115. $res4 = User::bcDec($orderInfo['uid'], 'now_money', $orderInfo['price'], 'uid');
  116. $res5 = UserBill::expend('充值金币', $uid, 'now_money', 'recharge', $orderInfo['price'], $orderInfo['id'], bcsub($user_info['now_money'], $orderInfo['price'], 2), '余额支付' . floatval($orderInfo['price']) . '元充值' . $goldName);
  117. try {
  118. $res = $res1 && $res2 && $res3 && $res4 && ($res5 ? true : false);
  119. self::checkTrans($res);
  120. return $res;
  121. } catch (\Exception $e) {
  122. self::rollbackTrans();
  123. return self::setErrorInfo($e->getMessage());
  124. }
  125. }
  126. /**
  127. * //TODO用户微信充值成功后
  128. * @param $orderId
  129. */
  130. public static function rechargeSuccess($orderId)
  131. {
  132. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  133. if (!$order) return false;
  134. $user = User::getUserData($order['uid']);
  135. self::beginTrans();
  136. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  137. $goldName = SystemConfigService::get("gold_name");
  138. $res4 = true;
  139. if ($res1 && $order['recharge_type'] != 'yue') {
  140. $res4 = UserBill::expend('充值金币', $order['uid'], $order['recharge_type'], 'recharge', $order['price'], $order['id'], 0, '支付' . floatval($order['price']) . '元充值' . $goldName);
  141. }
  142. $goldNum = money_rate_num($order['price'], 'gold');
  143. $res2 = UserBill::income('用户充值' . $goldName, $order['uid'], 'gold_num', 'recharge', $goldNum, 0, $user['gold_num'], '用户充值' . $order['price'] . '元人民币获得' . $goldNum . '个' . $goldName);
  144. $res3 = User::bcInc($order['uid'], 'gold_num', $goldNum, 'uid');
  145. $res = $res1 && $res2 && $res3 && $res4;
  146. self::checkTrans($res);
  147. return $res;
  148. }
  149. }