我的项目包括Paypal rest api,如下代码所示:
private static string accessToken
{
get
{
string tokenAccess = new OAuthTokenCredential(ConfigManager.Instance.GetProperties()["ClientID"], ConfigManager.Instance.GetProperties()["ClientSecret"]).GetAccessToken();
return tokenAccess;
}
}
public ActionResult Pay(FormCollection form)
{
var appid = Guid.Parse(form["ApartId"]);
var username = form["EmailAddress"];
var price = form["Price"];
var amounts = form["Amount"];
var user = _bb.Users.Single(m => m.UserName == username);
var genismodel = _bb.Aparts.Include("Reservations").Single(m => m.ApartID == appid);
var reservasyon = new Reservation
{
ReservationStatus = false,
ReservationNumber = "0123456877",
DateForBegin = Convert.ToDateTime(form["BeginDate"]),
DateForEnd = Convert.ToDateTime(form["EndDate"]),
ReservationDate = DateTime.Now,
UserID = user.UserID,
ReservationTotal =Convert.ToDecimal(price),
ReservationAmount = Convert.ToDecimal(amounts),
ReservationType = "Paypal",
ReservationState = ""
};
reservasyon.Aparts.Add(genismodel);
_bb.Reservations.AddObject(reservasyon);
_bb.SaveChanges();
var reservid = reservasyon.ReservationID;
int orderId = reservid;
string requestUrl = Request.Url.OriginalString;
string returnUrl = ConfigurationManager.AppSettings["ReturnURL"];
string cancelUrl = ConfigurationManager.AppSettings["CancelURL"];
var amount = amounts;
var description = form["ApartName"];
Payment pymnt = null;
pymnt = CreatePayment(username, PaymentMethod.paypal, amount, description, returnUrl, cancelUrl);
if (pymnt != null)
{
var pymntId = pymnt.id;
var state = pymnt.state;
var reservasyonum = _bb.Reservations.Single(m => m.ReservationID == reservid);
reservasyonum.ReservationState = state;
reservasyonum.ReservationNumber = pymntId;
_bb.SaveChanges();
var dredirectUrl = GetApprovalUrl(pymnt);
return Redirect(dredirectUrl);
}
return View();
}
问题来自这里:
public Payment CreatePayment(string email, PaymentMethod paymntMethod, string orderAmount, string orderDescription, string returnUrl, string cancelUrl)
{
Payment pymnt = null;
var amountDetails = new AmountDetails {subtotal = orderAmount};
var amount = new Amount {currency = "USD"};
var total = Convert.ToDouble(orderAmount);
amount.total = total.ToString();
amount.details = amountDetails;
var redirectUrls = new RedirectUrls {return_url = returnUrl, cancel_url = cancelUrl};
var transaction = new Transaction {amount = amount, description = orderDescription};
var transactions = new List<Transaction> {transaction};
var payer = new Payer {payment_method = paymntMethod.ToString()};
var pyment = new Payment
{
intent = "sale",
payer = payer,
transactions = transactions,
redirect_urls = redirectUrls
};
pymnt = pyment.Create(accessToken);
return pymnt;
}
调用 payment.Create(accessToken) 时,错误为:
konum: PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, Dictionary2 headersMap, String payLoad)
konum: PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payLoad)
konum: PayPal.Api.Payments.Payment.Create(APIContext apiContext)
konum: PayPal.Api.Payments.Payment.Create(String accessToken)
konum: Cozy.Controllers.ReservationController.CreatePayment(String email, PaymentMethod paymntMethod, String orderAmount, String orderDescription, String returnUrl, String cancelUrl) C:\Users\user\Documents\Projects\CozyLast\Cozy\Controllers\ReservationController.cs içinde:萨特尔 141
konum: Cozy.Controllers.ReservationController.Pay(FormCollection form) C:\Users\user\Documents\Projects\CozyLast\Cozy\Controllers\ReservationController.cs içinde: satır 99
konum: lambda_method(Closure, ControllerBase, Object[])
konum: System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器, Object[] 参数)
konum: System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 参数)
konum: System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 参数)
konum:System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.b__41() konum:System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.b__7(IAsyncResult _) konum: System.Web.Mvc。 Async.AsyncResultWrapper.WrappedAsyncResult 1.End() konum:System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) konum:System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.b__33() konum:System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.b__49()
怎么了?感谢回复。