public PayPalAmount CalcAmount(PayPalCurrency currency, List <PayPalItem> items, double shippingPrice, double shippingDiscount)
        {
            try
            {
                double subTotal = 0;
                double taxes    = 0;
                foreach (var item in items)
                {
                    //calc subtotal
                    subTotal += item.Price * item.Quantity;

                    //calc taxes
                    taxes += item.Tax * item.Quantity;
                }

                return(new PayPalAmount
                {
                    Total = (shippingPrice - shippingDiscount) + subTotal + taxes,
                    Currency = currency,
                    Details = new PayPalAmountDetails()
                    {
                        Subtotal = subTotal,
                        Tax = taxes,
                        Shipping = shippingPrice,
                        ShippingDiscount = shippingDiscount
                    }
                });
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 public static bool DoExpressCheckoutPayment(PayPalEnvironment Environment, string expressCheckoutToken, string expressCheckoutPayerID, string orderTotalValue, PayPalCurrency currency)
 {
     if (Environment == PayPalEnvironment.SandBox)
     {
         return DoExpressCheckoutPaymentSandBox(expressCheckoutToken, expressCheckoutPayerID, orderTotalValue, currency);
     }
     else
     {
         return DoExpressCheckoutPaymentReal(expressCheckoutToken, expressCheckoutPayerID, orderTotalValue, currency);
     }
 }
 public static string CreatePaypalExpressPayment(PayPalEnvironment Environment, string orderDescription, double itemPrice, PayPalCurrency currency, int quantity, string ItemName, string ItemNumber, string ReturnUrl, string CancelUrl,  out string paypalToken)
 {
     if (Environment == PayPalEnvironment.SandBox)
     {
         return CreatePaypalExpressPaymentSandbox(orderDescription, itemPrice, currency, quantity, ItemName, ItemNumber, ReturnUrl, CancelUrl, out paypalToken);
     }
     else
     {
         return CreatePaypalExpressPaymentReal(orderDescription, itemPrice, currency, quantity, ItemName, ItemNumber, ReturnUrl, CancelUrl, out paypalToken);
     }
 }
 public PayPalTransaction(List <PayPalItem> items, PayPalShippingAddress shippingAddress, string invoiceNumber, string transactionDescription, AllowedPaymentMethod allowedPaymentMethod, PayPalCurrency currency, double shippingPrice, double shippingDiscount)
 {
     ItemList = new PayPalItemList {
         Items = items, ShippingAddress = shippingAddress
     };
     InvoiceNumber  = invoiceNumber;
     Description    = transactionDescription;
     PaymentOptions = new PayPalPaymentOption {
         AllowedPaymentMethod = allowedPaymentMethod
     };
     Amount = CalcAmount(currency, items, shippingPrice, shippingDiscount);
 }
 private static PayPalAPIHelper.PayPalWS.CurrencyCodeType ConvertProgramCurrencyToPayPal(PayPalCurrency currency)
 {
     return (PayPalAPIHelper.PayPalWS.CurrencyCodeType)Enum.Parse(typeof(PayPalAPIHelper.PayPalWS.CurrencyCodeType),
                       Enum.GetName(typeof(PayPalCurrency), currency));
 }
        private static bool DoExpressCheckoutPaymentSandBox(string expressCheckoutToken, string expressCheckoutPayerID, string orderTotalValue, PayPalCurrency currency)
        {
            bool success = false;
            PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsType paymentDetails = new PayPalSandboxWS.PaymentDetailsType();
            paymentDetails.OrderTotal = new PayPalSandboxWS.BasicAmountType();
            paymentDetails.OrderTotal.currencyID = ConvertProgramCurrencyToPayPalSandbox(currency);
            paymentDetails.OrderTotal.Value = orderTotalValue;

            // prepare for commiting transaction
            PayPalAPIHelper.PayPalSandboxWS.DoExpressCheckoutPaymentReq payReq = new PayPalAPIHelper.PayPalSandboxWS.DoExpressCheckoutPaymentReq()
            {
                DoExpressCheckoutPaymentRequest = new PayPalAPIHelper.PayPalSandboxWS.DoExpressCheckoutPaymentRequestType()
                {
                    Version = Version,
                    DoExpressCheckoutPaymentRequestDetails = new PayPalAPIHelper.PayPalSandboxWS.DoExpressCheckoutPaymentRequestDetailsType()
                    {

                        Token = expressCheckoutToken,
                        PaymentAction = PayPalAPIHelper.PayPalSandboxWS.PaymentActionCodeType.Sale,
                        PayerID = expressCheckoutPayerID,
                        PaymentDetails = new PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsType[] { paymentDetails },
                    }
                }
            };

            // commit transaction and display results to user
            PayPalAPIHelper.PayPalSandboxWS.DoExpressCheckoutPaymentResponseType doResponse =
                BuildPayPalSandboxWebservice().DoExpressCheckoutPayment(payReq);
            HandleErrorSandbox(doResponse);
            success = true;

            return success;
        }
        private static string CreatePaypalExpressPaymentSandbox(string orderDescription, double itemPrice, PayPalCurrency currency, int quantity, string ItemName, string ItemNumber, string ReturnUrl, string CancelUrl, out string paypalToken)
        {
            double totalPrice = itemPrice;// itemPrice* quantity;
            // build request
            PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutRequestDetailsType reqDetails = new PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutRequestDetailsType();

            reqDetails.ReturnURL = ReturnUrl;
            reqDetails.CancelURL = CancelUrl;
            reqDetails.NoShipping = "1";
            reqDetails.OrderDescription = orderDescription;
            reqDetails.OrderTotal = new PayPalAPIHelper.PayPalSandboxWS.BasicAmountType()
            {
                currencyID = ConvertProgramCurrencyToPayPalSandbox(currency),
                Value = totalPrice.ToString("0.##").Replace(",", ".")
            };

            PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsType paymentDetails = new PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsType();

            PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsItemType item = new PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsItemType();
            item.Amount = new PayPalAPIHelper.PayPalSandboxWS.BasicAmountType();
            item.Amount.currencyID = ConvertProgramCurrencyToPayPalSandbox(currency);
            item.Amount.Value = totalPrice.ToString("0.##").Replace(",", ".");
            item.Name = ItemName;
             item.Number = quantity.ToString();
            item.Quantity = "1"; //quantity.ToString();

            paymentDetails.PaymentDetailsItem = new PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsItemType[] { item };

            reqDetails.PaymentDetails = new PayPalAPIHelper.PayPalSandboxWS.PaymentDetailsType[] { paymentDetails };

            PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutReq req = new PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutReq()
            {
                SetExpressCheckoutRequest = new PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutRequestType()
                {
                    Version = Version,
                    SetExpressCheckoutRequestDetails = reqDetails
                }
            };

            // query PayPal and get token
            PayPalAPIHelper.PayPalSandboxWS.SetExpressCheckoutResponseType resp = BuildPayPalSandboxWebservice().SetExpressCheckout(req);
            HandleErrorSandbox(resp);
            paypalToken = resp.Token;

            // return a URL to PayPal
            return (string.Format("{0}?cmd=_express-checkout&token={1}", "https://www.sandbox.paypal.com/cgi-bin/webscr", paypalToken));
        }