利用APIFOX对ABAP函数进行调用

发布时间 2023-06-02 15:52:12作者: Lovemywx2

1.安装APIFOX,当然也可以使用在线版,无需下载

官网地址:https://apifox.com/

 

2.新建项目

 3.为项目起一个名称,为相关开发测试人员授权

 

4.在根目录新增子目录

 

5.编辑开发/测试/生产环境的地址信息以及默认全局变量

 

6.将SAP提供的ABAPAPI地址配置进APIFOX,有关ABAP提供的服务地址,请参考文章:

1.ABAP 功能模块的 JSON 适配

2.RESTFUL方式 

 

 

7.与前一篇文章有所不同的是,本次在ABAP类中增加了更新模块,如图,请见代码:

 8.在APIFOX新建好服务URL后,可以在根目录设置用户密码,这样后面子节点就可以继承根目录的用户配置

 

9.新建调用接口[本例是SAP函数:BAPI_ACC_DOCUMENT_POST]

9.1在BODY页签,现在json格式,输入测试的报文

{
    "documentheader": {
        "obj_type": "",
        "obj_key": "",
        "obj_sys": "",
        "bus_act": "",
        "username": "CHENHUI",
        "header_txt": "抬头文本",
        "comp_code": "1710",
        "doc_date": "2023-06-02",
        "pstng_date": "2023-06-02",
        "trans_date": "0000-00-00",
        "fisc_year": 2023,
        "fis_period": 6,
        "doc_type": "SA",
        "ref_doc_no": "参照字段",
        "ac_doc_no": "",
        "obj_key_r": "",
        "reason_rev": "",
        "compo_acc": "",
        "ref_doc_no_long": "",
        "acc_principle": "",
        "neg_postng": "",
        "obj_key_inv": "",
        "bill_category": "",
        "vatdate": "0000-00-00",
        "invoice_rec_date": "0000-00-00",
        "ecs_env": "",
        "partial_rev": "",
        "doc_status": ""
    },
    "customercpd": {
        "name": "",
        "name_2": "",
        "name_3": "",
        "name_4": "",
        "postl_code": "",
        "city": "",
        "country": "",
        "country_iso": "",
        "street": "",
        "po_box": "",
        "pobx_pcd": "",
        "pobk_curac": "",
        "bank_acct": "",
        "bank_no": "",
        "bank_ctry": "",
        "bank_ctry_iso": "",
        "tax_no_1": "",
        "tax_no_2": "",
        "tax": "",
        "equal_tax": "",
        "region": "",
        "ctrl_key": "",
        "instr_key": "",
        "dme_ind": "",
        "langu_iso": "",
        "iban": "",
        "swift_code": "",
        "tax_no_3": "",
        "tax_no_4": "",
        "title": "",
        "tax_no_5": ""
    },
    "contractheader": {
        "doc_no": "",
        "doc_type_ca": "",
        "res_key": "",
        "fikey": "",
        "payment_form_ref": ""
    },
    "accountgl": [
        {
            "itemno_acc": 1,
            "gl_account": "0012121000",
            "item_text": "",
            "stat_con": "",
            "log_proc": "",
            "ac_doc_no": "",
            "ref_key_1": "",
            "ref_key_2": "",
            "ref_key_3": "",
            "acct_key": "",
            "acct_type": "",
            "doc_type": "",
            "comp_code": "",
            "bus_area": "",
            "func_area": "",
            "plant": "",
            "fis_period": 0,
            "fisc_year": 0,
            "pstng_date": "0000-00-00",
            "value_date": "2023-06-02",
            "fm_area": "",
            "customer": "",
            "cshdis_ind": "",
            "vendor_no": "",
            "alloc_nmbr": "",
            "tax_code": "",
            "taxjurcode": "",
            "ext_object_id": "",
            "bus_scenario": "",
            "costobject": "",
            "costcenter": "",
            "acttype": "",
            "profit_ctr": "",
            "part_prctr": "",
            "network": "",
            "wbs_element": "",
            "orderid": "",
            "order_itno": 0,
            "routing_no": 0,
            "activity": "",
            "cond_type": "",
            "cond_count": 0,
            "cond_st_no": 0,
            "fund": "",
            "funds_ctr": "",
            "cmmt_item": "",
            "co_busproc": "",
            "asset_no": "",
            "sub_number": "",
            "bill_type": "",
            "sales_ord": "",
            "s_ord_item": 0,
            "distr_chan": "",
            "division": "",
            "salesorg": "",
            "sales_grp": "",
            "sales_off": "",
            "sold_to": "",
            "de_cre_ind": "",
            "p_el_prctr": "",
            "xmfrw": "",
            "quantity": 0,
            "base_uom": "",
            "base_uom_iso": "",
            "inv_qty": 0,
            "inv_qty_su": 0,
            "sales_unit": "",
            "sales_unit_iso": "",
            "po_pr_qnt": 0,
            "po_pr_uom": "",
            "po_pr_uom_iso": "",
            "entry_qnt": 0,
            "entry_uom": "",
            "entry_uom_iso": "",
            "volume": 0,
            "volumeunit": "",
            "volumeunit_iso": "",
            "gross_wt": 0,
            "net_weight": 0,
            "unit_of_wt": "",
            "unit_of_wt_iso": "",
            "item_cat": "",
            "material": "",
            "matl_type": "",
            "mvt_ind": "",
            "reval_ind": "",
            "orig_group": "",
            "orig_mat": "",
            "serial_no": 0,
            "part_acct": "",
            "tr_part_ba": "",
            "trade_id": "",
            "val_area": "",
            "val_type": "",
            "asval_date": "0000-00-00",
            "po_number": "",
            "po_item": 0,
            "itm_number": 0,
            "cond_category": "",
            "func_area_long": "",
            "cmmt_item_long": "",
            "grant_nbr": "",
            "cs_trans_t": "",
            "measure": "",
            "segment": "",
            "partner_segment": "",
            "res_doc": "",
            "res_item": 0,
            "billing_period_start_date": "0000-00-00",
            "billing_period_end_date": "0000-00-00",
            "ppa_ex_ind": "",
            "fastpay": "",
            "partner_grant_nbr": "",
            "budget_period": "",
            "partner_budget_period": "",
            "partner_fund": "",
            "itemno_tax": 0,
            "payment_type": "",
            "expense_type": "",
            "program_profile": "",
            "material_long": "",
            "housebankid": "",
            "housebankacctid": "",
            "person_no": 0,
            "acrobj_type": "",
            "acrobj_id": "",
            "acrsubobj_id": "",
            "acritem_type": "",
            "valobjtype": "",
            "valobj_id": "",
            "valsubobj_id": ""
        },
        {
            "itemno_acc": 2,
            "gl_account": "0012401100",
            "item_text": "",
            "stat_con": "",
            "log_proc": "",
            "ac_doc_no": "",
            "ref_key_1": "",
            "ref_key_2": "",
            "ref_key_3": "",
            "acct_key": "",
            "acct_type": "",
            "doc_type": "",
            "comp_code": "",
            "bus_area": "",
            "func_area": "",
            "plant": "",
            "fis_period": 0,
            "fisc_year": 0,
            "pstng_date": "0000-00-00",
            "value_date": "2023-06-02",
            "fm_area": "",
            "customer": "",
            "cshdis_ind": "",
            "vendor_no": "",
            "alloc_nmbr": "",
            "tax_code": "",
            "taxjurcode": "",
            "ext_object_id": "",
            "bus_scenario": "",
            "costobject": "",
            "costcenter": "",
            "acttype": "",
            "profit_ctr": "",
            "part_prctr": "",
            "network": "",
            "wbs_element": "",
            "orderid": "",
            "order_itno": 0,
            "routing_no": 0,
            "activity": "",
            "cond_type": "",
            "cond_count": 0,
            "cond_st_no": 0,
            "fund": "",
            "funds_ctr": "",
            "cmmt_item": "",
            "co_busproc": "",
            "asset_no": "",
            "sub_number": "",
            "bill_type": "",
            "sales_ord": "",
            "s_ord_item": 0,
            "distr_chan": "",
            "division": "",
            "salesorg": "",
            "sales_grp": "",
            "sales_off": "",
            "sold_to": "",
            "de_cre_ind": "",
            "p_el_prctr": "",
            "xmfrw": "",
            "quantity": 0,
            "base_uom": "",
            "base_uom_iso": "",
            "inv_qty": 0,
            "inv_qty_su": 0,
            "sales_unit": "",
            "sales_unit_iso": "",
            "po_pr_qnt": 0,
            "po_pr_uom": "",
            "po_pr_uom_iso": "",
            "entry_qnt": 0,
            "entry_uom": "",
            "entry_uom_iso": "",
            "volume": 0,
            "volumeunit": "",
            "volumeunit_iso": "",
            "gross_wt": 0,
            "net_weight": 0,
            "unit_of_wt": "",
            "unit_of_wt_iso": "",
            "item_cat": "",
            "material": "",
            "matl_type": "",
            "mvt_ind": "",
            "reval_ind": "",
            "orig_group": "",
            "orig_mat": "",
            "serial_no": 0,
            "part_acct": "",
            "tr_part_ba": "",
            "trade_id": "",
            "val_area": "",
            "val_type": "",
            "asval_date": "0000-00-00",
            "po_number": "",
            "po_item": 0,
            "itm_number": 0,
            "cond_category": "",
            "func_area_long": "",
            "cmmt_item_long": "",
            "grant_nbr": "",
            "cs_trans_t": "",
            "measure": "",
            "segment": "",
            "partner_segment": "",
            "res_doc": "",
            "res_item": 0,
            "billing_period_start_date": "0000-00-00",
            "billing_period_end_date": "0000-00-00",
            "ppa_ex_ind": "",
            "fastpay": "",
            "partner_grant_nbr": "",
            "budget_period": "",
            "partner_budget_period": "",
            "partner_fund": "",
            "itemno_tax": 0,
            "payment_type": "",
            "expense_type": "",
            "program_profile": "",
            "material_long": "",
            "housebankid": "",
            "housebankacctid": "",
            "person_no": 0,
            "acrobj_type": "",
            "acrobj_id": "",
            "acrsubobj_id": "",
            "acritem_type": "",
            "valobjtype": "",
            "valobj_id": "",
            "valsubobj_id": ""
        }
    ],
    "accountreceivable": [],
    "accountpayable": [],
    "accounttax": [],
    "currencyamount": [
        {
            "itemno_acc": 1,
            "curr_type": "00",
            "currency": "CNY",
            "currency_iso": "",
            "amt_doccur": 100.0000,
            "exch_rate": 0,
            "exch_rate_v": 0,
            "amt_base": 0,
            "disc_base": 0,
            "disc_amt": 0,
            "tax_amt": 0,
            "amt_doccur_long": 0,
            "amt_base_long": 0,
            "disc_base_long": 0,
            "disc_amt_long": 0,
            "tax_amt_long": 0
        },
        {
            "itemno_acc": 2,
            "curr_type": "00",
            "currency": "CNY",
            "currency_iso": "",
            "amt_doccur": -100.0000,
            "exch_rate": 0,
            "exch_rate_v": 0,
            "amt_base": 0,
            "disc_base": 0,
            "disc_amt": 0,
            "tax_amt": 0,
            "amt_doccur_long": 0,
            "amt_base_long": 0,
            "disc_base_long": 0,
            "disc_amt_long": 0,
            "tax_amt_long": 0
        }
    ],
    "criteria": [],
    "valuefield": [],
    "extension1": [],
    "return": [],
    "paymentcard": [],
    "contractitem": [],
    "extension2": [
        {
            "structure": "ZSENBSEG",
            "valuepart1": "000000000140      字段一       字段二       字段三       字段四       字段五",
            "valuepart2": "",
            "valuepart3": "",
            "valuepart4": ""
        },
        {
            "structure": "ZSENBSEG",
            "valuepart1": "000000000250      字段一       字段二       字段三       字段四       字段五",
            "valuepart2": "",
            "valuepart3": "",
            "valuepart4": ""
        }
    ],
    "realestate": [],
    "accountwt": []
}
View Code

测试的报文是由ABAP测试生成而来:

*&---------------------------------------------------------------------*
*& Report Z3426008A
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z3426008a.
DATA:ls_header TYPE bapiache09,
     lv_key    TYPE bapiache09-obj_key,
     lv_msg    TYPE string,
     ls_struct TYPE zsenbseg,
     lv_posnr  TYPE i.
DATA:lt_gl TYPE bapiacgl09 OCCURS 0 WITH HEADER LINE,
     lt_cr TYPE bapiaccr09 OCCURS 0 WITH HEADER LINE,
     lt_ex TYPE bapiparex OCCURS 0 WITH HEADER LINE,
     lt_rt TYPE bapiret2 OCCURS 0 WITH HEADER LINE.

*参考会计凭证 100000006 F011 2021
ls_header-header_txt = '抬头文本'.
ls_header-comp_code = '1710'.
ls_header-doc_type = 'SA'.
*ls_header-obj_type = 'BKPF'.
ls_header-username = sy-uname.
ls_header-doc_date = sy-datum.
ls_header-pstng_date = sy-datum.
ls_header-fisc_year = sy-datum+0(4).
ls_header-fis_period = sy-datum+4(2).
ls_header-ref_doc_no = '参照字段'.

lv_posnr = lv_posnr + 1.
lt_gl-itemno_acc = lv_posnr.
lt_gl-gl_account = '0012121000'.
lt_gl-value_date = sy-datum.
APPEND lt_gl.

lt_cr-itemno_acc = lv_posnr.             " 会计凭证行项目编号
lt_cr-curr_type  = '00'.          " 货币和评估视图
lt_cr-currency   = 'CNY'.         " 货币码
lt_cr-amt_doccur = '100'.
APPEND lt_cr.

ls_struct-posnr = lv_posnr.
ls_struct-bschl = '40'.
ls_struct-rstgr = ''.
ls_struct-zzfield1 = '字段一'.
ls_struct-zzfield2 = '字段二'.
ls_struct-zzfield3 = '字段三'.
ls_struct-zzfield4 = '字段四'.
ls_struct-zzfield5 = '字段五'.
lt_ex-structure = 'ZSENBSEG'.
lt_ex-valuepart1 = ls_struct.
APPEND lt_ex.

lv_posnr = lv_posnr + 1.
lt_gl-itemno_acc = lv_posnr.
lt_gl-gl_account = '0012401100'.
lt_gl-value_date = sy-datum.
APPEND lt_gl.

lt_cr-itemno_acc = lv_posnr.      " 会计凭证行项目编号
lt_cr-curr_type  = '00'.          " 货币和评估视图
lt_cr-currency   = 'CNY'.         " 货币码
lt_cr-amt_doccur = '-100'.
APPEND lt_cr.

ls_struct-posnr = lv_posnr.
ls_struct-bschl = '50'.
ls_struct-rstgr = ''.
ls_struct-zzfield1 = '字段一'.
ls_struct-zzfield2 = '字段二'.
ls_struct-zzfield3 = '字段三'.
ls_struct-zzfield4 = '字段四'.
ls_struct-zzfield5 = '字段五'.
lt_ex-structure = 'ZSENBSEG'.
lt_ex-valuepart1 = ls_struct.
APPEND lt_ex.

DATA:BEGIN OF ls_request,
       documentheader    LIKE bapiache09,
       customercpd       LIKE bapiacpa09,
       contractheader    LIKE bapiaccahd,
       accountgl         LIKE TABLE OF bapiacgl09,
       accountreceivable LIKE TABLE OF bapiacar09,
       accountpayable    LIKE TABLE OF bapiacap09,
       accounttax        LIKE TABLE OF bapiactx09,
       currencyamount    LIKE TABLE OF bapiaccr09,
       criteria          LIKE TABLE OF bapiackec9,
       valuefield        LIKE TABLE OF bapiackev9,
       extension1        LIKE TABLE OF bapiacextc,
       return            LIKE TABLE OF bapiret2,
       paymentcard       LIKE TABLE OF bapiacpc09,
       contractitem      LIKE TABLE OF bapiaccait,
       extension2        LIKE TABLE OF bapiparex,
       realestate        LIKE TABLE OF bapiacre09,
       accountwt         LIKE TABLE OF bapiacwt09,
     END OF ls_request.

DATA:BEGIN OF ls_response,
       obj_type          LIKE  bapiache09-obj_type,
       obj_key           LIKE  bapiache09-obj_key,
       obj_sys           LIKE  bapiache09-obj_sys,
       accountgl         LIKE TABLE OF bapiacgl09,
       accountreceivable LIKE TABLE OF bapiacar09,
       accountpayable    LIKE TABLE OF bapiacap09,
       accounttax        LIKE TABLE OF bapiactx09,
       currencyamount    LIKE TABLE OF bapiaccr09,
       criteria          LIKE TABLE OF bapiackec9,
       valuefield        LIKE TABLE OF bapiackev9,
       extension1        LIKE TABLE OF bapiacextc,
       return            LIKE TABLE OF bapiret2,
       paymentcard       LIKE TABLE OF bapiacpc09,
       contractitem      LIKE TABLE OF bapiaccait,
       extension2        LIKE TABLE OF bapiparex,
       realestate        LIKE TABLE OF bapiacre09,
       accountwt         LIKE TABLE OF bapiacwt09,
     END OF ls_response.

ls_request-documentheader   = ls_header.
ls_request-accountgl[]      = lt_gl[].
ls_request-currencyamount[] = lt_cr[].
ls_request-extension2[]     = lt_ex[].

DATA:lv_fname    TYPE string,
     lv_request  TYPE string,
     lv_response TYPE string.

lv_fname = `BAPI_ACC_DOCUMENT_POST`.
lv_request  = /ui2/cl_json=>serialize( data = ls_request compress = abap_false pretty_name = 'L' ).
PERFORM frm_call_function USING lv_fname lv_request CHANGING lv_response.
/ui2/cl_json=>deserialize( EXPORTING json = lv_response pretty_name = 'X' CHANGING data = ls_response ).

FORM frm_call_function USING iv_fname TYPE string
                             iv_request TYPE string
                    CHANGING ov_response TYPE string.
  DATA:lv_url      TYPE string,
       lv_hostname TYPE string,
       lv_port     TYPE string.

  CALL FUNCTION 'TH_GET_VIRT_HOST_DATA'
    EXPORTING
      protocol = 0
      virt_idx = 0
    IMPORTING
      hostname = lv_hostname
      port     = lv_port.

  lv_url = 'http://' && lv_hostname && ':' && lv_port && '/abapapi/' && iv_fname.
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = lv_url
    IMPORTING
      client             = DATA(lo_http_client)
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.

  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.
  lo_http_client->authenticate( EXPORTING username = CONV string( 'HTTP_USER' ) password = CONV string( '123qazQAZ' ) ).
  "lo_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ).
  lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
  lo_http_client->request->set_header_field( name = if_http_header_fields=>content_type value = 'application/json; charset=utf-8' ).
  lo_http_client->request->set_cdata( data   = iv_request offset = 0 length = strlen( iv_request ) ).
  lo_http_client->send(  EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 ).
  lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state  = 2 http_processing_failed = 3 ).
  lo_http_client->get_last_error( IMPORTING message = DATA(lv_message) code = DATA(lv_code) ).
  ov_response = lo_http_client->response->get_cdata( ).
  lo_http_client->close( ).
ENDFORM.
View Code

9.2在header页签输入参数:Commit,值为X,目的是提交数据

 

9.3,在左上角切换环境进行发送测试,如有COOKie干扰测试,可以删除COOKie,点击发送并保存

 

10已经成功生成会计凭证

 11.进到SAP系统,使用事务码FB03查看该会计凭证

 

12.同理也可以调用其他SAP提供的函数或者自开发的函数

13.点击左下角的“文档模式”,切换编辑模式,将测试的报文进行修改,按项目要求生成对方能够理解的接口定义书

 14.点击修改文档页签,点击“JSON/XML智能识别、快捷导入”按钮,将测试报文粘贴后,会自动转成测试的结构对象,按转义将项目要求的字段和字段描述加上,用不上的字段可以保留或者剔除

 

15.如果网络涉及到代理和证书,点击右上角设置按钮,可以配置代理和证书