验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
语言: Java
分类: Android 组件
最后更新于 2018-12-09 10:07
谷歌内购Demo示例
原始数据 复制代码
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.trivialdrive.util.IabHelper;
import com.google.android.trivialdrive.util.IabResult;
import com.google.android.trivialdrive.util.Inventory;
import com.google.android.trivialdrive.util.Purchase;
/**
* Created by WQ on 2018/2/6.
* @Describe 支付方式
*/
public class DemoPayMethodActivity extends Activity {
private static final int REQUEST_CODE = 0x090;
public final String TAG = getClass().getSimpleName();
IabHelper mHelper;
/**
* 谷歌内购- 加密串, 填写你自己谷歌上的RSA加密许可
*/
private String base64EncodedPublicKey = "";
/**
* 内购产品唯一id, 填写你自己添加的内购商品id
*/
private String SKU = "";//
/**
* 支付附加项- 这里放的是订单号
*/
private String mDeveloperPayload = "201803131027009010";
/**
* 谷歌服务是否已正常安装
*/
private boolean isGooglePlaySetup = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建谷歌支付帮助类
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.enableDebugLogging(true);
/**
* 初始化和连接谷歌服务
*/
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Log.e("PayMethodActivity", "Problem setting up In-app Billing: " + result);
return;
}
/**
* 初始化成功,记录下
*/
isGooglePlaySetup = true;
if (mHelper == null) {
return;
}
}
});
}
/**
* 支付
*
* @param view
*/
public void onClick(View view) {
/**
* 谷歌服务未初始化成功,或者未安装,进行提示
*/
if (!isGooglePlaySetup) {
Toast.makeText(this, "谷歌服务连接失败,或未安装谷歌服务,请稍后重试", Toast.LENGTH_SHORT).show();
return;
}
checkUnconsume();
}
/**
* 查询是否有未消费的
*/
public void checkUnconsume() {
try {
mHelper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
if (result.isSuccess() && inv.hasPurchase(SKU)) {
//消费, 并下一步, 这里Demo里面我没做提示,将购买了,但是没消费掉的商品直接消费掉, 正常应该
//给用户一个提示,存在未完成的支付订单,是否完成支付
consumeProduct(inv.getPurchase(SKU), false, "消费成功", "消费失败");
} else {
buyProduct();
}
}
});
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
}
/**
* 产品购买
*/
private void buyProduct() {
try {
mHelper.launchPurchaseFlow(this, SKU, REQUEST_CODE, new IabHelper.OnIabPurchaseFinishedListener() {
@Override
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
Log.e("PayMethodActivity", "Error purchasing: " + result);
Toast.makeText(DemoPayMethodActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
return;
}
Log.d(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU)) {
// bought 1/4 tank of gas. So consume it.
Log.d(TAG, "Purchase is gas. Starting gas consumption.");
//購買成功,調用消耗產品
consumeProduct(purchase, false, "支付成功", "支付失败");
}
}
}, mDeveloperPayload);
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
Toast.makeText(DemoPayMethodActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
}
}
/**
* 消费掉已购买商品
*
* @param purchase
* @param needNext
* @param tipmsg1
* @param tipmsg2
*/
private void consumeProduct(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()) {
Log.e("PayMethodActivity", "Problem setting up In-app Billing: " + result);
if (!needNext) {
//处理内购中断的情况, 仅仅只是消费掉上一次未正常完成的商品
Toast.makeText(DemoPayMethodActivity.this, tipmsg1, Toast.LENGTH_SHORT).show();
return;
}
try {
//向服务器提交内购验证
//UIUtils.showLoadDialog(that, "验证支付结果");
// BaseQuestStart.IosneigouIndexGooglePayUrl_(that, purchase.getOriginalJson(), purchase.getSignature());
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
Toast.makeText(DemoPayMethodActivity.this, tipmsg2, Toast.LENGTH_SHORT).show();
}
}
});
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
Toast.makeText(DemoPayMethodActivity.this, tipmsg2, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mHelper == null) return;
/**
* 将回调交给帮助类来处理, 否则会出现支付正在进行的错误
*/
mHelper.handleActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
/**
* 释放掉资源
*/
if (mHelper != null) {
try {
mHelper.dispose();
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
}
mHelper = null;
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助