UserMobileRecharge.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\AlipayService;
  10. use crmeb\services\MiniProgramService;
  11. use crmeb\services\MobileRechargeService;
  12. use crmeb\services\SystemConfigService;
  13. use crmeb\services\WechatService;
  14. use crmeb\traits\ModelTrait;
  15. use think\facade\Log;
  16. /**
  17. * TODO 用户充值
  18. * Class UserRecharge
  19. * @package app\models\user
  20. */
  21. class UserMobileRecharge extends BaseModel
  22. {
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'user_mobile_recharge';
  33. use ModelTrait;
  34. protected $insert = ['add_time'];
  35. protected function setAddTimeAttr()
  36. {
  37. return time();
  38. }
  39. /**
  40. * 创建充值订单
  41. * @param $uid
  42. * @param $price
  43. * @param string $recharge_type
  44. * @param int $paid
  45. * @return UserRecharge|bool|\think\Model
  46. */
  47. public static function addRecharge($uid, $mobile, $price, $recharge_type = 'weixin', $get_money = 0, $paid = 0)
  48. {
  49. $order_id = self::getNewOrderId($uid);
  50. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  51. $add_time = time();
  52. return self::create(compact('order_id', 'get_money', 'uid', 'mobile', 'price', 'recharge_type', 'paid', 'add_time'));
  53. }
  54. /**
  55. * 生成充值订单号
  56. * @param int $uid
  57. * @return bool|string
  58. */
  59. public static function getNewOrderId($uid = 0)
  60. {
  61. if (!$uid) return false;
  62. $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();
  63. return 'mr' . date('YmdHis', time()) . (10000 + $count + $uid);
  64. }
  65. /**
  66. * 充值js支付
  67. * @param $orderInfo
  68. * @return array|string
  69. * @throws \Exception
  70. */
  71. public static function jsPay($orderInfo)
  72. {
  73. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费');
  74. }
  75. /**
  76. * 微信H5支付
  77. * @param $orderInfo
  78. * @return mixed
  79. */
  80. public static function wxH5Pay($orderInfo)
  81. {
  82. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费', '', 'MWEB');
  83. }
  84. /**
  85. * 公众号支付
  86. * @param $orderInfo
  87. * @return array|string
  88. * @throws \Exception
  89. */
  90. public static function wxPay($orderInfo)
  91. {
  92. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费');
  93. }
  94. /**
  95. * //TODO用户充值成功后
  96. * @param $orderId
  97. */
  98. public static function rechargeSuccess($orderId)
  99. {
  100. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  101. if (!$order) return false;
  102. self::beginTrans();
  103. try {
  104. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  105. $res = MobileRechargeService::fastRecharge($order['mobile'], $orderId, $order['get_money']);
  106. @file_put_contents('mobile_recharge.txt', json_encode($res), 8);
  107. if ($res['return_code'] != 200 || $res['result_code'] != 'SUCCESS') {
  108. Log::error($res['return_msg']);
  109. $res = MobileRechargeService::slowRecharge($order['mobile'], $orderId, $order['get_money']);
  110. if ($res['return_code'] != 200 || $res['result_code'] != 'SUCCESS') {
  111. Log::error($res['return_msg']);
  112. }
  113. }
  114. } catch (\Exception $e) {
  115. Log::error($e->getMessage());
  116. }
  117. //充值
  118. self::checkTrans($res1);
  119. return $res1;
  120. }
  121. }