public SapOutgoingPaymentModel MapSapOutgoingPayment(SapCreditNoteResponse sapCreditNoteResponse, string transferReference) { var newIncomingPayment = new SapOutgoingPaymentModel { DocDate = sapCreditNoteResponse.DocDate.ToString("yyyy-MM-dd"), TransferDate = _dateTimeProvider.GetDateByTimezoneId(_dateTimeProvider.UtcNow, _timezoneConfig.InvoicesTimeZone).ToString("yyyy-MM-dd"), TaxDate = _dateTimeProvider.GetDateByTimezoneId(_dateTimeProvider.UtcNow, _timezoneConfig.InvoicesTimeZone).ToString("yyyy-MM-dd"), CardCode = sapCreditNoteResponse.CardCode, DocType = "rCustomer", DocCurrency = _currencyCode, TransferAccount = _transferAccount, TransferSum = sapCreditNoteResponse.DocTotal, JournalRemarks = $"Outgoing Payments - {sapCreditNoteResponse.CardCode}", TransferReference = transferReference, U_ClaseCashfloCaja = _uClaseCashfloCaja, PaymentInvoices = new List <SapOutgoingPaymentLineModel> { new SapOutgoingPaymentLineModel { LineNum = 0, SumApplied = sapCreditNoteResponse.DocTotal, DocEntry = sapCreditNoteResponse.DocEntry, InvoiceType = "it_CredItnote" } } }; return(newIncomingPayment); }
private async Task <SapTaskResult> CancelCreditNote(SapCreditNoteResponse sapCreditNoteResponse, SapTask dequeuedTask, string sapSystem) { var serviceSetting = SapServiceSettings.GetSettings(_sapConfig, sapSystem); var uriString = $"{serviceSetting.BaseServerUrl}{serviceSetting.BillingConfig.CreditNotesEndpoint}({sapCreditNoteResponse.DocEntry})/Cancel"; var sapResponse = await SendMessage(null, sapSystem, uriString, HttpMethod.Post); var taskResult = new SapTaskResult { IsSuccessful = sapResponse.IsSuccessStatusCode, SapResponseContent = await sapResponse.Content.ReadAsStringAsync(), TaskName = "Canceling Credit Note" }; return(taskResult); }
public SapOutgoingPaymentModel MapSapOutgoingPayment(SapCreditNoteResponse sapCreditNoteResponse, string transferReference) { //Is not implemented because at the moment is not necessary the send the Payment to SAP throw new System.NotImplementedException(); }
private async Task <SapTaskResult> SendOutgoingPaymentToSap(string sapSystem, SapCreditNoteResponse response, string transferReference) { var billingMapper = GetMapper(sapSystem); var outgoingPaymentRequest = billingMapper.MapSapOutgoingPayment(response, transferReference); var serviceSetting = SapServiceSettings.GetSettings(_sapConfig, sapSystem); var message = new HttpRequestMessage { RequestUri = new Uri($"{serviceSetting.BaseServerUrl}{serviceSetting.BillingConfig.OutgoingPaymentEndpoint}"), Content = new StringContent(JsonConvert.SerializeObject(outgoingPaymentRequest), Encoding.UTF8, "application/json"), Method = HttpMethod.Post }; var sapTaskHandler = _sapServiceSettingsFactory.CreateHandler(sapSystem); var cookies = await sapTaskHandler.StartSession(); message.Headers.Add("Cookie", cookies.B1Session); message.Headers.Add("Cookie", cookies.RouteId); var client = _httpClientFactory.CreateClient(); var sapResponse = await client.SendAsync(message); if (!sapResponse.IsSuccessStatusCode) { _logger.LogError($"Outgoing Payment could'n create to SAP because exists an error: '{sapResponse.Content.ReadAsStringAsync()}'."); } return(new SapTaskResult { IsSuccessful = sapResponse.IsSuccessStatusCode, IdUser = response.CardCode, SapResponseContent = await sapResponse.Content.ReadAsStringAsync(), TaskName = "Creating Credit Note with Refund Request" }); }
public async Task CreditNoteHandler_ShouldBeCancelCreditNoteInSap_WhenQueueHasValidElement() { var sapCreditNoteResponse = new SapCreditNoteResponse { DocEntry = 1, CardCode = "CD001" }; var sapConfigMock = new Mock <IOptions <SapConfig> >(); var timeZoneConfigurations = new TimeZoneConfigurations { InvoicesTimeZone = TimeZoneHelper.GetTimeZoneByOperativeSystem("Argentina Standard Time") }; var dateTimeProviderMock = new Mock <IDateTimeProvider>(); dateTimeProviderMock.Setup(x => x.UtcNow) .Returns(new DateTime(2019, 09, 25)); var billingMappers = new List <IBillingMapper> { new BillingForArMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations), new BillingForUsMapper(Mock.Of <ISapBillingItemsService>(), dateTimeProviderMock.Object, timeZoneConfigurations) }; sapConfigMock.Setup(x => x.Value) .Returns(new SapConfig { SapServiceConfigsBySystem = new Dictionary <string, SapServiceConfig> { { "US", new SapServiceConfig { CompanyDB = "CompanyDb", Password = "******", UserName = "******", BaseServerUrl = "http://123.123.123/", BusinessPartnerConfig = new BusinessPartnerConfig { Endpoint = "BusinessPartners" }, BillingConfig = new BillingConfig { Endpoint = "Invoices", CreditNotesEndpoint = "CreditNotes" } } } } }); var httpClientFactoryMock = new Mock <IHttpClientFactory>(); var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"") }); var httpClient = new HttpClient(httpMessageHandlerMock.Object); httpClientFactoryMock.Setup(_ => _.CreateClient(It.IsAny <string>())) .Returns(httpClient); var sapTaskHandlerMock = new Mock <ISapTaskHandler>(); sapTaskHandlerMock.Setup(x => x.StartSession()) .ReturnsAsync(new SapLoginCookies { B1Session = "session", RouteId = "route" }); sapTaskHandlerMock.Setup(x => x.TryGetCreditNoteByCreditNoteId(It.IsAny <int>())) .ReturnsAsync(sapCreditNoteResponse); var sapServiceSettingsFactoryMock = new Mock <ISapServiceSettingsFactory>(); sapServiceSettingsFactoryMock.Setup(x => x.CreateHandler("US")).Returns(sapTaskHandlerMock.Object); var handler = new CreditNoteHandler( sapConfigMock.Object, Mock.Of <ILogger <CreditNoteHandler> >(), sapServiceSettingsFactoryMock.Object, httpClientFactoryMock.Object, billingMappers); var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("") }; httpResponseMessage.Headers.Add("Set-Cookie", ""); httpMessageHandlerMock.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(httpResponseMessage); var result = await handler.CancelCreditNoteHandle(new SapTask { CancelCreditNoteRequest = new CancelCreditNoteRequest { BillingSystemId = 2, CreditNoteId = 1 }, TaskType = Enums.SapTaskEnum.CancelCreditNote }); Assert.True(result.IsSuccessful); Assert.Equal("Canceling Credit Note", result.TaskName); }