FUNCTION zfm_sd_so_send_api.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(SALESORDER) TYPE VBELN
*" TABLES
*" IT_OUTPUT TYPE ZTT_SO_API
*"----------------------------------------------------------------------
TYPES: BEGIN OF tp_res,
massistorderid TYPE string,
billnumber TYPE char10, "String,
dmsstatus TYPE string,
message TYPE string,
END OF tp_res.
DATA:gs_res TYPE STANDARD TABLE OF tp_res.
DATA : wa_output TYPE zst_so_api,
wa_output_item TYPE zst_so_api_item.
DATA: lv_service_url TYPE string,
lv_sap_res TYPE string,
l_respuests TYPE string,
lv_response TYPE string,
gs_json TYPE string,
gs_http_vp TYPE ihttpnvp,
gt_http_vp TYPE tihttpnvp,
lv_json TYPE string.
DATA:lo_http_client TYPE REF TO if_http_client,
lo_http_request TYPE REF TO if_http_request.
SELECT SINGLE * FROM vbak
INTO @DATA(wa_vbak)
WHERE vbeln = @salesorder.
IF sy-subrc = 0.
SELECT * FROM vbap
INTO TABLE @DATA(it_vbap)
WHERE vbeln = @salesorder.
IF sy-subrc = 0.
SELECT SINGLE * FROM vbkd
INTO @DATA(wa_vbkd)
WHERE vbeln = @salesorder.
wa_output-salesordertype = wa_vbak-auart.
wa_output-salesorganization = wa_vbak-vkorg.
wa_output-distributionchannel = wa_vbak-vtweg.
wa_output-organizationdivision = wa_vbak-spart.
wa_output-soldtoparty = wa_vbak-kunnr.
* wa_output-salesempid = .
wa_output-customerpaymentterms = wa_vbkd-zterm.
* wa_output-quote_sf_id = .
* wa_output-sap_order_id = wa_vbak.
* wa_output-sap_order_number =.
wa_output-created_date = wa_vbak-erdat.
wa_output-createdbyname = wa_vbak-ernam.
* wa_output-sub_total = wa_vbak-.
* wa_output-total_vat.
* wa_output-totalprice .
ENDIF.
LOOP AT it_vbap INTO DATA(wa_vbap).
wa_output_item-sap_order_item_id = wa_vbap-posnr.
wa_output_item-product_sap_id = wa_vbap-matnr .
wa_output_item-uom = wa_vbap-meins.
wa_output_item-delivered_quantity = wa_vbap-ablfz.
wa_output_item-invoiced_quantity = wa_vbap-zmeng.
* wa_output_item-quantity = .
* wa_output_item-price_unit_price.
wa_output_item-sub_total = wa_vbap-kzwi1.
* wa_output_item-vat = wa_vbap.
* wa_output_item-total_vat.
* wa_output_item-total_price.
APPEND wa_output_item TO wa_output-item_data.
CLEAR : wa_output_item , wa_vbap.
ENDLOOP.
APPEND wa_output TO it_output.
CLEAR : wa_vbkd ,wa_vbak.
ENDIF.
IF it_output[] IS NOT INITIAL.
SELECT SINGLE url,user_name,pass,app_name FROM zapi_url INTO @DATA(ls_api)
WHERE app_name = 'SO_API'.
lv_service_url = ls_api-url.
"fetch token url
TRY.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_service_url " URL
proxy_host = 'proxy' " Logical destination
(specified in function call)
proxy_service = '3128' " Port Number
* ssl_id = " SSL Identity
* sap_username = " ABAP System, User Logon Name
* sap_client = " R/3 System, Client Number from
Logon
* proxy_user = " Proxy user
* proxy_passwd = " Proxy password
* do_not_use_client_cert = abap_false " SSL identity not used for
logon
IMPORTING
client = lo_http_client " HTTP Client Abstraction
EXCEPTIONS
argument_not_found = 1 " Communication Parameters (Host
or Service) Not Available
plugin_not_active = 2 " HTTP/HTTPS communication not
available
internal_error = 3 " Internal Error (e.g. name too
long)
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CALL METHOD lo_http_client->request->set_method
EXPORTING
method = 'POST'.
DATA:lv_user TYPE string.
DATA:lv_pass TYPE string.
lv_user = ls_api-user_name.
lv_pass = ls_api-pass.
lo_http_client->request->set_authorization(
EXPORTING
username = lv_user " " User Name
password = lv_pass " " Password
).
IF wa_output IS NOT INITIAL.
/ui2/cl_json=>serialize(
EXPORTING
data = wa_output " Data to serialize
RECEIVING
r_json = lv_json " JSON string
).
ENDIF.
CALL METHOD lo_http_client->request->append_cdata
EXPORTING
data = lv_json
offset = 0.
"send post
CALL METHOD lo_http_client->send
EXPORTING
timeout = 15 " Timeout of Answer
Waiting Time
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing
method
http_invalid_timeout = 4 " Invalid Time Entry
OTHERS = 5.
IF sy-subrc <> 0.
ENDIF.
"disable
lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1 " Communication Error
http_invalid_state = 2 " Invalid state
http_processing_failed = 3 " Error when processing
method
OTHERS = 4.
IF sy-subrc <> 0.
ENDIF.
lv_json = lo_http_client->response->get_cdata( ).
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_json " JSON string
CHANGING
data = gs_res " Data to serialize
).
IF gs_res IS NOT INITIAL.
ENDIF.
CATCH cx_root.
ENDTRY.
ENDIF.
ENDFUNCTION.