Exemplo n.º 1
0
        public ActionResult Index(string orderReference, int amount)
        {
            PaypalExpressPart paypalExpressPart = _services.WorkContext.CurrentSite.As <PaypalExpressPart>();

            // set the success and cancel urls
            string baseUrl = "http://" + Request.Url.Authority + Request.Path;

            paypalExpressPart.CancelUrl  = baseUrl + "/Cancel";
            paypalExpressPart.SuccessUrl = baseUrl + "/Success";

            // retrieve the order from the repo
            OrderPart order = _orderService.GetOrderByNumber(orderReference);

            // call PaymentExpressService
            PaypalExpressResult result = _paypalExpressServices.SetExpressCheckout(paypalExpressPart, order);

            // log paypal transaction
            _paypalTransactionService.LogTransaction(order, result);

            if (string.Compare(result.Ack, "success", true) == 0)
            {
                // Redirect to PayPal
                return(new RedirectResult(paypalExpressPart.AuthorizationUrl + "&token=" + result.Token));
            }

            // if we get here then we were unable to get a token from SetExpressCheckout
            // advise user we have a technical problem, please try again/later
            var shape = _services.New.UnableToContactPaypal(RedirectTo: "~/Cascade.WebShop/Checkout/Summary");

            return(new ShapeResult(this, shape));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Call the Paypal GetExpressCheckoutDetails API (https://www.x.com/developers/paypal/documentation-tools/api/getexpresscheckoutdetails-api-operation-nvp)
        /// </summary>
        /// <param name="paypalExpressPart">part</param>
        /// <param name="token">Paypal token</param>
        /// <returns>result containing token, ack and payerId</returns>
        public PaypalExpressResult GetExpressCheckoutDetails(PaypalExpressPart paypalExpressPart, string token)
        {
            PaypalExpressResult result = new PaypalExpressResult {
                Method = "GetExpressCheckoutDetails"
            };
            HttpWebRequest request = BuildGetDetailsRequest(paypalExpressPart, token);

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                string[] lines = GetResponseLines(response);
                result.Ack           = GetValue(lines, "Ack").FirstOrDefault();
                result.Token         = GetValue(lines, "Token").FirstOrDefault();
                result.PayerId       = GetValue(lines, "PayerId").FirstOrDefault();
                result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault();

                // TODO: parse out useful information about the payer, address, etc
                // and store somewhere: update the order? add to PaypalTransaction? customer?

                if (string.Compare(result.Ack, "success", true) != 0)
                {
                    ExtractErrors(lines, result);
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        private void ExtractErrors(IEnumerable <string> lines, PaypalExpressResult result)
        {
            // log error messages
            DateTime timestamp;

            if (DateTime.TryParse(GetValue(lines, "TIMESTAMP").FirstOrDefault(), out timestamp))
            {
                result.Timestamp = timestamp;
            }
            result.ErrorCodes    = string.Join("; ", GetValue(lines, "L_ERRORCODE").ToArray());
            result.ShortMessages = string.Join("; ", GetValue(lines, "L_SHORTMESSAGE").ToArray());
            result.LongMessages  = string.Join("; ", GetValue(lines, "L_LONGMESSAGE").ToArray());
        }
 public void LogTransaction(OrderRecord order, PaypalExpressResult result)
 {
     TransactionRecord transaction = new TransactionRecord { 
         Token = result.Token, 
         Ack = result.Ack, 
         Method = result.Method, 
         OrderRecord_Id=order.Id, 
         DateTime=DateTime.Now,
         Timestamp=result.Timestamp,
         ErrorCodes=result.ErrorCodes,
         ShortMessages=result.ShortMessages,
         LongMessages=result.LongMessages
     };
     _paypalTransactions.Create(transaction);
 }
Exemplo n.º 5
0
        public void LogTransaction(OrderPart order, PaypalExpressResult result)
        {
            TransactionRecord transaction = new TransactionRecord {
                Token          = result.Token,
                Ack            = result.Ack,
                Method         = result.Method,
                OrderRecord_Id = order.Id,
                DateTime       = DateTime.Now,
                Timestamp      = result.Timestamp,
                ErrorCodes     = result.ErrorCodes,
                ShortMessages  = result.ShortMessages,
                LongMessages   = result.LongMessages
            };

            _paypalTransactions.Create(transaction);
        }
        /// <summary>
        /// Call the Paypal SetExpressCheckout API (https://www.x.com/developers/paypal/documentation-tools/api/setexpresscheckout-api-operation-nvp)
        /// </summary>
        /// <param name="paypalExpressPart">part</param>
        /// <param name="order">WebShop order</param>
        /// <returns>result containing token and ack</returns>
        public PaypalExpressResult SetExpressCheckout(PaypalExpressPart paypalExpressPart, OrderRecord order)
        {
            PaypalExpressResult result = new PaypalExpressResult { Method = "SetExpressCheckout" };
            HttpWebRequest request = BuildSetRequest(paypalExpressPart, order);

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                string[] lines = GetResponseLines(response);
                result.Ack = GetValue(lines, "Ack").FirstOrDefault();
                result.Token = GetValue(lines, "Token").FirstOrDefault();
                result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault();
                if (string.Compare(result.Ack, "success", true) != 0)
                    ExtractErrors(lines, result);
            }

            return result;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Call the Paypal SetExpressCheckout API (https://www.x.com/developers/paypal/documentation-tools/api/setexpresscheckout-api-operation-nvp)
        /// </summary>
        /// <param name="paypalExpressPart">part</param>
        /// <param name="order">WebShop order</param>
        /// <returns>result containing token and ack</returns>
        public PaypalExpressResult SetExpressCheckout(PaypalExpressPart paypalExpressPart, OrderPart order)
        {
            PaypalExpressResult result = new PaypalExpressResult {
                Method = "SetExpressCheckout"
            };
            HttpWebRequest request = BuildSetRequest(paypalExpressPart, order);

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                string[] lines = GetResponseLines(response);
                result.Ack           = GetValue(lines, "Ack").FirstOrDefault();
                result.Token         = GetValue(lines, "Token").FirstOrDefault();
                result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault();
                if (string.Compare(result.Ack, "success", true) != 0)
                {
                    ExtractErrors(lines, result);
                }
            }

            return(result);
        }
        /// <summary>
        /// Call the Paypal GetExpressCheckoutDetails API (https://www.x.com/developers/paypal/documentation-tools/api/getexpresscheckoutdetails-api-operation-nvp)
        /// </summary>
        /// <param name="paypalExpressPart">part</param>
        /// <param name="token">Paypal token</param>
        /// <returns>result containing token, ack and payerId</returns>
        public PaypalExpressResult GetExpressCheckoutDetails(PaypalExpressPart paypalExpressPart, string token)
        {
            PaypalExpressResult result = new PaypalExpressResult { Method = "GetExpressCheckoutDetails" };
            HttpWebRequest request = BuildGetDetailsRequest(paypalExpressPart, token);

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                string[] lines = GetResponseLines(response);
                result.Ack = GetValue(lines, "Ack").FirstOrDefault();
                result.Token = GetValue(lines, "Token").FirstOrDefault();
                result.PayerId = GetValue(lines, "PayerId").FirstOrDefault();
                result.CorrelationId = GetValue(lines, "CORRELATIONID").FirstOrDefault();

                // TODO: parse out useful information about the payer, address, etc
                // and store somewhere: update the order? add to PaypalTransaction? customer?

                if (string.Compare(result.Ack, "success", true) != 0)
                    ExtractErrors(lines, result);
            }

            return result;
        }
 private void ExtractErrors(IEnumerable<string> lines, PaypalExpressResult result)
 {
     // log error messages
     DateTime timestamp;
     if (DateTime.TryParse(GetValue(lines, "TIMESTAMP").FirstOrDefault(), out timestamp))
         result.Timestamp = timestamp;
     result.ErrorCodes = string.Join("; ", GetValue(lines, "L_ERRORCODE").ToArray());
     result.ShortMessages = string.Join("; ", GetValue(lines, "L_SHORTMESSAGE").ToArray());
     result.LongMessages = string.Join("; ", GetValue(lines, "L_LONGMESSAGE").ToArray());
 }