//string baseUrl; //RedirectUrls redirectUrls; public IActionResult BeginPayment(Controller controller, string invoiceNumber, Amount amount, ItemList itemList, string intent = "sale") { string paymentGuid = Convert.ToString((new Random()).Next(100000)); Payer payer = new Payer() { payment_method = "paypal", }; string baseUrl = controller.Request.Scheme + "://" + controller.Request.Host + controller.Request.Path + "?" + QueryKeys.PaymentGuid + "=" + paymentGuid; RedirectUrls redirectUrls = new RedirectUrls() { cancel_url = baseUrl + "&" + QueryKeys.PaymentState + "=" + QueryValues.States.Canceled, return_url = baseUrl + "&" + QueryKeys.PaymentState + "=" + QueryValues.States.Created }; List <Transaction> transactions = new List <Transaction> { new Transaction() { description = "Transaction description.", invoice_number = invoiceNumber, amount = amount, item_list = itemList, } }; Payment payment = new Payment() { intent = intent, payer = payer, transactions = transactions, redirect_urls = redirectUrls }; //try //{ Payment createdPayment = payment.Create(apiContext); string redirectUrl = createdPayment.links.Where(x => x.rel.ToLower().Trim().Equals("approval_url")).Select(x => x.href).FirstOrDefault(); //} //catch(PayPal.PaymentsException e) //{ // e.Details //} if (redirectUrl == null) { throw new Exception("No approval_url found in the response."); } controller.HttpContext.Session.SetString(SessionKeys.PaymentId(paymentGuid), createdPayment.id); return(controller.Redirect(redirectUrl)); }
public Payment CompletePayment(Controller controller) { //try //{ string state = controller.HttpContext.Request.Query[QueryKeys.PaymentState]; switch (state) { case QueryValues.States.Canceled: return(null); case QueryValues.States.Created: break; default: throw new Exception("Unknown payment result: " + controller.HttpContext.Request.Query); } string paymentGuid = controller.Request.Query[QueryKeys.PaymentGuid]; string paymentId = controller.HttpContext.Session.GetString(SessionKeys.PaymentId(paymentGuid)); string payerId = controller.Request.Query[QueryKeys.PayerId]; PaymentExecution paymentExecution = new PaymentExecution { payer_id = payerId }; Payment payment = new Payment() { id = paymentId }; Payment executedPayment = payment.Execute(apiContext, paymentExecution); return(executedPayment); //} //catch(Exception e) //{ // return PaymentResults.Failed; //} }