c# 使用Google.Apis.AndroidPublisher.v3进行谷歌支付服务端验证

c# 使用Google.Apis.AndroidPublisher.v3进行谷歌支付服务端验证

1 需要引入Google.Apis.AndroidPublisher.v3类库

准备参数:
ServiceAccountEmail----- 账号
PrivateKey ----- 加密私钥

  /// <summary>
  /// 谷歌支付验证
  /// </summary>
  /// <param name="payRequest"></param>
  public async Task<GoogleVerifyPayrResult?> GooglePayRequest(GooglePayRequest payRequest)
  {
      try
      {
          ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(ServiceAccountEmail)
          {
              Scopes = new[] { AndroidPublisherService.Scope.Androidpublisher }

          }.FromPrivateKey(PrivateKey));

          var service = new AndroidPublisherService(new Google.Apis.Services.BaseClientService.Initializer
          {
              HttpClientInitializer = credential,
              ApplicationName = "fdgame"
          });

          //验证订单
          var response = await service.Purchases.Products.Get(payRequest.PackageName, payRequest.ProductId, payRequest.PurchaseToken).ExecuteAsync();

          if (response.IsNull())
          {
              return null;
          }
          //检查订单是否有效
          GoogleVerifyPayrResult objGoogleVerifyPayrResult = new GoogleVerifyPayrResult(response);
          return objGoogleVerifyPayrResult;
      }
      catch (Exception ex)
      {
          return null;
      }
     
  }
public class GooglePayRequest
{
    public string ProductId { get; set; }

    public string PurchaseToken { get; set; }

    public string PackageName { get; set; }
}
  public class GoogleVerifyPayrResult
  {

      public GoogleVerifyPayrResult() { }

      public GoogleVerifyPayrResult(Google.Apis.AndroidPublisher.v3.Data.ProductPurchase purchase)
      {
          this.AcknowledgementState= purchase.AcknowledgementState;
          this.ConsumptionState= purchase.ConsumptionState;
          this.DeveloperPayload= purchase.DeveloperPayload;
          this.Kind=purchase.Kind;
          this.ObfuscatedExternalProfileId= purchase.ObfuscatedExternalProfileId;
          this.ObfuscatedExternalAccountId= purchase.ObfuscatedExternalAccountId;
          this.OrderId=purchase.OrderId;
          this.ProductId=purchase.ProductId;
          this.PurchaseTimeMillis=purchase.PurchaseTimeMillis;
          this.PurchaseToken=purchase.PurchaseToken;
          this.PurchaseState= purchase.PurchaseState;
          this.PurchaseType=purchase.PurchaseType;
          this.Quantity=purchase.Quantity;
          this.RefundableQuantity=purchase.RefundableQuantity;
          this.ETag=purchase.ETag;

      }
      /// <summary>
      /// 内购产品的确认状态,(1-被确认)
      /// </summary>
      public int? AcknowledgementState { get; set; }

      /// <summary>
      /// 产品的消费状态(0-未消费,1-已消费)
      /// </summary>
      public  int? ConsumptionState { get; set; }


      /// <summary>
      /// 开发人员指定的字符串
      /// </summary>
      public  string DeveloperPayload { get; set; }

      /// <summary>
      /// 这种表示androidpublisher服务中的inappPurchase对象。
      /// </summary>
      public  string Kind { get; set; }



      /// <summary>
      /// 与用户帐户唯一关联的id的模糊版本
      /// </summary>
      public string ObfuscatedExternalAccountId { get; set; }


      /// <summary>
      /// 用户配置文件唯一关联的id的模糊版本
      /// </summary>
      public string ObfuscatedExternalProfileId { get; set; }


      /// <summary>
      /// 与购买inapp产品相关联的订单id
      /// </summary>
      public string OrderId { get; set; }


      /// <summary>
      ///  内购产品标识
      /// </summary>
      public string ProductId { get; set; }

      /// <summary>
      /// 订单的购买状态。可能的值为:0。购买1。取消
      /// </summary>
      public int? PurchaseState { get; set; }

      /// <summary>
      /// 自纪元(1970年1月1日)以来,购买产品的时间(以毫秒为单位)
      /// </summary>
      public long? PurchaseTimeMillis { get; set; }

      /// <summary>
      /// 标识此购买而生成的购买令牌
      /// </summary>
      public string PurchaseToken { get; set; }


      /// <summary>
      /// 不是使用标准的应用内计费流制作的。可能的值为:0。
      //测试(即从许可证测试帐户购买)1。促销(即购买
      //使用促销代码)2。奖励(即观看视频广告而非付费)
      /// </summary>
      public int? PurchaseType { get; set; }

      /// <summary>
      /// 相关联的数量
      /// </summary>
      public int? Quantity { get; set; }

   
      /// <summary>
      /// 退款数量
      /// </summary>
      public  int? RefundableQuantity { get; set; }

     

      public  string RegionCode { get; set; }

      //
      // 摘要:
      //     The ETag of the item.
      public  string ETag { get; set; }
  }
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值