public virtual HttpResponseMessage ProcessPayment([FromBody] AutomatedPaymentArgs automatedPaymentArgs, [FromUri] bool enableDuplicateChecking = true, [FromUri] bool enableScheduleAdherenceProtection = true)
        {
            var errorMessage = string.Empty;

            var rockContext = Service.Context as RockContext;
            var automatedPaymentProcessor = new AutomatedPaymentProcessor(GetPersonAliasId(rockContext), automatedPaymentArgs, rockContext, enableDuplicateChecking, enableScheduleAdherenceProtection);

            if (!automatedPaymentProcessor.AreArgsValid(out errorMessage))
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            if (automatedPaymentProcessor.IsRepeatCharge(out errorMessage) ||
                !automatedPaymentProcessor.IsAccordingToSchedule(out errorMessage))
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.Conflict, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            var transaction      = automatedPaymentProcessor.ProcessCharge(out errorMessage);
            var gatewayException = automatedPaymentProcessor.GetMostRecentException();

            if (!string.IsNullOrEmpty(errorMessage))
            {
                if (gatewayException != null)
                {
                    throw gatewayException;
                }

                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            if (transaction == null)
            {
                if (gatewayException != null)
                {
                    throw gatewayException;
                }

                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No transaction was created");
                throw new HttpResponseException(errorResponse);
            }

            var response = ControllerContext.Request.CreateResponse(HttpStatusCode.Created, transaction.Id);

            return(response);
        }
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Execute(IJobExecutionContext context)
        {
            var rockContext        = new RockContext();
            var transactionService = new FinancialTransactionService(rockContext);
            var futureTransactions = transactionService.GetFutureTransactions().Where(ft => ft.FutureProcessingDateTime <= RockDateTime.Now).ToList();
            var errors             = new List <string>();
            var successCount       = 0;

            foreach (var futureTransaction in futureTransactions)
            {
                var automatedPaymentProcessor = new AutomatedPaymentProcessor(futureTransaction, rockContext);
                automatedPaymentProcessor.ProcessCharge(out var errorMessage);

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    errors.Add(errorMessage);
                }
                else
                {
                    successCount++;
                }
            }

            context.Result = string.Format("{0} future transactions charged", successCount);

            if (errors.Any())
            {
                var sb = new StringBuilder();
                sb.AppendLine();
                sb.AppendLine(string.Format("{0} Errors: ", errors.Count()));
                errors.ForEach(e => sb.AppendLine(e));

                var errorMessage = sb.ToString();
                context.Result += errorMessage;

                var exception = new Exception(errorMessage);
                var context2  = HttpContext.Current;
                ExceptionLogService.LogException(exception, context2);

                throw exception;
            }
        }
        public virtual System.Net.Http.HttpResponseMessage ProcessPayment(int scheduledTransactionId, [FromUri] bool enableDuplicateChecking = true, [FromUri] bool enableScheduleAdherenceProtection = true, [FromUri] string idempotencyKey = null)
        {
            var financialScheduledTransactionService = Service as FinancialScheduledTransactionService;
            var financialScheduledTransaction        = financialScheduledTransactionService.Queryable()
                                                       .AsNoTracking()
                                                       .Include(s => s.ScheduledTransactionDetails)
                                                       .FirstOrDefault(t => t.Id == scheduledTransactionId);

            if (financialScheduledTransaction == null)
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "The scheduledTransactionId did not resolve");
                throw new HttpResponseException(errorResponse);
            }

            if (!financialScheduledTransaction.FinancialGatewayId.HasValue)
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The scheduled transaction does not have an assigned gateway ID");
                throw new HttpResponseException(errorResponse);
            }

            var details = financialScheduledTransaction.ScheduledTransactionDetails.Select(d =>
                                                                                           new AutomatedPaymentArgs.AutomatedPaymentDetailArgs
            {
                AccountId = d.AccountId,
                Amount    = d.Amount
            }
                                                                                           ).ToList();

            var automatedPaymentArgs = new AutomatedPaymentArgs
            {
                ScheduledTransactionId  = scheduledTransactionId,
                AuthorizedPersonAliasId = financialScheduledTransaction.AuthorizedPersonAliasId,
                AutomatedGatewayId      = financialScheduledTransaction.FinancialGatewayId.Value,
                AutomatedPaymentDetails = details,
                IdempotencyKey          = idempotencyKey
            };

            var errorMessage = string.Empty;
            var rockContext  = Service.Context as RockContext;

            var automatedPaymentProcessor = new AutomatedPaymentProcessor(GetPersonAliasId(rockContext), automatedPaymentArgs, rockContext, enableDuplicateChecking, enableScheduleAdherenceProtection);

            if (!automatedPaymentProcessor.AreArgsValid(out errorMessage))
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            if (automatedPaymentProcessor.IsRepeatCharge(out errorMessage) ||
                !automatedPaymentProcessor.IsAccordingToSchedule(out errorMessage))
            {
                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.Conflict, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            var transaction      = automatedPaymentProcessor.ProcessCharge(out errorMessage);
            var gatewayException = automatedPaymentProcessor.GetMostRecentException();

            if (!string.IsNullOrEmpty(errorMessage))
            {
                if (gatewayException != null)
                {
                    throw gatewayException;
                }

                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, errorMessage);
                throw new HttpResponseException(errorResponse);
            }

            if (transaction == null)
            {
                if (gatewayException != null)
                {
                    throw gatewayException;
                }

                var errorResponse = ControllerContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No transaction was created");
                throw new HttpResponseException(errorResponse);
            }

            var response = ControllerContext.Request.CreateResponse(HttpStatusCode.Created, transaction.Id);

            return(response);
        }
Exemple #4
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();
            var continueOnError = GetAttributeValue(action, "ContinueOnError").AsBoolean();

            // Get the gateway
            var gateway = GetEntityFromAttributeValue <FinancialGateway>(action, "FinancialGateway", true, rockContext);

            if (gateway == null)
            {
                errorMessages.Add("The gateway is not valid");
            }

            // Get the person
            var person = GetPersonFromAttributeValue(action, "Person", true, rockContext);

            if (person == null || !person.PrimaryAliasId.HasValue)
            {
                errorMessages.Add("A valid person was not provided.");
            }

            // Get the amount
            var amount = GetAttributeValue(action, "Amount", true).AsDecimalOrNull();

            if (!amount.HasValue || amount.Value < 1m)
            {
                errorMessages.Add("A valid amount was not provided.");
            }

            // Get the account
            var account = GetEntityFromAttributeValue <FinancialAccount>(action, "Account", true, rockContext);

            if (account == null)
            {
                errorMessages.Add("The account is not valid");
            }

            if (errorMessages.Any())
            {
                return(HandleExit(action, errorMessages, continueOnError));
            }

            var detailArgs = new AutomatedPaymentArgs.AutomatedPaymentDetailArgs
            {
                AccountId = account.Id,
                Amount    = amount.Value
            };

            var automatedPaymentArgs = new AutomatedPaymentArgs
            {
                AuthorizedPersonAliasId = person.PrimaryAliasId.Value,
                AutomatedGatewayId      = gateway.Id,
                AutomatedPaymentDetails = new List <AutomatedPaymentArgs.AutomatedPaymentDetailArgs> {
                    detailArgs
                }
            };

            var enableDuplicateChecking   = GetAttributeValue(action, "EnableDuplicateChecking").AsBooleanOrNull() ?? true;
            var automatedPaymentProcessor = new AutomatedPaymentProcessor(null, automatedPaymentArgs, rockContext, enableDuplicateChecking, true);
            var transaction = automatedPaymentProcessor.ProcessCharge(out var errorMessage);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                errorMessages.Add(errorMessage);
                return(HandleExit(action, errorMessages, continueOnError));
            }

            if (transaction == null)
            {
                errorMessages.Add("No transaction was produced");
                return(HandleExit(action, errorMessages, continueOnError));
            }

            action.AddLogEntry(string.Format(
                                   "{0} made a payment of {1} to {2} resulting in transaction ID {3}",
                                   person.FullName,
                                   amount.FormatAsCurrency(),
                                   account.PublicName,
                                   transaction.Id));

            var attribute = SetWorkflowAttributeValue(action, "ResultAttribute", transaction.Id);

            if (attribute != null)
            {
                action.AddLogEntry(string.Format("Set '{0}' attribute to '{1}'.", attribute.Name, transaction.Id));
            }

            return(HandleExit(action, errorMessages, continueOnError));
        }