1

I am trying to integrate Payfast api with my app. In WebView I can pass requested data to given https address properly but when ıt came to https request side errors occur. Simply I passed the same data to same path. What is the problem, your help is really appreciated. Thanks for now

payfast

Below code when I press the submit button it redirects proper payment page;

                  WebView(

                        initialUrl:  new Uri.dataFromString('''   <!DOCTYPE html> <html>
                        <style> 
                        input[type=button], input[type=submit], input[type=reset] {
  background-color: #FF0000;
  border: none;
  color: white;
  padding: 16px 32px;
  text-decoration: none;
  margin: 8px 4px;
  cursor: pointer;
}
</style>
  <body style="background-color:#ffffff;">
    


 <form action="https://sandbox.payfast.co.za/eng/process" method="post">
   <input type="hidden" name="merchant_id" value="10022814">
   <input type="hidden" name="merchant_key" value="aemfu0aapxale">
   <input type="hidden" name="amount" value="$miktar">
   <input type="hidden" name="item_name" value="Test Product">
   <input type="submit" value="PAY NOW">
</form>

  </body>



  </html>
                          ''',
                            mimeType: 'text/html').toString(),
                        javascriptMode: JavascriptMode.unrestricted,
                        gestureNavigationEnabled: true,
           
                    ),

But 403 error Occur when http request is made.

    HttpClient httpClient = new HttpClient();
    HttpClientRequest request = await httpClient.postUrl(Uri.parse( "https://sandbox.payfast.co.za/onsite/process"));
    request.headers.set('accept' ,  'application/json');
    request.headers.set('merchant-id' ,  '10022814');
    request.headers.set('merchant_key' , 'aemfu0aapxale');





//sbtu01@payfast.co.za
//headers
//request.headers.set('Accept' ,  'application/json');
    Map body = { 'merchant_id' : '10022814',
      'merchant_key' : 'aemfu0aapxale',
      'amount' : '100.00',
      'item_name' : '#000001'} ;

//body
    request.add(utf8.encode(json.encode(body)));


//response cevap
    HttpClientResponse response = await request.close();
    print(response.statusCode); // baglanti yapildi mi yapilmadi mi 200 ise yapildi
    String reply = await response.transform(utf8.decoder).join();
  
    httpClient.close();
    print("payfast ici odeme");
    print(reply);
Muhtar
  • 1,506
  • 1
  • 8
  • 33
  • The HTTP 403 is a HTTP status code meaning access to the requested resource is forbidden. The server understood the request, but will not fulfill it. Are you sure you sent the correct credentials to the server? – Jaime Ortiz May 30 '21 at 19:42
  • I am not sure that I sent all creadentials but It seems the two requests are same. I couldn't figure out what to do now – Muhtar May 30 '21 at 19:47
  • which two requests are the same? a 404 error usually means you do not have permission to access that URL so my guess is that the account you are using to access that API does no have permission or you are sending an invalid API key... the server could also be expecting some information you are not sending. – Jaime Ortiz May 30 '21 at 20:00
  • I meant webview request and http request. First part of my repo shows how I show webview and when submit clicked everything is fine. But as you refer it is possible me to miss some vars in request but I could not get from the developer documentation of Payfast – Muhtar May 30 '21 at 20:06
  • same problem, when i run API in web it's working correctly but not working in flutter app – Syed Daniyal Ali Mar 14 '22 at 12:01

1 Answers1

0

Is the API that you're trying to send the request to valid? It's likely that the payload that's being sent is invalid hence the 4XX error - which is usually caused by unauthenticated requests. If the request is valid, you can try sending the http request as form-data:

sendRequest() async {
  final uri = 'https://sandbox.payfast.co.za/onsite/process';

  http.Response response = await http.post(
      uri, body: {
    'merchant_id':'10022814',
    'merchant_key':'aemfu0aapxale',
    'amount':'100.00',
    'item_name':'#000001'
  });

  debugPrint(response.body);
}
Omatt
  • 8,564
  • 2
  • 42
  • 144