驗證地址

本快速入門課程將說明如何透過 Google Cloud 適用的 ABAP SDK 的 SAP BTP 版本,使用Address Validation API 建立程式,驗證儲存在 SAP 資料庫中的商家地址。

事前準備

執行本快速入門前,請確認您或管理員已完成下列先決條件:

建立 ABAP 類別來驗證地址

  1. 建立套件:

    1. 在 ADT 中前往「Project Explorer」。
    2. ZLOCAL 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Package」
    3. 請輸入下列包裹詳細資料:

      • 「Name」:輸入 ZABAPSDK_TEST
      • 說明:輸入 ABAP SDK Test Package
    4. 點選「下一步」

    5. 在「Select a Transport Request」對話方塊中,選取「Create a new request」核取方塊。

    6. 輸入傳輸要求的說明。

    7. 按一下「完成」

  2. 建立 ABAP 類別來呼叫 Address Validation API:

    1. 在 ABAP 套件上按一下滑鼠右鍵,然後依序選取「New」>「ABAP Class」
    2. 請為 ABAP 類別輸入下列詳細資料:

      • 「Name」:輸入 ZGOOG_CL_QS_ADDRESS_VALIDATION
      • 說明:輸入 Quick start for Address Validation API
    3. 點選「下一步」

    4. 選取運送要求,然後按一下「完成」

  3. 在程式碼編輯器中,將預設程式碼替換為以下程式碼片段:

    " --------------------------------------------------------------------
    "  Copyright 2024 Google LLC                                         -
    "                                                                    -
    "  Licensed under the Apache License, Version 2.0 (the "License");   -
    "  you may not use this file except in compliance with the License.  -
    "  You may obtain a copy of the License at                           -
    "      https://www.apache.org/licenses/LICENSE-2.0                   -
    "  Unless required by applicable law or agreed to in writing,        -
    "  software distributed under the License is distributed on an       -
    "  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,      -
    "  either express or implied.                                        -
    "  See the License for the specific language governing permissions   -
    "  and limitations under the License.                                -
    " --------------------------------------------------------------------
    CLASS zcl_qs_validate_address DEFINITION
      PUBLIC FINAL
      CREATE PUBLIC.
    
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun.
    ENDCLASS.
    
    
    
    CLASS ZCL_QS_VALIDATE_ADDRESS IMPLEMENTATION.
    
    
      METHOD if_oo_adt_classrun~main.
    
        DATA ls_input             TYPE /goog/cl_addrvaldn_v1=>ty_012.
        DATA lo_address_validator TYPE REF TO /goog/cl_addrvaldn_v1.
    
        TRY.
            " Open HTTP connection
            " The client key DEMO_ADDR_VAL is an example, replace this with actual value
            lo_address_validator = NEW #( iv_key_name = 'DEMO_ADDR_VAL' ).
    
            " Pass the address to be validated
            ls_input-address-region_code = 'US'.
            ls_input-address-locality    = 'Mountain View'.
            APPEND '1600, Amphitheatre, Parkway' TO ls_input-address-address_lines.
    
            " Call the API Method to validate address
            lo_address_validator->validate_address( EXPORTING is_input    = ls_input
                                                    IMPORTING es_output   = DATA(ls_output)
                                                              ev_ret_code = DATA(lv_ret_code)
                                                              ev_err_text = DATA(lv_err_text)
                                                              es_err_resp = DATA(ls_err_resp) ).
    
            IF     lo_address_validator->is_success( lv_ret_code ) = abap_true
               AND ls_output-result-verdict-address_complete       = abap_true.
              out->write( 'Address is complete' ).
            ENDIF.
    
          CATCH /goog/cx_sdk INTO DATA(lo_exception).
            " Handle exception here
        ENDTRY.
      ENDMETHOD.
    ENDCLASS.
    

    DEMO_ADDR_VAL 替換為用戶端金鑰名稱。

  4. 儲存並啟用變更。

  5. 執行應用程式:

    1. 選取 ABAP 類別 ZGOOG_CL_QS_ADDRESS_VALIDATION
    2. 依序點選「執行」>「以身分執行」>「ABAP 應用程式 (控制台)」。或者,按下 F9 鍵。如果成功,系統會顯示下列輸出內容:
      'Address is complete'
      

後續步驟