Esempio n. 1
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);
        }
Esempio n. 2
0
        public static Task <PaymentRequestResult> CreateRequest(
            Invoice invoice,
            HttpContext httpContext,
            SamanGatewayAccount account,
            HttpClient httpClient,
            SamanGatewayOptions gatewayOptions,
            MessagesOptions messagesOptions,
            CancellationToken cancellationToken)
        {
            if (invoice.IsSamanMobileGatewayEnabled())
            {
                return(CreateMobilePaymentRequest(invoice, httpContext, account, httpClient, gatewayOptions, messagesOptions, cancellationToken));
            }

            return(CreateWebPaymentRequest(invoice, httpContext, account, httpClient, gatewayOptions, messagesOptions, cancellationToken));
        }
Esempio n. 3
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 }
            }));
        }
Esempio n. 4
0
        public static string GetVerificationUrl(InvoiceContext invoiceContext, SamanGatewayOptions gatewayOptions)
        {
            var record = invoiceContext
                         .Transactions
                         .SingleOrDefault(transaction => transaction.Type == TransactionType.Request);

            if (record == null || record.AdditionalData.IsNullOrEmpty())
            {
                return(gatewayOptions.WebApiUrl);
            }

            if (AdditionalDataConverter.ToDictionary(record).TryGetValue(MobileGatewayKey, out var isMobileGatewayEnabled) && bool.Parse(isMobileGatewayEnabled))
            {
                return(gatewayOptions.MobileApiVerificationUrl);
            }

            return(gatewayOptions.WebApiUrl);
        }