其他分享
首页 > 其他分享> > 【GooglePay支付】GooglePay支付通道对接(一)

【GooglePay支付】GooglePay支付通道对接(一)

作者:互联网

需求背景

  海外APP,安卓APP 需要商家Google商店,为保证一期顺利上架Google商店,支付系统对接原生的GooglePay

项目架构

  后端:SpringCloud + SpringBoot(版本号:2.1.x)+MySQL +Redis+Maven
  前端:安卓 APP
  Maven相关依赖

        <!--谷歌支付sdk-->
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-androidpublisher</artifactId>
            <version>v3-rev24-1.24.1</version>
        </dependency>

集成方式

  GooglePay的在In-APP场景下支付时的集成方式,类似于ApplePay ,整个支付流程可以在客户端闭环,
  不同点是在于校验客户端订单状态时,Apple提供了校验支付凭证的接口,但是GooglePay没有提供校验凭证的接口,GooglePay提供了一个校验订单状态的接口,我们可以利用这个接口校验客户端传过来的订单号
  查询订单状态接口文档地址: 
    https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.products/get
  注意到
    这个接口需要提供三个参数:
    1.APP包名,2.产品ID,3.购买产品的token(客户端购买后生成)
  认证方式
    使用P12证书进行身份认证

支付时序图

  

 

 

 实例DEMO

@SneakyThrows
    @Override
    public ChannelPayResponse execute(ChannelPayRequest payRequest) {
        log.info("==========>开始进入Google支付,参数:{}", JSON.toJSONString(payRequest));
        log.info("==========>订单号:【{}】", payRequest.getOrderNo());
        GooglePayParam gParam = GooglePayFactory.getGoogleParam(payRequest);
        AndroidPublisher publisher = this.buildGooglePayRequest(gParam);
        AndroidPublisher.Purchases.Products products = publisher.purchases().products();
        log.info("==========>支付请求【开始】-查询订单状态请求:{}", JSON.toJSONString(payRequest));
        AndroidPublisher.Purchases.Products.Get product = products.get(gParam.getPackageName(), payRequest.getProductId(), payRequest.getPurchaseToken());
        ProductPurchase purchase = product.execute();
        log.info("==========>支付请求【结束】-查询订单状态响应:{}", JSON.toJSONString(purchase));
        String appOrderId = purchase.getDeveloperPayload();
        Integer purchaseState = purchase.getPurchaseState();
        ChannelPayResponse channelPayResp;

        if (!GooglePayStatus.PURCHASED.getValue().equals(purchaseState) &&
                !GoogleConsumptionStatus.YET_TO_BE_CONSUMED.getValue().equals(purchase.getConsumptionState())) {
            log.info("==========>订单校验【成功】 订单号:【{}】", appOrderId);
            channelPayResp = ChannelPayResponse.prePaySuccess();
        } else {
            log.info("==========>订单校验【失败】 订单号:【{}】 订单状态 【{}】", appOrderId, purchaseState);
            channelPayResp = ChannelPayResponse.fail();
        }
        channelPayResp.setAppOrderId(appOrderId);
        channelPayResp.setChannelPayNo(purchase.getOrderId());//谷歌支付第三方单号,当处于沙箱环境的时候,没有这个字段
        return channelPayResp;
    }

    /**
     * 构建Google支付请求
     *
     * @param gParam
     * @return
     * @throws GeneralSecurityException
     * @throws IOException
     */
    private AndroidPublisher buildGooglePayRequest(GooglePayParam gParam) throws Exception {
        HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
        PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
                new FileInputStream(new File(gParam.getP12KeyFilePath())),
                "notasecret", "privatekey", "notasecret");
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(transport).setJsonFactory(JacksonFactory.getDefaultInstance())
                .setServiceAccountId(gParam.getAccountId()) 
                .setServiceAccountScopes(AndroidPublisherScopes.all())
                .setServiceAccountPrivateKey(privateKey).build();
        return new AndroidPublisher.Builder(transport, JacksonFactory.getDefaultInstance(), credential).build();
    }

参考链接

  https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.products/get
  https://juejin.cn/post/6981730770180112397
  https://www.cnblogs.com/kevin-zou/p/4533091.html
  https://www.timiguo.com/archives/92/
  https://www.wuguozhang.com/archives/54/
  https://blog.csdn.net/ITzaibadong/article/details/83056731
  https://blog.csdn.net/liutong123987/article/details/79415897?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.pc_relevant_paycolumn_v3&spm=1001.2101.3001.4242.1&utm_relevant_index=3
  https://blog.csdn.net/weixin_43878332/article/details/118524224?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14.pc_relevant_paycolumn_v3&utm_relevant_index=17
  https://blog.csdn.net/jack2350536098/article/details/106474704?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=1


  

 



  

标签:gParam,relevant,GooglePay,对接,utm,blog,pc,https,支付
来源: https://www.cnblogs.com/july-sunny/p/16221169.html