request->post('user_id'); if (!$userId) { $this->result('参数错误', [], 100); } if (!UsersModel::checkUserExist($userId)) { $this->result('用户不存在', [], 100); } $user = UsersModel::get($userId); $this->result('ok', ['quota' => $user->vip_discount_quota], 200); } /** * 话费充值 * * @ApiTitle (话费充值) * @ApiSummary (话费充值) * @ApiMethod (POST) * @ApiRoute (/api/recharge/recharge) * @ApiParams (name="user_id", type="int", required=true, description="用户id") * @ApiParams (name="price", type="int", required=true, description="充值面额") * @ApiParams (name="tel", type="string", required=true, description="充值手机号") * @ApiParams (name="pay_type", type="int", required=true, description="支付方式 1-微信 2-支付宝") */ public function recharge() { $userId = $this->request->post('user_id'); $price = $this->request->post('price'); $tel = $this->request->post('tel'); $payType = $this->request->post('pay_type'); if (!$userId || !$price || !$tel || !$payType) { $this->result('参数错误', [], 100); } $user = UsersModel::get($userId); if (!$user) { $this->result('用户不存在', [], 100); } //if ($user->user_level == 1) { // $this->result('您还不是VIP用户,不能进行充值', [], 100); //} // 检测手机号是否可以充值 $url = 'http://op.juhe.cn/ofpay/mobile/telcheck?phoneno='.$tel.'&cardnum='.$price.'&key=f5ff49671fede25118a6b01131ff88a4'; $res = $this->sendRequest($url); if ($res['error_code'] != 0) { $this->result('充值错误,请检查手机号码是否正确', [], 100); } // 根据会员等级判断是否享折扣 if ($user->user_level == 1) { $final_fee = $price; $isDiscount = 0; } else { // 折扣额度是否足够 if ($user->vip_discount_quota >= $price) { $final_fee = $price * 0.88; $isDiscount = 1; } else { $final_fee = $price; $isDiscount = 0; } } if ($tel == 18315626215 || $tel == 15615490741 || $tel == 18853911065) { $final_fee = 0.01; $isDiscount = 0; } // 生成充值订单 $out_trade_no = createOutTradeNo(); $orderInfo = array( 'uid' => $userId, 'tel' => $tel, 'price' => $price, 'create_time' => date('Y-m-d H:i:s', time()), 'pay_type' => $payType, 'out_trade_no' => $out_trade_no, 'final_fee' => $final_fee, 'is_discount' => $isDiscount ); $add = RechargeModel::create($orderInfo); if ($add) { if ($payType == 1) { $notify_url = config('site.httpurl').'/api/recharge/recharge_wx_notify'; $payObj = new WxPay(); $getPrePayInfo = $payObj->getPrePayOrder('话费充值', $out_trade_no, ($final_fee * 100), $notify_url); $getPayInfo = $payObj->getOrder($getPrePayInfo['prepay_id']); $this->result('订单创建成功', ['payInfoArray' => $getPayInfo], 200); } if ($payType == 2) { $notify_url2 = config('site.httpurl').'/api/recharge/recharge_alipay_notify'; $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 = '话费充值'; // 订单详情 $body = '话费充值'; // SDK已经封装掉了公共参数,这里只需要传入业务参数 $bizcontent = json_encode([ 'body' => $body, 'subject' => $subject, 'out_trade_no' => $out_trade_no, 'timeout_express' => '90m', 'total_amount' => $final_fee, '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); } } else { $this->result('订单创建失败', [], 200); } } /** * 1 * @ApiInternal */ public function sendRequest($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } /** * 话费充值微信支付异步回调 * @ApiInternal */ public function recharge_wx_notify() { //获取返回的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 = RechargeModel::where('out_trade_no', $result['out_trade_no'])->find(); if (!empty($order)) { // 修改订单支付状态和支付时间 $updatePayInfo = RechargeModel::where('out_trade_no', $result['out_trade_no']) ->update(['pay_status' => 1, 'pay_time' => date('Y-m-d H:i:s', time())]); // 修改充值状态 $updateRechargeStatus = RechargeModel::where('out_trade_no', $result['out_trade_no']) ->update(['recharge_status' => 2]); // 扣除优惠额度 if ($order->is_discount == 1) { UsersModel::where('user_id', $order->uid)->setDec('vip_discount_quota', $order->price); } // 调用聚合话费充值接口 $key = 'f5ff49671fede25118a6b01131ff88a4'; $phoneno = $order->tel; $cardnum = $order->price; $orderid = $order->out_trade_no; $sign = md5('JHfe821bbb6cef8f34d3dc7e68efec19ef'.$key.$phoneno.$cardnum.$orderid); $url = 'http://op.juhe.cn/ofpay/mobile/onlineorder?key='.$key.'&phoneno='.$phoneno.'&cardnum='.$cardnum.'&orderid='.$orderid.'&sign='.$sign; $this->sendRequest($url); if ($updatePayInfo && $updateRechargeStatus) { $successArray = array( 'return_code' => 'SUCCESS', 'return_msg' => 'OK' ); return $this->arrayToXml($successArray); } } } } } } } /** * 数组转xml * @ApiInternal */ public function arrayToXml($arr) { $xml = ""; foreach ($arr as $key=>$val) { if (is_numeric($val)) { $xml.="<".$key.">".$val.""; } else $xml.="<".$key.">"; } $xml.=""; return $xml; } /** * 话费充值支付宝支付异步回调 * @ApiInternal */ public function recharge_alipay_notify() { $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 = RechargeModel::where('out_trade_no', $params['out_trade_no'])->find(); if (!empty($order)) { // 修改订单支付状态和支付时间 $updatePayInfo = RechargeModel::where('out_trade_no', $params['out_trade_no']) ->update(['pay_status' => 1, 'pay_time' => date('Y-m-d H:i:s', time())]); // 修改充值状态 $updateRechargeStatus = RechargeModel::where('out_trade_no', $params['out_trade_no']) ->update(['recharge_status' => 2]); // 扣除优惠额度 if ($order->is_discount == 1) { UsersModel::where('user_id', $order->uid)->setDec('vip_discount_quota', $order->price); } // 调用聚合话费充值接口 $key = 'f5ff49671fede25118a6b01131ff88a4'; $phoneno = $order->tel; $cardnum = $order->price; $orderid = $order->out_trade_no; $sign = md5('JHfe821bbb6cef8f34d3dc7e68efec19ef'.$key.$phoneno.$cardnum.$orderid); $url = 'http://op.juhe.cn/ofpay/mobile/onlineorder?key='.$key.'&phoneno='.$phoneno.'&cardnum='.$cardnum.'&orderid='.$orderid.'&sign='.$sign; $this->sendRequest($url); if ($updatePayInfo && $updateRechargeStatus) { echo 'success'; } } } } } } /** * 话费充值聚合异步回调 废弃 * @ApiInternal */ public function recharge_notify() { $params = $this->request->post(); $order = RechargeModel::where('out_trade_no', $params['orderid'])->find(); if ($order) { $sign = md5('f5ff49671fede25118a6b01131ff88a4'.$order->sporder_id.$order->out_trade_no); if ($sign == $params['sign']) { //TODO } } } /** * 充值记录 * * @ApiTitle (充值记录) * @ApiSummary (充值记录) * @ApiMethod (POST) * @ApiRoute (/api/recharge/getRechargeRecord) * @ApiParams (name="user_id", type="int", required=true, description="用户id") */ public function getRechargeRecord() { $userId = $this->request->post('user_id'); if (!$userId) { $this->result('参数错误', [], 200); } $list = RechargeModel::where('uid', $userId) ->where('pay_status', 1) ->field('id,final_fee,create_time,recharge_status') ->select(); $this->result('ok', $list, 200); } /** * 充值详情 * * @ApiTitle (充值详情) * @ApiSummary (充值详情) * @ApiMethod (POST) * @ApiRoute (/api/recharge/rechargeDetail) * @ApiParams (name="id", type="int", required=true, description="记录id") */ public function rechargeDetail() { $id = $this->request->post('id'); if (!$id) { $this->result('参数错误', [], 200); } $order = RechargeModel::get($id); if (empty($order)) { $this->result('订单不存在', [], 200); } $orderid = $order->out_trade_no; $key = 'f5ff49671fede25118a6b01131ff88a4'; // 查询聚合订单状态信息 $url = 'http://op.juhe.cn/ofpay/mobile/ordersta?key='.$key.'&orderid='.$orderid; $result = $this->sendRequest($url); // 再根据聚合数据的订单状态更新数据库状态达到状态一致 if ($result['error_code'] === 0) { if ($result['result']['game_state'] == 1) { // 成功 RechargeModel::where('id', $id)->update(['recharge_status' => 2]); } if ($result['result']['game_state'] == 9) { // 失败 RechargeModel::where('id', $id)->update(['recharge_status' => 0]); } if ($result['result']['game_state'] == 0) { // 充值中 RechargeModel::where('id', $id)->update(['recharge_status' => 1]); } } // 再次查询 $order = RechargeModel::get($id); // 过滤字段 unset($order->id, $order->uid, $order->is_discount, $order->pay_status, $order->pay_time); $this->result('ok', $order, 200); } }