Пример #1
0
        public DoExpressCheckoutPaymentResponse SendPayPalDoExpressCheckoutPaymentRequest(ApplicationCart cart, string token, string payerId)
        {
            try
            {
                WebUILogging.LogMessage("SendPayPalDoExpressCheckoutPaymentRequest");
                DoExpressCheckoutPaymentResponse response = _payPalTransactionRegistrar.SendDoExpressCheckoutPayment(token, payerId, cart.Currency, cart.TotalPrice);

                // Add a PayPal transaction record
                PayPalTransaction transaction = new PayPalTransaction
                {
                    RequestId            = response.RequestId,
                    TrackingReference    = cart.Id.ToString(),
                    RequestTime          = DateTime.Now,
                    RequestStatus        = response.ResponseStatus.ToString(),
                    TimeStamp            = response.TIMESTAMP,
                    RequestError         = response.ErrorToString,
                    Token                = response.TOKEN,
                    RequestData          = response.ToString,
                    PaymentTransactionId = response.PaymentTransactionId,
                    PaymentError         = response.PaymentErrorToString,
                };

                // Store this transaction in your Database

                return(response);
            }
            catch (Exception ex)
            {
                WebUILogging.LogException(ex.Message, ex);
            }
            return(null);
        }
Пример #2
0
        public GetExpressCheckoutDetailsResponse SendPayPalGetExpressCheckoutDetailsRequest(string token)
        {
            try
            {
                WebUILogging.LogMessage("SendPayPalGetExpressCheckoutDetailsRequest");
                GetExpressCheckoutDetailsResponse response = _payPalTransactionRegistrar.SendGetExpressCheckoutDetails(token);

                // Add a PayPal transaction record
                PayPalTransaction transaction = new PayPalTransaction
                {
                    RequestId         = response.RequestId,
                    TrackingReference = response.TrackingReference,
                    RequestTime       = DateTime.Now,
                    RequestStatus     = response.ResponseStatus.ToString(),
                    TimeStamp         = response.TIMESTAMP,
                    RequestError      = response.ErrorToString,
                    Token             = response.TOKEN,
                    PayerId           = response.PAYERID,
                    RequestData       = response.ToString,
                };

                // Store this transaction in your Database

                return(response);
            }
            catch (Exception ex)
            {
                WebUILogging.LogException(ex.Message, ex);
            }
            return(null);
        }
        public ActionResult ConfirmPayPalPayment()
        {
            WebUILogging.LogMessage("Express Checkout Confirmation");
            ApplicationCart cart = (ApplicationCart)Session["Cart"];

            return(View(cart));
        }
        public ActionResult PostPaymentSuccess()
        {
            WebUILogging.LogMessage("Post Payment Result: Success");
            ApplicationCart cart = (ApplicationCart)Session["Cart"];

            ViewBag.TrackingReference = cart.Id;
            ViewBag.Description       = cart.PurchaseDescription;
            ViewBag.TotalCost         = cart.TotalPrice;
            ViewBag.Currency          = cart.Currency;
            return(View());
        }
Пример #5
0
        public SetExpressCheckoutResponse SendPayPalSetExpressCheckoutRequest(ApplicationCart cart, string serverURL, string userEmail = null)
        {
            try
            {
                ServicePointManager.SecurityProtocol       = SecurityProtocolType.Tls;
                ServicePointManager.Expect100Continue      = true;
                ServicePointManager.SecurityProtocol       = (SecurityProtocolType)3072;
                ServicePointManager.DefaultConnectionLimit = 9999;

                WebUILogging.LogMessage("SendPayPalSetExpressCheckoutRequest");

                // Optional handling of cart items: If there is only a single item being sold we don't need a list of expressCheckoutItems
                // However if you're selling a single item as a sale consider also adding it as an ExpressCheckoutItem as it looks better once you get to PayPal's site
                // Note: ExpressCheckoutItems are currently NOT stored by PayPal against the sale in the users order history so you need to keep your own records of what items were in a cart
                List <ExpressCheckoutItem> expressCheckoutItems = null;
                if (cart.Items != null)
                {
                    expressCheckoutItems = new List <ExpressCheckoutItem>();
                    foreach (ApplicationCartItem item in cart.Items)
                    {
                        expressCheckoutItems.Add(new ExpressCheckoutItem(item.Quantity, item.Price, item.Name, item.Description));
                    }
                }

                SetExpressCheckoutResponse response = _payPalTransactionRegistrar.SendSetExpressCheckout(cart.Currency, cart.TotalPrice, cart.PurchaseDescription, cart.Id.ToString(), serverURL, expressCheckoutItems, userEmail);

                // Add a PayPal transaction record
                PayPalTransaction transaction = new PayPalTransaction
                {
                    RequestId         = response.RequestId,
                    TrackingReference = cart.Id.ToString(),
                    RequestTime       = DateTime.Now,
                    RequestStatus     = response.ResponseStatus.ToString(),
                    TimeStamp         = response.TIMESTAMP,
                    RequestError      = response.ErrorToString,
                    Token             = response.TOKEN,
                };

                // Store this transaction in your Database

                return(response);
            }
            catch (Exception ex)
            {
                WebUILogging.LogException(ex.Message, ex);
            }
            return(null);
        }
        public ActionResult PayPalExpressCheckoutAuthorisedSuccess(string token, string PayerID) // Note "PayerID" is returned with capitalisation as written
        {
            // PayPal redirects back to here
            WebUILogging.LogMessage("Express Checkout Authorised");
            // GetExpressCheckoutDetails
            TempData["token"]   = token;
            TempData["payerId"] = PayerID;
            GetExpressCheckoutDetailsResponse transactionResponse = transactionService.SendPayPalGetExpressCheckoutDetailsRequest(token);

            if (transactionResponse == null || transactionResponse.ResponseStatus != PayPalMvc.Enums.ResponseType.Success)
            {
                SetUserNotification("Sorry there was a problem with initiating a PayPal transaction. Please try again and contact an Administrator if this still doesn't work.");
                string errorMessage = (transactionResponse == null) ? "Null Transaction Response" : transactionResponse.ErrorToString;
                WebUILogging.LogMessage("Error initiating PayPal GetExpressCheckoutDetails transaction. Error: " + errorMessage);
                return(RedirectToAction("Error", "Purchase"));
            }
            return(RedirectToAction("ConfirmPayPalPayment"));
        }
        public ActionResult PayPalExpressCheckout()
        {
            WebUILogging.LogMessage("Express Checkout Initiated");
            // SetExpressCheckout
            ApplicationCart            cart                = (ApplicationCart)Session["Cart"];
            string                     serverURL           = HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/");
            SetExpressCheckoutResponse transactionResponse = transactionService.SendPayPalSetExpressCheckoutRequest(cart, serverURL);

            // If Success redirect to PayPal for user to make payment
            if (transactionResponse == null || transactionResponse.ResponseStatus != PayPalMvc.Enums.ResponseType.Success)
            {
                SetUserNotification("Sorry there was a problem with initiating a PayPal transaction. Please try again and contact an Administrator if this still doesn't work.");
                string errorMessage = (transactionResponse == null) ? "Null Transaction Response" : transactionResponse.ErrorToString;
                WebUILogging.LogMessage("Error initiating PayPal SetExpressCheckout transaction. Error: " + errorMessage);
                return(RedirectToAction("Error", "Purchase"));
            }
            return(Redirect(string.Format(PayPalMvc.Configuration.Current.PayPalRedirectUrl, transactionResponse.TOKEN)));
        }
        public ActionResult ConfirmPayPalPayment(bool confirmed = true)
        {
            WebUILogging.LogMessage("Express Checkout Confirmed");
            ApplicationCart cart = (ApplicationCart)Session["Cart"];
            // DoExpressCheckoutPayment
            string token   = TempData["token"].ToString();
            string payerId = TempData["payerId"].ToString();
            DoExpressCheckoutPaymentResponse transactionResponse = transactionService.SendPayPalDoExpressCheckoutPaymentRequest(cart, token, payerId);

            if (transactionResponse == null || transactionResponse.ResponseStatus != PayPalMvc.Enums.ResponseType.Success)
            {
                if (transactionResponse != null && transactionResponse.L_ERRORCODE0 == "10486")
                {
                    // Redirect user back to PayPal in case of Error 10486 (bad funding method)
                    // https://www.x.com/developers/paypal/documentation-tools/how-to-guides/how-to-recover-funding-failure-error-code-10486-doexpresscheckout
                    WebUILogging.LogMessage("Redirecting User back to PayPal due to 10486 error (bad funding method - typically an invalid or maxed out credit card)");
                    return(Redirect(string.Format(PayPalMvc.Configuration.Current.PayPalRedirectUrl, token)));
                }
                SetUserNotification("Sorry there was a problem with taking the PayPal payment, so no money has been transferred. Please try again and contact an Administrator if this still doesn't work.");
                string errorMessage = (transactionResponse == null) ? "Null Transaction Response" : transactionResponse.ErrorToString;
                WebUILogging.LogMessage("Error initiating PayPal DoExpressCheckoutPayment transaction. Error: " + errorMessage);
                return(RedirectToAction("Error", "Purchase"));
            }

            if (transactionResponse.PaymentStatus == PaymentStatus.Completed)
            {
                payoutFunction(cart);
                return(RedirectToAction("PostPaymentSuccess"));
            }
            else
            {
                // Something went wrong or the payment isn't complete
                WebUILogging.LogMessage("Error taking PayPal payment. Error: " + transactionResponse.ErrorToString + " - Payment Error: " + transactionResponse.PaymentErrorToString);
                TempData["TransactionResult"] = transactionResponse.PAYMENTREQUEST_0_LONGMESSAGE;
                return(RedirectToAction("PostPaymentFailure"));
            }
        }
 public ActionResult PostPaymentFailure()
 {
     WebUILogging.LogMessage("Post Payment Result: Failure");
     ViewBag.ErrorMessage = TempData["TransactionResult"];
     return(View());
 }