Exemplo n.º 1
0
        public static async Task <PaymentVerifyResult> CreateVerifyResult(InvoiceContext context, HttpResponseMessage responseMessage, CallbackResultModel callbackResult, MessagesOptions messagesOptions)
        {
            if (!responseMessage.IsSuccessStatusCode)
            {
                return(PaymentVerifyResult.Failed($"Operation failed. Http Status: {responseMessage.StatusCode}"));
            }

            var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwaitFalse();

            var result = JsonConvert.DeserializeObject <VerificationRollbackResponseModel>(response);

            if (result == null)
            {
                return(PaymentVerifyResult.Failed(messagesOptions.InvalidDataReceivedFromGateway));
            }

            if (result.Status.IsNullOrWhiteSpace())
            {
                return(PaymentVerifyResult.Failed(messagesOptions.InvalidDataReceivedFromGateway));
            }

            PaymentVerifyResultStatus status;
            string message;

            switch (result.Status.ToLower())
            {
            case "ok":
                if (!long.TryParse(result.ReturnId, out var amount))
                {
                    message = $"{messagesOptions.InvalidDataReceivedFromGateway} Cannot parse the amount. Amount: {result.ReturnId}";
                    status  = PaymentVerifyResultStatus.Failed;
                }
                else if (amount != (long)context.Payment.Amount)
                {
                    status  = PaymentVerifyResultStatus.Failed;
                    message = $"{messagesOptions.PaymentFailed} The amount that received from the gateway and the actual invoice amount are not equal. Invoice amount: {(long)context.Payment.Amount}. Received amount: {result.ReturnId}";
                }
                else
                {
                    status  = PaymentVerifyResultStatus.Succeed;
                    message = messagesOptions.PaymentSucceed;
                }

                break;

            case "nok":
                status  = PaymentVerifyResultStatus.Failed;
                message = SepehrGatewayResultTranslator.Translate(result.ReturnId, messagesOptions);
                if (message.IsNullOrEmpty())
                {
                    message = messagesOptions.PaymentFailed;
                }
                break;

            case "duplicate":
                status  = PaymentVerifyResultStatus.AlreadyVerified;
                message = !result.Message.IsNullOrWhiteSpace() ? result.Message : messagesOptions.PaymentIsAlreadyProcessedBefore;
                break;

            default:
                status  = PaymentVerifyResultStatus.Failed;
                message = $"{messagesOptions.InvalidDataReceivedFromGateway} Verification failed. Status value is not valid.";
                break;
            }

            var verificationResult = new PaymentVerifyResult
            {
                Status          = status,
                Message         = message,
                TransactionCode = callbackResult.Rrn.ToString()
            };

            var verificationAdditionalData = new SepehrGatewayVerificationAdditionalData
            {
                TraceNumber    = callbackResult.TraceNumber,
                DigitalReceipt = callbackResult.DigitalReceipt,
                Rrn            = callbackResult.Rrn,
                CardNumber     = callbackResult.CardNumber
            };

            verificationResult.SetPayIrAdditionalData(verificationAdditionalData);
            var serializedAdditionalData = JsonConvert.SerializeObject(verificationAdditionalData);

            verificationResult.DatabaseAdditionalData.Add(VerificationAdditionalDataKey, serializedAdditionalData);

            return(verificationResult);
        }