google内购In-App Billing

开发环境eclipse,下载内购需要的文件sdkManager,windows>android SDK manager>Extra..

将下载文件中的

拷到src下面,代码里要用的下面文件

初始化

//产品ID列表
public static String[] productID_Array = {"60crystal", "300crystal", "980crystal", "1980crystal", "3280crystal", "6480crystal", "3000crystalmonth"};
//购买列表
public static Vector<Purchase> inventory_list = new Vector<Purchase>();

        String base64EncodedPublicKey = "";
        mHelper = new IabHelper(this, base64EncodedPublicKey);
        mHelper.enableDebugLogging(false);
        //执行服务绑定
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() 
        {
            public void onIabSetupFinished(IabResult result) 
            {
                if (!result.isSuccess()) {
                    return;
                }
                //产品id列表
                List<String> productList = new ArrayList<String>();
                for(int i = 0; i < productID_Array.length; i++)
                {
                    productList.add(productID_Array[i]);    
                } 
                //记录初始化是否成功的状态
                googleplay_iap = true;
                //检索产品详细信息
                mHelper.queryInventoryAsync(true, productList, mGotInventoryListener);                
            }
        });

base64EncodedPublicKey是googleplay后台的发布产品的时候生成提供的,最好放在服务端

//检索购买信息回调
    static IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {            
            if (result.isFailure()) {
                Log.i("购买信息回调:","失败");
                return;
            }
            Log.i("", "检索购买信息且回调成功.");
            for(int i = 0; i < productID_Array.length; i++)
            {
                //返回应用内商品的商品详情
                SkuDetails data=inventory.getSkuDetails(productID_Array[i]);
                if(data != null)
                {
                    //将放到列表中 1(未用到price,好像没用)
                    detail_list.addElement(data);
                }
                
                //返回给定产品的购买信息,如果没有购买,则返回null。
                final Purchase premiumPurchase = inventory.getPurchase(productID_Array[i]);
                if(premiumPurchase != null)
                {
                    //将产品购买信息放到列表中 1
                    Log.i("说明还有未消耗的产品","++++");
                    inventory_list.addElement(premiumPurchase);
                }     
            }

            if(inventory_list.size() > 0)
            {
                Log.i("进入处理消耗","++++");
                //继续请求消耗
                ConsumeOrder(inventory_list.elementAt(0).getSku());
            }
        }
    };    
    
    
    // 充值请求
    public static void doCharge(final String data)
    {
        JSONObject joRes;
        try {
            joRes = new JSONObject(data);
            extraData = joRes.optString("orderId");
            payIndex = Integer.parseInt(joRes.optString("index"));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (!googleplay_iap) {
            return;
        }

        payProductID = productID_Array[payIndex-1]; // 当前索引对应的订单id
        for (int i = 0; i < inventory_list.size(); i++) {
            if (payProductID.equals(inventory_list.elementAt(i).getSku())) {
                Log.i(TAG, "订单已拥有++");
                return;
            }
        }
        
        if (mHelper.mAsyncInProgress) {
            mHelper.flagEndAsync();
        }
        mainThreadHandler.post(new Runnable() {
            public void run() {
                mHelper.launchPurchaseFlow(instance, payProductID, RC_REQUEST,
                        mPurchaseFinishedListener, extraData);
            }
        });

    }
            
    // google 购买回调
    static IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if (result.isFailure()) {
                Log.i("", "支付回调失败++");
                return;
            }
            Log.i("", "支付回调成功++");
            final String curPid = purchase.getSku();
            try {
                JSONObject json = new JSONObject();
                json.put("ggOrderId", purchase.getOrderId());
                json.put("myOrderId", purchase.getDeveloperPayload());
                json.put("signture", purchase.getSignature());
                json.put("signtureJson", purchase.getOriginalJson());
                
                Cocos2dxLuaJavaBridge.callLuaGlobalFunctionWithString(
                        "platformGoogleRequestItem", json.toString());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            
            inventory_list.addElement(purchase); // 4
            instance.runOnGLThread(new Runnable() {
                public void run() {                    
                    ConsumeOrder(curPid);
                }
            });
        }
    };
                


    // 消耗
    public static void ConsumeOrder(String consumeProductID) {
        if (!googleplay_iap) {
            return;
        }
        if (consumeProductID != "") {
            for (purchaseIndex = 0; purchaseIndex < inventory_list.size(); purchaseIndex++) // 5
            {
                if (consumeProductID.equals(inventory_list.elementAt(purchaseIndex).getSku())) {
                    mainThreadHandler.post(new Runnable() {
                        public void run() {
                            // 向google发送消费请求
                            Log.i("", "向google发送消费请求。。");
                            mHelper.consumeAsync(inventory_list.elementAt(purchaseIndex),mConsumeFinishedListener);
                        }
                    });
                    break;
                }
            }
        }
    }
     
    // 消耗回调
    static IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {
            
            //从订单列表中取消消耗过的订单
            for (int i = 0; i < inventory_list.size(); i++) {
                if (purchase.getSku().equals(inventory_list.elementAt(i).getSku())) {
                    inventory_list.remove(i);
                    Log.i("", "从订单列表中移除消耗过的订单。。");
                    break;
                }
            }

            instance.runOnGLThread(new Runnable() {
                public void run() {
                    // 7如果有未消耗订单继续请求消耗
                    if (inventory_list.size() > 0) {
                        // 继续请求消耗
                        ConsumeOrder(inventory_list.elementAt(0).getSku());    
                    }
                }
            });
        }
    };

 

platformGoogleRequestItem是客户端函数,需要请求本地服务器发送游戏货币

再说测试,测试可以出个测试包上传到googleplay后台的alpha版本或者beta版本,要求签名和包名要与正式包的一样

然后添加测试账号,测试账号要绑定信用卡,开发者账户>账户明细>许可测试,这个地方也要添加测试账号,这样就不会扣钱啦。。。

 

posted @ 2017-05-08 19:38  Mr_Hippo  阅读(2868)  评论(0编辑  收藏  举报