error in myfatoorah payment getway for nopcommerce

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 года назад
I am using myfatoorah payment getway in my nopcommerce store when i confirm checkout i got this error

One or more errors occurred. (Unexpected character encountered while parsing value: <. Path '', line 0, position 0.)

this MyFatoorahRESTApi.cs

public class MyFatoorahRESTApi
    {
    
        private static HttpClient _httpClient = null;
        private SystemMode lIVE;

        public string BaseAddress { get; set; }
        public  string MerchandUserName { get; set; }
        public  string MerchandPassword { get; set; }
        private static SystemMode _systemMode { get; set; }
        static readonly object padlock = new Object();

        public static HttpClient httpClient
        {
            get
            {
                if (_httpClient == null)
                {
                    lock (padlock)
                    {
                        if (_httpClient == null)
                        {
                            _httpClient = new HttpClient();
                        }
                    }
                }
                return _httpClient;
            }
        }


        public MyFatoorahRESTApi(bool isSandBox=true)
        {
            _systemMode = isSandBox ? SystemMode.TEST : SystemMode.LIVE;
            
        }

        public MyFatoorahRESTApi(SystemMode lIVE)
        {
            this.lIVE = lIVE;
        }

        public async Task<TokenResponse> GetTokenAsync()
        {

            var response = string.Empty;

            var apiTokenRequest = new TokenRequest()
            {
                Grant_Type = "password",
                UserName = MerchandUserName,// "[email protected]",
                Password = MerchandPassword// "api12345*"
            };


            var apiUrl = string.Format("{0}/{1}", BaseAddress, "bearer wcaLFUGNxXc_P3yhdOG4YvpKpomCR1bMwfXDeoFnjWLjGnLgNXgnetm1Zwf1ha8LRqNfFNsUNDSW262VCyNC9EV4Vrk6gCvOwyR5PGl9PxYecODb8K0vKMQHAytkZdl2X2EG7IZnAKD2LNilY42Ry3A8GNbz4ylpyZxiuD3fXYNV8L3ZMplpsXjoQ4cpP-K076nDeefVS4fO-Gc-GGaJgGXElRY7U9Vy7t3sE0hzWZPPeQf1m0yeBS4oRQtG47nKu5AweZbytVB60XgyvoVewa-PY5cfVQSKheG4RqCXOK_6wYJDObpEmjIwH2tyScYrz34MrIvzovW-qzzcy");


            var pairs = new List<KeyValuePair<string, string>>();
            pairs.Add(new KeyValuePair<string, string>("grant_type", "password"));
            pairs.Add(new KeyValuePair<string, string>("username", apiTokenRequest.UserName));
            pairs.Add(new KeyValuePair<string, string>("password", apiTokenRequest.Password));

            var httpContent = new FormUrlEncodedContent(pairs);

            var request = String.Format("grant_type=password&username={0}&password={1}",
              apiTokenRequest.UserName, apiTokenRequest.Password);

            StringContent content = new StringContent(request, System.Text.Encoding.UTF8, "application/json");

            response = await UseHTTPClient(content, apiUrl, "POST", null);

            var apiTokenResposne = JsonConvert.DeserializeObject<TokenResponse>(response);
            return apiTokenResposne;
        }

        public async Task<TransactionResponse> GetOrderStatusRequestAsync(string paymentId, TokenResponse token)
        {
            var url = string.Format("{0}/{1}/{2}", BaseAddress, "ApiInvoices/Transaction", paymentId );

            var response = await UseHTTPClient(null, url, "GET", token);

            var apiTokenResposne = JsonConvert.DeserializeObject<TransactionResponse>(response);
            return apiTokenResposne;

        }
        public async Task<InvoiceResponse> CreateInvoice(TokenResponse tokenResponse, InvoiceRequest apiInvoiceRequest)
        {
            var url = string.Format("{0}/{1}", BaseAddress, "ApiInvoices/CreateInvoiceIso");

            var request = JsonConvert.SerializeObject(apiInvoiceRequest);

            StringContent content = new StringContent(request, System.Text.Encoding.UTF8, "application/json");

            var response = await UseHTTPClient(content, url, "POST", tokenResponse);
          
            var apiInvoiceResponse = JsonConvert.DeserializeObject<InvoiceResponse>(response);

            return apiInvoiceResponse;
        }
        private async Task<string> UseHTTPClient(StringContent httpContent, string url, string method, TokenResponse token)
        {
            if (token != null)
            {
                httpClient.BaseAddress = new Uri(BaseAddress);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            }

            HttpResponseMessage httpResponse = null;

            if (method == "POST")
            {
                httpResponse = await httpClient.PostAsync(url, httpContent).ConfigureAwait(false);
            }
            if (method == "GET")
            {
                httpResponse = await httpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
            }
            //if (httpResponse.IsSuccessStatusCode)
            var response = await httpResponse.Content.ReadAsStringAsync();

            return response;
        }
    }
}



I think the error from above code

plz help me for resolv this problem
3 года назад
Did you set a breakpoint in the routine and step through the code to find the error ?
3 года назад
Yidna wrote:
Did you set a breakpoint in the routine and step through the code to find the error ?


no
3 года назад
There error is on this line

            var apiTokenResposne = JsonConvert.DeserializeObject<TokenResponse>(response);

This is what is returned
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid URL</h2>
<hr><p>HTTP Error 400. The request URL is invalid.</p>
</BODY></HTML>

So looks like what you are sending them in this POST is not correct
            StringContent content = new StringContent(request, System.Text.Encoding.UTF8, "application/json");
            response = await UseHTTPClient(content, apiUrl, "POST", null);

This is the request data
"grant_type=password&[email protected]&password=api12345"

This is the apiUrl
https://apidemo.myfatoorah.com/bearer wcaLFUGNxXc_P3yhdOG4YvpKpomCR1bMwfXDeoFnjWLjGnLgNXgnetm1Zwf1ha8LRqNfFNsUNDSW262VCyNC9EV4Vrk6gCvOwyR5PGl9PxYecODb8K0vKMQHAytkZdl2X2EG7IZnAKD2LNilY42Ry3A8GNbz4ylpyZxiuD3fXYNV8L3ZMplpsXjoQ4cpP-K076nDeefVS4fO-Gc-GGaJgGXElRY7U9Vy7t3sE0hzWZPPeQf1m0yeBS4oRQtG47nKu5AweZbytVB60XgyvoVewa-PY5cfVQSKheG4RqCXOK_6wYJDObpEmjIwH2tyScYrz34MrIvzovW-qzzcy
3 года назад
Yidna wrote:
There error is on this line

            var apiTokenResposne = JsonConvert.DeserializeObject<TokenResponse>(response);

This is what is returned
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid URL</h2>
<hr><p>HTTP Error 400. The request URL is invalid.</p>
</BODY></HTML>

So looks like what you are sending them in this POST is not correct
            StringContent content = new StringContent(request, System.Text.Encoding.UTF8, "application/json");
            response = await UseHTTPClient(content, apiUrl, "POST", null);

This is the request data
"grant_type=password&[email protected]&password=api12345"

This is the apiUrl
https://apidemo.myfatoorah.com/bearer wcaLFUGNxXc_P3yhdOG4YvpKpomCR1bMwfXDeoFnjWLjGnLgNXgnetm1Zwf1ha8LRqNfFNsUNDSW262VCyNC9EV4Vrk6gCvOwyR5PGl9PxYecODb8K0vKMQHAytkZdl2X2EG7IZnAKD2LNilY42Ry3A8GNbz4ylpyZxiuD3fXYNV8L3ZMplpsXjoQ4cpP-K076nDeefVS4fO-Gc-GGaJgGXElRY7U9Vy7t3sE0hzWZPPeQf1m0yeBS4oRQtG47nKu5AweZbytVB60XgyvoVewa-PY5cfVQSKheG4RqCXOK_6wYJDObpEmjIwH2tyScYrz34MrIvzovW-qzzcy


thank you for replay i solve this error but i got another error  :


One or more errors occurred. (This instance has already started one or more requests. Properties can only be modified before sending the first request.)
3 года назад
https://www.google.com/search?q=This+instance+has+already+started+one+or+more+requests.+Properties+can+only+be+modified+before+sending+the+first+request.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.