Пример #1
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)
            });
        }