调用外部openapi
...
调用外部openapi
这次我调用的是每刻的发票台账标记接口公开api,使用的是IF_HTTP_CLIENT
注意点
- 第一次返回404,排查发现是SAP对接口的地址有拦截,让basis加一下
- 返回500,有可能是网络不好,也有可能是SSL加密证书没有导入SAP,导入下
- 记得先调用接口认证接口获取token等必要数据,当然不是每个api都需要
- 通过json发送的,注意发生的json格式和对方要求的一样
- ABAP结构序列化转换为json发生json,反序列化从json转换为ABAP结构接收信息
- 网络不好常常出现500 Connection Timed Out,SSL握手失败ICMETIMEOUT(-5),RZ11将参数icm/conn_timeout从5s改成10s
全部代码
FUNCTION ZFIFU005.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(IS_REQ) TYPE /ZJT/SAPPO_REQ_INFO OPTIONAL
*" VALUE(IV_BELNR) TYPE BELNR_D OPTIONAL
*" VALUE(IV_ZOA) TYPE ZDE_ZNUMOA OPTIONAL
*" EXPORTING
*" VALUE(ES_RET) TYPE /ZJT/SAPPO_RET_INFO
*" VALUE(ES_DATA) TYPE ZSFIFU005
*"----------------------------------------------------------------------
DATA: LV_LEN TYPE I, "发送报文长度
LV_URL TYPE STRING, "接口地址
LV_ZNAME TYPE STRING,
LV_ZPASSWD TYPE STRING,
LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, "http客户端
LV_SEND_JSON TYPE STRING, "发送的JSON
LV_REC_JSON TYPE STRING, "接收的JSON
LV_REC_JSON1 TYPE STRING, "接收的JSON
LV_REC_JSON2 TYPE STRING, "接收的JSON
LV_AUTHORIZATION TYPE STRING,
LV_CODE TYPE I, "HTTP 返回状态
LV_REASON TYPE STRING. " HTTP 状态描述
TYPES: BEGIN OF TY_RETURN,
CODE TYPE STRING,
MESSAGE TYPE STRING,
DATA TYPE TABLE OF STRING WITH DEFAULT KEY,
END OF TY_RETURN.
DATA ES_RETURN TYPE TY_RETURN.
"TOKEN的结构
TYPES: BEGIN OF TY_TOKEN,
EXPIRES TYPE STRING,
START_TIME TYPE STRING,
ACCESS_TOKEN TYPE STRING,
MESSAGE TYPE STRING,
STATUS_CODE TYPE STRING,
END OF TY_TOKEN.
TYPES: BEGIN OF TY_TOKEN_DATA,
ENTCODE TYPE STRING,
TOKENID TYPE STRING,
REFRESHTOKENID TYPE STRING,
END OF TY_TOKEN_DATA.
TYPES: BEGIN OF TY_ROOT_JSON,
DATA TYPE TY_TOKEN_DATA,
SUCCESS TYPE STRING,
MESSAGE TYPE STRING,
CODE TYPE STRING,
END OF TY_ROOT_JSON.
DATA: LS_TOKEN TYPE TY_ROOT_JSON.
TYPES:BEGIN OF TY_DATA,
SECRET TYPE STRING,
APP_CODE TYPE STRING,
TIMESTAMP TYPE STRING,
END OF TY_DATA.
DATA LS_SEND TYPE TY_DATA.
DATA LV_ZENTCODE TYPE STRING.
*SHA256加密
DATA:LV_APP_SECRET TYPE STRING,
LV_APP_CODE TYPE STRING,
LV_TIME_STAMP TYPE STRING,
LV_STRING_TO_HASH TYPE STRING,
LV_RESULT TYPE STRING.
*时间戳,java中的System.currentTimeMillis()方法
DATA: LV_TIMESTAMP TYPE SYTABIX.
DATA:ZSJC TYPE STRING.
DATA:DATE LIKE SY-DATUM.
DATA:TIME LIKE SY-UZEIT.
TYPES: BEGIN OF TY_INVOICE_STATE_DEFINITION,
INVOICE_DATA_CODE TYPE STRING, "公司code必填
INVOICE_STATE_CODE TYPE STRING, "发票code必填
INVOICE_STATE_REMARK TYPE STRING,
ENT_CODE TYPE STRING,
CANCELLATION_MARK TYPE STRING,
CANCELLATION_MARK_UPDATE_TIME TYPE I, " 假设时间戳为长整型
SUPPLIER_UNUSUAL TYPE STRING,
SUPPLIER_UNUSUAL_UPDATE_TIME TYPE I, " 假设时间戳为长整型
END OF TY_INVOICE_STATE_DEFINITION.
TYPES: BEGIN OF TY_ROOT,
INVOICE_STATE_DEFINITION_DTOS TYPE TABLE OF TY_INVOICE_STATE_DEFINITION WITH DEFAULT KEY,
END OF TY_ROOT.
DATA: LS_ROOT TYPE TY_ROOT.
DATA: LV_TIME TYPE I,
LV_MKURL TYPE STRING.
*发票查询接口发送json
TYPES:BEGIN OF TY_QUERY,
FORM_CODE TYPE STRING,
END OF TY_QUERY.
DATA LS_QUERY TYPE TY_QUERY.
*发票查询接口接收json的abap结构
TYPES:BEGIN OF TY_LIST,
INVOICE_DATA_CODE TYPE STRING,
END OF TY_LIST.
DATA: BEGIN OF LR_DATA,
BEGIN OF DATA,
LIST TYPE TABLE OF TY_LIST,
HAS_NEXT_PAGE TYPE STRING,
END OF DATA,
SUCCESS TYPE STRING,
MESSAGE TYPE STRING,
CODE TYPE STRING,
END OF LR_DATA.
DATA LV_INVOICEDATACODE TYPE STRING.
DATA LC_LIMIT_MAX TYPE I VALUE '3'.
ZJT-RFC-LOG-B. "日志记录开始,函数进入时写
DO LC_LIMIT_MAX TIMES.
SELECT SINGLE * INTO @DATA(LS_CONFIGURATION) FROM ZFIT029 WHERE ZFILX = '1'. "配置表
CHECK LS_CONFIGURATION-URL IS NOT INITIAL.
CHECK LS_CONFIGURATION-UZEIT IS NOT INITIAL AND LS_CONFIGURATION-DATUM IS NOT INITIAL.
DATA(LV_TOKEN) = LS_CONFIGURATION-ZABAPTOKEN && LS_CONFIGURATION-ZABAPTOKEN2.
LV_MKURL = LS_CONFIGURATION-URL.
CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
EXPORTING
T1 = LS_CONFIGURATION-UZEIT
T2 = SY-UZEIT
D1 = LS_CONFIGURATION-DATUM
D2 = SY-DATUM
IMPORTING
MINUTES = LV_TIME.
IF SY-SUBRC <> 0.
ES_RET-CODE = 'E'.
ES_RET-MSG = '配置表存在错误'.
EXIT.
* Implement suitable error handling here
ELSEIF LV_TIME > '20'.
*------------------------------接口认证接口获取token--------------------------------------------
*SHA256加密
* LV_TIME_STAMP = |{ SY-SYSID }{ SY-DATUM }{ SY-UZEIT }|.
* LV_TIME_STAMP = |{ SY-DATUM }{ SY-UZEIT }|.
IF SY-MANDT = '800'.
LV_APP_CODE = '**********'.
LV_APP_SECRET = '*************'.
ELSEIF SY-MANDT = '300'.
LV_APP_CODE = '**********'.
LV_APP_SECRET = '*************'.
ENDIF.
*格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)
DATE = '19700101'.
TIME = '080000'.
CALL FUNCTION 'SWI_DURATION_DETERMINE'
EXPORTING
START_DATE = DATE
END_DATE = SY-DATUM "应用服务器日期
* END_DATE = SY-DATLO"用户本地日期
START_TIME = TIME
END_TIME = SY-UZEIT "应用服务器时间
* END_TIME = SY-TIMLO"用户本地时间
IMPORTING
DURATION = LV_TIMESTAMP.
ZSJC = LV_TIMESTAMP && '000'."拼接目的为计算毫秒级时间戳
**获取用户所在时区,UTC+8
* DATA LV_TIMEZONE TYPE TIMEZONE.
* CALL FUNCTION 'TZON_GET_USER_TIMEZONE'
* EXPORTING
* IF_USERNAME = SY-UNAME
* IMPORTING
* EF_TIMEZONE = LV_TIMEZONE.
" 将appSecret、appCode和时间戳拼接成字符串
LV_STRING_TO_HASH = |{ LV_APP_SECRET }:{ LV_APP_CODE }:{ ZSJC }|.
TRY.
CL_ABAP_MESSAGE_DIGEST=>CALCULATE_HASH_FOR_CHAR(
EXPORTING
IF_ALGORITHM = 'SHA256'
IF_DATA = LV_STRING_TO_HASH
IMPORTING
EF_HASHSTRING = LV_RESULT
).
CATCH CX_ROOT.
" Eh, what're you gonna do?
ENDTRY.
TRANSLATE LV_RESULT TO LOWER CASE. "字符大写转小写
*LV_URL = 'https://ng-uat.maycur.com/api/openapi/auth/login'.
LV_URL = LV_MKURL && '/api/openapi/auth/login'.
"创建http客户端
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = 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->REQUEST->SET_CONTENT_TYPE(
* CONTENT_TYPE = 'application/json; charset=utf-8'
CONTENT_TYPE = 'application/json'
).
* CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
* EXPORTING
* NAME = 'appCode'
* VALUE = 'AP3GYSNO1TYXRV'.
* CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
* EXPORTING
* NAME = 'secret'
* VALUE = 'wHVFVGslCvPQXPjAzanf92XpOUZfxCxA'.
* CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
* EXPORTING
* NAME = 'timestamp'
** VALUE = 'AP3GYSNO1TYXRV'.
* VALUE = LV_RESULT.
CLEAR LS_SEND.
LS_SEND-SECRET = LV_RESULT.
* LS_SEND-APP_CODE = 'AP3GYSNO1TYXRV'.
LS_SEND-APP_CODE = LV_APP_CODE.
LS_SEND-TIMESTAMP = ZSJC.
"SAP不区分大小写,JAVA/C会区分,
"一定让对方修改代码,不然只能SBB的拼接
CLEAR:LV_SEND_JSON.
LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_SEND COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
"设置待传输内容长度
CLEAR:LV_LEN.
LV_LEN = STRLEN( LV_SEND_JSON ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_SEND_JSON
OFFSET = 0
LENGTH = LV_LEN.
"设置http method 为post
LO_HTTP_CLIENT->REQUEST->SET_METHOD(
*IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
).
* "SAP不区分大小写,JAVA/C会区分,
* "一定让对方修改代码,不然只能SBB的拼接
* CLEAR:LV_SEND_JSON.
* LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = OBJID COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
* "设置待传输内容长度
* CLEAR:LV_LEN.
* LV_LEN = STRLEN( LV_SEND_JSON ).
*
* CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
* EXPORTING
* DATA = LV_SEND_JSON
* OFFSET = 0
* LENGTH = LV_LEN.
"发送
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* IF SY-SUBRC = 0.
* RTMSG = '发送成功'.
* ELSE.
* RTMSG = '发送失败'.
* ENDIF.
"提取返回字符串
CLEAR:LV_CODE,LV_REASON.
CALL METHOD LO_HTTP_CLIENT->RESPONSE->GET_STATUS
IMPORTING
CODE = LV_CODE
REASON = LV_REASON.
"获取返回的JSON
CLEAR:LV_REC_JSON.
LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
* SPLIT LV_REC_JSON AT '"orgCode"' INTO LV_REC_JSON1 LV_REC_JSON2.
*
* LV_REC_JSON1 = LV_REC_JSON1 && '"success": true } }'.
*将 JSON 字符串反序列化(解析)为 ABAP 数据结构
*PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE:指定在反序列化过程中,
*JSON 对象的键(key)应该被转换为驼峰式命名法(CamelCase)。这意味着所有 JSON 键都将被转换为首字母大写的形式,例如 "userName" 将被转换为 "UserName"。
*CHANGING DATA = LS_TOKEN:提供用于存储反序列化后数据的 ABAP 数据结构。LS_TOKEN 是一个 ABAP 结构,它应该与传入的 JSON 对象的结构相匹配。
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING DATA = LS_TOKEN ).
"关闭接口
CALL METHOD LO_HTTP_CLIENT->CLOSE.
IF LS_TOKEN-CODE <> 'ACK'.
ES_DATA-CODE = LS_TOKEN-CODE.
ES_DATA-MESSAGE = 'token接口失败,' && LS_TOKEN-MESSAGE.
ES_RET-CODE = 'E'.
ES_RET-MSG = ES_DATA-MESSAGE.
WAIT UP TO '5' SECONDS.
IF ES_RET-MSG CS '错误' OR ES_RET-MSG CS '必填'.
EXIT.
ELSE.
CONTINUE.
ENDIF.
ENDIF.
"获取Token
"如果有值,证明返回了正确的TOKEN,就可以继续
IF LS_TOKEN-CODE = 'ACK' AND LS_TOKEN-DATA IS NOT INITIAL.
UPDATE ZFIT029 SET ZABAPTOKEN = LS_TOKEN-DATA-TOKENID+0(255) ZABAPTOKEN2 = LS_TOKEN-DATA-TOKENID+255(6) DATUM = SY-DATUM UZEIT = SY-UZEIT
ZENTCODE = LS_TOKEN-DATA-ENTCODE WHERE ZFILX = '1' AND URL = LV_MKURL.
COMMIT WORK AND WAIT.
*------------------------------发票台账查询接口--------------------------------------------
CLEAR LV_URL.
* LV_URL = 'https://ng-uat.maycur.com/api/openapi/invoice-ledger/v2/list-invoice-info'.
LV_URL = LV_MKURL && '/api/openapi/invoice-ledger/v2/list-invoice-info'.
"创建http客户端
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = 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->REQUEST->SET_CONTENT_TYPE(
* CONTENT_TYPE = 'application/json; charset=utf-8'
CONTENT_TYPE = 'application/json'
).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'tokenId'
VALUE = LS_TOKEN-DATA-TOKENID.
* VALUE = LV_TOKEN.
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'entCode'
VALUE = LS_TOKEN-DATA-ENTCODE.
* VALUE = 'EC2WXQWMPR0WMC'.
"设置http method 为post
LO_HTTP_CLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
* IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET
).
CLEAR LS_QUERY.
LS_QUERY-FORM_CODE = IV_ZOA.
"SAP不区分大小写,JAVA/C会区分,
"一定让对方修改代码,不然只能SBB的拼接
CLEAR:LV_SEND_JSON.
* LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = INVOICE_STATE_DEFINITION_DTOS COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_QUERY COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
"设置待传输内容长度
CLEAR:LV_LEN.
LV_LEN = STRLEN( LV_SEND_JSON ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_SEND_JSON
OFFSET = 0
LENGTH = LV_LEN.
"发送
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* IF SY-SUBRC = 0.
* RTMSG = '发送成功'.
* ELSE.
* RTMSG = '发送失败'.
* ENDIF.
CLEAR:LV_REC_JSON.",LS_JSON.
LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
* 解析JSON串
CLEAR:ES_DATA.
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING DATA = LR_DATA ).
DATA(LT_RDATA) = LR_DATA-DATA-LIST[].
LOOP AT LT_RDATA ASSIGNING FIELD-SYMBOL(<FS_RDATA>) WHERE INVOICE_DATA_CODE IS NOT INITIAL.
LV_INVOICEDATACODE = <FS_RDATA>-INVOICE_DATA_CODE.
ENDLOOP.
IF LR_DATA-CODE <> 'ACK'.
ES_DATA-CODE = LR_DATA-CODE.
ES_DATA-MESSAGE = '发票查询接口失败,' && LR_DATA-MESSAGE.
ES_RET-CODE = 'E'.
ES_RET-MSG = ES_DATA-MESSAGE.
WAIT UP TO '5' SECONDS.
IF ES_RET-MSG CS '错误' OR ES_RET-MSG CS '必填'.
EXIT.
ELSE.
CONTINUE.
ENDIF.
ENDIF.
* DATA: LO_JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
* LO_JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.
* IF LO_JSON_DES IS INITIAL.
* CREATE OBJECT LO_JSON_DES.
* ENDIF.
*
* CLEAR LV_MESSAGE.
* CALL METHOD LO_JSON_DES->DESERIALIZE
* EXPORTING
* JSON = LV_REC_JSON
* IMPORTING
* ABAP = LV_MESSAGE.
"关闭接口
CALL METHOD LO_HTTP_CLIENT->CLOSE.
*------------------------------发票台账标记接口--------------------------------------------
CLEAR LV_URL.
* LV_URL = 'https://ng-uat.maycur.com/api/openapi/invoice/stateDefinition'.
LV_URL = LV_MKURL && '/api/openapi/invoice/stateDefinition'.
"创建http客户端
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = 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->REQUEST->SET_CONTENT_TYPE(
* CONTENT_TYPE = 'application/json; charset=utf-8'
CONTENT_TYPE = 'application/json'
).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'tokenId'
VALUE = LS_TOKEN-DATA-TOKENID.
* VALUE = LV_TOKEN.
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'entCode'
VALUE = LS_TOKEN-DATA-ENTCODE.
* VALUE = 'EC2WXQWMPR0WMC'.
"设置http method 为post
LO_HTTP_CLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
* IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET
).
CLEAR LS_ROOT.
LS_ROOT-INVOICE_STATE_DEFINITION_DTOS = VALUE #( (
* INVOICE_DATA_CODE = LS_TOKEN-DATA-ENTCODE.
* INVOICE_DATA_CODE = 'INV110053748956491861'
INVOICE_DATA_CODE = LV_INVOICEDATACODE
INVOICE_STATE_REMARK = IV_BELNR
ENT_CODE = LS_TOKEN-DATA-ENTCODE
* ENT_CODE = 'EC2WXQWMPR0WMC'
INVOICE_STATE_CODE = ''
CANCELLATION_MARK = ''
CANCELLATION_MARK_UPDATE_TIME = ''
SUPPLIER_UNUSUAL = ''
SUPPLIER_UNUSUAL_UPDATE_TIME = ''
) ).
"SAP不区分大小写,JAVA/C会区分,
"一定让对方修改代码,不然只能SBB的拼接
CLEAR:LV_SEND_JSON.
* LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = INVOICE_STATE_DEFINITION_DTOS COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_ROOT COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
"设置待传输内容长度
CLEAR:LV_LEN.
LV_LEN = STRLEN( LV_SEND_JSON ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_SEND_JSON
OFFSET = 0
LENGTH = LV_LEN.
"发送
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* IF SY-SUBRC = 0.
* RTMSG = '发送成功'.
* ELSE.
* RTMSG = '发送失败'.
* ENDIF.
CLEAR:LV_REC_JSON.",LS_JSON.
LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
* 解析JSON串
CLEAR:ES_DATA.
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING DATA = ES_RETURN ).
MOVE-CORRESPONDING ES_RETURN TO ES_DATA.
ES_RET-CODE = ES_DATA-CODE.
IF ES_DATA-CODE <> 'ACK'.
ES_RET-CODE = 'E'.
ES_RET-MSG = '发票标记接口错误,'.
ELSE.
ES_RET-CODE = 'S'.
ENDIF.
ES_RET-MSG = ES_RET-MSG && ES_DATA-MESSAGE.
IF ES_DATA-MESSAGE IS INITIAL.
LOOP AT ES_RETURN-DATA[] INTO DATA(ES_RETURNDATA).
ES_RET-MSG = ES_RET-MSG && ES_RETURNDATA.
ENDLOOP.
ENDIF.
* DATA: LO_JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
* LO_JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.
* IF LO_JSON_DES IS INITIAL.
* CREATE OBJECT LO_JSON_DES.
* ENDIF.
*
* CLEAR LV_MESSAGE.
* CALL METHOD LO_JSON_DES->DESERIALIZE
* EXPORTING
* JSON = LV_REC_JSON
* IMPORTING
* ABAP = LV_MESSAGE.
"关闭接口
CALL METHOD LO_HTTP_CLIENT->CLOSE.
ENDIF.
ELSEIF LV_TIME <= '20' AND LV_TOKEN IS NOT INITIAL.
*------------------------------发票台账查询接口--------------------------------------------
CLEAR LV_URL.
* LV_URL = 'https://ng-uat.maycur.com/api/openapi/invoice-ledger/v2/list-invoice-info'.
LV_URL = LV_MKURL && '/api/openapi/invoice-ledger/v2/list-invoice-info'.
"创建http客户端
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = 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->REQUEST->SET_CONTENT_TYPE(
* CONTENT_TYPE = 'application/json; charset=utf-8'
CONTENT_TYPE = 'application/json'
).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'tokenId'
* VALUE = LS_TOKEN-DATA-TOKENID.
VALUE = LV_TOKEN.
LV_ZENTCODE = LS_CONFIGURATION-ZENTCODE.
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'entCode'
VALUE = LV_ZENTCODE.
* VALUE = 'EC2WXQWMPR0WMC'.
"设置http method 为post
LO_HTTP_CLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
* IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET
).
CLEAR LS_QUERY.
LS_QUERY-FORM_CODE = IV_ZOA.
"SAP不区分大小写,JAVA/C会区分,
"一定让对方修改代码,不然只能SBB的拼接
CLEAR:LV_SEND_JSON.
* LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = INVOICE_STATE_DEFINITION_DTOS COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_QUERY COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
"设置待传输内容长度
CLEAR:LV_LEN.
LV_LEN = STRLEN( LV_SEND_JSON ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_SEND_JSON
OFFSET = 0
LENGTH = LV_LEN.
"发送
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* IF SY-SUBRC = 0.
* RTMSG = '发送成功'.
* ELSE.
* RTMSG = '发送失败'.
* ENDIF.
CLEAR:LV_REC_JSON.",LS_JSON.
LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
* 解析JSON串
CLEAR:ES_DATA.
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING DATA = LR_DATA ).
REFRESH LT_RDATA.
CLEAR LT_RDATA.
LT_RDATA = LR_DATA-DATA-LIST[].
LOOP AT LT_RDATA ASSIGNING <FS_RDATA> WHERE INVOICE_DATA_CODE IS NOT INITIAL.
LV_INVOICEDATACODE = <FS_RDATA>-INVOICE_DATA_CODE.
ENDLOOP.
IF LR_DATA-CODE <> 'ACK'.
ES_DATA-CODE = LR_DATA-CODE.
ES_DATA-MESSAGE = '发票查询接口失败,' && LR_DATA-MESSAGE.
ES_RET-CODE = 'E'.
ES_RET-MSG = ES_DATA-MESSAGE.
WAIT UP TO '5' SECONDS.
IF ES_RET-MSG CS '错误' OR ES_RET-MSG CS '必填'.
EXIT.
ELSE.
CONTINUE.
ENDIF.
ENDIF.
* DATA: LO_JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
* LO_JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.
* IF LO_JSON_DES IS INITIAL.
* CREATE OBJECT LO_JSON_DES.
* ENDIF.
*
* CLEAR LV_MESSAGE.
* CALL METHOD LO_JSON_DES->DESERIALIZE
* EXPORTING
* JSON = LV_REC_JSON
* IMPORTING
* ABAP = LV_MESSAGE.
"关闭接口
CALL METHOD LO_HTTP_CLIENT->CLOSE.
*------------------------------发票台账标记接口--------------------------------------------
"创建http客户端
CLEAR LV_URL.
*LV_URL = 'https://ng-uat.maycur.com/api/openapi/invoice/stateDefinition'.
LV_URL = LV_MKURL && '/api/openapi/invoice/stateDefinition'.
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = LV_URL
IMPORTING
CLIENT = 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->REQUEST->SET_CONTENT_TYPE(
* CONTENT_TYPE = 'application/json; charset=utf-8'
CONTENT_TYPE = 'application/json'
).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'tokenId'
* VALUE = LS_TOKEN-DATA-TOKENID.
VALUE = LV_TOKEN.
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = 'entCode'
VALUE = LV_ZENTCODE.
* VALUE = 'EC2WXQWMPR0WMC'.
"设置http method 为post
LO_HTTP_CLIENT->REQUEST->SET_METHOD(
IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST
* IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET
).
CLEAR LS_ROOT.
LS_ROOT-INVOICE_STATE_DEFINITION_DTOS = VALUE #( (
* INVOICE_DATA_CODE = LS_TOKEN-DATA-ENTCODE.
* INVOICE_DATA_CODE = 'INV110053748956491861'
INVOICE_DATA_CODE = LV_INVOICEDATACODE
INVOICE_STATE_REMARK = IV_BELNR
* ENT_CODE = LS_TOKEN-DATA-ENTCODE
ENT_CODE = 'EC2WXQWMPR0WMC'
INVOICE_STATE_CODE = ''
CANCELLATION_MARK = ''
CANCELLATION_MARK_UPDATE_TIME = ''
SUPPLIER_UNUSUAL = ''
SUPPLIER_UNUSUAL_UPDATE_TIME = ''
) ).
"SAP不区分大小写,JAVA/C会区分,
"一定让对方修改代码,不然只能SBB的拼接
CLEAR:LV_SEND_JSON.
* LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = INVOICE_STATE_DEFINITION_DTOS COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
LV_SEND_JSON = /UI2/CL_JSON=>SERIALIZE( DATA = LS_ROOT COMPRESS = ABAP_TRUE PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE ).
"设置待传输内容长度
CLEAR:LV_LEN.
LV_LEN = STRLEN( LV_SEND_JSON ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_SEND_JSON
OFFSET = 0
LENGTH = LV_LEN.
"发送
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
HTTP_INVALID_TIMEOUT = 4
OTHERS = 5.
"接收
CALL METHOD LO_HTTP_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3.
* IF SY-SUBRC = 0.
* RTMSG = '发送成功'.
* ELSE.
* RTMSG = '发送失败'.
* ENDIF.
CLEAR:LV_REC_JSON.",LS_JSON.
LV_REC_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
* 解析JSON串
CLEAR:ES_DATA.
/UI2/CL_JSON=>DESERIALIZE( EXPORTING JSON = LV_REC_JSON
PRETTY_NAME = /UI2/CL_JSON=>PRETTY_MODE-CAMEL_CASE
CHANGING DATA = ES_RETURN ).
MOVE-CORRESPONDING ES_RETURN TO ES_DATA.
ES_RET-CODE = ES_DATA-CODE.
IF ES_DATA-CODE <> 'ACK'.
ES_RET-CODE = 'E'.
ES_RET-MSG = '发票标记接口错误,'.
ELSE.
ES_RET-CODE = 'S'.
ENDIF.
ES_RET-MSG = ES_RET-MSG && ES_DATA-MESSAGE.
IF ES_DATA-MESSAGE IS INITIAL.
LOOP AT ES_RETURN-DATA[] INTO ES_RETURNDATA.
ES_RET-MSG = ES_RET-MSG && ES_RETURNDATA.
ENDLOOP.
ENDIF.
* DATA: LO_JSON_SER TYPE REF TO CL_TREX_JSON_SERIALIZER,
* LO_JSON_DES TYPE REF TO CL_TREX_JSON_DESERIALIZER.
* IF LO_JSON_DES IS INITIAL.
* CREATE OBJECT LO_JSON_DES.
* ENDIF.
*
* CLEAR LV_MESSAGE.
* CALL METHOD LO_JSON_DES->DESERIALIZE
* EXPORTING
* JSON = LV_REC_JSON
* IMPORTING
* ABAP = LV_MESSAGE.
"关闭接口
CALL METHOD LO_HTTP_CLIENT->CLOSE.
ENDIF.
IF ES_RET-CODE = 'S' OR ( ES_RET-CODE ='E' AND ES_RET-MSG IS NOT INITIAL ).
EXIT.
ELSE.
WAIT UP TO '5' SECONDS.
ENDIF.
ENDDO.
ZJT-RFC-LOG-E IS_REQ ES_RET.
ENDFUNCTION.
你认为这篇文章怎么样?
- 0
- 0
- 0
- 0
- 0
- 0