Untitled

mail@pastecode.io avatar
unknown
csharp
23 days ago
3.2 kB
5
Indexable
Never
    private async Task SendMerchantsToStoneAcquirerNotificationHandler(IEnumerable<MerchantIdAndMerchantReference>
        merchants, IEnumerable<Product> products, MerchantAgreementStatus merchantAgreementStatus, CancellationToken
        cancellationToken)
    {
        const string webhookLogMessage = "WebhookNotifications";

        IList<object> webhookLogs = 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 SendMerchantToStoneAcquirerNotificationHandler(merchantReference,
                products, merchantAgreementStatus, cancellationToken);

            if (stoneWebhookException is null)
                continue;

            exception = new MerchantException(merchantReference.Unformat(), stoneWebhookException);
            success = false;

            var exceptionDnauthWebhook = await SendMerchantToDnauthNotificationHandler(merchantReference);

            if (exceptionDnauthWebhook is null)
            {
                webhookLogs.Add(new
                {
                    Success = success,
                    Exception = exception,
                    SentToDnauthWebhook = !success,
                    DnauthWebhookSuccess = true,
                });
                continue;
            }

            exception = new MerchantException(merchantReference, exceptionDnauthWebhook);
            webhookLogs.Add(new
            {
                Success = success,
                Exception = exception,
                SentToDnauthWebhook = !success,
                DnauthWebhookSuccess = false,
            });
            resumeLog.SetSeverityIfMoreSevere(LogSeverity.Error);
        }

        resumeLog.SetAdditionalData(webhookLogs, webhookLogMessage);
    }

    private async Task<Exception> SendMerchantToStoneAcquirerNotificationHandler(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> SendMerchantToDnauthNotificationHandler(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