Untitled

mail@pastecode.io avatar
unknown
csharp
17 days ago
2.9 kB
2
Indexable
Never
private async Task SendMerchantsToStoneAcquirerNotificationHandlerNew(IEnumerable<MerchantIdAndMerchantReference> merchants,
IEnumerable<Product> products, MerchantAgreementStatus merchantAgreementStatus, CancellationToken cancellationToken)
{
    const string webhookLogMessage = "WebhookNotifications";
    const string dnauthWebhookErrorsMessage = "DnauthWebhookErrors";

    IList<object> webhookLogs = new List<object>();
    IList<object> dnauthWebhookErrorLogs = new List<object>();

    foreach (var merchantReference in merchants.OrderBy(x => x.MerchantReference).Select(merchant =>
        merchant.MerchantReference))
    {
        var success = true;
        Exception exception = default;

        var stoneWebhookException = await SendToStoneNotificationHandler(merchantReference, products,
            merchantAgreementStatus, cancellationToken);
        if (stoneWebhookException is null)
            continue;

        exception = new MerchantException(merchantReference.Unformat(), stoneWebhookException);
        success = false;
        webhookLogs.Add(new { Success = success, Exception = exception, SentToDnauthWebhook = !success });

        var exceptionDnauthWebhook = await SendToDnauthNotificationHandler(merchantReference);
        if (exceptionDnauthWebhook is null)
            continue;
        Exception merchantException = new MerchantException(merchantReference, exceptionDnauthWebhook);
        dnauthWebhookErrorLogs.Add(new { Exception = merchantException });
    }

    resumeLog.SetAdditionalData(webhookLogs, webhookLogMessage);
    if (!dnauthWebhookErrorLogs.IsEmpty())
    {
        resumeLog.SetAdditionalData(dnauthWebhookErrorLogs, dnauthWebhookErrorsMessage);
        resumeLog.SetSeverityIfMoreSevere(LogSeverity.Error);
    }
}

private async Task<Exception> SendToStoneNotificationHandler(string merchantReference, IEnumerable<Product> products,
MerchantAgreementStatus merchantAgreementStatus, CancellationToken cancellationToken)
{
    try
    {
        var notification = CreatePermissionAuthorizationUpdatedEvent(merchantReference.Unformat(),
            requestContext.CardBrand.Scheme, products.OrderBy(x => x), merchantAgreementStatus);
        await stoneAcquirerNotificationHandler.Execute(notification, cancellationToken);
        return null;
    }
    catch (Exception ex)
    {
        return new MerchantException(merchantReference.Unformat(), ex);
    }
}

private async Task<Exception> SendToDnauthNotificationHandler(string merchantReference)
{
    IEnumerable<string> merchant = new List<string> { merchantReference };
    try
    {
        await dnauthNotificationHandler.Execute(merchant, default);
        return null;
    }
    catch (Exception e)
    {
        return new MerchantException(merchant.First().Unformat(), e);
    }
}
Leave a Comment