123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2018/01/05
- */
- namespace app\models\user;
- use crmeb\basic\BaseModel;
- use crmeb\services\AlipayService;
- use crmeb\services\MiniProgramService;
- use crmeb\services\MobileRechargeService;
- use crmeb\services\SystemConfigService;
- use crmeb\services\WechatService;
- use crmeb\traits\ModelTrait;
- use think\facade\Log;
- /**
- * TODO 用户充值
- * Class UserRecharge
- * @package app\models\user
- */
- class UserMobileRecharge extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_mobile_recharge';
- use ModelTrait;
- protected $insert = ['add_time'];
- protected function setAddTimeAttr()
- {
- return time();
- }
- /**
- * 创建充值订单
- * @param $uid
- * @param $price
- * @param string $recharge_type
- * @param int $paid
- * @return UserRecharge|bool|\think\Model
- */
- public static function addRecharge($uid, $mobile, $price, $recharge_type = 'weixin', $get_money = 0, $paid = 0)
- {
- $order_id = self::getNewOrderId($uid);
- if (!$order_id) return self::setErrorInfo('订单生成失败!');
- $add_time = time();
- return self::create(compact('order_id', 'get_money', 'uid', 'mobile', 'price', 'recharge_type', 'paid', 'add_time'));
- }
- /**
- * 生成充值订单号
- * @param int $uid
- * @return bool|string
- */
- public static function getNewOrderId($uid = 0)
- {
- if (!$uid) return false;
- $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();
- return 'mr' . date('YmdHis', time()) . (10000 + $count + $uid);
- }
- /**
- * 充值js支付
- * @param $orderInfo
- * @return array|string
- * @throws \Exception
- */
- public static function jsPay($orderInfo)
- {
- return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费');
- }
- public static function aliPay($orderInfo)
- {
- if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
- if ($orderInfo['paid']) exception('支付已支付!');
- if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
- $site_name = sys_config('site_name');
- if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
- $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
- $aliPay = new AlipayService();
- $aliPay->setAppid($alipay['alipay_app_id']);
- $aliPay->setNotifyUrl($notifyUrl);
- $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
- $aliPay->setTotalFee($orderInfo['price']);
- $aliPay->setOutTradeNo($orderInfo['order_id']);
- $aliPay->setOrderName('用户充值话费 - ' . $site_name);
- $aliPay->setPassbackParams(['attach' => 'mobile_recharge']);
- $orderStr = $aliPay->getOrderStr();
- return $orderStr;
- }
- public static function appPay($orderInfo)
- {
- if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
- if ($orderInfo['paid']) exception('支付已支付!');
- if ($orderInfo['price'] <= 0) exception('该支付无需支付!');
- $site_name = sys_config('site_name');
- $wechat = SystemConfigService::more(['weixin_open_appid', 'weixin_open_appsecret']);
- $payment = SystemConfigService::more(['pay_weixin_mchid', 'pay_weixin_client_cert', 'pay_weixin_client_key', 'pay_weixin_key', 'pay_weixin_open']);
- if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
- $config = array(
- 'appid' => $wechat['weixin_open_appid'], //填写高级调用功能的app id
- 'appsecret' => $wechat['weixin_open_appsecret'], //填写高级调用功能的app secret
- 'mchid' => $payment['pay_weixin_mchid'], //商户id
- 'key' => $payment['pay_weixin_key'], //填写你设定的key
- 'sslcert_path' => $payment['pay_weixin_client_cert'],
- 'sslkey_path' => $payment['pay_weixin_client_key'],
- 'transfer_rsa_public_path' => '', //企业转账到银行卡rsa公钥证书文件路径
- );
- $wechatpay = new \JiaLeo\Payment\Wechatpay\AppPay($config);
- $pay_data = [
- 'body' => '用户充值话费 - ' . $site_name, //内容
- 'attach' => 'mobile_recharge', //商家数据包
- 'out_trade_no' => $orderInfo['order_id'], //商户订单号
- 'total_fee' => bcmul($orderInfo['price'], 100, 0), //支付价格(单位:分)
- 'notify_url' => sys_config('site_url') . '/api/wechat/notify', //后台回调地址
- ];
- $url = $wechatpay->handle($pay_data);
- return $url;
- }
- /**
- * 微信H5支付
- * @param $orderInfo
- * @return mixed
- */
- public static function wxH5Pay($orderInfo)
- {
- return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费', '', 'MWEB');
- }
- /**
- * 公众号支付
- * @param $orderInfo
- * @return array|string
- * @throws \Exception
- */
- public static function wxPay($orderInfo)
- {
- return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'mobile_recharge', '用户充值话费');
- }
- /**
- * //TODO用户充值成功后
- * @param $orderId
- */
- public static function rechargeSuccess($orderId)
- {
- $order = self::where('order_id', $orderId)->where('paid', 0)->find();
- if (!$order) return false;
- self::beginTrans();
- $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
- $res = MobileRechargeService::fastRecharge($order['mobile'], $orderId, $order['get_money']);
- if ($res['return_code'] != 200 || $res['result_code'] != 'SUCCESS') {
- Log::error($res['return_msg']);
- $res = MobileRechargeService::slowRecharge($order['mobile'], $orderId, $order['get_money']);
- if ($res['return_code'] != 200 || $res['result_code'] != 'SUCCESS') {
- Log::error($res['return_msg']);
- }
- }
- //充值
- self::checkTrans($res1);
- return $res1;
- }
- }
|