准备工作

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. [leetcode] Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  2. C编程常见问题总结

    本文是C编程中一些常见错误的总结,有些是显而易见的,有些则是不容易发现 本文地址:http://www.cnblogs.com/archimedes/p/get-screwed-c.html,转载请注 ...

  3. NavigationController的使用整理

    1.设置NavigationBar的背景色: self.navigationController.navigationBar.barTintColor = [UIColor redColor]; 2. ...

  4. iOS单元测试1

    iOS单元测试1 iOS单元测试分为两种类型的测试: 应用测试.应用程序测试可以检查app的代码组件,比如计算机的算术运算的例子.你可以利用应用程序测试来确保你的UI空间控件保持原有位置,并且你的控件 ...

  5. jQuery 插件开发文章收集

    A jQuery plugin boilerplate http://jonathannicol.com/blog/2012/05/06/a-jquery-plugin-boilerplate/ jQ ...

  6. 修复ORACLETNS-12545 因目标主机或对象不存在错误

    现象: ORACLE启动不了,输入cmd->lsnrctl后,出现如下错误, 经查资料,发现是主机名可能解析有问题,后来在D:\oracle\ora92\network\admin下打开list ...

  7. Effective Java 43 Return empty arrays or collections, not nulls

    Feature Return empty arrays or collection Return nulls Avoids the expense of allocating the array N ...

  8. 使用culr

    使用curl在采集有语言要求的网站时,首先需要发送带有语言设置的请求,再发送你要的请求如: 注:vget(); 这里没提供: $url='http://www.hotels.com/?locale=e ...

  9. Java中的 WeakReference 和 SoftReference

    我们知道Java语言中没有指针,取而代之的是引用reference.Java中的引用又可以分为四种:强引用,弱引用(WeakReference),软引用(SoftReference),虚引用(Phan ...

  10. 更改ubuntu下mysql的密码

    1.首先,进入环境中去,即 mysql  -u  root  -p   ,然后输入原始密码 2.此时会出现 mysql > 3.开始修改密码: mysql >   use  mysql ; ...