拖了这么久,终于开始写第三篇文章。废话不多说,今天来讲讲怎么接入google pay。
写在前面:
1.如果你的账号是买的,首次登录Google play Console后出现这个提示 “payment need verification” 一定要求对方更换账号,不然后续账号出现问题的几率很大。
2.谷歌账号分地区。比如你买中国地区的谷歌账号,那只能绑定中国地区的visa卡。
3.想测试google pay必须在google play商店上架,且app内不要暴露出其他支付方式。
4.在Beta渠道或者内部测试渠道大胆上传并发布你的应用,只有发布成功才能测试支付。
一、Google play Console后台配置
2.完成“应用内商品”这一项的填写。管理产品代表消耗类型产品(如:APP内购买礼物所需的钻石等货币);订阅代表持续一段时间的服务类型产品(如:APP内VIP权限等);奖励产品常见于游戏APP内(如:看一段视频广告,赠送一张道具)
注意填写产品时状态(Status)这一项选活性(ACTIVE),这样商品才可以在APP内被购买。
添加完长这样
3.添加沙盒测试账号(可以测试google pay):首页-setting-Developer account-Account detail 找到License Testing 添加测试账号进去(切记不要是此APP的开发者账号),如图:
4.添加APP测试人员账号,如图:
注意:首次提交的APP不会立即生成测试APP地址(就是图中我打马赛克的部分),google会审核你的应用(官方说首次提交APP审核最长需要48小时,实测可能需要更长时间),等到上传的APP处于发布状态,才会生成测试地址。如图:
这样Google play Console后台配置就完成了。
二、代码部分,这一部分官方文档讲解很清楚,依赖什么的就不说了,直接上支付页面相关代码。
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.根据谷歌后台设置的商品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 skuDetailsList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& skuDetailsList != null) {
if (skuDetailsList.size()>0){
skuDetail = skuDetailsList.get(0);
// ToastUtil.show(skuDetail.toString());
}
}
}
});
}
3.调起支付界面。注意这里我打印了responseCode,0是调起成功,其余错误码在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.支付完成后回调。包括向google确认订单,通知自己服务端下单了给我发货。
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List 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种类型(消耗型、订阅型、奖励型),本文是消耗性产品的通知方式,其它方式请看官方文档
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();
//通知服务端
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处于待发布状态,就等待谷歌审核通过,如已处于发布状态,把生成的测试链接发给测试人员就可以了。
测试人员通过手机谷歌浏览器打开链接,加入测试计划,根据提示点击会跳转到谷歌商店下载App。
相关链接
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账号出现payment need verification时会用到,用来更换支付、收款时绑定的visa卡)