准备工作

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.");
  }
}

Google play billing(Google play 内支付)的更多相关文章

  1. Google play billing(Google play 内支付) 上篇

    写在前面: 最近Google貌似又被全面封杀了,幸好在此之前,把Google play billing弄完了,现在写篇 博客来做下记录.这篇博客一是自己做个记录,二是帮助其他有需要的人.因为现在基本登 ...

  2. Google play billing(Google play 内支付) 下篇

    开篇: 如billing开发文档所说,要在你的应用中实现In-app Billing只需要完成以下几步就可以了. 第一,把你上篇下载的AIDL文件添加到你的工程里,第二,把 <uses-perm ...

  3. SDK接入(2)之Android Google Play内支付(in-app Billing)接入

    SDK接入(2)之Android Google Play内支付(in-app Billing)接入 继上篇SDK接入(1)之Android Facebook SDK接入整理完Facebook接入流程之 ...

  4. Google In-App Billing 实现(内含Unity 实现经验)

    实现内购计费 傻逼目录   Adding the AIDL file Updating Your Manifest Creating a ServiceConnection Making In-app ...

  5. Google Adsense(Google网站联盟)广告申请指南

    Google AdSense 是一种获取收入的快速简便的方法,适合于各种规模的网站发布商.它可以在网站的内容网页上展示相关性较高的 Google 广告,并且这些广告不会过分夸张醒目.由于所展示的广告同 ...

  6. 应用内支付(IAP)可加入三方支付

    Windows Phone 放开政策 - 应用内支付(IAP)可加入三方支付   Windows Phone 应用商店在 今年(2013)11月04号 修改了商店政策 允许公司账户的应用使用三方支付S ...

  7. Google帝国研究——Google的产业构成

                                                                                        Google帝国研究--Goog ...

  8. Windows Phone 放开政策 - 应用内支付(IAP)可加入三方支付

    Windows Phone 应用商店在 今年(2013)11月04号 修改了商店政策 允许公司账户的应用使用三方支付SDK. 通过 App certification requirements cha ...

  9. SDK接入(3)之iOS内支付(In-App Purchase)接入

    SDK接入(3)之iOS内支付(In-App Purchase)接入 继整理了Android平台的SDK接入过程.再来分享下iOS平台的内支付(In-App Purchase)接入,作为笔者在游戏开发 ...

随机推荐

  1. view.performClick()触发点击事件

    1.主要作用 自动触发控件的点击事件 2.界面的布局文件  activity_main.xml <RelativeLayout xmlns:android="http://schema ...

  2. IOC基础

    Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  3. java 的方法注释写在哪里?

    如果有接口,写在接口方法上即可.鼠标滑过方法名时时会显示 如果没有接口,写在每个方法上方. eclipse 分三步 ① 找到方法,并将光标移动至方法名的上方 ②/** ③回车 那,效果是酱紫

  4. Monyer's Game 0~5关过关方法

    自从Monyer编写了这个通关小游戏,可谓是好事坏事参半吧! 好事是Monyer认识了许多电脑高手,包括netpatch.luoluo等,连LCX这种骨灰级选手也过来了,可谓是收获不小(所以既然我已经 ...

  5. 关于String StringBuffer StringBuilder

    0. String对象的创建       1.关于类对象的创建,很普通的一种方式就是利用构造器,String类也不例外:String s=new String("Hello world&qu ...

  6. Effective Java 26 Favor generic types

    Use generic types to replace the object declaration Add one or more type parameters to its declarati ...

  7. Effective Java 49 Prefer primitive types to boxed primitives

    No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...

  8. LightSpeed的批量更新和批量删除

    1.Update对于批量操作 无论是Update还是Remove  都是使用LightSpeed的Query对象来完成. 注:Student是要进行Update的表(实体),StuName是表Stud ...

  9. device eth0 does not seem to be present, delaying initialization

    在搭建LVS+Keepalived高可用负载均衡环境的过程中,使用VirtualBox复制了两个Centos的环境,并且选中了“重新初始化网卡”的选项,但是在启动这两个复制的Centos环境的时候,发 ...

  10. c# 反射简单使用

    类库dll,将生成ExampleLib.dll文件 namespace ExampleLib { public class Example { public static string FuncA() ...