java集成Google Pay内购

挺简单的直接上代码:

api入参

@Data
@ApiModel("google支付表单信息")
public class GooglePayForm {
    /**
     * 包名
     */
    @ApiModelProperty("包名")
    String packageName;

    /**
     * 商品id
     */
    @ApiModelProperty("商品id")
    String productId;

    /**
     * 支付凭证
     */
    @ApiModelProperty("支付凭证")
    String purchaseToken;
    
    /**
     * 订单id
     */
    @ApiModelProperty("订单id")
    private String payOrderId;
}
GooglePayController
 private GooglePayServer googlePayServer;

    @RequiresAuthentication
    @ApiOperation("google内购校验")
    @PostMapping("/google_pay")
    public ResultVo checkOrder(@RequestBody GooglePayForm googlePayForm,
                               HttpServletRequest request) throws IOException, GeneralSecurityException {
        String uId =getTokenUid(request);
        logger.info("GooglePayController.checkOrder>>>>>>>>>>>{google_pay}google内购校验:"+ googlePayForm);
        return googlePayServer.checkOrder(googlePayForm,uId);
    }

GooglePayServer


    /**
     * google内购校验
     * @param googlePayForm
     * @return
     * @throws IOException
     * @throws GeneralSecurityException
     */
    ResultVo checkOrder(GooglePayForm googlePayForm,String uId)throws IOException, GeneralSecurityException;

GooglePayServerImpl

/**
     * google内购校验
     * @param googlePayForm
     * @return
     * @throws IOException
     * @throws GeneralSecurityException
     */
    @Override
    public ResultVo checkOrder(GooglePayForm googlePayForm,String uId) throws IOException, GeneralSecurityException {
        //使用服务帐户Json文件获取Google凭据
        List<String> scopes = new ArrayList<>();
        scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("/static/pc-api-8935772698575949796-951-69e718e16e40.json");
        GoogleCredential credential = GoogleCredential.fromStream(resource.getInputStream())
                .createScoped(scopes);
        //使用谷歌凭据和收据从谷歌获取购买信息
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
                .setApplicationName("孩子币").build();
        AndroidPublisher.Purchases purchases = publisher.purchases();
        final AndroidPublisher.Purchases.Products.Get request = purchases.products().get(googlePayForm.getPackageName(), googlePayForm.getProductId(),googlePayForm.getPurchaseToken());
        System.out.println("==============="+request+"================");
        final ProductPurchase purchase = request.execute();
        logger.info("google验证返回josn:"+purchase);
        // TODO 处理业务
        if (purchase.getConsumptionState() > 0 && purchase.getPurchaseState() == 0){
            boolean result = payOrderService.upPayOrder(uId,purchase.getOrderId(),googlePayForm.getProductId(),googlePayForm.getPayOrderId());
            if (result){
                logger.info("交易成功,新增并处理订单:{}transactionId{}: " + purchase.getOrderId());
                return ResultVoUtil.success("SUCCESS");
            }
            return ResultVoUtil.error(1,"FAIL");
        }
        return ResultVoUtil.error(2,"PAYMENT ERROR");
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值