Reputation: 1684
I've incorporated Payfast via their custom integration. I'm on the last step to validate the request with Payfast by making a request to the url: https://sandbox.payfast.co.za/eng/query/validate
.
string concat = "pf_payment_id=185322&payment_status=COMPLETE&item_name=ON_123&amount_gross=261.50&merchant_id=00030878...etc";
using (var httpClient = new HttpClient())
{
string jsonString = JsonConvert.SerializeObject(concat);
HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var url = _configuration["Payment:Payfast:ValidateUrl"];
using (var response = await httpClient.PostAsync(url, content))
{
}
}
Response:
{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Server: nginx/1.19.6
Date: Thu, 21 Sep 2023 18:52:08 GMT
Cache-Control: no-cache, private
Via: 1.1 google
Strict-Transport-Security: max-age=63072000 ; includeSubDomains
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Content-Type: text/html; charset=UTF-8
Content-Length: 630
Allow: GET, HEAD
}}
According to their documentation the method is POST. What am I doing wrong?
Upvotes: 0
Views: 806
Reputation: 383
First add httpclient
services to your project
builder.Services.AddHttpClient();
Next use dependency injection
private readonly IHttpClientFactory _clientFactory;
public SendDataController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
And send data like this :
var values = new Dictionary<string, string>()
{
{ "id", "1255" },
{ "status", "Complete"},
{ "item_name", "ON_123" },
//Other values
};
var content = new FormUrlEncodedContent(values);
using var Request = new HttpRequestMessage();
Request.Method = HttpMethod.Post;
Request.RequestUri = new Uri("YourURL");
Request.Content = content;
using var client = _clientFactory.CreateClient();
using var response = await client.SendAsync(Request);
Request.Dispose();
string result = await response.Content.ReadAsStringAsync();
response.Dispose();
//Other Operations
Update
If you must send json can use this code :
object obj = new { id = 1599, status = "payed" /*Other values*/ };
string content = JsonConvert.SerializeObject(obj);
using var client = _clientFactory.CreateClient();
using var Request = new HttpRequestMessage();
Request.Method = HttpMethod.Post;
Request.RequestUri = new Uri("YourUrl");
Request.Content = new StringContent(content, Encoding.UTF8, "application/json");
using var response = await client.SendAsync(Request);
Request.Dispose();
string result = await response.Content.ReadAsStringAsync();
response.Dispose();
//Other Operations
Any where is unclear call me.
Upvotes: 0