Google Play支付校验

关于Google Play支付校验我之前在网上也找过大量的相关资料,发现大多数都是采用publicKey的方式来校验订单,但是在Google Play提供的官方实例中publicKey其实在客户端也是存在的,所以这种校验想要伪造其实是非常容易的,Google并未像Apple那样提供一个接口来校验订单的信息,但是提供了一个获取订单状态的接口,我们可以通过这个接口在GooglePlay服务器获取某个订单,查看其状态是否合法达到校验目的。

接口地址:https://developers.google.com/android-publisher/api-ref/purchases/products要使用上面的接口获取订单首先是需要登录认证的,这相比其它的平台稍微复杂了一点点,但是Google也提供了完整的库,并不需要我们做过多的开发。

在开发之前我们需要设置一些基本参数(接口地址:https://play.google.com/apps/publish/?dev_acc=08522487669089675329#ApiAccessPlace

1. 首先要在Google Developers Console上创建一个项目(参考图下)

Google Play支付校验

2. 项目创建成功之后需要创建Service Account(参考图下)

Google Play支付校验

---------------------------------------------------------------------------------------------------------

Google Play支付校验

---------------------------------------------------------------------------------------------------------

Google Play支付校验

---------------------------------------------------------------------------------------------------------

Google Play支付校验

3. Service account 授权(参考图下)

Google Play支付校验

4. Service account创建成功之后生成P12 key文件(参考图下)

Google Play支付校验

+++++------------------------------------------------------------------------------------------+++++ 准备工作至此结束

1. 添加Maven项目依赖

<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-androidpublisher</artifactId>
<version>v2-rev19-1.20.0</version>
</dependency>

2. 获取订单状态信息完成校验

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.SecurityUtils;
import com.google.api.services.androidpublisher.AndroidPublisher;
import com.google.api.services.androidpublisher.AndroidPublisherScopes;
import com.google.api.services.androidpublisher.model.ProductPurchase;
import java.io.File;
import java.io.FileInputStream;
import java.security.PrivateKey; /**
* Google Play 支付校验示例.
*
* @author Kevin Zou <kevinz@skfiy.org>
*/
public class GooglePlaySample { public static void main(String[] args) throws Exception { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(
SecurityUtils.getPkcs12KeyStore(),
new FileInputStream(new File("{P12 key file}")), // 生成的P12文件
"notasecret", "privatekey", "notasecret"); GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(transport).setJsonFactory(JacksonFactory.getDefaultInstance())
.setServiceAccountId("{Email address}") // e.g.: 626891557797-frclnjv31rn4ss81ch746g9t6pd3mmej@developer.gserviceaccount.com
.setServiceAccountScopes(AndroidPublisherScopes.all())
.setServiceAccountPrivateKey(privateKey).build(); AndroidPublisher publisher = new AndroidPublisher.Builder(transport,
JacksonFactory.getDefaultInstance(), credential).build(); AndroidPublisher.Purchases.Products products = publisher.purchases().products(); // 参数详细说明: https://developers.google.com/android-publisher/api-ref/purchases/products/get
AndroidPublisher.Purchases.Products.Get product = products.get("{packageName}",
"{productId}", "{token}"); // 获取订单信息
// 返回信息说明: https://developers.google.com/android-publisher/api-ref/purchases/products
// 通过consumptionState, purchaseState可以判断订单的状态
ProductPurchase purchase = product.execute();
}
}
上一篇:Linux常用命令操作说明(链接)


下一篇:JavaScript 将多个引用(样式或者脚本)放入一个文件进行引用