/// <summary> /// Handle a payment from a REST endpoint or other automated means. This payment can only be made with a saved account. /// </summary> /// <param name="financialGateway">The financial gateway.</param> /// <param name="paymentInfo">The payment info.</param> /// <param name="errorMessage">The error message.</param> /// <param name="metadata">Optional. Metadata key value pairs to send to the gateway</param> /// <returns></returns> public Payment AutomatedCharge(FinancialGateway financialGateway, ReferencePaymentInfo paymentInfo, out string errorMessage, Dictionary <string, string> metadata = null) { MostRecentException = null; errorMessage = string.Empty; var transaction = Charge(financialGateway, paymentInfo, out errorMessage); if (!string.IsNullOrEmpty(errorMessage)) { MostRecentException = new Exception(errorMessage); return(null); } if (transaction == null) { errorMessage = "No error was indicated but the transaction was null"; MostRecentException = new Exception(errorMessage); return(null); } return(new Payment { TransactionCode = transaction.TransactionCode, NameOnCard = $"{paymentInfo.FirstName} {paymentInfo.LastName}", Amount = paymentInfo.Amount }); }
/// <summary> /// Safely load entities that have not yet been assigned a non-null value based on the arguments. /// </summary> private void LoadEntities() { if (_automatedPaymentArgs.ScheduledTransactionId.HasValue && _financialScheduledTransaction == null) { _financialScheduledTransaction = _financialScheduledTransactionService.Queryable() .AsNoTracking() .Include(s => s.TransactionFrequencyValue) .FirstOrDefault(s => s.Id == _automatedPaymentArgs.ScheduledTransactionId.Value); } if (_authorizedPerson == null) { _authorizedPerson = _personAliasService.GetPersonNoTracking(_automatedPaymentArgs.AuthorizedPersonAliasId); } if (_financialGateway == null) { _financialGateway = _financialGatewayService.GetNoTracking(_automatedPaymentArgs.AutomatedGatewayId); } if (_financialGateway != null && _automatedGatewayComponent == null) { _automatedGatewayComponent = _financialGateway.GetGatewayComponent(); } if (_financialAccounts == null) { var accountIds = _automatedPaymentArgs.AutomatedPaymentDetails.Select(d => d.AccountId).ToList(); _financialAccounts = _financialAccountService.GetByIds(accountIds).AsNoTracking().ToDictionary(fa => fa.Id, fa => fa); } if (_authorizedPerson != null && _financialPersonSavedAccount == null && _financialGateway != null) { // Pick the correct saved account based on args or default for the user var financialGatewayId = _financialGateway.Id; var savedAccounts = _financialPersonSavedAccountService .GetByPersonId(_authorizedPerson.Id) .AsNoTracking() .Where(sa => sa.FinancialGatewayId == financialGatewayId) .Include(sa => sa.FinancialPaymentDetail) .OrderByDescending(sa => sa.CreatedDateTime ?? DateTime.MinValue) .ToList(); if (_automatedPaymentArgs.FinancialPersonSavedAccountId.HasValue) { // If there is an indicated saved account to use, don't assume any other saved account even with a schedule var savedAccountId = _automatedPaymentArgs.FinancialPersonSavedAccountId.Value; _financialPersonSavedAccount = savedAccounts.FirstOrDefault(sa => sa.Id == savedAccountId); } else { // If there is a schedule and no indicated saved account to use, try to use payment info associated with the schedule if (_financialScheduledTransaction != null) { _financialPersonSavedAccount = // sa.ReferenceNumber == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.ReferenceNumber) && sa.ReferenceNumber == _financialScheduledTransaction.TransactionCode) ?? // sa.GatewayPersonIdentifier == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.GatewayPersonIdentifier) && sa.GatewayPersonIdentifier == _financialScheduledTransaction.TransactionCode) ?? // sa.FinancialPaymentDetailId == fst.FinancialPaymentDetailId savedAccounts.FirstOrDefault(sa => sa.FinancialPaymentDetailId.HasValue && sa.FinancialPaymentDetailId == _financialScheduledTransaction.FinancialPaymentDetailId) ?? // sa.TransactionCode == fst.TransactionCode savedAccounts.FirstOrDefault(sa => !string.IsNullOrEmpty(sa.TransactionCode) && sa.TransactionCode == _financialScheduledTransaction.TransactionCode); } if (_financialPersonSavedAccount == null) { // Use the default or first if no default _financialPersonSavedAccount = savedAccounts.FirstOrDefault(sa => sa.IsDefault) ?? savedAccounts.FirstOrDefault(); } } } if (_financialPersonSavedAccount != null && _referencePaymentInfo == null) { _referencePaymentInfo = _financialPersonSavedAccount.GetReferencePayment(); } if (_transactionType == null) { _transactionType = DefinedValueCache.Get(_automatedPaymentArgs.TransactionTypeGuid ?? SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid()); } if (_financialSource == null) { _financialSource = DefinedValueCache.Get(_automatedPaymentArgs.FinancialSourceGuid ?? SystemGuid.DefinedValue.FINANCIAL_SOURCE_TYPE_WEBSITE.AsGuid()); } }
/// <summary> /// Populates the properties of the referencePaymentInfo from this gateway's <seealso cref="M:Rock.Financial.IHostedGatewayComponent.GetHostedPaymentInfoControl(Rock.Model.FinancialGateway,System.String)">hostedPaymentInfoControl</seealso> /// This includes the ReferenceNumber, plus any other fields that the gateway wants to set /// </summary> /// <param name="financialGateway">The financial gateway.</param> /// <param name="hostedPaymentInfoControl">The hosted payment information control.</param> /// <param name="referencePaymentInfo">The reference payment information.</param> /// <param name="errorMessage">The error message.</param> public void UpdatePaymentInfoFromPaymentControl(FinancialGateway financialGateway, Control hostedPaymentInfoControl, ReferencePaymentInfo referencePaymentInfo, out string errorMessage) { TestGatewayPaymentControl testGatewayPaymentControl = hostedPaymentInfoControl as TestGatewayPaymentControl; referencePaymentInfo.ReferenceNumber = testGatewayPaymentControl.PaymentInfoToken; referencePaymentInfo.PaymentExpirationDate = testGatewayPaymentControl.PaymentExpirationDate; referencePaymentInfo.MaskedAccountNumber = testGatewayPaymentControl.CreditCardNumber.Masked(); referencePaymentInfo.GatewayPersonIdentifier = "person_" + Guid.NewGuid().ToString("N"); errorMessage = null; }
/// <summary> /// Creates the customer account using a token received and returns a customer account token that can be used for future transactions. /// </summary> /// <param name="financialGateway">The financial gateway.</param> /// <param name="paymentInfo">The payment information.</param> /// <param name="errorMessage">The error message.</param> /// <returns></returns> public string CreateCustomerAccount(FinancialGateway financialGateway, ReferencePaymentInfo paymentInfo, out string errorMessage) { errorMessage = string.Empty; return(Guid.NewGuid().ToString("N")); }
/// <summary> /// Gets the reference information. /// </summary> /// <param name="savedAccountId">The saved account unique identifier.</param> /// <returns></returns> private ReferencePaymentInfo GetReferenceInfo( int savedAccountId ) { using ( new UnitOfWorkScope() ) { var savedAccount = new FinancialPersonSavedAccountService().Get( savedAccountId ); if ( savedAccount != null ) { var reference = new ReferencePaymentInfo(); reference.TransactionCode = savedAccount.TransactionCode; reference.ReferenceNumber = savedAccount.ReferenceNumber; reference.MaskedAccountNumber = savedAccount.MaskedAccountNumber; reference.InitialCurrencyTypeValue = DefinedValueCache.Read( savedAccount.CurrencyTypeValue ); if ( reference.InitialCurrencyTypeValue.Guid.Equals( new Guid( Rock.SystemGuid.DefinedValue.CURRENCY_TYPE_CREDIT_CARD ) ) ) { reference.InitialCreditCardTypeValue = DefinedValueCache.Read( savedAccount.CreditCardTypeValue ); } return reference; } } return null; }