1. 设备中安装Android2.2以上版本的谷歌商店,并且谷歌账户已开通GoogleWallet服务
2. 将关于谷歌支付的相关内容添加到你的项目当中,IInAppBillingService.aidl文件和com.example.android.trivialdrivesample.util包添加到你的项目中,这里面包含了谷歌支付的验证流程,和安全机制;IInAppBillingService.aidi是一个安卓的接口定义语言,通过此文件对谷歌商店的支付服务进行调用(如下图)
2.1:SkuDetails类,该类代表了谷歌商店中一个商品的具体详情信息,包括了产品的id、类型、价格、名称以及对产品的描述
2.2:Security类,验证64位的RSA和是否和包名一致
2.3:IabHelper类,该类为谷歌支付的核心类,该类为支付控制类,它简化了谷歌支付的连接,以及对支付结果的处理,实现了异步的支付流,查询流,消费流等支付相关的不同方面的支付流程,方便了用户对于谷歌支付的操作
2.4:Base64解码64位的RSA,并返回解码的字节数组
3. 在AndroidMani中添加谷歌支付所需的权限用于谷歌的支付.
4.在游戏启动的时候对谷歌支付进行初始化,并传入你的Activity对象和一个64为的RSA(授權金鑰)
下面是代码
IabHelper mIabHelper = newIabHelper(Activity对象,”your_publish_key”)(也就是你64位的RSA)
// enable debug logging (for a production application, you should set
// this to false).
mIapHelper.enableDebugLogging(true);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
AndLog.d(TAG, "Starting setup.");
// 初始化,未联网状态也可以初始化成功
try {
mIapHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
AndLog.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// there was a problem.
AndLog.e(TAG, "init google play pay service failed!result is " + result);
BubbleApplication.getInstance().toast("GoogleWallet init failed, please update your GooglePlay.");
BubbleApplication
.getInstance()
.getAnalysisTool()
.onEvent(mActivity, BubbleAnalysisConstants.EVENT_BILLING_INIT_FAILED,
"init google play pay service failed!result is " + result);
// complain("Problem setting up in-app billing: " + result);
return;
}
// 检查用户已经购买到的虚拟物品
queryInventory(false);
if (mBuyMoneyTypes == null) {
// 从google play上面更新道具价格
updateItemPrice(null);
}
}
});
} catch (Exception e) {
BubbleApplication.getInstance().toast("Please install the Google store");
}
7.在谷歌支付初始化成功后,查询购买清单,如果有玩家已经购买的计费点没有被消耗,将其消耗,这样谷歌才允许用户再次购买同一个计费点。
/**
* 查询购买清单,如果有玩家已经购买的美钞未被消耗,则将其消耗(这样google play才允许用户继续购买该商品),并给用户增加货币
*
* @param pShowDialog
* 查询的过程中是否显示等待框
*/
private void queryInventory(final boolean pShowDialog) {
AndLog.d(TAG, "GooglePlay计费接口初始化完成,查询用户已经购买到的物品");
if (pShowDialog) {
BubbleApplication.getInstance().showProgressDialog("Query", "querying virtual goods...");
}
mIapHelper.queryInventoryAsync(new QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
BubbleApplication.getInstance().hideProgressDialog();
if (result.isFailure()) {
AndLog.w(TAG, "查询用户已经购买到的物品失败!result=" + result);
return;
}
AndLog.d(TAG, "查询用户已经购买到的物品成功!");
for (String sku : GooglePlayItemConstants.getALLSKUs()) {
if (inventory.hasPurchase(sku)) {
int cash2Add = GooglePlayItemConstants.getCash2Add(sku);
AndLog.d(TAG, "用户已经购买了" + sku + ",申请为用户增加" + cash2Add + "美钞");
consumeAndAddCach(inventory.getPurchase(sku), cash2Add, pShowDialog);
}
}
AndLog.d(TAG, "Initial inventory query finished; enabling main UI.");
}
});
}
8.消耗掉指定的计费点,并给用户添加一定数量金额的虚拟币(每次购买成功后都要将已经购买的物品消耗掉,因为谷歌不容许对已经拥有的商品再此购买,在购买之前一定要调用consumeAsync()将其消耗掉,以便进行下一次购买),在consumeAsync()成功之后,才给用户添加相应数量的虚拟币,如果中途由于网络等原因,导致付费成功且没有成功添加虚拟币,便会在上述7中,查询清单,并添加相应数量的虚拟币
/**
* 消耗掉指定的虚拟物品,并给用户增加指定金额的金币
*
* @param pPurchase
*/
private void consumeAndAddCach(Purchase pPurchase, final int pCash2Add, final boolean pShowProgressDialog) {
if (pShowProgressDialog) {
BubbleApplication.getInstance().showProgressDialog("Add cash", "adding cash");
}
mIapHelper.consumeAsync(pPurchase, new OnConsumeFinishedListener() {
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {
BubbleApplication.getInstance().hideProgressDialog();
if (result.isSuccess()) {
AndLog.d(TAG, "消费成功。增加" + pCash2Add + "美钞");
BubbleApplication.getInstance().addMoney(pCash2Add);
BubbleApplication.getInstance().toast(R.string.buy_success);
// 发送事件
BubbleApplication
.getInstance()
.getAnalysisTool()
.onEvent(BubbleApplication.getInstance(), BubbleAnalysisConstants.EVENT_BUY_MONEY_BY_RMB,
"SKU:" + purchase.getSku());
// 发送充值成功事件
BubbleApplication.getInstance().getGameAnalysisTool().onChargeSuccess(purchase.getOrderId());
} else {
BubbleApplication.getInstance().toast("Buy cash failed, please check your internet or restart the game.");
}
}
});
};
9.代码混淆后在proguard-project.txt中添加如下代码:-keep classcom.android.vending.billing.**
10:申请计费代码时需注意的:
Allpurchases are “managed” (that is, Google Play keeps track of the user'sownership of in-app products). The user cannot own multiple copies of an in-appitem; only one copy can be owned at any point in time
Purchased items can be consumed. Whenconsumed, the item reverts to the "unowned" state and can bepurchased again from Google Play
谷歌里的计费点以后要统一设置为受管理的,收管理的计费点在购买后会在谷歌的服务器给予记录并管理,在购买成功后,手机遇到异常没有正常的发放游戏里的虚拟币,便可通过查询谷歌(上面第七步中的方法),给予重新发放虚拟币,在发放成功后要手动此商品consume掉,因为谷歌不运行用户拥有同一个商品的多个复制品,在consume掉后,便可重新购买.
在谷歌支付version2的版本当中,还将计费点区分为”受管理的”和”不受管理的”,不受管理的计费点,需要自行对商品的持有情况,剩余量给予保存,在支付成功后,如若遇到异常崩溃,则将无法再保证给用于正确的发送游戏中的商品,其实在谷歌支付的version 3当中,不受管理的商品也已经默认为受管理的商品了,受管理和不受管理其实已经基本等同.
11:.接下来将正式的计费点代码发布,然后将apk包上传到alpha通道,最后添加测试帐号,便可进行支付的测试了,测试的时候需要注意,发布商自己的帐号是不能够用作购买测试的
如果出现(此版本的应用未配置为通过Googleplay结算,有关详情,请访问帮助中心)可能是以下几种情况导致的
11.1.在AndroidManifest当中忘记了添加谷歌支付的权限
11.2也可能上传谷歌后台的apk包的签名和现如今的不一样
11.3如若在购买的时候显示链接超时,则最后是打开vpn连接.
11.4在更换谷歌帐号后可能会出现初始化失败的问题,这时需要登录一下谷歌商店,以便谷歌获取帐号的相关信息
11.5申请计费点发布后可能会有延迟,立马测试的话也会有上面的提示
11.6添加测试帐号的延迟可能会出现(此用户不符合购买此商品的条件)