Information about a credit card swipe payment to be processed by a financial gateway. A swipe payment is used when the card is present (physically used)
Inheritance: PaymentInfo
Example #1
0
        private bool ValidateCard(FinancialGateway financialGateway, PaymentInfo paymentInfo, out string errorMessage)
        {
            string cardNumber = string.Empty;

            CreditCardPaymentInfo ccPayment = paymentInfo as CreditCardPaymentInfo;

            if (ccPayment != null)
            {
                if (ccPayment.Code == "911")
                {
                    errorMessage = "Error processing Credit Card!";
                    return(false);
                }

                cardNumber = ccPayment.Number;
            }

            SwipePaymentInfo swipePayment = paymentInfo as SwipePaymentInfo;

            if (swipePayment != null)
            {
                cardNumber = swipePayment.Number;
            }

            if (!string.IsNullOrWhiteSpace(cardNumber))
            {
                var declinedNumers = GetAttributeValue(financialGateway, "DeclinedCardNumbers");
                if (!string.IsNullOrWhiteSpace(declinedNumers))
                {
                    if (declinedNumers.SplitDelimitedValues().Any(n => cardNumber.EndsWith(n)))
                    {
                        errorMessage = "Error processing Credit Card!";
                        return(false);
                    }
                }
            }

            errorMessage = string.Empty;
            return(true);
        }
Example #2
0
        private bool ValidateCard(FinancialGateway financialGateway, PaymentInfo paymentInfo, out string errorMessage)
        {
            string cardNumber         = string.Empty;
            var    declinedCVV        = this.GetAttributeValue(financialGateway, AttributeKey.DeclinedCVV);
            int    maxExpirationYears = this.GetAttributeValue(financialGateway, AttributeKey.MaxExpirationYears).AsIntegerOrNull() ?? 10;

            CreditCardPaymentInfo ccPayment = paymentInfo as CreditCardPaymentInfo;

            if (ccPayment != null)
            {
                if (declinedCVV.IsNotNullOrWhiteSpace() && ccPayment.Code == declinedCVV)
                {
                    errorMessage = "Declined CVV";
                    return(false);
                }

                cardNumber = ccPayment.Number;

                if (ccPayment.ExpirationDate < RockDateTime.Now.Date)
                {
                    errorMessage = "Card Expired";
                    return(false);
                }

                if (ccPayment.ExpirationDate > RockDateTime.Now.AddYears(maxExpirationYears))
                {
                    errorMessage = "Invalid Card Expiration";
                    return(false);
                }

                if (ccPayment.Number.IsNullOrWhiteSpace())
                {
                    errorMessage = "Card number is required.";
                    return(false);
                }

                if (ccPayment.Code.IsNullOrWhiteSpace())
                {
                    errorMessage = "CVV is required.";
                    return(false);
                }
            }

            SwipePaymentInfo swipePayment = paymentInfo as SwipePaymentInfo;

            if (swipePayment != null)
            {
                cardNumber = swipePayment.Number;
            }

            if (!string.IsNullOrWhiteSpace(cardNumber))
            {
                var declinedNumbers = GetAttributeValue(financialGateway, AttributeKey.DeclinedCardNumbers);
                if (!string.IsNullOrWhiteSpace(declinedNumbers))
                {
                    if (declinedNumbers.SplitDelimitedValues().Any(n => cardNumber.EndsWith(n)))
                    {
                        errorMessage = "Declined Card";
                        return(false);
                    }
                }
            }

            errorMessage = string.Empty;
            return(true);
        }
        //
        // Swipe Panel Events
        //
        private void ProcessSwipe( string swipeData )
        {
            try
            {
                using ( var rockContext = new RockContext() )
                {
                    // create swipe object
                    SwipePaymentInfo swipeInfo = new SwipePaymentInfo( swipeData );
                    swipeInfo.Amount = this.Amounts.Sum( a => a.Value );

                    // if not anonymous then add contact info to the gateway transaction
                    if ( this.AnonymousGiverPersonAliasId != this.SelectedGivingUnit.PersonAliasId )
                    {
                        var giver = new PersonAliasService( rockContext ).Queryable( "Person, Person.PhoneNumbers" ).Where( p => p.Id == this.SelectedGivingUnit.PersonAliasId ).FirstOrDefault();
                        swipeInfo.FirstName = giver.Person.NickName;
                        swipeInfo.LastName = giver.Person.LastName;

                        if ( giver.Person.PhoneNumbers != null )
                        {
                            Guid homePhoneValueGuid = new Guid( Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_HOME );
                            var homephone = giver.Person.PhoneNumbers.Where( p => p.NumberTypeValue.Guid == homePhoneValueGuid ).FirstOrDefault();
                            if ( homephone != null )
                            {
                                swipeInfo.Phone = homephone.NumberFormatted;
                            }
                        }

                        var homeLocation = giver.Person.GetHomeLocation();

                        if ( homeLocation != null )
                        {
                            swipeInfo.Street1 = homeLocation.Street1;

                            if ( !string.IsNullOrWhiteSpace( homeLocation.Street2 ) )
                            {
                                swipeInfo.Street2 = homeLocation.Street2;
                            }

                            swipeInfo.City = homeLocation.City;
                            swipeInfo.State = homeLocation.State;
                            swipeInfo.PostalCode = homeLocation.PostalCode;
                        }

                    }

                    // add comment to the transation
                    swipeInfo.Comment1 = GetAttributeValue( "PaymentComment" );

                    // get gateway
                    FinancialGateway financialGateway = null;
                    GatewayComponent gateway = null;
                    Guid? gatewayGuid = GetAttributeValue( "CreditCardGateway" ).AsGuidOrNull();
                    if ( gatewayGuid.HasValue )
                    {
                        financialGateway = new FinancialGatewayService( rockContext ).Get( gatewayGuid.Value );
                        if ( financialGateway != null )
                        {
                            financialGateway.LoadAttributes( rockContext );
                        }
                        gateway = financialGateway.GetGatewayComponent();
                    }

                    if ( gateway != null )
                    {

                        string errorMessage = string.Empty;
                        var transaction = gateway.Charge( financialGateway, swipeInfo, out errorMessage );

                        if ( transaction != null )
                        {

                            var txnChanges = new List<string>();
                            txnChanges.Add( "Created Transaction (from kiosk)" );

                            _transactionCode = transaction.TransactionCode;
                            History.EvaluateChange( txnChanges, "Transaction Code", string.Empty, transaction.TransactionCode );

                            var personName = new PersonAliasService( rockContext )
                                .Queryable().AsNoTracking()
                                .Where( a => a.Id == this.SelectedGivingUnit.PersonAliasId )
                                .Select( a => a.Person.NickName + " " + a.Person.LastName )
                                .FirstOrDefault();

                            transaction.AuthorizedPersonAliasId = this.SelectedGivingUnit.PersonAliasId;
                            History.EvaluateChange( txnChanges, "Person", string.Empty, personName );

                            transaction.TransactionDateTime = RockDateTime.Now;
                            History.EvaluateChange( txnChanges, "Date/Time", null, transaction.TransactionDateTime );

                            transaction.FinancialGatewayId = financialGateway.Id;
                            History.EvaluateChange( txnChanges, "Gateway", string.Empty, financialGateway.Name );

                            var txnType = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION ) );
                            transaction.TransactionTypeValueId = txnType.Id;
                            History.EvaluateChange( txnChanges, "Type", string.Empty, txnType.Value );

                            transaction.Summary = swipeInfo.Comment1;
                            History.EvaluateChange( txnChanges, "Transaction Code", string.Empty, transaction.Summary );

                            if ( transaction.FinancialPaymentDetail == null )
                            {
                                transaction.FinancialPaymentDetail = new FinancialPaymentDetail();
                            }
                            transaction.FinancialPaymentDetail.SetFromPaymentInfo( swipeInfo, gateway, rockContext, txnChanges );

                            Guid sourceGuid = Guid.Empty;
                            if ( Guid.TryParse( GetAttributeValue( "Source" ), out sourceGuid ) )
                            {
                                var source = DefinedValueCache.Read( sourceGuid );
                                if ( source != null )
                                {
                                    transaction.SourceTypeValueId = source.Id;
                                    History.EvaluateChange( txnChanges, "Source", string.Empty, source.Value );
                                }
                            }

                            foreach ( var accountAmount in this.Amounts.Where( a => a.Value > 0 ) )
                            {
                                var transactionDetail = new FinancialTransactionDetail();
                                transactionDetail.Amount = accountAmount.Value;
                                transactionDetail.AccountId = accountAmount.Key;
                                transaction.TransactionDetails.Add( transactionDetail );
                                var account = new FinancialAccountService( rockContext ).Get( accountAmount.Key );
                                if ( account != null )
                                {
                                    History.EvaluateChange( txnChanges, account.Name, 0.0M.FormatAsCurrency(), transactionDetail.Amount.FormatAsCurrency() );
                                }
                            }

                            var batchService = new FinancialBatchService( rockContext );

                            // Get the batch
                            var batch = batchService.Get(
                                GetAttributeValue( "BatchNamePrefix" ),
                                swipeInfo.CurrencyTypeValue,
                                swipeInfo.CreditCardTypeValue,
                                transaction.TransactionDateTime.Value,
                                financialGateway.GetBatchTimeOffset() );

                            var batchChanges = new List<string>();

                            if ( batch.Id == 0 )
                            {
                                batchChanges.Add( "Generated the batch" );
                                History.EvaluateChange( batchChanges, "Batch Name", string.Empty, batch.Name );
                                History.EvaluateChange( batchChanges, "Status", null, batch.Status );
                                History.EvaluateChange( batchChanges, "Start Date/Time", null, batch.BatchStartDateTime );
                                History.EvaluateChange( batchChanges, "End Date/Time", null, batch.BatchEndDateTime );
                            }

                            decimal newControlAmount = batch.ControlAmount + transaction.TotalAmount;
                            History.EvaluateChange( batchChanges, "Control Amount", batch.ControlAmount.FormatAsCurrency(), newControlAmount.FormatAsCurrency() );
                            batch.ControlAmount = newControlAmount;

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

                            rockContext.WrapTransaction( () =>
                            {

                                rockContext.SaveChanges();
                                HistoryService.SaveChanges(
                                    rockContext,
                                    typeof( FinancialBatch ),
                                    Rock.SystemGuid.Category.HISTORY_FINANCIAL_BATCH.AsGuid(),
                                    batch.Id,
                                    batchChanges
                                );

                                HistoryService.SaveChanges(
                                    rockContext,
                                    typeof( FinancialBatch ),
                                    Rock.SystemGuid.Category.HISTORY_FINANCIAL_TRANSACTION.AsGuid(),
                                    batch.Id,
                                    txnChanges,
                                    personName,
                                    typeof( FinancialTransaction ),
                                    transaction.Id
                                );
                            } );

                            // send receipt in one is configured and not giving anonymously
                            if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "ReceiptEmail" ) ) && ( this.AnonymousGiverPersonAliasId != this.SelectedGivingUnit.PersonAliasId ) )
                            {
                                _receiptSent = true;

                                SendReceipt();
                            }

                            HidePanels();
                            ShowReceiptPanel();
                        }
                        else
                        {
                            lSwipeErrors.Text = String.Format( "<div class='alert alert-danger'>An error occurred while process this transaction. Message: {0}</div>", errorMessage );
                        }

                    }
                    else
                    {
                        lSwipeErrors.Text = "<div class='alert alert-danger'>Invalid gateway provided. Please provide a gateway. Transaction not processed.</div>";
                    }
                }
            }
            catch ( Exception ex )
            {
                lSwipeErrors.Text = String.Format( "<div class='alert alert-danger'>An error occurred while process this transaction. Message: {0}</div>", ex.Message );
            }
        }