伸手系列-谷歌google支付-java

伸手系列直接上代码

服务器端只处理回验以及 验证成功后的 交易记录以及道具添加逻辑

1.Controller

 public BaseResponse androidPay(@RequestBody GooglePayDto googlePayDto)
    {
        String productId=googlePayDto.getProductId();
        String orderId=googlePayDto.getOrderId();
        String purchaseToken=googlePayDto.getPurchaseToken();
        String email="xxpay@pc-api-5677xxxxxxx-94.iam.gserviceaccount.com";
        try {

            //初始化身份googleConfig.getJsonpath()是证书地址
            GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(googleConfig.getJsonpath()))
                    .createScoped(AndroidPublisherScopes.all());//createScoped给令牌访问权限设置使用的权限范围
            //credential.refreshToken();//注意这里
            if(credential==null) {
                logger.error("Get GoogleCredential fails获取谷歌凭证失败!");
            }
            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
            AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, JSON_FACTORY, credential).build();

            AndroidPublisher.Purchases.Products products = publisher.purchases().products();
            AndroidPublisher.Purchases.Products.Get product = products.get("com.shierke.umeapp", productId,purchaseToken);
            ProductPurchase purchase= product.execute();
            if(purchase!=null) {
                if (purchase.getPurchaseState() == 0)//0. Purchased 1. Canceled 2. Pending
                {
                    if (purchase.getConsumptionState() == 1)//0. Yet to be consumed 1. Consumed
                    {
                        if(purchase.getPurchaseType().equals(0))
                        {

                            logger.info("沙盒环境谷歌返回JSON:"+purchase.toString());
                        }

                        if (!vipOrdrService.isExist("android", orderId)) {
                            处理业务逻辑

                                return ReturnResponseUtil.Success("验证成功!",null);
                            }
                            else
                                return ReturnResponseUtil.Error(ReturnResponseUtil.BUSSINESS_ERROR, "数据库保存订单失败");

                        }
                        else
                            return ReturnResponseUtil.Error(ReturnResponseUtil.BUSSINESS_ERROR, "android请勿重复消费");

                        //
                    }
                    else
                    {
                        return ReturnResponseUtil.Error(ReturnResponseUtil.BUSSINESS_ERROR,"订单未支付");
                    }
                }
                else
                {
                    return ReturnResponseUtil.Error(ReturnResponseUtil.BUSSINESS_ERROR,"订单未支付");
                }
            }
            else
                return ReturnResponseUtil.Error(ReturnResponseUtil.BUSSINESS_ERROR,"支付失败purchase为空");
            // 通过consumptionState, purchaseState可以判断订单的状态

        }
        catch (Exception ex){
            ex.printStackTrace();
            logger.info(ex.getMessage());
        }
        return null;
    }

附上https://github.com/TianM/ApplePayAndGooglePay

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值