[英]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. 經過進一步調查,我發現這是由於Plan Id
無效,但我不知道為什么會這樣,因為我已經在 model、 BillingPlansController
和 PayPal 儀表板中創建了它。
這是來自 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."
}
]
}
這是我的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"
}
};
}
這是我的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);
}
請注意,在上面的代碼中,我的 controller 中有一個 GetAPIContext() 方法。
這是發送到 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"
}
}
鏈接到 GitHub 存儲庫: https://github.com/spatrick195/OMS
看起來 PayPal 已棄用我正在使用的 API 的版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.