public CreateOrderRequest GetCreateOrderRequest(PaymentModel model, Data.Models.Payment.Order order, Guid guid, IEnumerable <CatalogObject> taxes)
        {
            var orderLineItems = order.Items.Select(x => x.ToOrderLineItem()).ToList();

            var _taxes = (taxes ?? new List <CatalogObject>())
                         .Where(x => !EXCLUDE.Any(y => x.TaxData.Name.ToLower().Contains(y)))
                         .Select(x => new OrderLineItemTax(
                                     name: x.TaxData.Name,
                                     type: x.TaxData.InclusionType,
                                     percentage: x.TaxData.Percentage)
                                 ).ToList();

            model.PickUpInMinutes = string.IsNullOrWhiteSpace(model.PickUpInMinutes) ? "0" : model.PickUpInMinutes;
            var pickUpInMinutes = 0;

            int.TryParse(model.PickUpInMinutes, out pickUpInMinutes);

            var isScheduled = false;

            if (pickUpInMinutes > 0)
            {
                isScheduled = true;
            }
            else
            {
                pickUpInMinutes = 10;
            }

            var pickupDetails = new OrderFulfillmentPickupDetails(
                recipient: new OrderFulfillmentRecipient(displayName: model.Name, emailAddress: model.Email, phoneNumber: model.Phone),
                scheduleType: (isScheduled ? "SCHEDULED" : "ASAP"),
                prepTimeDuration: "P10M", // 10 minutes
                pickupAt: ToRfc3339String(DateTime.UtcNow.AddMinutes(pickUpInMinutes))
                );

            var fulfill = new List <OrderFulfillment>()
            {
                new OrderFulfillment(
                    type: "PICKUP",
                    pickupDetails:  pickupDetails
                    )
            };

            var squareOrder = new Square.Models.Order(LocationId,
                                                      taxes: _taxes,
                                                      referenceId: order.Id.ToString(),
                                                      customerId: model.Email.Replace("@", "_at_").Replace(".", "-"),
                                                      lineItems: orderLineItems,
                                                      fulfillments: fulfill
                                                      );

            var orderRequest = new CreateOrderRequest(squareOrder, guid.ToString());

            return(orderRequest);
        }
        public override OrderReference GetOrderReference(HttpRequestBase request, SquareSettings settings)
        {
            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 orderApi = client.OrdersApi;

            SquareSdk.Models.Order squareOrder = null;

            var squareEvent = GetSquareWebhookEvent(request, settings);

            if (squareEvent != null && squareEvent.IsValid)
            {
                try
                {
                    var referenceId = "";

                    var orderId = GetOrderId(squareEvent);
                    if (!string.IsNullOrWhiteSpace(orderId))
                    {
                        var result = orderApi.BatchRetrieveOrders(
                            new BatchRetrieveOrdersRequest(new List <string>()
                        {
                            orderId
                        }));

                        squareOrder = result.Orders.FirstOrDefault();
                        referenceId = squareOrder.ReferenceId;

                        if (!string.IsNullOrWhiteSpace(referenceId) && Guid.TryParse(referenceId, out var vendrOrderId))
                        {
                            OrderReadOnly vendrOrder = Vendr.Services.OrderService.GetOrder(vendrOrderId);
                            return(vendrOrder.GenerateOrderReference());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Vendr.Log.Error <SquareCheckoutOnetimePaymentProvider>(ex, "Square - GetOrderReference");
                }
            }

            return(base.GetOrderReference(request, settings));
        }
        public override CallbackResult ProcessCallback(OrderReadOnly order, HttpRequestBase request, SquareSettings settings)
        {
            var accessToken = settings.SandboxMode ? settings.SandboxAccessToken : settings.LiveAccessToken;
            var environment = settings.SandboxMode ? SquareSdk.Environment.Sandbox : SquareSdk.Environment.Production;

            var squareEvent = GetSquareWebhookEvent(request, settings);

            if (squareEvent != null && squareEvent.IsValid)
            {
                try
                {
                    var client = new SquareSdk.SquareClient.Builder()
                                 .Environment(environment)
                                 .AccessToken(accessToken)
                                 .Build();

                    var orderApi = client.OrdersApi;

                    var orderId = GetOrderId(squareEvent);

                    var paymentStatus = PaymentStatus.PendingExternalSystem;
                    SquareSdk.Models.Order squareOrder = null;

                    if (!string.IsNullOrWhiteSpace(orderId))
                    {
                        var result = orderApi.BatchRetrieveOrders(
                            new BatchRetrieveOrdersRequest(new List <string>()
                        {
                            orderId
                        }));

                        squareOrder = result.Orders.FirstOrDefault();
                    }

                    if (squareOrder != null)
                    {
                        var orderStatus = squareOrder.State ?? "";

                        switch (orderStatus.ToUpper())
                        {
                        case "COMPLETED":
                        case "AUTHORIZED":
                            paymentStatus = PaymentStatus.Authorized;
                            break;

                        case "CANCELED":
                            paymentStatus = PaymentStatus.Cancelled;
                            break;
                        }
                    }

                    var callbackResult = CallbackResult.Ok(new TransactionInfo
                    {
                        AmountAuthorized = order.TransactionAmount.Value.WithTax,
                        TransactionFee   = 0m,
                        TransactionId    = squareOrder.Id,
                        PaymentStatus    = paymentStatus
                    });

                    return(callbackResult);
                }
                catch (Exception ex)
                {
                    Vendr.Log.Error <SquareCheckoutOnetimePaymentProvider>(ex, "Square - ProcessCallback");
                }
            }

            return(CallbackResult.BadRequest());
        }