GetByNameAndDate() публичный метод

Gets the by name and date.
public GetByNameAndDate ( string batchName, System.DateTime transactionDate, System.TimeSpan batchTimeOffset, List batches = null ) : FinancialBatch
batchName string Name of the batch.
transactionDate System.DateTime The transaction date.
batchTimeOffset System.TimeSpan The batch time offset.
batches List The batches.
Результат FinancialBatch
Пример #1
0
        /// <summary>
        /// Process a refund for a transaction.
        /// </summary>
        /// <param name="transaction">The refund transaction.</param>
        /// <param name="amount">The amount.</param>
        /// <param name="reasonValueId">The reason value identifier.</param>
        /// <param name="summary">The summary.</param>
        /// <param name="process">if set to <c>true</c> [process].</param>
        /// <param name="batchNameSuffix">The batch name suffix.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        public FinancialTransaction ProcessRefund(FinancialTransaction transaction, decimal?amount, int?reasonValueId, string summary, bool process, string batchNameSuffix, out string errorMessage)
        {
            errorMessage = string.Empty;

            // Validate parameters
            if (transaction == null)
            {
                errorMessage = "A valid transaction is required";
                return(null);
            }

            if (transaction.Batch == null)
            {
                errorMessage = "Transaction must belong to a batch";
                return(null);
            }

            if (!amount.HasValue || amount.Value <= 0.0m)
            {
                amount = transaction.TotalAmount;
            }

            if (!amount.HasValue || amount.Value <= 0.0m)
            {
                errorMessage = string.Format("Amount must be greater than {0}", 0.0m.FormatAsCurrency());
                return(null);
            }


            FinancialTransaction refundTransaction = null;

            // If processing the refund through gateway, get the gateway component and process a "Credit" transaction.
            if (process)
            {
                if (transaction.FinancialGateway == null || transaction.TransactionCode.IsNullOrWhiteSpace())
                {
                    errorMessage = "When processing the refund through the Gateway, the transaction must have a valid Gateway and Transaction Code";
                    return(null);
                }

                var gatewayComponent = transaction.FinancialGateway.GetGatewayComponent();
                if (gatewayComponent == null)
                {
                    errorMessage = "Could not get the Gateway component in order to process the refund";
                    return(null);
                }

                refundTransaction = gatewayComponent.Credit(transaction, amount.Value, summary, out errorMessage);
                if (refundTransaction == null)
                {
                    return(null);
                }
            }
            else
            {
                refundTransaction = new FinancialTransaction();
            }

            refundTransaction.AuthorizedPersonAliasId = transaction.AuthorizedPersonAliasId;
            refundTransaction.TransactionDateTime     = RockDateTime.Now;
            refundTransaction.FinancialGatewayId      = transaction.FinancialGatewayId;
            refundTransaction.TransactionTypeValueId  = transaction.TransactionTypeValueId;
            refundTransaction.SourceTypeValueId       = transaction.SourceTypeValueId;
            if (transaction.FinancialPaymentDetail != null)
            {
                refundTransaction.FinancialPaymentDetail = new FinancialPaymentDetail();
                refundTransaction.FinancialPaymentDetail.AccountNumberMasked      = transaction.FinancialPaymentDetail.AccountNumberMasked;
                refundTransaction.FinancialPaymentDetail.BillingLocationId        = transaction.FinancialPaymentDetail.BillingLocationId;
                refundTransaction.FinancialPaymentDetail.CreditCardTypeValueId    = transaction.FinancialPaymentDetail.CreditCardTypeValueId;
                refundTransaction.FinancialPaymentDetail.CurrencyTypeValueId      = transaction.FinancialPaymentDetail.CurrencyTypeValueId;
                refundTransaction.FinancialPaymentDetail.ExpirationMonthEncrypted = transaction.FinancialPaymentDetail.ExpirationMonthEncrypted;
                refundTransaction.FinancialPaymentDetail.ExpirationYearEncrypted  = transaction.FinancialPaymentDetail.ExpirationYearEncrypted;
                refundTransaction.FinancialPaymentDetail.NameOnCardEncrypted      = transaction.FinancialPaymentDetail.NameOnCardEncrypted;
            }

            decimal remainingBalance = amount.Value;

            foreach (var account in transaction.TransactionDetails.Where(a => a.Amount > 0))
            {
                var transactionDetail = new FinancialTransactionDetail();
                transactionDetail.AccountId    = account.AccountId;
                transactionDetail.EntityId     = account.EntityId;
                transactionDetail.EntityTypeId = account.EntityTypeId;
                refundTransaction.TransactionDetails.Add(transactionDetail);

                if (remainingBalance >= account.Amount)
                {
                    transactionDetail.Amount = 0 - account.Amount;
                    remainingBalance        -= account.Amount;
                }
                else
                {
                    transactionDetail.Amount = 0 - remainingBalance;
                    remainingBalance         = 0.0m;
                }

                if (remainingBalance <= 0.0m)
                {
                    break;
                }
            }

            if (remainingBalance > 0 && refundTransaction.TransactionDetails.Any())
            {
                refundTransaction.TransactionDetails.Last().Amount += remainingBalance;
            }

            var rockContext = this.Context as Rock.Data.RockContext;

            var registrationEntityType = EntityTypeCache.Get(typeof(Rock.Model.Registration));

            if (registrationEntityType != null)
            {
                foreach (var transactionDetail in refundTransaction.TransactionDetails
                         .Where(d =>
                                d.EntityTypeId.HasValue &&
                                d.EntityTypeId.Value == registrationEntityType.Id &&
                                d.EntityId.HasValue))
                {
                    var registrationChanges = new History.HistoryChangeList();
                    registrationChanges.AddChange(History.HistoryVerb.Process, History.HistoryChangeType.Record, $"{transactionDetail.Amount.FormatAsCurrency()} Refund");
                    HistoryService.SaveChanges(
                        rockContext,
                        typeof(Registration),
                        Rock.SystemGuid.Category.HISTORY_EVENT_REGISTRATION.AsGuid(),
                        transactionDetail.EntityId.Value,
                        registrationChanges
                        );
                }
            }

            refundTransaction.RefundDetails = new FinancialTransactionRefund();
            refundTransaction.RefundDetails.RefundReasonValueId   = reasonValueId;
            refundTransaction.RefundDetails.RefundReasonSummary   = summary;
            refundTransaction.RefundDetails.OriginalTransactionId = transaction.Id;

            string batchName = transaction.Batch.Name;

            if (batchNameSuffix.IsNotNullOrWhiteSpace() && !batchName.EndsWith(batchNameSuffix))
            {
                batchName += batchNameSuffix;
            }

            // Get the batch
            var      batchService = new FinancialBatchService(rockContext);
            TimeSpan timespan     = new TimeSpan();

            if (transaction.FinancialGateway != null)
            {
                timespan = transaction.FinancialGateway.GetBatchTimeOffset();
            }
            var batch = batchService.GetByNameAndDate(batchName, refundTransaction.TransactionDateTime.Value, timespan);

            // If this is a new Batch, SaveChanges so that we can get the Batch.Id
            if (batch.Id == 0)
            {
                rockContext.SaveChanges();
            }

            refundTransaction.BatchId = batch.Id;
            Add(refundTransaction);
            rockContext.SaveChanges();

            batchService.IncrementControlAmount(batch.Id, refundTransaction.TotalAmount, null);
            rockContext.SaveChanges();

            return(refundTransaction);
        }
Пример #2
0
        /// <summary>
        /// Gets the batch.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="financialGateway">The financial gateway.</param>
        /// <param name="suffix">The suffix.</param>
        /// <returns></returns>
        private FinancialBatch GetBatch( RockContext rockContext, FinancialGateway financialGateway, string suffix )
        {
            if ( suffix == null )
            {
                suffix = string.Empty;
            }

            if ( suffix.Length > 1 )
            {
                suffix = char.ToUpper( suffix[0] ) + suffix.Substring( 1 );
            }

            var batchService = new FinancialBatchService( rockContext );
            var batchName = string.Format( "{0}: {1}", BATCH_PREFIX, suffix );
            return batchService.GetByNameAndDate( batchName, RockDateTime.Now, financialGateway.GetBatchTimeOffset() );
        }
Пример #3
0
        /// <summary>
        /// Handles the SaveClick event of the mdRefund control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void mdRefund_SaveClick( object sender, EventArgs e )
        {
            decimal refundAmount = tbRefundAmount.Text.AsDecimal();
            if ( refundAmount > 0.0m )
            {
                int? txnId = hfTransactionId.Value.AsIntegerOrNull();
                if ( txnId.HasValue )
                {
                    using ( var rockContext = new RockContext() )
                    {
                        var txnService = new FinancialTransactionService( rockContext );
                        var txn = txnService.Get( txnId.Value );
                        if ( txn != null && txn.Batch != null )
                        {
                            FinancialTransaction refundTxn = null;

                            if ( !string.IsNullOrWhiteSpace( txn.TransactionCode ) && txn.FinancialGateway != null &&
                                cbProcess.Visible && cbProcess.Checked )
                            {
                                var gateway = txn.FinancialGateway.GetGatewayComponent();
                                if ( gateway != null )
                                {
                                    string errorMessage = string.Empty;
                                    refundTxn = gateway.Credit( txn, refundAmount, tbRefundSummary.Text, out errorMessage );
                                    if ( refundTxn == null )
                                    {
                                        nbRefundError.Title = "Refund Error";
                                        nbRefundError.Text = string.Format( "<p>{0}</p>", errorMessage );
                                        nbRefundError.Visible = true;
                                        return;
                                    }
                                }
                                else
                                {
                                    nbRefundError.Title = "Gateway Error";
                                    nbRefundError.Text = "<p>Transaction has a valid gateway, but we could not use it.</p>";
                                    nbRefundError.Visible = true;
                                    return;
                                }
                            }
                            else
                            {
                                refundTxn = new FinancialTransaction();
                            }

                            refundTxn.AuthorizedPersonAliasId = txn.AuthorizedPersonAliasId;
                            refundTxn.TransactionDateTime = RockDateTime.Now;
                            refundTxn.FinancialGatewayId = txn.FinancialGatewayId;
                            refundTxn.TransactionTypeValueId = txn.TransactionTypeValueId;
                            if ( txn.FinancialPaymentDetail != null )
                            {
                                refundTxn.FinancialPaymentDetail = new FinancialPaymentDetail();
                                refundTxn.FinancialPaymentDetail.AccountNumberMasked = txn.FinancialPaymentDetail.AccountNumberMasked;
                                refundTxn.FinancialPaymentDetail.BillingLocationId = txn.FinancialPaymentDetail.BillingLocationId;
                                refundTxn.FinancialPaymentDetail.CreditCardTypeValueId = txn.FinancialPaymentDetail.CreditCardTypeValueId;
                                refundTxn.FinancialPaymentDetail.CurrencyTypeValueId = txn.FinancialPaymentDetail.CurrencyTypeValueId;
                                refundTxn.FinancialPaymentDetail.ExpirationMonthEncrypted = txn.FinancialPaymentDetail.ExpirationMonthEncrypted;
                                refundTxn.FinancialPaymentDetail.ExpirationYearEncrypted = txn.FinancialPaymentDetail.ExpirationYearEncrypted;
                                refundTxn.FinancialPaymentDetail.NameOnCardEncrypted = txn.FinancialPaymentDetail.NameOnCardEncrypted;
                            }

                            decimal remBalance = refundAmount;
                            foreach ( var account in txn.TransactionDetails.Where( a => a.Amount > 0 ) )
                            {
                                var transactionDetail = new FinancialTransactionDetail();
                                transactionDetail.AccountId = account.AccountId;
                                transactionDetail.EntityId = account.EntityId;
                                transactionDetail.EntityTypeId = account.EntityTypeId;
                                refundTxn.TransactionDetails.Add( transactionDetail );

                                if ( remBalance >= account.Amount )
                                {
                                    transactionDetail.Amount = 0 - account.Amount;
                                    remBalance -= account.Amount;
                                }
                                else
                                {
                                    transactionDetail.Amount = 0 - remBalance;
                                    remBalance = 0.0m;
                                }

                                if ( remBalance <= 0.0m )
                                {
                                    break;
                                }
                            }

                            if ( remBalance > 0 && refundTxn.TransactionDetails.Any() )
                            {
                                refundTxn.TransactionDetails.Last().Amount += remBalance;
                            }

                            var registrationEntityType = EntityTypeCache.Read( typeof( Rock.Model.Registration ) );
                            if ( registrationEntityType != null )
                            {
                                foreach ( var transactionDetail in refundTxn.TransactionDetails
                                    .Where( d =>
                                        d.EntityTypeId.HasValue &&
                                        d.EntityTypeId.Value == registrationEntityType.Id &&
                                        d.EntityId.HasValue ) )
                                {
                                    var registrationChanges = new List<string>();
                                    registrationChanges.Add( string.Format( "Processed refund for {0}.", transactionDetail.Amount.FormatAsCurrency() ) );
                                    HistoryService.SaveChanges(
                                        rockContext,
                                        typeof( Registration ),
                                        Rock.SystemGuid.Category.HISTORY_EVENT_REGISTRATION.AsGuid(),
                                        transactionDetail.EntityId.Value,
                                        registrationChanges
                                    );
                                }
                            }

                            refundTxn.RefundDetails = new FinancialTransactionRefund();
                            refundTxn.RefundDetails.RefundReasonValueId = ddlRefundReason.SelectedValueAsId();
                            refundTxn.RefundDetails.RefundReasonSummary = tbRefundSummary.Text;
                            refundTxn.RefundDetails.OriginalTransactionId = txn.Id;

                            string batchName = txn.Batch.Name;
                            string suffix = GetAttributeValue( "RefundBatchNameSuffix" );
                            if ( !string.IsNullOrWhiteSpace( suffix ) && !batchName.EndsWith( suffix ) )
                            {
                                batchName += suffix;
                            }

                            // Get the batch
                            var batchService = new FinancialBatchService( rockContext );
                            TimeSpan timespan = new TimeSpan();
                            if ( txn.FinancialGateway != null )
                            {
                                timespan = txn.FinancialGateway.GetBatchTimeOffset();
                            }
                            var batch = batchService.GetByNameAndDate( batchName, refundTxn.TransactionDateTime.Value, timespan );
                            decimal controlAmount = batch.ControlAmount + refundTxn.TotalAmount;
                            batch.ControlAmount = controlAmount;

                            refundTxn.BatchId = batch.Id;
                            batch.Transactions.Add( refundTxn );

                            rockContext.SaveChanges();

                            var updatedTxn = GetTransaction( txn.Id );
                            if ( updatedTxn != null )
                            {
                                updatedTxn.LoadAttributes( rockContext );
                                updatedTxn.FinancialPaymentDetail.LoadAttributes(rockContext);
                                ShowReadOnlyDetails( updatedTxn );
                            }
                        }
                        else
                        {
                            nbRefundError.Title = "Transaction Error";
                            nbRefundError.Text = "<p>Existing transaction does not hava a valid batch.</p>";
                            nbRefundError.Visible = true;
                            return;
                        }

                    }
                }
            }

            HideDialog();
        }