Member.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\api\controller;
  3. use alipay\aop\AopClient;
  4. use alipay\aop\request\AlipayTradeAppPayRequest;
  5. use app\admin\model\BuyVipLogModel;
  6. use app\admin\model\Cardtip;
  7. use app\api\model\UsersModel;
  8. use app\common\controller\Api;
  9. use app\admin\model\Equity;
  10. use app\common\lib\WxPay;
  11. use think\Db;
  12. use app\common\lib\Promote;
  13. /**
  14. * 开通VIP接口
  15. */
  16. class Member extends Api
  17. {
  18. protected $noNeedLogin = '*';
  19. /**
  20. * 开通VIP页面
  21. *
  22. * @ApiTitle (开通VIP页面)
  23. * @ApiSummary (开通VIP页面)
  24. * @ApiMethod (POST)
  25. * @ApiRoute (/api/member/index)
  26. * @ApiParams (name="user_id", type="int", required=true, description="用户id")
  27. */
  28. public function index()
  29. {
  30. $userId = $this->request->post('user_id');
  31. if (!$userId) {
  32. $this->result('参数错误', [], 200);
  33. }
  34. $user = UsersModel::get($userId);
  35. if (!$user) {
  36. $this->result('用户不存在', [], 100);
  37. }
  38. //权益列表
  39. $list = Equity::select();
  40. foreach ($list as &$v) {
  41. $v['desc'] = explode(' ', $v['desc']);
  42. }
  43. //头部卡片
  44. $info = Cardtip::find(1);
  45. $info['avatar'] = UsersModel::where('user_id', $userId)->value('user_avatar');
  46. $info['desc'] = explode(' ', $info['desc']);
  47. $this->result('ok', ['card' => $info, 'equityList' => $list], 200);
  48. }
  49. /**
  50. * 开通VIP
  51. *
  52. * @ApiTitle (开通VIP)
  53. * @ApiSummary (开通VIP)
  54. * @ApiMethod (POST)
  55. * @ApiRoute (/api/member/buyVip)
  56. * @ApiParams (name="user_id", type="int", required=true, description="用户id")
  57. * @ApiParams (name="pay_type", type="int", required=true, description="支付类型 2-微信 3-支付宝")
  58. */
  59. public function buyVip()
  60. {
  61. $userId = $this->request->post('user_id');
  62. $payType = $this->request->post('pay_type');
  63. $amount = 0.01;
  64. // 微信回调
  65. $notify_url = config('site.httpurl').'/api/member/notify_url_vip';
  66. // 支付宝回调
  67. $notify_url2 = config('site.httpurl').'/api/member/notify_url_vip2';
  68. if (!$userId || !$payType) {
  69. $this->result('参数错误', [], 200);
  70. }
  71. $user = UsersModel::get($userId);
  72. if (!$user) {
  73. $this->result('用户不存在', [], 100);
  74. }
  75. // 查询剩余开通次数
  76. $canUseOpenNum = UsersModel::where('user_id', $userId)->value('vip_use_num');
  77. if ($canUseOpenNum == 5) {
  78. $this->result('您本月可开通VIP的次数已用完', [], 200);
  79. }
  80. $out_trade_no = createOutTradeNo();
  81. $orderInfo = array(
  82. 'uid' => $userId,
  83. 'out_trade_no' => $out_trade_no,
  84. 'amount' => $amount,
  85. 'time' => date('Y-m-d H:i:s', time())
  86. );
  87. Db::startTrans();
  88. $insert = BuyVipLogModel::insert($orderInfo);
  89. if ($insert) {
  90. Db::commit();
  91. // 支付类型
  92. // 微信支付
  93. if ($payType == 2) {
  94. $payObj = new WxPay();
  95. $getPrePayInfo = $payObj->getPrePayOrder('VIP会员购买', $out_trade_no, ($amount * 100), $notify_url);
  96. if ($getPrePayInfo) {
  97. $payInfo = $payObj->getOrder($getPrePayInfo['prepay_id']);
  98. if ($payInfo) {
  99. $this->result('支付订单发起成功', $payInfo, 200);
  100. } else {
  101. $this->result('支付订单发起失败,请稍后再试', [], 100);
  102. }
  103. } else {
  104. $this->result('支付订单发起失败,请稍后再试', [], 100);
  105. }
  106. }
  107. // 支付宝支付
  108. if ($payType == 3) {
  109. $aop = new AopClient;
  110. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  111. $aop->appId = config('alipay.app_id');
  112. $aop->rsaPrivateKey = config('alipay.private_key');
  113. $aop->format = "json";
  114. $aop->charset = "utf-8";
  115. $aop->signType = "RSA2";
  116. $aop->alipayrsaPublicKey = config('alipay.public_key');
  117. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  118. $request = new AlipayTradeAppPayRequest();
  119. // 订单标题
  120. $subject = 'VIP会员购买';
  121. // 订单详情
  122. $body = '优享街68元VIP会员购买';
  123. // SDK已经封装掉了公共参数,这里只需要传入业务参数
  124. $bizcontent = json_encode([
  125. 'body' => $body,
  126. 'subject' => $subject,
  127. 'out_trade_no' => $out_trade_no,
  128. 'timeout_express' => '90m',
  129. 'total_amount' => $amount,
  130. 'product_code' => 'QUICK_MSECURITY_PAY'
  131. ]);
  132. $request->setNotifyUrl($notify_url2);
  133. $request->setBizContent($bizcontent);
  134. // 这里和普通的接口调用不同,使用的是sdkExecute
  135. $response = $aop->sdkExecute($request);
  136. // 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
  137. // return $response;
  138. $this->result('订单发起成功', ['payInfoString' => $response], 200);
  139. }
  140. }
  141. }
  142. /**
  143. * 购买会员微信支付异步回调
  144. * @ApiInternal
  145. */
  146. public function notify_url_vip()
  147. {
  148. //获取返回的xml格式数据
  149. $payXml = file_get_contents("php://input");
  150. //将xml格式转化为json格式
  151. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  152. //将json格式转成数组格式
  153. $result = json_decode($jsonXml, true);
  154. if ($result) {
  155. //如果成功返回
  156. if ($result['return_code'] == 'SUCCESS') {
  157. if ($result['result_code'] == 'SUCCESS') {
  158. // sign 值校验
  159. // 校验时不包含返回的 sign 字段,需踢除 sign 字段
  160. foreach($result as $k => $v) {
  161. if ($k == 'sign') {
  162. $sign = $result[$k];
  163. unset($result[$k]);
  164. };
  165. }
  166. //按字典排序
  167. ksort($result);
  168. //转为 url 键值对
  169. $signTemp = http_build_query($result);
  170. //md5处理,$key 为微信商户平台的 api 安全密钥
  171. $key = 'b3ae6bbf3cc4fa017eb169ae219e2c27';
  172. $signTemp = md5($signTemp.'&key='.$key);
  173. //转大写得最终 sign 值
  174. $resultSign = strtoupper($signTemp);
  175. //如果sign值正确
  176. if ($sign === $resultSign) {
  177. $order = BuyVipLogModel::where('out_trade_no', $result['out_trade_no'])->find();
  178. // 查询用户
  179. $user = UsersModel::get($order->uid);
  180. if ($order) {
  181. // 更新支付状态
  182. $updatePayStatus = BuyVipLogModel::where('out_trade_no', $result['out_trade_no'])
  183. ->setField('pay_status', 1);
  184. // 修改会员等级
  185. if ($user->user_level == 1) {
  186. $updateVipLevel = UsersModel::where('user_id', $order->uid)->update(['user_level' => 2]);
  187. $p = new Promote();
  188. $p->uid = $order->uid;
  189. $p->getMyBoss();
  190. } else {
  191. $updateVipLevel = true;
  192. }
  193. // 限购份数加一
  194. $addLimitNum = UsersModel::where('user_id', $order->uid)->setInc('vip_use_num', 1);
  195. // 增加话费优惠额度
  196. $addChargeQuota = UsersModel::where('user_id', $order->uid)->setInc('vip_discount_quota', 100);
  197. // 查询我的推荐人手机号
  198. $myRecTel = UsersModel::where('user_id', $order->uid)->value('user_tjtel');
  199. // 查询我的推荐人user_id
  200. $myRecId = UsersModel::where('user_tel', $myRecTel)->value('user_id');
  201. // 我加一次排队次数
  202. $addMyQueueNum = UsersModel::where('user_id', $order->uid)->setInc('get_queue_num', 1);
  203. // 推荐人加一次排队次数
  204. $addRecQueueNum = UsersModel::where('user_id', $myRecId)->setInc('get_queue_num', 1);
  205. if ($updatePayStatus && $updateVipLevel && $addLimitNum && $addChargeQuota && $addMyQueueNum && $addRecQueueNum) {
  206. $successArray = array(
  207. 'return_code' => 'SUCCESS',
  208. 'return_msg' => 'OK'
  209. );
  210. return $this->arrayToXml($successArray);
  211. }
  212. }
  213. }
  214. } else {
  215. return false;
  216. }
  217. } else {
  218. return false;
  219. }
  220. }
  221. }
  222. /**
  223. * 数组转xml
  224. * @ApiInternal
  225. */
  226. public function arrayToXml($arr)
  227. {
  228. $xml = "<xml>";
  229. foreach ($arr as $key=>$val)
  230. {
  231. if (is_numeric($val))
  232. {
  233. $xml.="<".$key.">".$val."</".$key.">";
  234. }
  235. else
  236. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  237. }
  238. $xml.="</xml>";
  239. return $xml;
  240. }
  241. /**
  242. * 购买会员支付宝支付异步回调
  243. * @ApiInternal
  244. */
  245. public function notify_url_vip2()
  246. {
  247. $params = $this->request->post();
  248. if (!empty($params) && $params['trade_status'] == 'TRADE_SUCCESS') {
  249. // 验证签名
  250. $aop = new AopClient();
  251. $aop->alipayrsaPublicKey = config('alipay.public_key');
  252. // 此处反转义参数中的字符,否则验签不通过
  253. $params['fund_bill_list'] = htmlspecialchars_decode($params['fund_bill_list']);
  254. $checkSign = $aop->rsaCheckV1($params, null, 'RSA2');
  255. if ($checkSign) {
  256. // 是不是向此商户号付款
  257. if ($params['app_id'] == config('alipay.app_id')) {
  258. // 查询订单是否存在
  259. $order = BuyVipLogModel::where('out_trade_no', $params['out_trade_no'])->find();
  260. // 查询用户
  261. $user = UsersModel::get($order->uid);
  262. if (!empty($order)) {
  263. // 更新支付状态
  264. $updatePayStatus = BuyVipLogModel::where('out_trade_no', $params['out_trade_no'])->setField('pay_status', 1);
  265. // 修改会员等级
  266. if ($user->user_level == 1) {
  267. $updateVipLevel = UsersModel::where('user_id', $order->uid)->update(['user_level' => 2]);
  268. $p = new Promote();
  269. $p->uid = $order->uid;
  270. $p->getMyBoss();
  271. } else {
  272. $updateVipLevel = true;
  273. }
  274. // 限购份数加一
  275. $addLimitNum = UsersModel::where('user_id', $order->uid)->setInc('vip_use_num', 1);
  276. // 增加话费优惠额度
  277. $addChargeQuota = UsersModel::where('user_id', $order->uid)->setInc('vip_discount_quota', 100);
  278. // 查询我的推荐人手机号
  279. $myRecTel = UsersModel::where('user_id', $order->uid)->value('user_tjtel');
  280. // 查询我的推荐人user_id
  281. $myRecId = UsersModel::where('user_tel', $myRecTel)->value('user_id');
  282. // 我加一次排队次数
  283. $addMyQueueNum = UsersModel::where('user_id', $order->uid)->setInc('get_queue_num', 1);
  284. // 推荐人加一次排队次数
  285. $addRecQueueNum = UsersModel::where('user_id', $myRecId)->setInc('get_queue_num', 1);
  286. if ($updatePayStatus && $updateVipLevel && $addLimitNum && $addChargeQuota && $addMyQueueNum && $addRecQueueNum) {
  287. echo 'success';
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. public function tt()
  295. {
  296. $p = new Promote();
  297. $p->uid = 6;
  298. $result = $p->getMyBoss();
  299. halt($result);
  300. }
  301. }