Exemple #1
0
        public static PaymentRequestResult CreateRequestResult(
            string webServiceResponse,
            HttpContext httpContext,
            MellatGatewayOptions gatewayOptions,
            MessagesOptions messagesOptions,
            GatewayAccount account)
        {
            var result = XmlHelper.GetNodeValueFromXml(webServiceResponse, "return");

            var arrayResult = result.Split(',');

            var resCode = arrayResult[0];
            var refId   = arrayResult.Length > 1 ? arrayResult[1] : string.Empty;

            var isSucceed = resCode == OkResult;

            if (!isSucceed)
            {
                var message = resCode == DuplicateOrderNumberResult
                    ? messagesOptions.DuplicateTrackingNumber
                    : MellatGatewayResultTranslator.Translate(resCode, messagesOptions);

                return(PaymentRequestResult.Failed(message, account.Name));
            }

            return(PaymentRequestResult.SucceedWithPost(
                       account.Name,
                       httpContext,
                       gatewayOptions.PaymentPageUrl,
                       new Dictionary <string, string>
            {
                { "RefId", refId }
            }));
        }
        public static PaymentRequestResult CreateRequestResult(
            string response,
            AsanPardakhtGatewayAccount account,
            HttpContext httpContext,
            AsanPardakhtGatewayOptions gatewayOptions,
            MessagesOptions messagesOptions)
        {
            var result = XmlHelper.GetNodeValueFromXml(response, "RequestOperationResult", "http://tempuri.org/");

            var splitedResult = result.Split(',');

            var isSucceed = splitedResult.Length == 2 && splitedResult[0] == "0";

            if (!isSucceed)
            {
                var message = AsanPardakhtResultTranslator.TranslateRequest(splitedResult[0], messagesOptions);

                return(PaymentRequestResult.Failed(message, account.Name));
            }

            return(PaymentRequestResult.SucceedWithPost(
                       account.Name,
                       httpContext,
                       gatewayOptions.PaymentPageUrl,
                       new Dictionary <string, string>
            {
                { "RefId", splitedResult[1] }
            }));
        }
Exemple #3
0
        public static PaymentRequestResult CreateRequestResult(
            string webServiceResponse,
            IranKishGatewayAccount account,
            IranKishGatewayOptions gatewayOptions,
            HttpContext httpContext,
            MessagesOptions messagesOptions)
        {
            var result  = XmlHelper.GetNodeValueFromXml(webServiceResponse, "result", "http://schemas.datacontract.org/2004/07/Token");
            var message = XmlHelper.GetNodeValueFromXml(webServiceResponse, "message", "http://schemas.datacontract.org/2004/07/Token");
            var token   = XmlHelper.GetNodeValueFromXml(webServiceResponse, "token", "http://schemas.datacontract.org/2004/07/Token");

            var isSucceed = result.Equals("true", StringComparison.OrdinalIgnoreCase) && !token.IsNullOrEmpty();

            if (!isSucceed)
            {
                if (message.IsNullOrEmpty())
                {
                    message = messagesOptions.InvalidDataReceivedFromGateway;
                }

                return(PaymentRequestResult.Failed(message, account.Name));
            }

            return(PaymentRequestResult.SucceedWithPost(
                       account.Name,
                       httpContext,
                       gatewayOptions.PaymentPageUrl,
                       new Dictionary <string, string>
            {
                { "merchantid", account.MerchantId },
                { "token", token }
            }));
        }
Exemple #4
0
        private static async Task <PaymentRequestResult> CreateMobilePaymentRequest(
            Invoice invoice,
            HttpContext httpContext,
            SamanGatewayAccount account,
            HttpClient httpClient,
            SamanGatewayOptions gatewayOptions,
            MessagesOptions messagesOptions,
            CancellationToken cancellationToken)
        {
            var data = new SamanMobilePaymentTokenRequest
            {
                TerminalId  = account.MerchantId,
                ResNum      = invoice.TrackingNumber.ToString(),
                Amount      = invoice.Amount,
                RedirectUrl = invoice.CallbackUrl,
                Action      = "Token"
            };

            var responseMessage = await httpClient.PostJsonAsync(gatewayOptions.MobileApiTokenUrl, data, cancellationToken);

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

            var tokenResponse = JsonConvert.DeserializeObject <SamanMobilePaymentTokenResponse>(response);

            if (tokenResponse == null)
            {
                var message = $"{messagesOptions.InvalidDataReceivedFromGateway} Serialized token response is null.";
                return(PaymentRequestResult.Failed(message, account.Name));
            }

            if (tokenResponse.Status == -1)
            {
                return(PaymentRequestResult.Failed(tokenResponse.GetError(), account.Name));
            }

            var result = PaymentRequestResult.SucceedWithPost(
                account.Name,
                httpContext,
                gatewayOptions.MobilePaymentPageUrl,
                new Dictionary <string, string>
            {
                { "Token", tokenResponse.Token }
            });

            result.DatabaseAdditionalData.Add(MobileGatewayKey, true.ToString());

            return(result);
        }
Exemple #5
0
        private static async Task <PaymentRequestResult> CreateWebPaymentRequest(
            Invoice invoice,
            HttpContext httpContext,
            SamanGatewayAccount account,
            HttpClient httpClient,
            SamanGatewayOptions gatewayOptions,
            MessagesOptions messagesOptions,
            CancellationToken cancellationToken)
        {
            var data = CreateTokenRequest(invoice, account);

            var responseMessage = await httpClient.PostXmlAsync(gatewayOptions.WebApiTokenUrl, data, cancellationToken);

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

            var token = XmlHelper.GetNodeValueFromXml(response, "result");

            string message   = null;
            var    isSucceed = true;

            if (token.IsNullOrEmpty())
            {
                message   = $"{messagesOptions.InvalidDataReceivedFromGateway} Token is null or empty.";
                isSucceed = false;
            }
            else if (long.TryParse(token, out var longToken) && longToken < 0)
            {
                message   = SamanResultTranslator.Translate(longToken, messagesOptions);
                isSucceed = false;
            }

            if (!isSucceed)
            {
                return(PaymentRequestResult.Failed(message, account.Name));
            }

            return(PaymentRequestResult.SucceedWithPost(
                       account.Name,
                       httpContext,
                       gatewayOptions.WebPaymentPageUrl,
                       new Dictionary <string, string>
            {
                { "Token", token },
                { "RedirectURL", invoice.CallbackUrl }
            }));
        }
        public static PaymentRequestResult CreateRequestResult(
            Invoice invoice,
            HttpContext httpContext,
            PasargadGatewayAccount account,
            IPasargadCrypto crypto,
            PasargadGatewayOptions gatewayOptions)
        {
            var invoiceDate = GetTimeStamp(DateTime.Now);

            var timeStamp = invoiceDate;

            var dataToSign = string.Format("#{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}#",
                                           account.MerchantCode,
                                           account.TerminalCode,
                                           invoice.TrackingNumber,
                                           invoiceDate,
                                           (long)invoice.Amount,
                                           invoice.CallbackUrl,
                                           ActionNumber,
                                           timeStamp);

            var signedData = crypto.Encrypt(account.PrivateKey, dataToSign);

            var result = PaymentRequestResult.SucceedWithPost(
                account.Name,
                httpContext,
                gatewayOptions.PaymentPageUrl,
                new Dictionary <string, string>
            {
                { "merchantCode", account.MerchantCode },
                { "terminalCode", account.TerminalCode },
                { "invoiceNumber", invoice.TrackingNumber.ToString() },
                { "invoiceDate", invoiceDate },
                { "amount", invoice.Amount.ToLongString() },
                { "redirectAddress", invoice.CallbackUrl },
                { "action", ActionNumber },
                { "timeStamp", timeStamp },
                { "sign", signedData }
            });

            result.DatabaseAdditionalData.Add("timeStamp", timeStamp);

            return(result);
        }
Exemple #7
0
        public static PaymentRequestResult CreateRequestResult(
            string webServiceResponse,
            Invoice invoice,
            HttpContext httpContext,
            MellatGatewayOptions gatewayOptions,
            MessagesOptions messagesOptions,
            GatewayAccount account)
        {
            var result = XmlHelper.GetNodeValueFromXml(webServiceResponse, "return");

            var arrayResult = result.Split(',');

            var resCode = arrayResult[0];
            var refId   = arrayResult.Length > 1 ? arrayResult[1] : string.Empty;

            var isSucceed = resCode == OkResult;

            if (!isSucceed)
            {
                var message = resCode == DuplicateOrderNumberResult
                    ? messagesOptions.DuplicateTrackingNumber
                    : MellatGatewayResultTranslator.Translate(resCode, messagesOptions);

                return(PaymentRequestResult.Failed(message, account.Name));
            }

            var form = new Dictionary <string, string>
            {
                { "RefId", refId }
            };

            var additionalData = invoice.GetMellatAdditionalData();

            if (!string.IsNullOrWhiteSpace(additionalData?.MobileNumber))
            {
                form.Add("MobileNo", additionalData.MobileNumber);
            }

            return(PaymentRequestResult.SucceedWithPost(
                       account.Name,
                       httpContext,
                       gatewayOptions.PaymentPageUrl,
                       form));
        }
Exemple #8
0
        public static async Task <PaymentRequestResult> CreateRequestResult(
            HttpResponseMessage responseMessage,
            HttpContext httpContext,
            SepehrGatewayAccount account,
            SepehrGatewayOptions gatewayOptions,
            MessagesOptions messages)
        {
            if (!responseMessage.IsSuccessStatusCode)
            {
                return(PaymentRequestResult.Failed($"Operation failed. Http Status: {responseMessage.StatusCode}", account.Name));
            }

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

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

            if (result == null)
            {
                return(PaymentRequestResult.Failed(messages.InvalidDataReceivedFromGateway, account.Name));
            }

            if (result.Status != 0)
            {
                var message = SepehrGatewayResultTranslator.Translate(result.Status.ToString(), messages);

                return(PaymentRequestResult.Failed(message, account.Name));
            }

            if (result.AccessToken.IsNullOrWhiteSpace())
            {
                return(PaymentRequestResult.Failed(messages.InvalidDataReceivedFromGateway, account.Name));
            }

            return(PaymentRequestResult.SucceedWithPost(account.Name,
                                                        httpContext,
                                                        gatewayOptions.PaymentPageUrl,
                                                        new Dictionary <string, string>
            {
                { "TerminalID", account.TerminalId.ToString() },
                { "token", result.AccessToken }
            }));
        }
        /// <inheritdoc />
        public override async Task <IPaymentRequestResult> RequestAsync(Invoice invoice, CancellationToken cancellationToken = default)
        {
            var httpContext = _httpContextAccessor.HttpContext;

            var account = await GetAccountAsync(invoice).ConfigureAwaitFalse();

            var url = $"{httpContext.Request.Scheme}" +
                      "://" +
                      $"{httpContext.Request.Host.ToUriComponent()}" +
                      $"{_options.Value.GatewayPath}";

            return(PaymentRequestResult.SucceedWithPost(
                       account.Name,
                       httpContext,
                       url,
                       new Dictionary <string, string>
            {
                { "CommandType", "request" },
                { "trackingNumber", invoice.TrackingNumber.ToString() },
                { "amount", invoice.Amount.ToLongString() },
                { "redirectUrl", invoice.CallbackUrl }
            }));
        }