前言
各位同学大家好 ,有段时间没有给各位更新文章了,具体多久我也不记得了, 最近接了google 的内购支付,遇到一些问题 。
准备工作
申请内购id 还有 firebase 的 google-services.json 文件
支付初始化
public void initGooglePay(final Context context) {
Log.e(TAG, "initGooglePay: "+"支付初始化");
billingClient = BillingClient.newBuilder(context)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases().build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
// Logic from ServiceConnection.onServiceConnected should be moved here.
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.e(TAG, "initGooglePay: "+"支付初始化--成功");
// The BillingClient is ready. You can query purchases here.
}
}
@Override
public void onBillingServiceDisconnected() {
// Logic from ServiceConnection.onServiceDisconnected should be moved here.
}
});
}
支付回调处理
PurchasesUpdatedListener purchasesUpdatedListener =new PurchasesUpdatedListener(){
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List purchases) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null) {
Log.e(TAG, "onPurchasesUpdated: "+"支付成功" );
Log.e(TAG, "onPurchasesUpdated: purchasesUpdatedListener -- > "+purchasesUpdatedListener );
for (Purchase purchase : purchases) {
handlePurchase(purchase);
Log.e(TAG, "onPurchasesUpdated: "+purchase.getPurchaseToken() );
Log.e(TAG, "onPurchasesUpdated: purchase "+purchase);
// // // handlePurchase(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
//Toast.makeText(payContext, R.string.purchase_cancel, Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(payContext, R.string.purchase_cancel, Toast.LENGTH_SHORT).show();
}
}
};
支付消耗逻辑
private void handlePurchase(final Purchase purchase) {
Log.e(TAG, "handlePurchase: "+"消耗-- 》");
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
ConsumeResponseListener listener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// Handle the success of the consume operation.
try {
JSONObject jsonObject=new JSONObject(purchase.getOriginalJson());
String transaction_id=jsonObject.optString("orderId");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
billingClient.consumeAsync(consumeParams, listener);
}
查询商品id 信息并且调起支付
/**
* @param context
* @param productId
* @param
*/
private void querySkuDetails(final Activity context, String productId) {
Log.e(TAG, "querySkuDetails:productId --- " +productId );
String skuToSell = productId;
List skuList = new ArrayList<>();
skuList.add(skuToSell);
Log.e(TAG, "querySkuDetails: skuList "+skuList.size() );
//skuList.addAll(cosumerData.getProductIdData());
final SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, final List skuDetailsList) {
if(skuDetailsList!=null&&skuDetailsList.size()>0){
Log.e(TAG, "run:skuDetailsList.size -- > "+skuDetailsList.size() );
skuDetails=skuDetailsList.get(0);
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
int responseCode = billingClient.launchBillingFlow((Activity) context, billingFlowParams).getResponseCode();
if(responseCode== BillingClient.BillingResponseCode.OK){
}
}else{
Log.e(TAG, "run: "+"商品ID 错误" );
}
}
});
}
查询消耗的
/**
* 查询已经购买过但是没有被消耗的商品,可能网络不稳定或者中断导致的未被消耗
* 如果购买成功没消耗,就去消耗,消耗完成视为完整的流程。
*/
public void queryPurchases() {
if (!billingClient.isReady()) {
Log.i(TAG, "queryPurchases: BillingClient is not ready");
}
Log.i(TAG, "queryPurchases: INAPP");
billingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP, new PurchasesResponseListener(){
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List purchasesList) {
if (purchasesList != null) {
Log.i(TAG, "processPurchases: " + purchasesList.size() + " purchase(s)");
for (int i = 0; i < purchasesList.size(); i++) {
Purchase purchase = purchasesList.get(i);
handlePurchase(purchase);
}
} else {
Log.i(TAG, "processPurchases: with no purchases");
}
}
});
}