UserRecharge.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/05
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\MiniProgramService;
  10. use crmeb\services\SystemConfigService;
  11. use crmeb\services\WechatService;
  12. use crmeb\traits\ModelTrait;
  13. /**
  14. * TODO 用户充值
  15. * Class UserRecharge
  16. * @package app\models\user
  17. */
  18. class UserRecharge extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'user_recharge';
  30. use ModelTrait;
  31. protected $insert = ['add_time'];
  32. protected function setAddTimeAttr()
  33. {
  34. return time();
  35. }
  36. /**
  37. * 创建充值订单
  38. * @param $uid
  39. * @param $price
  40. * @param string $recharge_type
  41. * @param int $paid
  42. * @return UserRecharge|bool|\think\Model
  43. */
  44. public static function addRecharge($uid, $price, $recharge_type = 'weixin', $give_price = 0, $paid = 0)
  45. {
  46. $order_id = self::getNewOrderId($uid);
  47. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  48. $add_time = time();
  49. return self::create(compact('order_id', 'uid', 'price', 'recharge_type', 'paid', 'add_time', 'give_price'));
  50. }
  51. /**
  52. * 生成充值订单号
  53. * @param int $uid
  54. * @return bool|string
  55. */
  56. public static function getNewOrderId($uid = 0)
  57. {
  58. if (!$uid) return false;
  59. $count = (int)self::where('uid', $uid)->where('add_time', '>=', strtotime(date("Y-m-d")))->where('add_time', '<', strtotime(date("Y-m-d", strtotime('+1 day'))))->count();
  60. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  61. }
  62. /**
  63. * 充值js支付
  64. * @param $orderInfo
  65. * @return array|string
  66. * @throws \Exception
  67. */
  68. public static function jsPay($orderInfo)
  69. {
  70. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  71. }
  72. /**
  73. * 微信H5支付
  74. * @param $orderInfo
  75. * @return mixed
  76. */
  77. public static function wxH5Pay($orderInfo)
  78. {
  79. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
  80. }
  81. /**
  82. * 公众号支付
  83. * @param $orderInfo
  84. * @return array|string
  85. * @throws \Exception
  86. */
  87. public static function wxPay($orderInfo)
  88. {
  89. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  90. }
  91. public static function appPay($orderInfo)
  92. {
  93. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  94. if ($orderInfo['paid']) exception('支付已支付!');
  95. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  96. $site_name = sys_config('site_name');
  97. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret']);
  98. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']);
  99. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  100. $config = array(
  101. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  102. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  103. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  104. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  105. 'sslcert_path' => $payment['pay_weixin_client_cert'],
  106. 'sslkey_path' => $payment['pay_weixin_client_key'],
  107. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  108. );
  109. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  110. $pay_data = [
  111. 'body' => '用户充值 - ' . $site_name, //内容
  112. 'attach' => 'user_recharge', //商家数据包
  113. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  114. 'total_fee' => bcmul($orderInfo['price'], 100, 0), //支付价格(单位:分)
  115. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  116. ];
  117. $url = $wechatpay->handle($pay_data);
  118. return $url;
  119. }
  120. /**
  121. * //TODO用户充值成功后
  122. * @param $orderId
  123. */
  124. public static function rechargeSuccess($orderId)
  125. {
  126. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  127. if (!$order) return false;
  128. $user = User::getUserInfo($order['uid']);
  129. self::beginTrans();
  130. $price = bcadd($order['price'], $order['give_price'], 2);
  131. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  132. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  133. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  134. $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  135. $res = $res1 && $res2 && $res3;
  136. self::checkTrans($res);
  137. event('RechargeSuccess', [$order]);
  138. return $res;
  139. }
  140. /**
  141. * 导入佣金到余额
  142. * @param $uid 用户uid
  143. * @param $price 导入金额
  144. * @return bool
  145. * @throws \think\Exception
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. */
  150. public static function importNowMoney($uid, $price)
  151. {
  152. $user = User::getUserInfo($uid);
  153. self::beginTrans();
  154. try {
  155. $broken_time = intval(sys_config('extract_time'));
  156. $search_time = time() - 86400 * $broken_time;
  157. //返佣 +
  158. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  159. ->where('add_time', '>', $search_time)
  160. ->where('pm', 1)
  161. ->sum('number');
  162. //退款退的佣金 -
  163. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  164. ->where('add_time', '>', $search_time)
  165. ->where('pm', 0)
  166. ->sum('number');
  167. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  168. if ($broken_commission < 0)
  169. $broken_commission = 0;
  170. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  171. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  172. $commission = bcmul($price,bcdiv(sys_config('commission',10),100,3),2);
  173. $integral = bcmul($price,bcdiv(sys_config('integral',20),100,3),2);
  174. $res1 = User::bcInc($uid, 'now_money', bcsub($price,bcadd($commission,$integral,2),2), 'uid');
  175. $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
  176. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  177. $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  178. $extractInfo = [
  179. 'uid' => $uid,
  180. 'real_name' => $user['nickname'],
  181. 'extract_type' => 'balance',
  182. 'extract_price' => $price,
  183. 'balance' => bcsub($user['brokerage_price'], $price, 2),
  184. 'add_time' => time(),
  185. 'status' => 1,
  186. 'commission'=>$commission,
  187. 'integral'=>$integral,
  188. ];
  189. $res4 = UserExtract::create($extractInfo);
  190. $res = $res2 && $res1 && $res3;
  191. self::checkTrans($res);
  192. if ($res) {
  193. event('ImportNowMoney', [$uid, $price]);
  194. }
  195. return $res;
  196. } catch (\Exception $e) {
  197. self::rollbackTrans();
  198. return self::setErrorInfo($e->getMessage());
  199. }
  200. }
  201. }