コード例 #1
0
        public IActionResult OnPost()
        {
            ICheckoutApi checkoutApi = client.CheckoutApi;

            try
            {
                // create line items for the order
                // This example assumes the order information is retrieved and hard coded
                // You can find different ways to retrieve order information and fill in the following lineItems object.
                List <OrderLineItem> lineItems = new List <OrderLineItem>();

                Money firstLineItemBasePriceMoney = new Money.Builder()
                                                    .Amount(500L)
                                                    .Currency("USD")
                                                    .Build();

                OrderLineItem firstLineItem = new OrderLineItem.Builder("1")
                                              .Name("Test Item A")
                                              .BasePriceMoney(firstLineItemBasePriceMoney)
                                              .Build();

                lineItems.Add(firstLineItem);

                Money secondLineItemBasePriceMoney = new Money.Builder()
                                                     .Amount(1000L)
                                                     .Currency("USD")
                                                     .Build();

                OrderLineItem secondLineItem = new OrderLineItem.Builder("3")
                                               .Name("Test Item B")
                                               .BasePriceMoney(secondLineItemBasePriceMoney)
                                               .Build();

                lineItems.Add(secondLineItem);

                // create Order object with line items
                Order order = new Order.Builder(locationId)
                              .LineItems(lineItems)
                              .Build();

                // create order request with order
                CreateOrderRequest orderRequest = new CreateOrderRequest.Builder()
                                                  .Order(order)
                                                  .Build();

                // create checkout request with the previously created order
                CreateCheckoutRequest createCheckoutRequest = new CreateCheckoutRequest.Builder(
                    Guid.NewGuid().ToString(),
                    orderRequest)
                                                              .Build();

                // create checkout response, and redirect to checkout page if successful
                CreateCheckoutResponse response = checkoutApi.CreateCheckout(locationId, createCheckoutRequest);
                return(Redirect(response.Checkout.CheckoutPageUrl));
            }
            catch (ApiException e)
            {
                return(RedirectToPage("Error", new { error = e.Message }));
            }
        }
コード例 #2
0
        public ActionResult SquarePayment(string id)
        {
            if (id == null)
            {
                return(RedirectToAction("/Home/Login"));
            }

            Models.Order order = db.Orders.Where(i => i.ID == id).First();
            if (order == null)
            {
                return(HttpNotFound());
            }

            Models.OrderBilling billing = db.Billing.FirstOrDefault(i => i.orderId == order.orderId);
            if (billing == null)
            {
                return(HttpNotFound());
            }

            CheckoutSquare square      = new CheckoutSquare();
            ICheckoutApi   checkoutApi = square.client.CheckoutApi;

            try
            {
                // create line items for the order
                // This example assumes the order information is retrieved and hard coded
                // You can find different ways to retrieve order information and fill in the following lineItems object.
                List <OrderLineItem> lineItems = new List <OrderLineItem>();

                Money firstLineItemBasePriceMoney = new Money.Builder()
                                                    .Amount(order.billing.totalChargesSquareAPI)
                                                    .Currency("USD")
                                                    .Build();

                OrderLineItem firstLineItem = new OrderLineItem.Builder("1")
                                              .Name(order.service)
                                              .BasePriceMoney(firstLineItemBasePriceMoney)
                                              .Build();

                lineItems.Add(firstLineItem);

                // create Order object with line items
                Square.Models.Order squareOrder = new Square.Models.Order.Builder(square.locationId)
                                                  .LineItems(lineItems)
                                                  .Build();

                // create order request with order
                CreateOrderRequest orderRequest = new CreateOrderRequest.Builder()
                                                  .Order(squareOrder)
                                                  .IdempotencyKey(order.contractNumber)
                                                  .Build();

                // create checkout request with the previously created order
                CreateCheckoutRequest createCheckoutRequest = new CreateCheckoutRequest.Builder(
                    Guid.NewGuid().ToString(),
                    orderRequest)
                                                              .RedirectUrl(WebConfigurationManager.AppSettings["SQUARE_RedirectUrl"] + id)
                                                              .MerchantSupportEmail("*****@*****.**")
                                                              .PrePopulateBuyerEmail(order.email)
                                                              .Build();

                // create checkout response, and redirect to checkout page if successful
                CreateCheckoutResponse response = checkoutApi.CreateCheckout(square.locationId, createCheckoutRequest);

                // Save checkout on the base
                db.CreatePayment(order.orderId, response.Checkout.Id, "SQUARE API");

                return(Redirect(response.Checkout.CheckoutPageUrl));
            }
            catch (ApiException e)
            {
                return(RedirectToAction("Error", new { error = e.Message }));
            }
        }
        public override PaymentFormResult GenerateForm(OrderReadOnly order, string continueUrl, string cancelUrl, string callbackUrl, SquareSettings settings)
        {
            var currency     = Vendr.Services.CurrencyService.GetCurrency(order.CurrencyId);
            var currencyCode = currency.Code.ToUpperInvariant();

            // Ensure currency has valid ISO 4217 code
            if (!Iso4217.CurrencyCodes.ContainsKey(currencyCode))
            {
                throw new Exception("Currency must be a valid ISO 4217 currency code: " + currency.Name);
            }

            var accessToken = settings.SandboxMode ? settings.SandboxAccessToken : settings.LiveAccessToken;
            var environment = settings.SandboxMode ? SquareSdk.Environment.Sandbox : SquareSdk.Environment.Production;

            var client = new SquareSdk.SquareClient.Builder()
                         .Environment(environment)
                         .AccessToken(accessToken)
                         .Build();

            var checkoutApi = client.CheckoutApi;

            var bodyOrderOrderSource = new OrderSource.Builder()
                                       .Name("Vendr")
                                       .Build();

            var orderAmount = AmountToMinorUnits(order.TransactionAmount.Value);

            var bodyOrderOrderLineItems = new List <OrderLineItem>()
            {
                new OrderLineItem("1",
                                  order.Id.ToString(),
                                  order.OrderNumber,
                                  basePriceMoney: new Money(orderAmount, currencyCode))
            };

            var orderReference      = order.GenerateOrderReference();
            var shortOrderReference = $"{order.Id},{order.OrderNumber}";

            var bodyOrderOrder = new SquareSdk.Models.Order.Builder(settings.LocationId)
                                 .CustomerId(order.CustomerInfo.CustomerReference)
                                 .ReferenceId(order.Id.ToString())
                                 .Source(bodyOrderOrderSource)
                                 .LineItems(bodyOrderOrderLineItems)
                                 .Build();

            var bodyOrder = new CreateOrderRequest.Builder()
                            .Order(bodyOrderOrder)
                            .LocationId(settings.LocationId)
                            .IdempotencyKey(Guid.NewGuid().ToString())
                            .Build();

            var body = new CreateCheckoutRequest.Builder(
                Guid.NewGuid().ToString(), bodyOrder)
                       .RedirectUrl(continueUrl)
                       .Build();

            var result = checkoutApi.CreateCheckout(settings.LocationId, body);

            return(new PaymentFormResult()
            {
                Form = new PaymentForm(result.Checkout.CheckoutPageUrl, FormMethod.Get)
            });
        }
コード例 #4
0
        public IActionResult OnPost()
        {
            ICheckoutApi checkoutApi = client.CheckoutApi;

            try
            {
                var rand  = new Random();
                int rand1 = rand.Next(100000, 999999);
                int id    = HttpContext.Session.GetInt32("logID").GetValueOrDefault();
                DropShipModels.User CurrUser = dbContext.Users
                                               .FirstOrDefault(user => user.UserId == id);
                if (CurrUser != null)
                {
                    var RetrievedCart = dbContext.Carts.Where(c => c.UserId == CurrUser.UserId)
                                        .Include(carts => carts.P)
                                        .ToList();
                    string OrderNumberTemp       = id.ToString() + '-' + rand1.ToString();
                    string ProductBoughtListTemp = "";

                    List <OrderLineItem> lineItems = new List <OrderLineItem>();
                    foreach (var j in RetrievedCart)
                    {
                        Money ItemPrice = new Money.Builder()
                                          .Amount(j.P.ProductPrice * 100)
                                          .Currency("USD")
                                          .Build();

                        OrderLineItem LineItem = new OrderLineItem.Builder(j.Quantity.ToString())
                                                 .Name(j.P.ProductName)
                                                 .BasePriceMoney(ItemPrice)
                                                 .Note("Order Number     " + OrderNumberTemp)
                                                 .Build();

                        lineItems.Add(LineItem);
                        dbContext.Carts.Remove(j);
                        dbContext.SaveChanges();
                        ProductBoughtListTemp += j.P.ProductName + " x " + j.Quantity + ", ";
                    }
                    ProductBoughtListTemp = ProductBoughtListTemp.Remove(ProductBoughtListTemp.Length - 1, 1);
                    ProductBoughtListTemp = ProductBoughtListTemp.Remove(ProductBoughtListTemp.Length - 1, 1);
                    DropShipModels.Order newOrder = new DropShipModels.Order();
                    newOrder.UserId            = id;
                    newOrder.OrderNumber       = OrderNumberTemp.ToString();
                    newOrder.ProductBoughtList = ProductBoughtListTemp;
                    newOrder.Filled            = false;
                    newOrder.ShippingMethod    = "Standard";
                    dbContext.Orders.Add(newOrder);
                    dbContext.SaveChanges();
                    // create line items for the order.

                    // create Order object with line items
                    Order order = new Order.Builder(locationId)
                                  .LineItems(lineItems)
                                  .Build();

                    // create order request with order
                    CreateOrderRequest orderRequest = new CreateOrderRequest.Builder()
                                                      .Order(order)
                                                      .Build();

                    // create checkout request with the previously created order
                    CreateCheckoutRequest createCheckoutRequest = new CreateCheckoutRequest.Builder(
                        Guid.NewGuid().ToString(),
                        orderRequest)
                                                                  .AskForShippingAddress(true)
                                                                  // .RedirectUrl("www.google.com")
                                                                  .PrePopulateBuyerEmail(CurrUser.Email)
                                                                  .MerchantSupportEmail("*****@*****.**")
                                                                  .Build();
                    // create checkout response, and redirect to checkout page if successful
                    CreateCheckoutResponse response = checkoutApi.CreateCheckout(locationId, createCheckoutRequest);
                    return(Redirect(response.Checkout.CheckoutPageUrl));
                }

                else
                {
                    if (HttpContext.Session.GetString("TempCart") != null)
                    {
                        var cartStr = HttpContext.Session.GetString("TempCart");
                        List <List <int> > cart1                 = JsonConvert.DeserializeObject <List <List <int> > >(cartStr);
                        string             OrderNumberTemp       = "0-" + rand1.ToString();
                        string             ProductBoughtListTemp = "";

                        List <OrderLineItem> lineItems = new List <OrderLineItem>();
                        foreach (var k in cart1)
                        {
                            string ProdName  = dbContext.Products.FirstOrDefault(p => p.ProductId == k[0]).ProductName;
                            Money  ItemPrice = new Money.Builder()
                                               .Amount(dbContext.Products.FirstOrDefault(p => p.ProductId == k[0]).ProductPrice * 100)
                                               .Currency("USD")
                                               .Build();

                            OrderLineItem LineItem = new OrderLineItem.Builder(k[1].ToString())
                                                     .Name(ProdName)
                                                     .BasePriceMoney(ItemPrice)
                                                     .Note("Order Number     " + OrderNumberTemp)
                                                     .Build();

                            lineItems.Add(LineItem);
                            ProductBoughtListTemp += ProdName + " x " + k[1].ToString() + ", ";
                        }
                        DropShipModels.User tempUser = dbContext.Users.FirstOrDefault(user => user.Password == "Temp");
                        ProductBoughtListTemp = ProductBoughtListTemp.Remove(ProductBoughtListTemp.Length - 1, 1);
                        ProductBoughtListTemp = ProductBoughtListTemp.Remove(ProductBoughtListTemp.Length - 1, 1);
                        DropShipModels.Order newOrder = new DropShipModels.Order();
                        newOrder.UserId            = tempUser.UserId;
                        newOrder.OrderNumber       = OrderNumberTemp.ToString();
                        newOrder.ProductBoughtList = ProductBoughtListTemp;
                        newOrder.Filled            = false;
                        newOrder.ShippingMethod    = "Standard";
                        dbContext.Orders.Add(newOrder);
                        dbContext.SaveChanges();
                        // create line items for the order
                        // This example assumes the order information is retrieved and hard coded
                        // You can find different ways to retrieve order information and fill in the following lineItems object.

                        // create Order object with line items
                        Order order = new Order.Builder(locationId)
                                      .LineItems(lineItems)
                                      .Build();

                        // create order request with order
                        CreateOrderRequest orderRequest = new CreateOrderRequest.Builder()
                                                          .Order(order)
                                                          .Build();
                        // create checkout request with the previously created order
                        // var tempPerson = new Person();
                        CreateCheckoutRequest createCheckoutRequest = new CreateCheckoutRequest.Builder(
                            Guid.NewGuid().ToString(),
                            orderRequest)
                                                                      .AskForShippingAddress(true)
                                                                      .MerchantSupportEmail("*****@*****.**")
                                                                      .Build();
                        // create checkout response, and redirect to checkout page if successful
                        CreateCheckoutResponse response = checkoutApi.CreateCheckout(locationId, createCheckoutRequest);
                        return(Redirect(response.Checkout.CheckoutPageUrl));
                    }
                    else
                    {
                        return(Redirect("/"));
                    }
                }
            }
            catch (ApiException e)
            {
                return(RedirectToPage("Error", new { error = e.Message }));
            }
        }