Google play billing(Google play 内支付)

准备工作

1. 通过Android SDK Manager下载extras中的Google Play services和Google Play Billing Library两个包。

 

 

 2. 把下载的.aidl文件加入到你的工程中:在你的工程里建一个如下的包名com.android.vending.billing,再把这个aidl文件拷贝到里面,最后刷新一下你的工程就可以了,如果工程没有生成相关代码,可以执行下android update命令,update下你的工程。

 

 

3. 在你工程的AndroidMainfest.xml里添加权限:

<uses-permission android:name="com.android.vending.BILLING" />

完成这些后,你的工程就有Google billing了,在你的程序打包签名后,Google Play后台也会认可你的程序了,能够允许你在后台添加内购商品了。在正式接入支付代码前,你可以先把这个apk上传到Google Play后台,这个APK就相当于你要发布的APK了,当然你不会真的发布它,这里要说的就是,这个上传的APK是需要签名的,而且包名以及签名要与你以后上传的正式APK保持一致。

 

4. beta版APK上传后你就可以设置应用内商品了。此外我们将得到一个PublicKey,用于支付验证。

 

5. 在google后台加入测试号,并登录选择使用网址确认。

 

 

 6. 准备一个VPN账号、Google商店账号、一台带有Google Play的手机(“我用的是天天模拟器”)

 

7. 在手机上连接VPN并登录google商店,然后把你的账号绑定信用卡。若在推荐应用中看到了付费项目,并能搜到自己的应用(只有测试号才能搜到),就可以开始后续工作了。要是不行,则需要清除下google商店的国内访问缓存,重启手机。用模拟器的可以清下dns缓存。

 

开发工作

1. 新建一个包,把之前下载的代码拷贝到工程中。

  

 

2. 初始化

labHelper这个是支付的关键代码,其中已经把设置billing,商品查询,商品购买,商品回调,商品验证以及回调方法都写好了。(“具体使用都在samples中”)

    private void initGooglePlay()

    {

         mHelper = new IabHelper(mActivity, mPublicKey);

         mHelper.enableDebugLogging(true);

         mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {

         public void onIabSetupFinished(IabResult result) {

             Log.d(TAG, "Setup finished.");

            

             if (!result.isSuccess()) {

              Log.d(TAG, "Setup failed."+ result.toString());

                  return;

                }

            

             if (mHelper == null) return;

             Log.d(TAG, "Setup successful. Querying inventory.");

            //初始化成功则检测目前拥有的商品,即已购买但未消耗的商品。google的商品如果设置的是可重复商品,当你在成功购买这个商品后是需要主动消耗的,只有消耗成功后才可以再次购买。

             mHelper.queryInventoryAsync(mGotInventoryListener);            }

        });

    }

 

 

3. 检测商品

IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {

public void onQueryInventoryFinished(IabResult result, Inventory inventory) {

            Log.d(TAG, "Query inventory finished.");

            if (mHelper == null) return;

            if (result.isFailure()) {

                //表示没有未消耗的商品

                return;

            }

            //到此表示有未消耗的商品,我们可遍历所有添加的商品,找到对应商品并消耗掉。

            //skuName为我们之前创建的商品的金Key

            Purchase gasPurchase = inventory.getPurchase(skuName);                if(gasPurchase != null){

                //如果逻辑是到账后消耗掉对应商品,则说明玩家对应商品没到账,应补发。

                //消耗掉对应商品。

                mHelper.consumeAsync(gasPurchase, mConsumeFinishedListener);

            }

          }

    };

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {

        public void onConsumeFinished(Purchase purchase, IabResult result) {

            Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);

            // if we were disposed of in the meantime, quit.

            if (mHelper == null) return;

            // We know this is the "gas" sku because it's the only one we consume,

            // so we don't check which sku was consumed. If you have more than one

            // sku, you probably should check...

            if (result.isSuccess()) {

                Log.d(TAG, "Consumption successful. Provisioning.");

            }

            else {

                //complain("Error while consuming: " + result);

            }

            Log.d(TAG, "End consumption flow.");

        }

    };

 

 

 

4.购买商品

    public void buy(String orderId, )

    {

        mActivity.runOnUiThread(new Runnable() {

            @Override

            public void run() {    

                //这里product就是我们要购买的对应商品的金Key

                mHelper.launchPurchaseFlow(mActivity, Product, RC_REQUEST,

                        mPurchaseFinishedListener, orderId);

            }

        });

    }

 

// Callback for when a purchase is finished

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {

        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {

            // if we were disposed of in the meantime, quit.

            if (mHelper == null) return;

            if (result.isFailure()) {

                return;

            }

             String signNature = purchase.getSignature();

            String purchaseInfo = purchase.getPurchaseInfo();

 

            //把signNature, purchaseInfo发到服务端做验证。getPurchaseInfo函数本没有,自己到Purchase.java中添加并返回mOriginalJson就好。

            SDKHelper.googlePlayCheckOrder(signNature, purchaseInfo);

 

            //确认到账后消耗掉对应商品,此处直接处理了。

            mHelper.consumeAsync(purchase, mConsumeFinishedListener);

        }

    };

 

 

想PurchseListener被调用到,需要重写onActivityResult方法,该方法会在支付结束,你的程序重新回到前台的时候调用。

这里调用了 IabHelper 里的 handleActivityResult 方法,然后此方法会调用 PurchseListener。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
  if (mHelper == null) return;

    // Pass on the activity result to the helper for handling
  if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
    // not handled, so handle it ourselves (here's where you'd
    // perform any handling of activity results not related to in-app
    // billing...
    super.onActivityResult(requestCode, resultCode, data);
  }
  else {
    Log.d(TAG, "onActivityResult handled by IABUtil.");
  }
}

posted @ 2016-07-07 18:15  吴筱军  阅读(3057)  评论(1编辑  收藏  举报