挺简单的直接上代码:
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");
}