(未完成,可能存在误解的地方)
详细文档参考: https://developer.android.com/google/play/billing/billing_integrate.html#billing-add-aidl
如果在Google Play发布应用,不能使用第三方充值渠道,只能使用Google的In-app Billing, V3版本的接口是google发布的最新充值接口,支持以服务的形式异步的与Google Play进行交互,更加安全可靠,减少交易中的不确定因素造成的损失。 但是暂时只支持 In-app Products,不支持订阅。
基本步骤如下:
将 IInAppBillingService.aidl文件拷贝到src文件夹下,正常的话会在gen目录下生成对应的.java文件
在AndroidManifest.xml 文件中添加如下权限
<uses-permission android:name="com.android.vending.BILLING" />
创建一个 ServiceConnection 并将其与 IInAppBillingService绑定。这个是最核心的功能,用于与Google Play进行通信。详细代码参考IabHelper.java 核心代码如下:
IInAppBillingService mService;
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService(new
Intent("com.android.vending.billing.InAppBillingService.BIND"),
mServiceConn, Context.BIND_AUTO_CREATE);
@Override
public void onDestroy() {
super.onDestroy();
if (mServiceConn != null) {
unbindService(mServiceConn);
}
}
一旦成功与Google Play建立连接,就可以使用上面创建的service与其进行通信。比如获取商品信息,发布购买请求等等。获取商品信息的代码示例(注意,不要在主线程调用此代码,涉及网络请求可能会阻塞主线程,应该另开一个线程调用):
ArrayList skuList = new ArrayList();
skuList.add("premiumUpgrade");
skuList.add("gas");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);
Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList responseList
= skuDetails.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
if (sku.equals("premiumUpgrade")) mPremiumUpgradePrice = price;
else if (sku.equals("gas")) mGasPrice = price;
}
}
申请购买商品:
// 购买代码
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(),
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
// 结果返回
Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString("productId");
alert("You have bought the " + sku + ". Excellent choice,
adventurer!");
}
catch (JSONException e) {
alert("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
// 返回结果示例
'{
"orderId":"12999763169054705758.1371079406387615",
"packageName":"com.example.app",
"productId":"exampleSku",
"purchaseTime":1345678900000,
"purchaseState":0,
"developerPayload":"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ",
"purchaseToken":"rojeslcdyyiapnqcynkjyyjh"
}'
设置物品已消耗(默认情况下,物品一旦被购买就属于用户,不允许再次被购买,如果是消耗型商品,比如货币或者装备,就需要向Google Play发送信息通知该物品已被消耗。注意,这个步骤要在真正提供商品之前进行,之后正确的获取到返回值才提供商品给玩家。)
int response = mService.consumePurchase(3, getPackageName(), token);
Handle In-app Billing responses from Google Play.
分享到:
相关推荐
Android In-App Billing v3 Library This is a simple, straight-forward implementation of the Android v3 In-app billing API. It supports: In-App Product Purchases (both non-consumable and consumable) ...
自己转换的Android开发指南中的In-app Billing章节, 制作成了6寸的PDF,专门为Kindle3等6寸电纸书设计。
Google Play 应用内支付(In-app Billing V2 + V3)官方示例打包下载
Google Play 应用内购
NULL 博文链接:https://mickey0811.iteye.com/blog/1461014
安卓计费Android 测试应用程序专注于 Google Play In-app Billing。 第一个版本或多或少基于。
react-native-billing, 在Android上,将本地桥反应到InApp计费 用于 Android 的InApp计费 本机计费 为 InApp Billing提供一个易于使用的界面,它通过包装anjlab计费库的 InApp来完成。const InAppBilling = require...
Reactive Billing is a lightweight reactive wrapper around In App Billing API v3 for Android. Features Reactive: Exposes the In App Billing service methods as Observable, allowing to implement easy ...
Godot-Myket-In-App-Billing 适用于Godot Game Engine的软应用内结算插件
Android In-App Billing v3 Library:开发者通过Android v3 API轻松处理应用程序支付问题
一个基于Google的可示例代码来处理In-App-Billing V3( )的Android库。 该项目的目标是构建一个可靠且经过测试的库,可以轻松地将其作为包含在您的(基于Maven的)项目中。 Google的示例代码已经过重构并可以测试...
NULL 博文链接:https://jgtang82.iteye.com/blog/282364
Based on the initial work of Anjlab (https://github.com/anjlab/android-inapp-billing-v3). This library provides the existing functionality of Anjlab's initial work and as well a lot of helper methods ...
资源来自pypi官网。 资源全名:django-aws-billing-0.1.3.tar.gz
Checkout (Android In-App Billing Library) Description Checkout is an implementation of Android In-App Billing API (v3 ). Its main goal is to make integration of in-app products as simple and straight...
AndroidBillingLibrary 可以实现 In-app Billing 的所有规范,并提供更高级的类来进行使用。
在应用程序内购买 在应用内购买Android应用程序 在应用内购买类型: 当前订阅类型的演示 为Google Play设置IAP
googleplay支付demo in-app-billing-v02 in-app-billing-v03
android-play-billing-master.zip源码对你学习android会有帮助