123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace app\api\controller;
- use alipay\aop\AopClient;
- use alipay\aop\request\AlipayTradeAppPayRequest;
- use app\admin\model\BuyVipLogModel;
- use app\admin\model\Cardtip;
- use app\api\model\UsersModel;
- use app\common\controller\Api;
- use app\admin\model\Equity;
- use app\common\lib\WxPay;
- use think\Db;
- use app\common\lib\Promote;
- /**
- * 开通VIP接口
- */
- class Member extends Api
- {
- protected $noNeedLogin = '*';
- /**
- * 开通VIP页面
- *
- * @ApiTitle (开通VIP页面)
- * @ApiSummary (开通VIP页面)
- * @ApiMethod (POST)
- * @ApiRoute (/api/member/index)
- * @ApiParams (name="user_id", type="int", required=true, description="用户id")
- */
- public function index()
- {
- $userId = $this->request->post('user_id');
- if (!$userId) {
- $this->result('参数错误', [], 200);
- }
- $user = UsersModel::get($userId);
- if (!$user) {
- $this->result('用户不存在', [], 100);
- }
- //权益列表
- $list = Equity::select();
- foreach ($list as &$v) {
- $v['desc'] = explode(' ', $v['desc']);
- }
- //头部卡片
- $info = Cardtip::find(1);
- $info['avatar'] = UsersModel::where('user_id', $userId)->value('user_avatar');
- $info['desc'] = explode(' ', $info['desc']);
- $this->result('ok', ['card' => $info, 'equityList' => $list], 200);
- }
- /**
- * 开通VIP
- *
- * @ApiTitle (开通VIP)
- * @ApiSummary (开通VIP)
- * @ApiMethod (POST)
- * @ApiRoute (/api/member/buyVip)
- * @ApiParams (name="user_id", type="int", required=true, description="用户id")
- * @ApiParams (name="pay_type", type="int", required=true, description="支付类型 2-微信 3-支付宝")
- */
- public function buyVip()
- {
- $userId = $this->request->post('user_id');
- $payType = $this->request->post('pay_type');
- $amount = 0.01;
- // 微信回调
- $notify_url = config('site.httpurl').'/api/member/notify_url_vip';
- // 支付宝回调
- $notify_url2 = config('site.httpurl').'/api/member/notify_url_vip2';
- if (!$userId || !$payType) {
- $this->result('参数错误', [], 200);
- }
- $user = UsersModel::get($userId);
- if (!$user) {
- $this->result('用户不存在', [], 100);
- }
- // 查询剩余开通次数
- $canUseOpenNum = UsersModel::where('user_id', $userId)->value('vip_use_num');
- if ($canUseOpenNum == 5) {
- $this->result('您本月可开通VIP的次数已用完', [], 200);
- }
- $out_trade_no = createOutTradeNo();
- $orderInfo = array(
- 'uid' => $userId,
- 'out_trade_no' => $out_trade_no,
- 'amount' => $amount,
- 'time' => date('Y-m-d H:i:s', time())
- );
- Db::startTrans();
- $insert = BuyVipLogModel::insert($orderInfo);
- if ($insert) {
- Db::commit();
- // 支付类型
- // 微信支付
- if ($payType == 2) {
- $payObj = new WxPay();
- $getPrePayInfo = $payObj->getPrePayOrder('VIP会员购买', $out_trade_no, ($amount * 100), $notify_url);
- if ($getPrePayInfo) {
- $payInfo = $payObj->getOrder($getPrePayInfo['prepay_id']);
- if ($payInfo) {
- $this->result('支付订单发起成功', $payInfo, 200);
- } else {
- $this->result('支付订单发起失败,请稍后再试', [], 100);
- }
- } else {
- $this->result('支付订单发起失败,请稍后再试', [], 100);
- }
- }
- // 支付宝支付
- if ($payType == 3) {
- $aop = new AopClient;
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = config('alipay.app_id');
- $aop->rsaPrivateKey = config('alipay.private_key');
- $aop->format = "json";
- $aop->charset = "utf-8";
- $aop->signType = "RSA2";
- $aop->alipayrsaPublicKey = config('alipay.public_key');
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new AlipayTradeAppPayRequest();
- // 订单标题
- $subject = 'VIP会员购买';
- // 订单详情
- $body = '优享街68元VIP会员购买';
- // SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = json_encode([
- 'body' => $body,
- 'subject' => $subject,
- 'out_trade_no' => $out_trade_no,
- 'timeout_express' => '90m',
- 'total_amount' => $amount,
- 'product_code' => 'QUICK_MSECURITY_PAY'
- ]);
- $request->setNotifyUrl($notify_url2);
- $request->setBizContent($bizcontent);
- // 这里和普通的接口调用不同,使用的是sdkExecute
- $response = $aop->sdkExecute($request);
- // 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
- // return $response;
- $this->result('订单发起成功', ['payInfoString' => $response], 200);
- }
- }
- }
- /**
- * 购买会员微信支付异步回调
- * @ApiInternal
- */
- public function notify_url_vip()
- {
- //获取返回的xml格式数据
- $payXml = file_get_contents("php://input");
- //将xml格式转化为json格式
- $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
- //将json格式转成数组格式
- $result = json_decode($jsonXml, true);
- if ($result) {
- //如果成功返回
- if ($result['return_code'] == 'SUCCESS') {
- if ($result['result_code'] == 'SUCCESS') {
- // sign 值校验
- // 校验时不包含返回的 sign 字段,需踢除 sign 字段
- foreach($result as $k => $v) {
- if ($k == 'sign') {
- $sign = $result[$k];
- unset($result[$k]);
- };
- }
- //按字典排序
- ksort($result);
- //转为 url 键值对
- $signTemp = http_build_query($result);
- //md5处理,$key 为微信商户平台的 api 安全密钥
- $key = 'b3ae6bbf3cc4fa017eb169ae219e2c27';
- $signTemp = md5($signTemp.'&key='.$key);
- //转大写得最终 sign 值
- $resultSign = strtoupper($signTemp);
- //如果sign值正确
- if ($sign === $resultSign) {
- $order = BuyVipLogModel::where('out_trade_no', $result['out_trade_no'])->find();
- // 查询用户
- $user = UsersModel::get($order->uid);
- if ($order) {
- // 更新支付状态
- $updatePayStatus = BuyVipLogModel::where('out_trade_no', $result['out_trade_no'])
- ->setField('pay_status', 1);
- // 修改会员等级
- if ($user->user_level == 1) {
- $updateVipLevel = UsersModel::where('user_id', $order->uid)->update(['user_level' => 2]);
- $p = new Promote();
- $p->uid = $order->uid;
- $p->getMyBoss();
- } else {
- $updateVipLevel = true;
- }
- // 限购份数加一
- $addLimitNum = UsersModel::where('user_id', $order->uid)->setInc('vip_use_num', 1);
- // 增加话费优惠额度
- $addChargeQuota = UsersModel::where('user_id', $order->uid)->setInc('vip_discount_quota', 100);
- // 查询我的推荐人手机号
- $myRecTel = UsersModel::where('user_id', $order->uid)->value('user_tjtel');
- // 查询我的推荐人user_id
- $myRecId = UsersModel::where('user_tel', $myRecTel)->value('user_id');
- // 我加一次排队次数
- $addMyQueueNum = UsersModel::where('user_id', $order->uid)->setInc('get_queue_num', 1);
- // 推荐人加一次排队次数
- $addRecQueueNum = UsersModel::where('user_id', $myRecId)->setInc('get_queue_num', 1);
- if ($updatePayStatus && $updateVipLevel && $addLimitNum && $addChargeQuota && $addMyQueueNum && $addRecQueueNum) {
- $successArray = array(
- 'return_code' => 'SUCCESS',
- 'return_msg' => 'OK'
- );
- return $this->arrayToXml($successArray);
- }
- }
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- }
- /**
- * 数组转xml
- * @ApiInternal
- */
- public function arrayToXml($arr)
- {
- $xml = "<xml>";
- foreach ($arr as $key=>$val)
- {
- if (is_numeric($val))
- {
- $xml.="<".$key.">".$val."</".$key.">";
- }
- else
- $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
- }
- $xml.="</xml>";
- return $xml;
- }
- /**
- * 购买会员支付宝支付异步回调
- * @ApiInternal
- */
- public function notify_url_vip2()
- {
- $params = $this->request->post();
- if (!empty($params) && $params['trade_status'] == 'TRADE_SUCCESS') {
- // 验证签名
- $aop = new AopClient();
- $aop->alipayrsaPublicKey = config('alipay.public_key');
- // 此处反转义参数中的字符,否则验签不通过
- $params['fund_bill_list'] = htmlspecialchars_decode($params['fund_bill_list']);
- $checkSign = $aop->rsaCheckV1($params, null, 'RSA2');
- if ($checkSign) {
- // 是不是向此商户号付款
- if ($params['app_id'] == config('alipay.app_id')) {
- // 查询订单是否存在
- $order = BuyVipLogModel::where('out_trade_no', $params['out_trade_no'])->find();
- // 查询用户
- $user = UsersModel::get($order->uid);
- if (!empty($order)) {
- // 更新支付状态
- $updatePayStatus = BuyVipLogModel::where('out_trade_no', $params['out_trade_no'])->setField('pay_status', 1);
- // 修改会员等级
- if ($user->user_level == 1) {
- $updateVipLevel = UsersModel::where('user_id', $order->uid)->update(['user_level' => 2]);
- $p = new Promote();
- $p->uid = $order->uid;
- $p->getMyBoss();
- } else {
- $updateVipLevel = true;
- }
- // 限购份数加一
- $addLimitNum = UsersModel::where('user_id', $order->uid)->setInc('vip_use_num', 1);
- // 增加话费优惠额度
- $addChargeQuota = UsersModel::where('user_id', $order->uid)->setInc('vip_discount_quota', 100);
- // 查询我的推荐人手机号
- $myRecTel = UsersModel::where('user_id', $order->uid)->value('user_tjtel');
- // 查询我的推荐人user_id
- $myRecId = UsersModel::where('user_tel', $myRecTel)->value('user_id');
- // 我加一次排队次数
- $addMyQueueNum = UsersModel::where('user_id', $order->uid)->setInc('get_queue_num', 1);
- // 推荐人加一次排队次数
- $addRecQueueNum = UsersModel::where('user_id', $myRecId)->setInc('get_queue_num', 1);
- if ($updatePayStatus && $updateVipLevel && $addLimitNum && $addChargeQuota && $addMyQueueNum && $addRecQueueNum) {
- echo 'success';
- }
- }
- }
- }
- }
- }
- public function tt()
- {
- $p = new Promote();
- $p->uid = 6;
- $result = $p->getMyBoss();
- halt($result);
- }
- }
|