Reputation: 389
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.
This the response Json from the PayPal API:
{
"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.
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
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.
This is the data that is being sent to the 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
Upvotes: 2
Views: 633
Reputation: 389
It looks like PayPal deprecated the version of the API that I am using.
Upvotes: 1