/// <summary> /// /// </summary> /// <returns></returns> public static Payment translateToPayPalPayment(PayPalPaymentBean payment) { //Sync transaction object to bean data Transaction transaction = new Transaction() { amount = payment.amount, description = payment.description, invoice_number = payment.invoice_number, item_list = new ItemList() { items = new List <Item>() { payment.item } } }; return(new Payment() { intent = "sale", payer = new Payer() { payment_method = "paypal" }, redirect_urls = payment.redirect_urls, transactions = new List <Transaction>() { transaction } }); }
/// <summary> /// /// </summary> /// <param name="baseURI"></param> /// <param name="guid"></param> /// <returns></returns> public ActionResult RequestPayPalPayment(PayPalPaymentBean payment) { APIContext apiContext = Configuration.GetAPIContext(); if (apiContext == null) { logger.Info("Paypal API context not found"); throw new Exception("Paypal API context not found"); } Payment translatedPayment = ModelFactory.translateToPayPalPayment(payment); //Fills PayerID on Request Payment executedPayment = translatedPayment.Create(apiContext); //get links returned from paypal in response to Create function call var links = executedPayment.links.GetEnumerator(); string paypalRedirectUrl = null; while (links.MoveNext()) { Links lnk = links.Current; if (lnk.rel.ToLower().Trim().Equals("approval_url")) { //saving the payapalredirect URL to which user will be redirected for payment paypalRedirectUrl = lnk.href; } } // saving the paymentID in the key guid Session.Add(payment.guid, executedPayment.id); return(Redirect(paypalRedirectUrl)); }
/// <summary> /// Payment with PayPal account takes two steps. /// First get the payerId and payment identification /// Second perform payment confirmation /// </summary> /// <returns></returns> public ActionResult PaymentWithPaypal() { //Recover payerId from request to determine first or second step string payerId = Request.Params["PayerID"]; try { if (string.IsNullOrEmpty(payerId)) { //Return address string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Paypal/PaymentWithPayPal?"; //Payment identification to be stored at the session string guid = Convert.ToString((new Random()).Next(100000)); string redirectUrl = baseURI + "guid=" + guid; //Create sample payment PayPalPaymentBean samplePayment = ModelFactory.createSamplePayPalPayment(guid, redirectUrl); return(RequestPayPalPayment(samplePayment)); } else { string guid = Session[Request.Params["guid"]] as string; if (string.IsNullOrEmpty(guid)) { logger.Info("guid not found in session after payment request"); throw new Exception("guid not found in session after payment request"); } APIContext apiContext = Configuration.GetAPIContext(); if (apiContext == null) { logger.Info("Paypal API context not found"); throw new Exception("Paypal API context not found"); } var paymentExecution = new PaymentExecution() { payer_id = payerId }; Payment payment = new Payment() { id = guid }; Payment executedPayment = payment.Execute(apiContext, paymentExecution); if (executedPayment.state.ToLower() != "approved") { return(View("FailureView")); } return(View("SuccessView")); } } catch (Exception ex) { logger.Error("Error" + ex.Message); return(View("FailureView")); } }