0

I'm working on a bank integration in a project and stumbled upon a weird error.

I've a pretty simple JSON which looks like this:

{
    "MbrId": "*",
    "MerchantID": "0*********04",
    "UserCode": "Q******3DPAY",
    "UserPass": "U****",
    "OrderId": "T********3",
    "SecureType": "N****e",
    "InstallmentCount": "**",
    "TxnType": "****",
    "PurchAmount": "****",
    "Currency": "***",
    "CardHolderName": "*****e",
    "Pan": "4*********11",
    "Expiry": "****",
    "Cvv2": "****",
    "Lang": "**"
}

Sending this from POSTMAN works and it returns a success response. I also installed RestClient nuGet package. Copying and pasting the code on Postman also worked:

var client = new RestSharp.RestClient(" BANK'S URI ");
client.Timeout = -1;
var request = new RestRequest(Method.POST);

request.AddHeader("Content-Type", "application/json");

// I take the info from the object
request.AddParameter("application/json", $"{{\"MbrId\":\"{_paymentInfo.MbrId}\",\"MerchantID\":\"{_paymentInfo.MerchantID}\",\"UserCode\":\"{_paymentInfo.UserCode}\"," +
            $"\"UserPass\":\"{_paymentInfo.UserPass}\",\"OrderId\":\"{_paymentInfo.OrderId}\",\"SecureType\":\"{_paymentInfo.SecureType}\",\"TxnType\":\"{_paymentInfo.TxnType}\"," +
            $"\"PurchAmount\":\"{_paymentInfo.PurchAmount}\",\"Currency\":\"{_paymentInfo.Currency}\",\"CardHolderName\":\"{_paymentInfo.CardHolderName}\",\"Pan\":\"{_paymentInfo.Pan}\"," +
            $"\"Expiry\":\"{_paymentInfo.Expiry}\",\"Cvv2\":\"{_paymentInfo.Cvv2}\",\"Lang\":\"{_paymentInfo.Lang}\"}}",
ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

The problem is I cannot do the same with HttpClient:

  • I tried to use httpClient.PostAsJsonAsync() method which resulted in 403 response.

  • Tried to send request with PostAsync like this:

        var response = await _httpClient.PostAsync("URI", new StringContent(System.Text.Json.JsonSerializer.Serialize(_paymentInfo), Encoding.UTF8, "application/json;"));
        var resultString = await response.Content.ReadAsStringAsync();
    

    This resulted in 200 OK but bank's respond said plugin not found and request couldn't be handled.

  • I also tried this:

     var x = $"{{\"MbrId\":\"{_paymentInfo.MbrId}\",\"MerchantID\":\"{_paymentInfo.MerchantID}\",\"UserCode\":\"{_paymentInfo.UserCode}\"," +
                $"\"UserPass\":\"{_paymentInfo.UserPass}\",\"OrderId\":\"{_paymentInfo.OrderId}\",\"SecureType\":\"{_paymentInfo.SecureType}\",\"TxnType\":\"{_paymentInfo.TxnType}\"," +
                $"\"PurchAmount\":\"{_paymentInfo.PurchAmount}\",\"Currency\":\"{_paymentInfo.Currency}\",\"CardHolderName\":\"{_paymentInfo.CardHolderName}\",\"Pan\":\"{_paymentInfo.Pan}\"," +
                $"\"Expiry\":\"{_paymentInfo.Expiry}\",\"Cvv2\":\"{_paymentInfo.Cvv2}\",\"Lang\":\"{_paymentInfo.Lang}\"}}";
    
            _httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "BANK'S URI");
    
            req.Content = new StringContent(x, Encoding.UTF8, "application/json");
    
            var response = _httpClient.SendAsync(req).Result;
    
    
            var resultString = await response.Content.ReadAsStringAsync();
    

What's the difference between the HttpClient and RestClient? I tried to send same content made in Postman with HttpClient and it resulted in same error: Plugin Not Found. Is there something that I miss? Both Method is POST, content type is "application/json", encoding is UTF8. There are no auth or anything since it's the test endpoint. But supposedly same request works on RestClient while standard HttpClient does not.


Here is the bank's response on error:

{
  "PaymentRequest": {
    "RequestGuid": 0,
    "InsertDatetime": "0001-01-01T00:00:00",
    "MbrId": 101,
    "RequestIp": "my IP",
    "RequestStat": "1",
    "Currency": 0,
    "BonusAmount": "",
    "InstallmentCount": 0,
    "Ecommerce": 1,
    "ErrMsg": "Plugin not found",
    "ProcReturnCode": "M042",
    "BatchNo": 0,
    "ReqId": 0,
    "UsedPoint": 0.0,
    "SrcType": "VPO",
    "RefundedAmount": 0.0,
    "RefundedPoint": 0.0,
    "ReqDate": 0,
    "SysDate": 0,
    "F11": 0,
    "VposElapsedTime": 0,
    "BankingElapsedTime": 0,
    "SocketElapsedTime": 0,
    "HsmElapsedTime": 0,
    "MpiElapsedTime": 0,
    "hasOrderId": false,
    "TemplateType": 0,
    "HasAddressCount": false,
    "IsPaymentFacilitator": false,
    "F11_ORG": 0,
    "F12_ORG": 0,
    "F22_ORG": 0,
    "F25_ORG": 0,
    "MTI_ORG": 0,
    "IntervalType": 0,
    "IntervalDuration": 0,
    "RepeatCount": 0,
    "RequestClientIp": "my Ip",
    "VoidTime": 0,
    "PaymentLinkId": 0
  },
  "PaymentAddress": {
    "RequestGuid": 0
  },
  "ExtraParameters": [
    [
      "INCOMING_DATA",
      "<INCOMING_DATA></INCOMING_DATA>"
    ]
  ]
}

and here is a expected response: (I masked some info)

{
    "PaymentRequest": {
        "RequestGuid": 1000000060391232,
        "InsertDatetime": "0001-01-01T00:00:00",
        "MbrId": **,
        "MerchantID": "0********04",
        "OrderId": "T*******13",
        "RequestIp": "MY Ip",
        "RequestStat": "1,10",
        "SecureType": "NonSecure",
        "PurchAmount": "0.1",
        "Exponent": "2",
        "Currency": 949,
        "TerminalID": "******",
        "TxnType": "Auth",
        "CardType": "V",
        "Lang": "**",
        "BonusAmount": "",
        "InstallmentCount": 0,
        "AlphaCode": "**",
        "Ecommerce": 1,
        "MrcCountryCode": "****",
        "MrcName": "*****",
        "MerchantHomeUrl": " BANK'S URI",
        "CardHolderName": "B********e",
        "TxnStatus": "Y",
        "ErrMsg": "Success",
        "TxnResult": "Success",
        "AuthCode": "S66172",
        "ProcReturnCode": "00",
        "BatchNo": 2285,
        "CardMask": "415565******6111",
        "ReqId": 6947449,
        "UsedPoint": 0.0,
        "SrcType": "VPO",
        "RefundedAmount": 0.0,
        "RefundedPoint": 0.0,
        "ReqDate": 20210614,
        "SysDate": 20210614,
        "F11": 262812,
        "F37": "116513262812",
        "VposElapsedTime": 17,
        "BankingElapsedTime": 0,
        "SocketElapsedTime": 0,
        "HsmElapsedTime": 4,
        "MpiElapsedTime": 0,
        "hasOrderId": true,
        "TemplateType": 0,
        "HasAddressCount": false,
        "IsPaymentFacilitator": false,
        "MerchantCountryCode": "**",
        "F11_ORG": 0,
        "F12_ORG": 0,
        "F22_ORG": 0,
        "F25_ORG": 0,
        "MTI_ORG": 0,
        "IntervalType": 0,
        "IntervalDuration": 0,
        "RepeatCount": 0,
        "RequestClientIp": "my IP",
        "VoidTime": 0,
        "PaymentLinkId": 0
    },
    "PaymentAddress": {
        "RequestGuid": 0
    },
    "ExtraParameters": [
        [
            "INCOMING_DATA",
            "<INCOMING_DATA><MbrId>5</MbrId><MerchantID>********</MerchantID><UserCode>Q*******Y</UserCode><UserPass_masked>*****</UserPass_masked><OrderId>T*******3</OrderId><SecureType>NonSecure</SecureType><InstallmentCount>0</InstallmentCount><TxnType>Auth</TxnType><PurchAmount>0.1</PurchAmount><Currency>***</Currency><CardHolderName>B*******e</CardHolderName><Pan_masked>41*********6111</Pan_masked><Expiry>****</Expiry><Cvv2_masked>***</Cvv2_masked><Lang>TR</Lang></INCOMING_DATA>"
        ],
        [
            "PAYFORFROMJSONREQUEST",
            "1"
        ],
        [
            "SESSION_MRC_CODE",
            "085300000009704"
        ],
        [
            "SESSION_SYSTEM_USER",
            "0"
        ]
    ]
}

and this is how HttpClient is created at Startup.cs

services.AddScoped(sp =>
            {
                var client = new HttpClient
                {
                    BaseAddress = new Uri("BANK's URI")
                };
                return client;
            });
Kaan Taze
  • 1,066
  • 1
  • 11
  • 19
  • Can you post the full exception message? Your ```PostAsync``` call seems to be fine. Also, your creation of the ```HttpClient``` would be interesting. – devsmn Jun 14 '21 at 12:10
  • Added them all at the post – Kaan Taze Jun 14 '21 at 12:19
  • I vaguely remember having issues with using the ```BaseAddress``` property of the ```HttpClient```. Can you try removing that line completely and passing the full URL in the ```PostAsync``` call? Edit: Seems like [this question](https://stackoverflow.com/a/23438417/10196137) is related to that. Simply removing it all together might do the trick. – devsmn Jun 14 '21 at 12:21
  • 1
    `RestClient` is a type produced by RestSharp, a third party. `HttpClient` is a type included in .NET Core. (to answer the title question) – Heretic Monkey Jun 14 '21 at 12:22
  • You can fish my answer at this URL. "Maybes" include how I'm adding the content-headers.. and you can at least wire-up the security-callback method to make sure it is not barfing.... https://stackoverflow.com/questions/38494279/how-do-i-get-an-oauth-2-0-authentication-token-in-c-sharp/53787718#53787718 – granadaCoder Jun 14 '21 at 12:22
  • Since you got a 200 OK then the issue is you need to install the plugin on the deployed system. Do you know which plugin is missing? – jdweng Jun 14 '21 at 12:50

0 Answers0