官方文档地址:https://developers.google.cn/android-publisher/api-ref/rest/v3/purchases.products?hl=zh-cn

java依赖

        <!-- google play -->
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-androidpublisher</artifactId>
            <version>v3-rev20241003-2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-jackson2</artifactId>
            <version>1.45.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>1.28.0</version>
        </dependency>
        <!-- google play -->

java代码,

credential json文件再谷歌后台配置后获取,支付凭证让前端APP拉取支付后获取后给到服务端。

参考这个文档在Google后台配置服务端验证Google Pay订单的两种方式_谷歌支付验证-CSDN博客

@Test
    void test2() throws Exception {
        // GoogleCredentials credential = GoogleCredentials
        //         .fromStream(FileUtil.getInputStream("/opt/api-project-20840613-43d15092b1b0.json"))
        //         .createScoped(Collections.singletonList(AndroidPublisherScopes.ANDROIDPUBLISHER));
        String token = "json字符串base64编码";
        GoogleCredentials credential = GoogleCredentials
                .fromStream(new ByteArrayInputStream(Base64.decode(token)))
                .createScoped(Collections.singletonList(AndroidPublisherScopes.ANDROIDPUBLISHER));
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential);
        AndroidPublisher publisher = new AndroidPublisher.Builder(new NetHttpTransport(), new GsonFactory(), requestInitializer).build();
        AndroidPublisher.Purchases purchases = publisher.purchases();
        //获取凭证对应google订单信息
        final AndroidPublisher.Purchases.Products.Get request = purchases.products()
                .get("com.tangwan.xxx", "test01", "支付凭证token");
        final ProductPurchase purchase = request.execute();
        //凭证核销
        purchases.products()
                .consume("com.tangwan.cn", "test01", "支付凭证token")
                .execute();
        System.out.println(JSONUtil.toJsonStr(purchase));
    }

更多推荐