‎2022 May 18 9:27 AM
Hi experts,
I'm new working with APIs. I've been asked to use an API for pay by link.
This is how it looks the API on postman:

And this is how my report looks:
REPORT zzmaria.
TYPES: BEGIN OF ty_json_req,
reference TYPE string,
value TYPE string,
currency TYPE string,
countrycode TYPE string,
merchantaccount TYPE string,
shopperreference TYPE string,
shopperemail TYPE string,
shopperlocale TYPE string,
key TYPE string,
END OF ty_json_req.
DATA: l_datos TYPE ty_json_req,
t_datos TYPE STANDARD TABLE OF ty_json_req.
DATA gs_json TYPE string.
*HTTP Client Abstraction
DATA: lo_client TYPE REF TO if_http_client.
DATA : lo_http_client TYPE REF TO if_http_client,
lv_response TYPE string,
lv_sap_res TYPE string.
DATA : l_str_length TYPE i.
DATA : mime TYPE w3mimetabtype.
DATA : lo_http_request TYPE REF TO if_http_entity.
START-OF-SELECTION.
*Data variables for storing response in xstring and string
DATA : lv_xstring TYPE xstring,
lv_string TYPE string,
lv_body TYPE string,
lv_node_name TYPE string.
CLEAR : lv_xstring, lv_string, lv_node_name.
DATA : lv_cdata TYPE string.
DATA : lv_content_length_value TYPE i.
CLEAR : lv_cdata, lv_content_length_value, gs_json.
*Pass the URL to get Data
lv_string = 'https://XXXXXXXXXXXXXX/paymentLinks'.
*Creation of New IF_lo_http_client Object
cl_http_client=>create_by_url(
EXPORTING
url = lv_string
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
).
IF sy-subrc IS NOT INITIAL.
* Handle errors
ENDIF.
*lv_body = '{"reference": "FVM1", "amount": {"value": 125, "currency": "EUR" }, "countryCode": "ES", "merchantAccount": "XXXXXX", "shopperReference": "1234567890", "shopperEmail": "xxxxxx@xxxx.com",' &&
* '"shopperLocale": "es-ES", "metadata": {"key": "R0C060"}}'.
l_datos-reference = 'FVM1'.
l_datos-value = '125'.
l_datos-currency = 'EUR'.
l_datos-countrycode = 'ES'.
l_datos-merchantaccount = 'xxxxxxxx'.
l_datos-shopperreference = '1234567890'.
l_datos-shopperemail = 'xxxxx@xxxx.com'.
l_datos-shopperlocale = 'es-ES'.
l_datos-key = 'R0C060'.
/ui2/cl_json=>serialize(
EXPORTING
data = l_datos " Data to serialize
RECEIVING
r_json = gs_json " JSON string
).
PERFORM json_naming.
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
*set http method POST
CALL METHOD lo_http_client->request->set_method(
if_http_request=>co_request_method_post ).
*set protocol version
lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).
CALL METHOD lo_http_client->request->if_http_entity~set_formfield_encoding
EXPORTING
formfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw.
*content type
CALL METHOD lo_http_client->request->if_http_entity~set_content_type
EXPORTING
content_type = if_rest_media_type=>gc_appl_json.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'x-api-key:'
value = 'FSSGDGdgdgdghd'.
*get data
CLEAR : lv_sap_res, lv_response.
lo_http_request = lo_http_client->request.
**append data
*lo_http_client->request->set_cdata( EXPORTING data = lv_body ).
lo_http_request->append_cdata( data = gs_json ).
CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
*response
DATA(lo_res) = lo_http_client->response.
lv_response = lo_http_client->response->get_cdata( ).
WRITE : lv_response.
*&---------------------------------------------------------------------*
*& Form JSON_NAMING
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM json_naming .
REPLACE ALL OCCURRENCES OF 'REFERENCE' IN gs_json WITH 'reference'.
REPLACE ALL OCCURRENCES OF 'VALUE' IN gs_json WITH 'amount.value'.
REPLACE ALL OCCURRENCES OF 'CURRENCY' IN gs_json WITH 'amount.currency'.
REPLACE ALL OCCURRENCES OF 'COUNTRYCODE' IN gs_json WITH 'countryCode'.
REPLACE ALL OCCURRENCES OF 'MERCHANTACCOUNT' IN gs_json WITH 'merchantAccount'.
REPLACE ALL OCCURRENCES OF 'SHOPPERreference' IN gs_json WITH 'shopperReference'.
REPLACE ALL OCCURRENCES OF 'SHOPPEREMAIL' IN gs_json WITH 'shopperEmail'.
REPLACE ALL OCCURRENCES OF 'SHOPPERLOCALE' IN gs_json WITH 'shopperLocale'.
REPLACE ALL OCCURRENCES OF 'KEY' IN gs_json WITH 'metadata.key'.
ENDFORM.
<br>
I get an error with the body. I don't know how to use it.
this is the error:
{"status":400,"errorCode":"702","message":"Structure of CreatePaymentLinkRequest contains the following unknown fields: [amount.currency, metadata.key, amount.value]","errorType":"validation"}
Thanks in advance.
Maria
‎2022 May 18 12:01 PM
Given that your API call works in Postman, you have to use the proper nesting level in ABAP typing to be able to generate the proper JSON format:
TYPES BEGIN OF ty_json_req.
...
TYPES BEGIN OF amount.
TYPES value TYPE string.
TYPES currency TYPE string.
TYPES END OF amount.
....
TYPES END OF ty_json_req.<br>And you should use the following serialization parameter to get lower-case results, as it is much cleaner and safer then a post-correction:
pretty_name = /ui2/cl_json=>pretty_mode-camel_caseIf you name an ABAP component e.g. shopper_reference it should translate to shopperReference in JSON
‎2022 May 18 10:27 AM
If you say there is an error, give information about the error, where it happens, what are the exact details of this error, etc.
‎2022 May 18 11:25 AM
‎2022 May 18 12:01 PM
Given that your API call works in Postman, you have to use the proper nesting level in ABAP typing to be able to generate the proper JSON format:
TYPES BEGIN OF ty_json_req.
...
TYPES BEGIN OF amount.
TYPES value TYPE string.
TYPES currency TYPE string.
TYPES END OF amount.
....
TYPES END OF ty_json_req.<br>And you should use the following serialization parameter to get lower-case results, as it is much cleaner and safer then a post-correction:
pretty_name = /ui2/cl_json=>pretty_mode-camel_caseIf you name an ABAP component e.g. shopper_reference it should translate to shopperReference in JSON
‎2022 May 18 12:45 PM
Thanks Gábor, problem solved !!! 🙂
How can I retrieve one of the fields from LV_RESPONSE?
{"amount":{"currency":"EUR","value":125},"countryCode":"ES","expiresAt":"2022-05-19T11:41:15Z","merchantAccount":"XXXXX","metadata":{"key":"R0C060"},"reference":"FVM1","reusable":false,"shopperEmail":"XXXX@XXX.com","shopperLocale":"es-ES","shopperReference":"1234567890","id":"PL3E7Bsdfs025BBF3","status":"active","url":"https:\/\/test.adyen.linkRRRRR"}
I need the URL.
thanks !!
Maria
‎2022 May 18 12:55 PM
mariamerino
Its the same story in the opposite direction.
Create an ABAP structure with fields that corresponds to the structure of the response and use method DESERIALIZE.
‎2022 May 18 12:58 PM
‎2022 May 18 12:12 PM
Okay, at last line of code, LV_RESPONSE contains JSON data (that you have posted) which contains the error.
‎2022 May 18 12:14 PM
Your question should be different.
Your issue is that you don't find a way to initialize GS_JSON same as in PostMan.
So, your question is about how to use /UI2/CL_JSON. It's not related to "API post with body".
‎2022 May 18 12:58 PM
Sandra, it's the first time I work with APIs, sorry if I use the words correctly.