前段时间有事请耽搁了,今天跟大家一起看下Google的in-app Billing V3支付。
注意:类型根据VPN而定,我用的是L2TP/IPSec PSK,选择此类型时,编辑只需填写名称,服务器地址和IPSec预共享密钥即可,然后连接的时候填写帐号和密码。当打开Google商店能看到付费软件表名VPN已成功连接,如果显示VPN已连接但还看不到付费软件时,进入应用程序管理器分别清除Google Play服务和Google Play商店数据之后再打开Google商店即可。
四:测试用的app一定要跟上传到Google的测试版的包名、版本code、name、签名一致,否则无法进行支付测试。
1.下载in-app-billing-v03,下载地址:http://pan.baidu.com/share/link?shareid=1387554851&uk=473193131将下载后的压缩包解压:
将src目录下两个包及包中的java文件引入工程,例如:
2.添加权限:<uses-permission android:name="com.android.vending.BILLING" />
String base64EncodedPublicKey = "";此处填写Google控制台添加新应用程序后的appid
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[1], RC_REQUEST, mPurchaseFinishedListene
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
mHelper.queryInventoryAsync(mGotInventoryListener);
billingservice = mHelper.getService();
Bundle querySkus = new Bundle();
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) {
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.OnIabPurchaseFinishedLis
}else if (purchase.getSku().equals("double_income")) {
//受管理的商品,开启双倍经验
showMessage("支付成功", "成功购买双倍经验");
}
// Called when consumption is complete
IabHelper.OnConsumeFinishedListene
// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedLi
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);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mHelper != null) mHelper.dispose();
}
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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String base64EncodedPublicKey = "";//此处填写自己的appid
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iapHandler.sendEmptyMessage(1);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iapHandler.sendEmptyMessage(2);
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (iap_is_ok) {
mHelper.queryInventoryAsync(mGotInventoryListener);
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
}
});
@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();
}
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iapHandler.sendEmptyMessage(3);
}
});
}
//获取价格
private void getPrice(){
ArrayList<String> skus = new ArrayList<String>();
skus.add("double_income");
skus.add("coins_100");
billingservice = mHelper.getService();
Bundle querySkus = new Bundle();
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) {
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,
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
break;
case 2:
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[2], RC_REQUEST, mPurchaseFinishedListene
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
break;
case 3:
if (iap_is_ok) {
mHelper.launchPurchaseFlow(MainActivity.this, skus[0], RC_REQUEST, mPurchaseFinishedListene
}else {
showMessage("提示", "Google Play初始化失败,当前无法进行支付,请确定您所在地区支持Google Play支付或重启游戏再试!");
}
break;
default:
break;
}
};
};
// Callback for when a purchase is finished
}else if (purchase.getSku().equals("double_income")) {
//受管理的商品,开启双倍经验
showMessage("支付成功", "成功购买双倍经验");
}
showMessage("成功Restore双倍金币", "查询到有双倍金币需要恢复");
}else if(inventory.hasPurchase("cions_100")){
//查询到不受管理的商品支付成功需要将道具消耗掉
showMessage("成功Restore100金币","查询到有100金币需要恢复" );
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mHelper != null) mHelper.dispose();
}
void complain(String message) {
void alert(String message) {
private void showMessage(String title,String message){
new AlertDialog.Builder(MainActivity.this).setTitle(title).setMessage(message).setPositiveButton("确定", null).show();
}
}
以上就是完整的Google in-app Billing接入过程,接下来会跟大家一起走一遍亚马逊支付,如有疑问请留言。