/// <summary>
 /// Prepares the response for creating an item for cart (i.e. pending payment)
 /// </summary>
 /// <param name="message">For USSD channels, this message is displayed to the user before the payment flow kicks in</param>
 /// <param name="cartData">Must be valid and not null</param>
 /// <returns></returns>
 public ProgrammableServiceResponse AddToCart(string message, ProgrammableServicesResponseCartData cartData)
 {
     if (cartData == null)
     {
         throw new ArgumentNullException(nameof(cartData));
     }
     return(ProgrammableServiceResponse.AddToCartForCheckout(message, cartData, Request.IsUssd()));
 }
示例#2
0
        public void HandleRequest(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                //read payload, convert to request object
                //grab response from _programmableService instance, convert to JSON and send back
                var requestString = await context.Request.ReadRequestStreamAsStringAsync();

                ProgrammableServiceRequest request = null;
                ProgrammableServiceResponse response;
                try
                {
                    request = JsonConvert.DeserializeObject <ProgrammableServiceRequest>(requestString);
                }
                catch (Exception e)
                {
                    // we don't know what happened oooo
                }

                if (request == null)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    response = new ProgrammableServiceResponse
                    {
                        Message = "Something bad happened",
                        Type    = ProgrammableServiceActionType.Release.ToString("G"),
                        Label   = "Something bad happened"
                    };
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.OK;

                    response = await _programmableService.ExecuteInteraction(request, _controller, _action);
                }


                var responseJson = JsonConvert.SerializeObject(response, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Formatting       = Formatting.Indented
                });


                context.Response.ContentLength = responseJson.Length;
                context.Response.ContentType   = "application/json";


                await context.Response.WriteAsync(responseJson, Encoding.UTF8);
            });
        }
        /// <summary>
        /// Render <paramref name="message"/>
        /// </summary>
        /// <param name="message">Mainly for USSD screens</param>
        /// <param name="action">The next action to be invoked</param>
        /// <param name="controller">The controller which hosts the next action</param>
        /// <param name="label">To be displayed on rich clients</param>
        /// <param name="dataType">Must be valid and not null</param>
        /// <param name="dataItems">List of choices to be presented to the user</param>
        /// <param name="cartData">Must be valid and not null</param>
        /// <param name="fieldType"></param>
        /// <returns></returns>
        public ProgrammableServiceResponse Render(string message, List <ProgrammableServicesResponseData> dataItems = null, ProgrammableServicesResponseCartData cartData = null, string label = null, string dataType = null, string fieldType = "text", string fieldName = "", bool persistAsFavoriteStep = false, string action = null,
                                                  string controller = null)
        {
            if (message == null)
            {
                message = string.Empty;
            }

            string route = null;

            if (action != null)
            {
                route = Route(action, controller);
            }

            return(ProgrammableServiceResponse.Render(message, dataItems, cartData, label, dataType, fieldType, fieldName, persistAsFavoriteStep, route));
        }
 /// <summary>
 /// Redirect to specified <paramref name="controller"/>'s <paramref name="action"/>.
 /// If <paramref name="controller"/> is not specified this controller is used.
 /// </summary>
 /// <param name="action"></param>
 /// <param name="controller"></param>
 /// <returns></returns>
 public ProgrammableServiceResponse Redirect(string action, string controller = null)
 {
     return(ProgrammableServiceResponse.Redirect(Route(action, controller)));
 }