伸手系列直接上代码
服务器端只处理回验以及 验证成功后的 交易记录以及道具添加逻辑
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条)