소스 검색

增加内购

15954078560 2 년 전
부모
커밋
1db6da8c05
1개의 변경된 파일261개의 추가작업 그리고 0개의 파일을 삭제
  1. 261 0
      application/api/controller/Money.php

+ 261 - 0
application/api/controller/Money.php

@@ -241,4 +241,265 @@ class Money extends Common
         $this->success('获取钻石明细', $list);
     }
 
+    //苹果内购充值钻石
+    public function iospay() {
+        $purpose = 4;//$this->request->request("purpose");// 充值用途 1=认证,2=有眼缘,3=购买会员, 4=充值钻石
+//        $type = $this->request->request("type","wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
+//        $method = "app";
+//        if(!in_array($purpose,[1,2,3,4]) || !in_array($type,['wechat','alipay'])) {
+//            $this->error(__('Invalid parameters'));
+//        }
+        if(!in_array($purpose,[1,2,3,4])) {
+            $this->error(__('Invalid parameters'));
+        }
+//        if($type == 'wechat') $this->error(__('微信支付暂未开通!请选择支付宝支付!'));
+        $user_id = $this->auth->id;
+        $fate_user_id = 0;
+        $vip_config_id = 0;
+
+        switch ($purpose) {
+            case 1:
+                $title = "实名认证支付";
+                $amount = config("site.auth");
+                break;
+            case 2:
+                $fate_user_id = $this->request->request("fate_user_id",0); // 有眼缘用户ID
+                if(!$fate_user_id) {
+                    $this->error(__('Invalid parameters'));
+                }
+                $title = "有眼缘支付";
+                $amount = config("site.fate");
+                break;
+            case 3:
+                $vip_config_id = $this->request->request("vip_config_id",0);
+                if(!$vip_config_id) {
+                    $this->error(__('Invalid parameters'));
+                }
+                $title = "会员开通支付";
+                // 获取会员配置信息
+                if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
+                $vipConfigInfo = \app\admin\model\vip\Config::where(['id'=>$vip_config_id])->find();
+                if (!$vipConfigInfo) {
+                    $this->error('网络延迟,请稍后再试');
+                }
+                $amount = $vipConfigInfo['real_price'];
+                break;
+            case 4:
+                $vip_config_id = $this->request->request("vip_config_id",0);
+                if(!$vip_config_id) {
+                    $this->error(__('Invalid parameters'));
+                }
+                $title = "钻石充值支付";
+                // 获取会员配置信息
+                if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
+                $vipConfigInfo = Db::name('diamond_ios')->where(['id'=>$vip_config_id])->find();
+                if (!$vipConfigInfo) {
+                    $this->error('网络延迟,请稍后再试');
+                }
+                $amount = $vipConfigInfo['price'];
+                break;
+        }
+
+        $out_trade_no = date("YmdHis").rand(100000,999999);
+
+        if (!$amount || $amount < 0) {
+            $this->error("支付金额必须大于0");
+        }
+
+        $time = time();
+        $money = $amount;
+        $type = '苹果内购';
+
+        // 生成订单
+        $recharOrderMode = new \app\common\model\RecharOrder();
+        $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `vip_config_id`, `fate_user_id`, `money` ,`purpose`, `pay_type`, `createtime`) VALUES ($user_id , '$out_trade_no' , $vip_config_id, $fate_user_id, $money ,$purpose, '$type', $time)");
+        if(!$orderid) $this->error("订单创建失败!");
+
+        $this->success("参数获取成功!", $out_trade_no);
+    }
+
+    // 内购支付回调
+    public function result() {
+        //苹果内购的验证收据
+        $receipt_data = input('apple_receipt', '', 'trim');
+        $order_no = input('order_no', '', 'trim');
+
+        //error_log(print_r($receipt_data, 1), 3, './receipt_data.txt');
+        if (!$receipt_data || !$order_no) {
+            $this->error('缺少参数');
+        }
+
+        // 查找订单
+        $Order = Db::name('rechar_order');
+        $order_info = $Order->where(array('order_no' => $order_no))->find();
+        if (!$order_info) {
+            //error_log(print_r(11, 1), 3, './11.txt');
+            $this->error('订单丢失');
+        }
+        if ($order_info['status'] == 1) {
+            $this->success('购买成功');
+        }
+        //查询bundle_id
+        $vipConfigInfo = Db::name('diamond_ios')->where(['id' => $order_info['vip_config_id']])->find();
+        if (!$vipConfigInfo) {
+            $this->error('数据丢失');
+        }
+
+        // 验证支付状态
+        $result = $this->validate_apple_pay($receipt_data, $vipConfigInfo['bundle_id']);
+        if (!$result['status']) {
+            // 验证不通过
+            //error_log(print_r($result, 1), 3, './result.txt');
+            $this->error($result['message']);
+        }
+
+        $count = count($result['data']['receipt']['in_app']);
+        $use_count = $count - 1;
+        //p($apple_id);p($result['data']['receipt']['in_app'][$use_count]['product_id']);die;
+        if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != 'com.lnkj.authentication.3') {
+            $this->error('非法请求,请立刻停止');
+        }
+
+        Db::startTrans();
+        // 获取钻石配置信息
+        $userInfo = Db::name('user')->where(['id' => $order_info['user_id']])->find();
+        // 修改订单状态
+        $res1 = $Order->update(["status"=>1,"updatetime"=>time()],["order_no"=>$order_no, 'status' => 0]);
+        //修改用户钻石余额
+        $diamond = $userInfo['diamond'] + $vipConfigInfo['number'];
+        $res2 = Db::name('user')->where(['id' => $userInfo['id'], 'diamond' => $userInfo['diamond']])->setField('diamond', $diamond);
+        // 添加钻石明细
+        $diamond_log = Db::name('user_diamond_log')->where(['user_id' => $userInfo['id']])->order('id', 'desc')->find();
+        if (!$diamond_log && $userInfo['diamond'] > 0) {
+            $res3 = false;
+        }
+        if ($diamond_log && $diamond_log['after'] != $userInfo['diamond']) {
+            $res3 = false;
+        }
+        if ($res3) {
+            $data = [];
+            $data['user_id'] = $userInfo['id'];
+            $data['diamond'] = $vipConfigInfo['number'];
+            $data['before'] = $userInfo['diamond'];
+            $data['after'] = $diamond;
+            $data['memo'] = '充值';
+            $data['createtime'] = time();
+
+            $res3 = Db::name('user_diamond_log')->insertGetId($data);
+        }
+        if($res1 && $res2 !== false && $res3) {
+            Db::commit();
+            $this->success('购买成功');
+        } else {
+            Db::rollback();
+            $this->error('购买失败');
+        }
+    }
+
+    /**
+     * 验证AppStore内付
+     * @param string $receipt_data 付款后凭证
+     * @return array                验证是否成功
+     */
+    function validate_apple_pay($receipt_data = '', $bundle_id = '') {
+        // 验证参数
+        if (strlen($receipt_data) < 20) {
+            $result = array(
+                'status' => false,
+                'message' => '非法参数'
+            );
+            return $result;
+        }
+        // 请求验证
+        $html = $this->curl($receipt_data);
+        $data = json_decode($html, true);
+//        p($data);die;
+
+        if ($data['status'] == '21002') {
+            $this->error('21002');
+        }
+
+        // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
+        if ($data['status'] == '21007') {
+            // 请求验证
+            $html = $this->curl($receipt_data, 1);
+            $data = json_decode($html, true);
+            $data['sandbox'] = '1';
+        }
+        if (isset($_GET['debug'])) {
+            exit(json_encode($data));
+        }
+//        if ($data['receipt']['bundle_id'] != 'com.liuniukeji.mayivideo') {
+        if ($data['receipt']['bundle_id'] != $bundle_id) {
+            $result = array(
+                'status' => false,
+                'message' => '非法请求',
+                'data' => $data
+            );
+            return $result;
+        }
+        // 判断是否购买成功
+        if (intval($data['status']) === 0) {
+            $result = array(
+                'status' => true,
+                'message' => '购买成功',
+                'data' => $data
+            );
+        } else {
+            $result = array(
+                'status' => false,
+                'message' => '购买失败 status:' . $data['status']
+            );
+        }
+
+        return $result;
+    }
+
+    /**
+     * 21000 App Store不能读取你提供的JSON对象25
+     * 21002 receipt-data域的数据有问题
+     * 21003 receipt无法通过验证
+     * 21004 提供的shared secret不匹配你账号中的shared secret
+     * 21005 receipt服务器当前不可用
+     * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
+     * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
+     * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
+     */
+    function curl($receipt_data, $sandbox = 0) {
+        //小票信息
+        $POSTFIELDS = array("receipt-data" => $receipt_data);
+        $POSTFIELDS = json_encode($POSTFIELDS);
+//        $POSTFIELDS = '{' . '"receipt-data":' . '"' . $receipt_data . '"' .'}';
+//        p($POSTFIELDS);die;
+        //正式购买地址 沙盒购买地址
+        $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
+        $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
+
+        if ($sandbox > 0) {
+            $url = $url_buy;
+        } else {
+            $url = $url_sandbox;
+        }
+
+        // 上架通过直接使用正式地址
+//        $url = $url_sandbox;
+
+        $ch = curl_init($url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_POST, true);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  //这两行一定要加,不加会报SSL 错误
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+        $response = curl_exec($ch);
+        $errno = curl_errno($ch);
+        curl_close($ch);
+
+        if ($errno != 0) {
+            return $errno;
+        } else {
+            return $response;
+        }
+
+    }
+
 }