Member.php 13 KB

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