0

I need to send a request with the following data

"order": {
   "server_callback_url": "http://site.id/callback",
   "currency": "UAH",
   "amount": "1400",
   "order_type": "settlement",
   "response_url": "http://site.id/test/responsepage/",
   "order_id": "test1234561467462099.19",
   "operation_id": "test1234561467462099.19",
   "order_desc": "test order",
   "merchant_id": 700001,
   "receiver": [
     {
       "requisites": {
         "amount": 100,
         "merchant_id": 500001
       },
       "type": "merchant"
     },{
       "requisites": {
         "amount": 200,
         "merchant_id": 600001
       },
       "type": "merchant"
     },
  ]
}

I need to send them to https://api.fondy.eu/api/settlement

But I never did that. I am not familiar with DRF at all. Tell me how to implement it, please.

Valentin Garreau
  • 963
  • 1
  • 10
  • 32
unknown
  • 252
  • 3
  • 12
  • 37

2 Answers2

2

You could use DRF as the documentation : https://www.django-rest-framework.org/tutorial/2-requests-and-responses/

Or without DRF :

# importing the requests library 
import requests 

# api-endpoint 
URL = "https://api.fondy.eu/api/settlement"


# defining a params dict for the parameters to be sent to the API 
 data =
   "order": {
   "server_callback_url": "http://site.id/callback",
   "currency": "UAH",
   "amount": "1400",
   "order_type": "settlement",
   "response_url": "http://site.id/test/responsepage/",
   "order_id": "test1234561467462099.19",
   "operation_id": "test1234561467462099.19",
   "order_desc": "test order",
   "merchant_id": 700001,
   "receiver": [
     {
       "requisites": {
         "amount": 100,
         "merchant_id": 500001
       },
       "type": "merchant"
     },{
       "requisites": {
         "amount": 200,
         "merchant_id": 600001
       },
       "type": "merchant"
     },
  ]
}

# sending get request and saving the response as response object 
r = requests.POST(url = URL, data= data) 

# extracting data in json format 
data = r.json() 

Maybe you would like to try the API before writing code, there is tool like postman to do this quickly :)

Valentin Garreau
  • 963
  • 1
  • 10
  • 32
2

If we visit the endpoint, it says that only POST methods are allowed. We can make a POST request for example with the requests package.

import requests

data = {
  "order": {
     "server_callback_url": "http://site.id/callback",
     "currency": "UAH",
     "amount": "1400",
     "order_type": "settlement",
     "response_url": "http://site.id/test/responsepage/",
     "order_id": "test1234561467462099.19",
     "operation_id": "test1234561467462099.19",
     "order_desc": "test order",
     "merchant_id": 700001,
     "receiver": [
       {
         "requisites": {
         "amount": 100,
         "merchant_id": 500001
       },
       "type": "merchant"
     },{
       "requisites": {
           "amount": 200,
           "merchant_id": 600001
       },
       "type": "merchant"
     },
    ]
  }
}

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.post('https://api.fondy.eu/api/settlement', data=data, headers=headers)

The header can be important, since it tells you in what format you make your reqest, and some APIs do not work if you do not set the Content-type properly.

The response is here a Response object, and you can parse the response to a Python dictionary with:

response.json()

locally this gives me:

{'response': {'error_code': 1002,
              'error_message': 'Application error',
              'request_id': 'iUxQzJfyBuxdI'}}

The status code is 200, so that probably means that the callback you specified was not valid (or some other items in your request).

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks. And somehow you can understand what exactly gives error 1002? – unknown May 15 '19 at 15:18
  • @ДмитрийДмитрук: well you will need to consult the documentation of the endpoint you trigger, since that depends on the application. – Willem Van Onsem May 15 '19 at 15:19
  • Here is the documentation. Only she is in Russian, unfortunately ... https://docs.google.com/document/d/198d8Zbb7cgx-vAXAhNn_Oyhqc-fG1UXe4W0G-aQJzkE/edit# – unknown May 15 '19 at 15:21
  • 2
    https://docs.fondy.eu/docs/page/21/ says that error 1002 is an unknown error and you should contact support. – dirkgroten May 15 '19 at 15:56