(注:大家要先连接vpn,才可以进行支付操作。这里就不说了)
官方教程:
http://developer.android.com/google/play/billing/index.html
http://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample
base64EncodedPublicKey = "";填写上你应用程序的签名。PS:签名即Eclipse->Android
Tools->Export Signed Application Package...
2.添加新应用,此处有个“上传APK”,此处上传的apk上传到Bate版或者ALPHA版,但包名、版本code、版本name、签名需跟最终上线的产品保持一致。此处上传测试版本的目的是当你支付接入完毕后测试时用。
(2).Consuming In-app Products,消耗产品时的通信过程
fake order ID). In some cases, the JSON string is signed and the response includes the signature so you can test your signature verification implementation using these responses.
you cancel a user's order before it is charged.
you (the merchant). After you process a refund request through your Google Wallet merchant account, a refund message is sent to your application by Google Play. This occurs only when Google Play gets notification from Google Wallet that a refund has been made.
For more information about refunds, see Handling IN_APP_NOTIFY messages and In-app Billing Pricing.
时:
mHelper.launchPurchaseFlow(MainActivity.this,“android.test.purchased”, RC_REQUEST, mPurchaseFinishedListener);
一:受管理商品和不受管理商品
四:测试用的app一定要跟上传到Google的测试版的包名、版本code、name、签名一致,否则无法进行支付测试。
2.当包名不一致时错误界面如下:
接下来跟大家一起看一下代码具体实现:
1.下载in-app-billing-v03,下载地址:http://pan.baidu.com/share/link?shareid=1387554851&uk=473193131将下载后的压缩包解压:
将src目录下两个包及包中的java文件引入工程,例如:
String base64EncodedPublicKey = "";//此处填写Google控制台添加新应用程序后的appid
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(false);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
complain("Problem setting up in-app billing: " + result);
return;
}
iap_is_ok = true;
// Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
}
});
调用支付接口:
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[1], RC_REQUEST, mPurchaseFinishedListener);
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
调用查询接口:
mHelper.queryInventoryAsync(mGotInventoryListener);
调用获取道具价格接口:(因Google市场是根据不同国家显示不同货币价格,所以显示到游戏道具列表中的价格不是定值,而是动态获取的)
billingservice = mHelper.getService();
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skus);
try {
Bundle skuDetails = billingservice.getSkuDetails(3, MainActivity.this.getPackageName(),"inapp", querySkus);
ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
if (null!=responseList) {
for (String thisResponse : responseList) {
try {
SkuDetails d = new SkuDetails(thisResponse);
for (int i = 0; i < sku_list.size(); i++) {
if (sku_list.get(i).equals(d.getSku())) {
price_list.set(i, d.getPrice());
}
}
iapHandler.sendEmptyMessage(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
三个回调:
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
if (result.isFailure()) {
// Oh noes!
complain("Error purchasing: " + result);
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals("coins_100")||purchase.getSku().equals("android.test.purchased")) {
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}else if (purchase.getSku().equals("double_income")) {
//受管理的商品,开启双倍经验
showMessage("支付成功", "成功购买双倍经验");
}
}
};
// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);
// 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()) {
// successfully consumed, so we apply the effects of the item in our
// game world's logic, which in our case means filling the gas tank a bit
if (purchase.getSku().equals("coins_100")||purchase.getSku().equals("android.test.purchased")) {
showMessage("支付成功", "成功购买100猫币");
}
}
else {
complain("Error while consuming: " + result);
}
}
};
// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
complain("Failed to query inventory: " + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
if (inventory.hasPurchase("double_income")) {
//查询到有受管理的商品支付成功需要将道具给用户
showMessage("成功Restore双倍金币", "查询到有双倍金币需要恢复");
}else if(inventory.hasPurchase("cions_100")){
//查询到不受管理的商品支付成功需要将道具消耗掉
showMessage("成功Restore100金币","查询到有100金币需要恢复" );
}
}
};
处理返回Activity后的数据:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// 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.");
}
}
退出游戏后销毁IabHelper:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
最后附上MainActivity.java完整文件,源码下载地址:http://pan.baidu.com/share/link?shareid=1579953623&uk=473193131:
package cn.catcap.together;
import java.util.ArrayList;
import org.json.JSONException;
import com.android.vending.billing.IInAppBillingService;
import com.example.android.trivialdrivesample.util.IabHelper;
import com.example.android.trivialdrivesample.util.IabResult;
import com.example.android.trivialdrivesample.util.Inventory;
import com.example.android.trivialdrivesample.util.Purchase;
import com.example.android.trivialdrivesample.util.SkuDetails;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
// The helper object
IabHelper mHelper;
// Debug tag, for logging
static final String TAG = "TrivialDrive";
// Current amount of gas in tank, in units
int mTank;
// (arbitrary) request code for the purchase flow请求码
static final int RC_REQUEST = 10001;
private boolean iap_is_ok = false;
//double_income为受管理商品,coins_100为不受管理商品
private String[] skus = {"android.test.purchased","double_income","coins_100"};
private ArrayList<String> sku_list;
private ArrayList<String> price_list;
private IInAppBillingService billingservice;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String base64EncodedPublicKey = "";//此处填写自己的appid
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set this to false).
mHelper.enableDebugLogging(false);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
complain("Problem setting up in-app billing: " + result);
return;
}
iap_is_ok = true;
// Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
}
});
//购买双倍金币(受管理商品)
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iapHandler.sendEmptyMessage(1);
}
});
//购买100猫币(不受管理商品)
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iapHandler.sendEmptyMessage(2);
}
});
//Restore Order
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (iap_is_ok) {
mHelper.queryInventoryAsync(mGotInventoryListener);
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
}
});
//获取价格
findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sku_list = new ArrayList<String>();
price_list = new ArrayList<String>();
//添加默认值
sku_list.add("double_income");
price_list.add("HK$40");
sku_list.add("coins_100");
price_list.add("HK$8");
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getPrice();
}
}).start();
}
});
//测试订单
findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iapHandler.sendEmptyMessage(3);
}
});
//显示价格
tv = (TextView) findViewById(R.id.text);
}
//获取价格
private void getPrice(){
ArrayList<String> skus = new ArrayList<String>();
skus.add("double_income");
skus.add("coins_100");
billingservice = mHelper.getService();
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skus);
try {
Bundle skuDetails = billingservice.getSkuDetails(3, MainActivity.this.getPackageName(),"inapp", querySkus);
ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
if (null!=responseList) {
for (String thisResponse : responseList) {
try {
SkuDetails d = new SkuDetails(thisResponse);
for (int i = 0; i < sku_list.size(); i++) {
if (sku_list.get(i).equals(d.getSku())) {
price_list.set(i, d.getPrice());
}
}
iapHandler.sendEmptyMessage(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Handler iapHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 0:
tv.setText(price_list.get(0)+"\n"+price_list.get(1));
break;
case 1:
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[1], RC_REQUEST, mPurchaseFinishedListener);
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
break;
case 2:
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[2], RC_REQUEST, mPurchaseFinishedListener);
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
break;
case 3:
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[0], RC_REQUEST, mPurchaseFinishedListener);
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
break;
default:
break;
}
};
};
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
if (result.isFailure()) {
// Oh noes!
complain("Error purchasing: " + result);
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals("coins_100")||purchase.getSku().equals("android.test.purchased")) {
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}else if (purchase.getSku().equals("double_income")) {
//受管理的商品,开启双倍经验
showMessage("支付成功", "成功购买双倍经验");
}
}
};
// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);
// 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()) {
// successfully consumed, so we apply the effects of the item in our
// game world's logic, which in our case means filling the gas tank a bit
if (purchase.getSku().equals("coins_100")||purchase.getSku().equals("android.test.purchased")) {
showMessage("支付成功", "成功购买100猫币");
}
}
else {
complain("Error while consuming: " + result);
}
}
};
// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
complain("Failed to query inventory: " + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
if (inventory.hasPurchase("double_income")) {
//查询到有受管理的商品支付成功需要将道具给用户
showMessage("成功Restore双倍金币", "查询到有双倍金币需要恢复");
}else if(inventory.hasPurchase("cions_100")){
//查询到不受管理的商品支付成功需要将道具消耗掉
showMessage("成功Restore100金币","查询到有100金币需要恢复" );
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
// 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.");
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
void complain(String message) {
Log.e(TAG, "**** TrivialDrive Error: " + message);
alert("Error: " + message);
}
void alert(String message) {
AlertDialog.Builder bld = new AlertDialog.Builder(this);
bld.setMessage(message);
bld.setNeutralButton("OK", null);
Log.d(TAG, "Showing alert dialog: " + message);
bld.create().show();
}
private void showMessage(String title,String message){
new AlertDialog.Builder(MainActivity.this).setTitle(title).setMessage(message).setPositiveButton("确定", null).show();
}
}
以上就是完整的Google in-app Billing接入过程。