??拖了這么久,終于開始寫第三篇文章。廢話不多說,今天來講講怎么接入google pay。
??寫在前面:
??1.如果你的賬號是買的,首次登錄Google play Console后出現(xiàn)這個提示 “payment need verification” 一定要求對方更換賬號,不然后續(xù)賬號出現(xiàn)問題的幾率很大。
??2.谷歌賬號分地區(qū)。比如你買中國地區(qū)的谷歌賬號,那只能綁定中國地區(qū)的visa卡。
??3.想測試google pay必須在google play商店上架,且app內(nèi)不要暴露出其他支付方式。
??4.在Beta渠道或者內(nèi)部測試渠道大膽上傳并發(fā)布你的應(yīng)用,只有發(fā)布成功才能測試支付。
??一、Google play Console后臺配置

??2.完成“應(yīng)用內(nèi)商品”這一項的填寫。管理產(chǎn)品代表消耗類型產(chǎn)品(如:APP內(nèi)購買禮物所需的鉆石等貨幣);訂閱代表持續(xù)一段時間的服務(wù)類型產(chǎn)品(如:APP內(nèi)VIP權(quán)限等);獎勵產(chǎn)品常見于游戲APP內(nèi)(如:看一段視頻廣告,贈送一張道具)

??注意填寫產(chǎn)品時狀態(tài)(Status)這一項選活性(ACTIVE),這樣商品才可以在APP內(nèi)被購買。

添加完長這樣

??3.添加沙盒測試賬號(可以測試google pay):首頁-setting-Developer account-Account detail 找到License Testing 添加測試賬號進去(切記不要是此APP的開發(fā)者賬號),如圖:

??4.添加APP測試人員賬號,如圖:

??注意:首次提交的APP不會立即生成測試APP地址(就是圖中我打馬賽克的部分),google會審核你的應(yīng)用(官方說首次提交APP審核最長需要48小時,實測可能需要更長時間),等到上傳的APP處于發(fā)布狀態(tài),才會生成測試地址。如圖:
??這樣Google play Console后臺配置就完成了。

??二、代碼部分,這一部分官方文檔講解很清楚,依賴什么的就不說了,直接上支付頁面相關(guān)代碼。
??1.初始化BillingClient,官方文檔沒有說明,實際需要添加enablePendingPurchases()方法,重連方式參考官方例子。
官方文檔是這樣寫:
private BillingClient billingClient;
...
billingClient = BillingClient.newBuilder(activity).setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingResponse.OK) {
// The BillingClient is ready. You can query purchases here.
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
});
==============================我是分割線===========================
實際需要這樣寫:
billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// The billing client is ready. You can query purchases here.
isConnect = true;
Log.e("PayDiamondActivity", "谷歌支付鏈接成功")
} else {
Log.e("PayDiamondActivity", billingResult.getResponseCode() + "");
isConnect = false;
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
isConnect = false;
}
});
??2.根據(jù)谷歌后臺設(shè)置的商品id查詢商品詳情:
private void getSkuList(String goodsId) {
skuList.clear();
skuList.add(goodsId);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult,
List<SkuDetails> skuDetailsList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& skuDetailsList != null) {
if (skuDetailsList.size()>0){
skuDetail = skuDetailsList.get(0);
// ToastUtil.show(skuDetail.toString());
}
}
}
});
}
??3.調(diào)起支付界面。注意這里我打印了responseCode,0是調(diào)起成功,其余錯誤碼在BillingClient類里有介紹,這里不做詳述。
public void googlePay( String mOrderId) {
this.mOrderId = mOrderId;
if (isConnect) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetail)
.build();
int responseCode = billingClient.launchBillingFlow(PayDiamondActivity.this, flowParams).getResponseCode();
if (responseCode != 0) {
ToastUtil.show(responseCode + ":Current region does not support Google payments");
}
} else {
ToastUtil.show("Current region does not support Google payments");
}
}
??4.支付完成后回調(diào)。包括向google確認訂單,通知自己服務(wù)端下單了給我發(fā)貨。
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
ToastUtil.show("User cancel");
} else {
// Handle any other error codes.
}
}
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Grant entitlement to the user.
// Acknowledge the purchase if it hasn't already been acknowledged.
if (!purchase.isAcknowledged()) {
ConsumeParams acknowledgePurchaseParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
//注意這里通知方式分3種類型(消耗型、訂閱型、獎勵型),本文是消耗性產(chǎn)品的通知方式,其它方式請看官方文檔
billingClient.consumeAsync(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}
String DeveloperPayload = purchase.getDeveloperPayload();
String OrderId = purchase.getOrderId();
String OriginalJson = purchase.getOriginalJson();
String PackageName = purchase.getPackageName();
String PurchaseState = purchase.getPurchaseState() + "";
String PurchaseTime = purchase.getPurchaseTime() + "";
String PurchaseToken = purchase.getPurchaseToken();
String Signature = purchase.getSignature();
String Sku = purchase.getSku();
//通知服務(wù)端
postGooglePay(DeveloperPayload, OrderId, OriginalJson, PackageName,
PurchaseState, PurchaseTime, PurchaseToken, Signature, Sku);
}
}
private ConsumeResponseListener acknowledgePurchaseResponseListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
}
};
??至此代碼部分就完成了,升級一下版本號,重新打release包,上傳到之前上傳的渠道上。如果App處于待發(fā)布狀態(tài),就等待谷歌審核通過,如已處于發(fā)布狀態(tài),把生成的測試鏈接發(fā)給測試人員就可以了。
??測試人員通過手機谷歌瀏覽器打開鏈接,加入測試計劃,根據(jù)提示點擊會跳轉(zhuǎn)到谷歌商店下載App。
??相關(guān)鏈接
https://developer.android.com/google/play/billing/billing_library_overview#Enable
https://www.gameres.com/830049.html
https://pay.google.com/payments/u/0/home(當google賬號出現(xiàn)payment need verification時會用到,用來更換支付、收款時綁定的visa卡)