UserMobileRecharge.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. public static function aliPay($orderInfo)
  76. {
  77. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  78. if ($orderInfo['paid']) exception('支付已支付!');
  79. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  80. $site_name = sys_config('site_name');
  81. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  82. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  83. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  84. $aliPay = new AlipayService();
  85. $aliPay->setAppid($alipay['alipay_app_id']);
  86. $aliPay->setNotifyUrl($notifyUrl);
  87. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  88. $aliPay->setTotalFee($orderInfo['price']);
  89. $aliPay->setOutTradeNo($orderInfo['order_id']);
  90. $aliPay->setOrderName('用户充值话费 - ' . $site_name);
  91. $aliPay->setPassbackParams(['attach' => 'mobile_recharge']);
  92. $orderStr = $aliPay->getOrderStr();
  93. return $orderStr;
  94. }
  95. public static function appPay($orderInfo)
  96. {
  97. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  98. if ($orderInfo['paid']) exception('支付已支付!');
  99. if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
  100. $site_name = sys_config('site_name');
  101. $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret']);
  102. $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']);
  103. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  104. $config = array(
  105. 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
  106. 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
  107. 'mchid' => $payment['pay_weixin_mchid'], //商户id
  108. 'key' => $payment['pay_weixin_key'], //填写你设定的key
  109. 'sslcert_path' => $payment['pay_weixin_client_cert'],
  110. 'sslkey_path' => $payment['pay_weixin_client_key'],
  111. 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
  112. );
  113. $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
  114. $pay_data = [
  115. 'body' => '用户充值话费 - ' . $site_name, //内容
  116. 'attach' => 'mobile_recharge', //商家数据包
  117. 'out_trade_no' => $orderInfo['order_id'], //商户订单号
  118. 'total_fee' => bcmul($orderInfo['price'], 100, 0), //支付价格(单位:分)
  119. 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
  120. ];
  121. $url = $wechatpay->handle($pay_data);
  122. return $url;
  123. }
  124. /**
  125. * 微信H5支付
  126. * @param $orderInfo
  127. * @return mixed
  128. */
  129. public static function wxH5Pay($orderInfo)
  130. {
  131. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费', '', 'MWEB');
  132. }
  133. /**
  134. * 公众号支付
  135. * @param $orderInfo
  136. * @return array|string
  137. * @throws \Exception
  138. */
  139. public static function wxPay($orderInfo)
  140. {
  141. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费');
  142. }
  143. /**
  144. * //TODO用户充值成功后
  145. * @param $orderId
  146. */
  147. public static function rechargeSuccess($orderId)
  148. {
  149. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  150. if (!$order) return false;
  151. self::beginTrans();
  152. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  153. $res = MobileRechargeService::fastRecharge($order['mobile'], $orderId, $order['get_money']);
  154. if ($res['return_code'] != 200 || $res['result_code'] != 'SUCCESS') {
  155. Log::error($res['return_msg']);
  156. $res = MobileRechargeService::slowRecharge($order['mobile'], $orderId, $order['get_money']);
  157. if ($res['return_code'] != 200 || $res['result_code'] != 'SUCCESS') {
  158. Log::error($res['return_msg']);
  159. }
  160. }
  161. //充值
  162. self::checkTrans($res1);
  163. return $res1;
  164. }
  165. }