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.");
}
}
Google play billing(Google play 内支付)的更多相关文章
- Google play billing(Google play 内支付) 上篇
写在前面: 最近Google貌似又被全面封杀了,幸好在此之前,把Google play billing弄完了,现在写篇 博客来做下记录.这篇博客一是自己做个记录,二是帮助其他有需要的人.因为现在基本登 ...
- Google play billing(Google play 内支付) 下篇
开篇: 如billing开发文档所说,要在你的应用中实现In-app Billing只需要完成以下几步就可以了. 第一,把你上篇下载的AIDL文件添加到你的工程里,第二,把 <uses-perm ...
- SDK接入(2)之Android Google Play内支付(in-app Billing)接入
SDK接入(2)之Android Google Play内支付(in-app Billing)接入 继上篇SDK接入(1)之Android Facebook SDK接入整理完Facebook接入流程之 ...
- Google In-App Billing 实现(内含Unity 实现经验)
实现内购计费 傻逼目录 Adding the AIDL file Updating Your Manifest Creating a ServiceConnection Making In-app ...
- Google Adsense(Google网站联盟)广告申请指南
Google AdSense 是一种获取收入的快速简便的方法,适合于各种规模的网站发布商.它可以在网站的内容网页上展示相关性较高的 Google 广告,并且这些广告不会过分夸张醒目.由于所展示的广告同 ...
- 应用内支付(IAP)可加入三方支付
Windows Phone 放开政策 - 应用内支付(IAP)可加入三方支付 Windows Phone 应用商店在 今年(2013)11月04号 修改了商店政策 允许公司账户的应用使用三方支付S ...
- Google帝国研究——Google的产业构成
Google帝国研究--Goog ...
- Windows Phone 放开政策 - 应用内支付(IAP)可加入三方支付
Windows Phone 应用商店在 今年(2013)11月04号 修改了商店政策 允许公司账户的应用使用三方支付SDK. 通过 App certification requirements cha ...
- SDK接入(3)之iOS内支付(In-App Purchase)接入
SDK接入(3)之iOS内支付(In-App Purchase)接入 继整理了Android平台的SDK接入过程.再来分享下iOS平台的内支付(In-App Purchase)接入,作为笔者在游戏开发 ...
随机推荐
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q28-Q31)
Question28You have a Microsoft Office SharePoint Server 2007 site.You upgrade the site to SharePoint ...
- Android接口回调机制
开发中,接口回调是我们经常用到的. 接口回调的意思即,注册之后并不立马执行,而在某个时机触发执行. 举个例子: A有一个问题不会,他去问B,B暂时解决不出来,B说,等我(B)解决了再告诉你(A)此时A ...
- JAVA基础学习day27--反射机制
一.概述 1.1.概述 反射的概念: 在Java中的反射机制是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法; 这种动态获取信息以及动 ...
- WPF Caliburn.Micro ListView 批量删除,有其他方法的大家一起交流一下
做这种批量删除的时候我的想法是获取每行的ID,然后更具ID删除,看过一些博客,大部分都是直接写在.CS文件里面,将ListView和CheckBox关联起来,最后用checkbox ck=sender ...
- androidannotation study(1)---Activity, Fragment,Custom Class & Custom View
androidannotation 是github上的一个开源项目. 主要是注解机制,可以改善android写代码的效率. Activity 使用 1.@EActivity 注解 可想而知,servi ...
- struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明
一.目标 1.搭建传统的ssh开发环境,并成功运行(插入.查询) 2.了解c3p0连接池相关配置 3.了解验证hibernate的二级缓存,并验证 4.了解spring事物配置,并验证 5.了解spr ...
- Android Design Support Library——Floating Action Button
Floating Action Button是一种悬浮操作的圆形按钮,继承自ImageView,可以通过android:src或者ImageView的任意方法,来设置FloatingActionBut ...
- ipad横竖屏尺寸(转载)
iPad在横屏模式下,界面区域元素主要由下图所示构成: 横屏主要尺寸:宽度:1024px高度:768px状态栏(Status Bar)高度:20px导航条(Nav Bar)高度:44px主内容区域(M ...
- js中 字符串与Unicode 字符值序列的相互转换
一. 字符串转Unicode 字符值序列 var str = "abcdef"; var codeArr = []; for(var i=0;i<str.length;i++ ...
- MyCat 学习笔记 第十三篇.数据分片 之 通过HINT执行存储过程
1 环境说明 VM 模拟3台MYSQL 5.6 服务器 VM1 192.168.31.187:3307 VM2 192.168.31.212:3307 VM3 192.168.31.150: 330 ...