简体   繁体   English

PayPal 计费协议 - 无效的计划 ID .NET

[英]PayPal Billing Agreement - Invalid Plan Id .NET

I am trying to create a new agreement in PayPal with the .NET SDK, however when I am sending my agreement to the PayPal API with my plan & agreement details, I receive a 400 Bad Request error. I am trying to create a new agreement in PayPal with the .NET SDK, however when I am sending my agreement to the PayPal API with my plan & agreement details, I receive a 400 Bad Request error. Upon further investigation, I found that this is due to the Plan Id being invalid, but I have no idea why that could be as I have created it already in the model, in the BillingPlansController and in the PayPal dashboard.经过进一步调查,我发现这是由于Plan Id无效,但我不知道为什么会这样,因为我已经在 model、 BillingPlansController和 PayPal 仪表板中创建了它。

This the response Json from the PayPal API:这是来自 PayPal API 的响应 Json:

{
   "name":"TEMPLATE_ID_INVALID",
   "debug_id":"9e24570510a3e",
   "message":"",
   "information_link":"https://developer.paypal.com/docs/api/payments.billing-agreements#errors",
   "details":[
      {
         "field":"validation_error",
         "issue":"Incorrect Plan Id."
      }
   ]
}

Here is my Plan model, in where I create the plan to be used in my application.这是我的Plan model,我在其中创建了要在我的应用程序中使用的计划。

public class Plan
{
   public string PayPalPlanId { get; set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
   public int NumberOfEmployees { get; set; }
   public string Description { get; set; }
   public static List<Plan> Plans => new List<Plan>()
   {
      new Plan()
      {
          Name = "Starter Subscription",
          Price = 30,
          PayPalPlanId = "P-27H34871BG666983A2CPYWUI",
          NumberOfEmployees = 1,
          Description = "Access to the website for a monthly payment"
      }
   };
}

Here is the relevant portion of my SubscriptonController这是我的SubscriptonController的相关部分

public ActionResult Purchase(PurchaseVm model)
{
   var plan = Plan.Plans.FirstOrDefault(x => x.PayPalPlanId == model.Plan.PayPalPlanId);
   if (ModelState.IsValid)
   {
        var startDate = DateTime.UtcNow.AddMonths(1);
        var apiContext = GetApiContext();

        var subscription = new Subscription()
        {
             FirstName = model.FirstName,
             LastName = model.LastName,
             Email = model.Email,
             StartDate = startDate,
             NumberOfEmployees = plan.NumberOfEmployees,
             PayPalPlanId = plan.PayPalPlanId
        };
        _dbContext.Subscriptions.Add(subscription);
        _dbContext.SaveChanges();

        var agreement = new Agreement()
        {
             name = plan.Name,
             description = $"blah blah blah",
             start_date = startDate.ToString("yyyy-MM-ddTHH:mm:ssZ"),
             create_time = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"),
             plan = new PayPal.Api.Plan()
             {
                id = plan.PayPalPlanId,
             },
             payer = new Payer()
             {
                payment_method = "paypal",
                payer_info = new PayerInfo()
                {
                   first_name = model.FirstName,
                   last_name = model.LastName, 
                   email = model.Email
                }
             }
        };
        // the line below is where the error occurs
        var createdAgreement = agreement.Create(apiContext);
        subscription.PayPalAgreementToken = createdAgreement.token;
        _dbContext.SaveChanges();

       var approvalUrl = createdAgreement.links.FirstOrDefault(x => x.rel.Equals("approval_url",
           StringComparison.OrdinalIgnoreCase));
       return Redirect(approvalUrl.href);
    }
    model.Plan = plan;
    return View(model);
}

Note that In the code above, I have a GetAPIContext() method in my controller.请注意,在上面的代码中,我的 controller 中有一个 GetAPIContext() 方法。

This is the data that is being sent to the PayPal API这是发送到 PayPal API 的数据

{
   "name":"Starter Subscription",
   "description":"Access to the website for a monthly payment",
   "start_date":"2020-07-15T12:44:06Z",
   "payer":{
      "payment_method":"paypal",
      "payer_info":{
         "email":"xxxx",
         "first_name":"xxx",
         "last_name":"xxx"
      }
   },
   "plan":{
      "id":"P-4PV26268V7918953BL3S3ZHA"
   }
}

Link to GitHub Repository: https://github.com/spatrick195/OMS链接到 GitHub 存储库: https://github.com/spatrick195/OMS

It looks like PayPal deprecated the version of the API that I am using.看起来 PayPal 已弃用我正在使用的 API 的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM