Untitled

 avatar
unknown
csharp
a year ago
3.2 kB
8
Indexable
    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);
        }
    }
Editor is loading...
Leave a Comment