前言
之前写过了Facebook登录Google登录,那么Google支付能错过吗?当然是不能的了。今天来说说Google支付,我们这里只说集成方式,配置可以看Google(应用内支付)官方文档。
准备工作
要集成Google支付的准备工作有哪些呢?
- 1、手机上安装Google服务(当然是针对国内的用户,国外的相信大部分都有Google服务);
- 2、一个绑定信用卡的Google账号;
- 3、Google play客户端;
- 4、Google play console(用于配置应用内商品信息);
- 5、科学上网的工具。
首先
上面的准备工作都做好了之后需要干什么呢?当然是在Google play console上配置相关应用和商品信息了,至于如何配置具体可以参考Google支付配置。
- 1、集成工作
首先在AndroidManifest文件中加入支付权限和版本
<uses-permission android:name="com.android.vending.BILLING"/>
//在application的节点下添加
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
- 2、复制Google支付的aidl文件和util包中的各种文件

其次
Google支付管理类的创建(支付封装成一个工具类)
public class GooglePlayManager {
private static final String TAG = GooglePlayManager.class.getSimpleName();
@SuppressLint("StaticFieldLeak")
private static IabHelper mHelper;
//是否初始化
private static boolean mSetupDone = false;
//公钥
private static String mPublicKey;
//订单号
private static String payload;
/**
* 初始化
*
* @param context 上下文
* @param publicKey 公钥
* @param productID 商品ID
* @param goodsList 商品集合
* @param mListener 接口
*/
public static void init(final Context context, String publicKey, final String productID,
final List<String> goodsList, final OnGoogleInitListener mListener) {
mPublicKey = publicKey;
//创建谷歌帮助类
mHelper = new IabHelper(context, publicKey);
mHelper.enableDebugLogging(true);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
mListener.onGoogleInitFailed(result.getMessage());
mSetupDone = false;
} else {
mSetupDone = true;
mListener.onGoogleInitSuccess("init Success");
//getLTOrderID(LTAppID, LTAppKey,gid, packageId, params);
try {
mHelper.queryInventoryAsync(true, goodsList, goodsList,
new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if (result != null) {
if (result.isSuccess() && inv.hasPurchase(productID)) {
//消费
consumeProduct(context, inv.getPurchase(productID),
false, "Consumption success",
"Consumption failed");
}
}
}
});
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
}
}
});
}
/**
* 消费掉商品
*/
private static void consumeProduct(final Context context, Purchase purchase, final boolean needNext,
final String tipmsg1, final String tipmsg2) {
try {
mHelper.consumeAsync(purchase, new IabHelper.OnConsumeFinishedListener() {
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {
if (mHelper == null) {
return;
}
if (result.isSuccess()) {
if (!needNext) {
//处理中断的情况, 仅仅只是消费掉上一次未正常完成的商品
//Toast.makeText(context, tipmsg1, Toast.LENGTH_SHORT).show();
}
} else {
// Toast.makeText(context, tipmsg2, Toast.LENGTH_SHORT).show();
}
}
});
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
}
/**
* 购买
*
* @param context 上下文
* @param requestCode 请求码
* @param goodsList 商品集合
* @param productID 商品ID
* @param mListener 接口
*/
public static void recharge(final Context context,final int requestCode,
List<String> goodsList, final String productID,
final OnGooglePlayResultListener mListener) {
if (mSetupDone) {
//创建订单获取订单号(在自己的服务器上获取订单号)
try {
if (mHelper == null) return;
List<String> subSku = new ArrayList<>();
mHelper.queryInventoryAsync(true, goodsList, subSku,
new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if (result != null) {
if (result.isSuccess() && inv.hasPurchase(productID)) {
//消费
consumeProduct(context, inv.getPurchase(productID),
false, "Consumption success",
"Consumption failed");
} else {
getProduct((Activity) context, requestCode, productID, mListener);
}
}
}
});
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
} else {
if (TextUtils.isEmpty(mPublicKey)) {
//创建谷歌帮助类
mHelper = new IabHelper(context, mPublicKey);
mHelper.enableDebugLogging(true);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (result.isFailure()) {
mSetupDone = false;
}
if (result.isSuccess()) {
mSetupDone = true;
}
}
});
}
}
}
/**
* 产品获取
*
* @param context 上下文
* @param REQUEST_CODE 请求码
* @param SKU 产品唯一id, 填写你自己添加的商品id
* @param mListener 回调监听
*/
private static void getProduct(final Activity context, int REQUEST_CODE,
final String SKU, final OnGooglePlayResultListener mListener) {
if (!TextUtils.isEmpty(payload)) {
try {
mHelper.launchPurchaseFlow(context, SKU, REQUEST_CODE, new IabHelper.OnIabPurchaseFinishedListener() {
@Override
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
mListener.onPlayError(result.getMessage());
return;
}
mListener.onPlaySuccess("Purchase successful");
if (purchase.getSku().equals(SKU)) {
//购买成功,调用消耗
consumeProduct(context, purchase, false, "Payment success",
"Payment Failed");
}
}
}, payload);
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
} else {
mListener.onPlayError("Order creation failed");
}
}
/**
* 回调
*
*
* @param requestCode 请求码
* @param resultCode 结果码
* @param data 数据
*/
public static void onActivityResult( int requestCode, int resultCode, Intent data, int selfRequestCode) {
//将回调交给帮助类来处理, 否则会出现支付正在进行的错误
if (mHelper == null) return;
mHelper.handleActivityResult(requestCode, resultCode, data);
if (requestCode == selfRequestCode) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
//订单信息
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");
if (!TextUtils.isEmpty(purchaseData)) {
GoogleModel googleModel = new Gson().fromJson(purchaseData, GoogleModel.class);
Log.e(TAG, googleModel.getPurchaseToken());
Map<String, Object> params = new WeakHashMap<>();
params.put(购买成功的token(具体以自己服务的规定为准), googleModel.getPurchaseToken());
params.put(订单号, payload);
//上传到服务器进行验证之后再次调用消费用于消耗掉购买的商品
}
}
}
/**
* 释放资源
*/
public static void release() {
if (mHelper != null) {
mHelper.disposeWhenFinished();
}
mHelper = null;
}
}
流程说明:首先初始化Google支付,初始化成功后查询是否有未消费的,如果有则消费,如果没有则继续调用购买功能。当然,购买之前需要创建订单,之后传入Google支付需要的参数支付。支付成功后在onActivityResult方法里获取到Google返回的token,然后到自己的服务器做验证。验证成功后再次调用Google的消费功能,用于消耗此次购买。这一步千万不能省,要不下次可能就不能进行购买了。
最后
好了,这就是今天的Google支付说明了,写的不好还请谅解。如果有不懂的地方可以加群493180098联系我。
网友评论