Esempio n. 1
0
        public NVPCodec Execute(NVPCodec parameters)
        {
            GetCredentials(parameters);

            //string url = pendpointurl;

            ////To Add the credentials from the profile
            //string strPost = NvpRequest + "&" + buildCredentialsNVPString();
            //strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);

            var content = parameters.Encode();

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(endPoint);
            objRequest.Timeout = 30000;
            objRequest.Method = "POST";
            objRequest.ContentLength = content.Length;

            try
            {
                using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
                {
                    myWriter.Write(content);
                }
            }
            catch (Exception e)
            {
                /*
                if (log.IsFatalEnabled)
                {
                    log.Fatal(e.Message, this);
                }*/
            }

            //Retrieve the Response returned from the NVP API call to PayPal
            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            string result;
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }

            //Logging the response of the transaction
            /* if (log.IsInfoEnabled)
             {
                 log.Info("Result :" +
                           " Elapsed Time : " + (DateTime.Now - startDate).Milliseconds + " ms" +
                          result);
             }
             */

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(result);
            return decoder;
        }
        public GetShippingDetailsResponse Execute()
        {
            NVPCodec encoder = new NVPCodec();

            encoder["METHOD"] = METHOD;
            encoder["TOKEN"] = this.Token;

            var decoder = executorService.Execute(encoder);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                var address = new Address()
                {
                    Name = decoder["SHIPTONAME"],
                    AddressLineOne = decoder["SHIPTOSTREET"],
                    AddressLineTwo = decoder["SHIPTOSTREET2"],
                    Town = decoder["SHIPTOCITY"],
                    County = decoder["SHIPTOSTATE"],
                    PostCode = decoder["SHIPTOZIP"],
                    Country = decoder["SHIPTOCOUNTRYNAME"],
                    CountryCode = decoder["SHIPTOCOUNTRYCODE"]
                };

                var customer = new Customer() {
                    FirstName = decoder["FIRSTNAME"],
                    LastName = decoder["LASTNAME"],
                    Email = decoder["EMAIL"],
                    ContactAddress = address,
                    ExternalReference = decoder["PAYERID"],
                    CustomerType = "PayPal",
                    DateCreated = DateTime.Now
                };

                customer.Addresses = new List<Address>();
                customer.Addresses.Add(address);

                return new GetShippingDetailsResponse()
                {
                    ShippingAddress = address,
                    Customer = customer
                };
            }
            else
            {

                throw new PayPalExeception(decoder["L_ERRORCODE0"], decoder["L_SHORTMESSAGE0"], decoder["L_LONGMESSAGE0"]);
            }
        }
Esempio n. 3
0
        public ExpressCheckoutResponse Execute(Basket basket)
        {
            HttpContext context = HttpContext.Current;
            HttpRequest request = context.Request;

            string domain = request.Url.Scheme + System.Uri.SchemeDelimiter + request.Url.Host + (request.Url.IsDefaultPort ? "" : ":" + request.Url.Port);

            //TODO: This needs to come from elsewhere
            this.ReturnUrl = domain + "/paypal/shipping";
            this.CancelUrl = domain + "/paypal/cancel";
            this.Currency = "GBP";

            NVPCodec encoder = new NVPCodec();

            encoder["METHOD"] = METHOD;
            encoder["RETURNURL"] = this.ReturnUrl;
            encoder["CANCELURL"] = this.CancelUrl;
            encoder["AMT"] = basket.SubTotal.ToString("#.##");
            encoder["PAYMENTACTION"] = PAYMENT_ACTION;
            encoder["CURRENCYCODE"] = this.Currency;

            ////Optional Shipping Address entered on the merchant site
            //encoder["SHIPTONAME"] = shipToName;
            //encoder["SHIPTOSTREET"] = shipToStreet;
            //encoder["SHIPTOSTREET2"] = shipToStreet2;
            //encoder["SHIPTOCITY"] = shipToCity;
            //encoder["SHIPTOSTATE"] = shipToState;
            //encoder["SHIPTOZIP"] = shipToZip;
            //encoder["SHIPTOCOUNTRYCODE"] = shipToCountryCode;

            var decoder = executorService.Execute(encoder);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                var token = decoder["TOKEN"];

                return new ExpressCheckoutResponse() { Token = token, RedirectUrl = "https://" + executorService.Host + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + token };
            }
            else
            {

                throw new PayPalExeception(decoder["L_ERRORCODE0"], decoder["L_SHORTMESSAGE0"], decoder["L_LONGMESSAGE0"]);
            }
        }
Esempio n. 4
0
        public ConfirmPaymentResponse Execute(Order order)
        {
            NVPCodec encoder = new NVPCodec();
            if (order.Payment is PayPalPayment)
            {
                var payment = order.Payment as PayPalPayment;

                encoder["METHOD"] = METHOD;
                encoder["TOKEN"] = payment.Token;
                encoder["PAYMENTACTION"] = "Sale";
                encoder["PAYERID"] = payment.PayerID;
                encoder["AMT"] = order.GrandTotal.ToString("#.##");
                encoder["CURRENCYCODE"] = this.Currency;

                var decoder = executorService.Execute(encoder);

                string strAck = decoder["ACK"].ToLower();
                if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
                {
                    payment.CorrelationID = decoder["CORRELATIONID"];
                    payment.CurrencyCode = decoder["CURRENCYCODE"];
                    payment.FeeAmount = decimal.Parse(decoder["FEEAMT"]);
                    payment.PaymentAmount = decimal.Parse(decoder["AMT"]);
                    payment.Status = decoder["PAYMENTSTATUS"];
                    payment.PendingReason = decoder["PENDINGREASON"];
                    payment.ReasonCode = decoder["REASONCODE"];
                    payment.TransactionID = decoder["TRANSACTIONID"];
                    return new ConfirmPaymentResponse();
                }
                else
                {

                    throw new PayPalExeception(decoder["L_ERRORCODE0"], decoder["L_SHORTMESSAGE0"], decoder["L_LONGMESSAGE0"]);
                }
            }
            else
            {
                throw new PayPalExeception("The payment method on the order is not valid for paypal confirmation");
            }
        }
Esempio n. 5
0
        public RefundPaymentResponse Execute(PayPalPayment payment,
            bool partial = false,
            decimal amount = 0M,
            string note = null)
        {
            NVPCodec encoder = new NVPCodec();
            encoder["TRANSACTIONID"] = payment.TransactionID;
            encoder["METHOD"] = METHOD;

            if (partial)
            {
                encoder["REFUNDTYPE"] = "Partial";
                encoder["AMT"] = amount.ToString("#.##"); // TODO: Reference Decimal Format for PayPal
                encoder["CURRENCYCODE"] = this.Currency;
            }
            else
            {
                encoder["REFUNDTYPE"] = "Full";
            }

            var decoder = executorService.Execute(encoder);

            string strAck = decoder["ACK"].ToLower();
            if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
            {
                payment.RefundTransactionID = decoder["REFUNDTRANSACTIONID"];
                payment.FeeRefundAmount = decimal.Parse(decoder["FEEREFUNDAMT"]);
                payment.GrossRefundAmount = decimal.Parse(decoder["GROSSREFUNDAMT"]);
                payment.NetRefundAmount = decimal.Parse(decoder["NETREFUNDAMT"]);
                //payment.TotalRefundAmount = decimal.Parse(decoder["TOTALREFUNDEDAMT"]);
                payment.Refunded = true;
                return new RefundPaymentResponse() { RefundPayment = payment };
            }
            else
            {

                throw new PayPalExeception(decoder["L_ERRORCODE0"], decoder["L_SHORTMESSAGE0"], decoder["L_LONGMESSAGE0"]);
            }
        }
Esempio n. 6
0
        private void GetCredentials(NVPCodec parameters)
        {
            if (!String.IsNullOrWhiteSpace(userName))
                parameters["USER"] = userName;

            if (!String.IsNullOrWhiteSpace(password))
                parameters[PWD] = password;

            if (!String.IsNullOrWhiteSpace(signature))
                parameters[SIGNATURE] = signature;

            //if (!IsEmpty(Subject))
            //    codec["SUBJECT"] = Subject;

            parameters["VERSION"] = "2.3";
        }