コード例 #1
0
        public ActionResult CalculateShipping(string postalCode)
        {
            postalCode = postalCode.ToUpper().Replace(" ", "");
            // Get the shipping type
            Enums.ShippingType shippingType = ShippingService.GetShippingTypeByPostalCode(postalCode);


            // TODO : Rate the shipping depending on the products
            // If we are a guest on the site use cookies for the shopping cart
            if (!UserService.IsUserConnected(System.Web.HttpContext.Current.User))
            {
            }
            else // We are connected with a user account
            {
            }

            CanadaPost.CanadaPostRates rates = new CanadaPost.CanadaPostRates();
            double price = rates.GetRates(postalCode, shippingType);

            return(Json(new { price, error = price == 0 ? "The postal code must be valid" : null }, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        private Payment CreatePayment(APIContext apiContext, List <Product> products, ShippingAddress shippingAddress, string redirectUrl)
        {
            List <ShoppingCartProduct> shoppingCartProducts = new List <ShoppingCartProduct>();

            // If we are a guest on the site use cookies for the shopping cart
            if (!UserService.IsUserConnected(System.Web.HttpContext.Current.User))
            {
                // Get products using the CookieService
                shoppingCartProducts = CookieService.GetShoppingCartProducts(Request.Cookies);

                // Reverse the list so the most recent products are first
                shoppingCartProducts.Reverse();
            }
            else
            {
                ShoppingCart shoppingCart = new ShoppingCartManager().GetShoppingCartByUser(User.Identity.GetUserId());

                shoppingCartProducts = new ShoppingCartProductManager().GetShoppingCartProductByShoppingCartId(shoppingCart.ShoppingCartId);
            }



            //shippingAddress.country_code = "US";
            shippingAddress.country_code = "CA";
            string currency = CookieService.GetCurrency(Request.Cookies);
            //shippingAddress.country_code = CookieService.GetCountryCode(Request.Cookies);

            var items = new ItemList()
            {
                items = new List <Item>(),
                //shipping_address = new ShippingAddress()
                //{
                //    recipient_name = "john ros",
                //    line1 = "111 First Street",
                //    city = "Saratoga",
                //    state = "US",
                //    postal_code = "95070",
                //    country_code = "US",
                //    phone = "819 4443333"
                //}
                shipping_address = shippingAddress
            };

            foreach (ShoppingCartProduct scp in shoppingCartProducts)
            {
                items.items.Add(new Item()
                {
                    name     = scp.Product.Name,
                    currency = currency,//"CAD",
                    //price = "0",
                    price    = PriceService.GetPrice(scp, currency).ToString(),
                    quantity = scp.Quantity.ToString(),
                    sku      = scp.Product.ProductId.ToString() + "S" + scp.Size,
                });
            }
            //items.items.FirstOrDefault().price = "0.60";

            // FOR TESTING
            //items.items.Add(new Item()
            //{
            //    name = "Name-Test",
            //    currency = "CAD",
            //    price = 1.ToString(),
            //    quantity = 1.ToString(),
            //    sku = "sku",
            //});

            var payer = new Payer
            {
                payment_method = "paypal"
            };

            // Do the configuration RedirectURLs here with redirectURLs object
            RedirectUrls redirUrls = new RedirectUrls()
            {
                cancel_url = redirectUrl,
                return_url = redirectUrl
            };


            // clean this
            string postal_code = shippingAddress.postal_code.ToUpper().Replace(" ", "");
            double shipping    = new CanadaPost.CanadaPostRates().GetRates(postal_code, ShippingService.GetShippingTypeByPostalCode(postal_code));
            //double shipping = 0;

            // Create details object
            Details details = new Details()
            {
                //tax = "0",
                shipping = shipping.ToString(),
                //shipping = "1",
                //subtotal = "0.60",
                subtotal = ShoppingCartService.GetSubTotal(shoppingCartProducts, currency).ToString(),
            };

            // Create amount object
            Amount amount = new Amount()
            {
                currency = currency,//"CAD",
                total    = /*(Convert.ToDouble(details.tax) + */ (Convert.ToDouble(details.shipping) + Convert.ToDouble(details.subtotal)).ToString(),
                details  = details
            };

            // Create transaction
            List <Transaction> transactions = new List <Transaction>
            {
                new Transaction()
                {
                    description    = "Auralta Clothing Order",
                    invoice_number = Convert.ToString((new Random()).Next(100000)),
                    amount         = amount,
                    item_list      = items
                }
            };

            // Create the web experience profile
            var profile = new WebProfile()
            {
                name         = Guid.NewGuid().ToString(),
                presentation = new Presentation()
                {
                    brand_name  = "Auralta Clothing",
                    locale_code = CookieService.GetCountryCode(Request.Cookies),//"CA",
                    //logo_image = "https://www.paypal.com/",
                    note_to_seller_label = "Thank you for doing business with Auralta Clothing",
                    return_url_label     = "See you soon"
                },

                input_fields = new InputFields()
                {
                    address_override = 1,
                    allow_note       = true,
                    no_shipping      = 1
                },


                temporary = true
            };

            var createdProfile = profile.Create(apiContext);

            payment = new Payment()
            {
                intent        = "sale",
                payer         = payer,
                transactions  = transactions,
                redirect_urls = redirUrls,

                // TODO: this id is permenant so hard code it
                experience_profile_id = createdProfile.id,
            };

            return(payment.Create(apiContext));
        }