Beispiel #1
0
        internal static IDialog <CardToCardTransfer> MakeRoot()
        {
            return(Chain.From(() => FormDialog.FromForm(CardToCardFormBuilder.MakeForm))
                   .Do(async(context, order) =>
            {
                try
                {
                    var completed = await order;

                    var alfaService = new AlfabankService();
                    string expDate = completed.ValidThruYear.ToString() + ((int)completed.ValidThruMonth).ToString("D2");
                    string confirmationUrl = await alfaService.TransferMoney(completed.SourceCardNumber, expDate, completed.CVV, completed.DestinationCardNumber, completed.Amount);

                    await context.PostAsync($"Осталось только подтвердить платеж. Перейдите по адресу {confirmationUrl}");
                }
                catch (FormCanceledException <CardToCardTransfer> e)
                {
                    string reply;
                    if (e.InnerException == null)
                    {
                        reply = $"Вы прервали операцию, попробуем позже!";
                    }
                    else
                    {
                        reply = "Извините, произошла ошибка. Попробуйте позже.";
                    }
                    await context.PostAsync(reply);
                }
            }));
        }
        public static IForm <CardToCardTransfer> MakeForm()
        {
            FormBuilder <CardToCardTransfer> _order = new FormBuilder <CardToCardTransfer>();

            ValidateAsyncDelegate <CardToCardTransfer> validateCard =
                async(state, value) =>
            {
                var    cardNumber = value as string;
                string errorMessage;

                ValidateResult result = new ValidateResult();
                result.IsValid  = CardValidator.IsCardValid(cardNumber, out errorMessage);
                result.Feedback = errorMessage;

                return(result);
            };

            return(_order
                   .Message("Добро пожаловать в сервис перевода денег с карты на карту!")
                   .Field(nameof(CardToCardTransfer.SourceCardNumber), null, validateCard)
                   .Field(nameof(CardToCardTransfer.Fee), state => false)
                   .Field(nameof(CardToCardTransfer.ValidThruMonth))
                   .Field(nameof(CardToCardTransfer.ValidThruYear))
                   .Field(nameof(CardToCardTransfer.DestinationCardNumber), null, validateCard)
                   .Field(nameof(CardToCardTransfer.CVV))
                   .Field(nameof(CardToCardTransfer.Amount), null,
                          async(state, value) =>
            {
                int amount = int.Parse(value.ToString());

                var alfabankService = new AlfabankService();
                string auth = await alfabankService.AuthorizePartner();
                state.Fee = (double)await alfabankService.GetCommission(auth, state.SourceCardNumber, state.DestinationCardNumber, amount);

                ValidateResult result = new ValidateResult();
                result.IsValid = true;
                return result;
            })
                   .Confirm("Вы хотите перевести {Amount} рублей с карты {SourceCardNumber} на карту {DestinationCardNumber}? Комиссия составит {Fee} рублей. (y/n)")
                   .OnCompletionAsync(async(context, cardTocardTransfer) =>
            {
                Debug.WriteLine("{0}", cardTocardTransfer);
            })
                   .Build());
        }