taler-exchange-helper-measure-tops-address-check (5837B)
1 #!/bin/bash 2 # 3 # This file is part of TALER 4 # Copyright (C) 2024, 2025 Taler Systems SA 5 # 6 # TALER is free software; you can redistribute it and/or modify it under the 7 # terms of the GNU General Public License as published by the Free Software 8 # Foundation; either version 3, or (at your option) any later version. 9 # 10 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY 11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 # 14 # You should have received a copy of the GNU General Public License along with 15 # TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/license> 16 # 17 18 # Hard error reporting on. 19 set -eu 20 21 # Exit, with error message (hard failure) 22 function exit_fail() { 23 echo " FAIL: " "$@" >&2 24 EXIT_STATUS=1 25 exit "$EXIT_STATUS" 26 } 27 28 CONF="$HOME/.config/taler-exchange.conf" 29 VERBOSE=0 30 31 while getopts 'ac:hirvV' OPTION; 32 do 33 case "$OPTION" in 34 a) 35 # Address data is required. 36 echo "ADDRESS_LINES" 37 echo "ADDRESS_COUNTRY" 38 exit 0 39 ;; 40 c) 41 # shellcheck disable=SC2034 42 CONF="$OPTARG" 43 ;; 44 h) 45 echo "This is a KYC measure program that sets up a measure to validate the address of the customer via Challenger." 46 echo 'Supported options:' 47 echo ' -a -- show required attributes' 48 # shellcheck disable=SC2016 49 echo ' -c $CONF -- set configuration' 50 echo ' -h -- print this help' 51 echo ' -i -- show required inputs' 52 echo ' -r -- show required context' 53 echo ' -v -- show version' 54 echo ' -V -- be verbose' 55 exit 0 56 ;; 57 i) 58 # Need context and current_rules. 59 echo "attributes" 60 echo "current_rules" 61 exit 0 62 ;; 63 r) 64 # Nothing needed from context 65 exit 0 66 ;; 67 v) 68 echo "$0 v0.0.2" 69 exit 0 70 ;; 71 V) 72 VERBOSE=1 73 ;; 74 ?) 75 exit_fail "Unrecognized command line option" 76 ;; 77 esac 78 done 79 80 if [ 1 = "$VERBOSE" ] 81 then 82 echo "Running $0" 1>&2 83 fi 84 85 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlProgramInput 86 # for the full JSON with possible inputs. 87 88 # First, extract inputs we need 89 INPUTS=$(jq '{"current_rules":.current_rules,"attributes":.attributes}') 90 91 # Get address data 92 CUSTOMER_TYPE=$(echo "$INPUTS" | jq -r '.attributes.CUSTOMER_TYPE // null') 93 case "$CUSTOMER_TYPE" 94 in 95 "NATURAL_PERSON") 96 CONTACT_NAME=$(echo "$INPUTS" | jq -r '.attributes.FULL_NAME') 97 ;; 98 "LEGAL_ENTITY") 99 BUSINESS_NAME=$(echo "$INPUTS" | jq -r '.attributes.COMPANY_NAME') 100 CONTACT_PERSON=$(echo "$INPUTS" | jq -r '.attributes.CONTACT_PERSON_NAME // null') 101 if [ "null" != "$CONTACT_PERSON" ] 102 then 103 CONTACT_NAME=$(echo -en "${BUSINESS_NAME}\nAttn. ${CONTACT_PERSON_NAME}") 104 else 105 CONTACT_NAME="$BUSINESS_NAME" 106 fi 107 ;; 108 *) 109 # Strange, we don't know. Let's try everything... 110 CONTACT_NAME=$(echo "$INPUTS" | jq -r '.attributes.CONTACT_NAME // attributes.FULL_NAME // .attributes.COMPANY_NAME') 111 ;; 112 esac 113 114 ADDRESS_LINES=$(echo "$INPUTS" | jq '.attributes.DOMICILE_ADDRESS') 115 # We ONLY allow Swiss addresses 116 ADDRESS_COUNTRY='"CH"' 117 118 # Convert address data to Challenger format as best we can. 119 ADDRESS=$(jq -n \ 120 --argjson contact_name \""$CONTACT_NAME"\" \ 121 --argjson address_lines "$ADDRESS_LINES" \ 122 --argjson address_country "$ADDRESS_COUNTRY" \ 123 '{"CONTACT_NAME":$contact_name,"ADDRESS_LINES":$address_lines,"ADDRESS_COUNTRY":$address_country,"read_only":true}') 124 125 # Get current rules. 126 CURRENT_RULES=$(echo "$INPUTS" | jq '.current_rules // null') 127 # Get context values. 128 EXPIRATION_TIME=$(echo "$INPUTS" | jq '.context.expiration_time // .current_rules.expiration_time // null') 129 # Preserve successor measure. 130 SUCCESSOR_MEASURE=$(echo "$INPUTS" | jq '.current_rules.successor_measure // null') 131 CUSTOM_MEASURES=$(echo "$INPUTS" | jq '.context.custom_measures // null') 132 133 # Define custom measure for address validation 134 CUSTOM_AMEASURES=$(jq -n \ 135 --argjson address "$ADDRESS" \ 136 '{"custom-address-investigation":{"context":{"initial_address":$address},"check_name":"postal-registration","prog_name":"inform-investigate","operation_type":"DEPOSIT"}}') 137 138 # Then trigger Challenger address check via oauth2, kyc-check-postal-registration 139 NEW_RULES=$(echo "$CURRENT_RULES" | jq --argjson cm "$CUSTOM_MEASURES" '(.rules[] |= if (.measures[0]=="kyx" or .rule_name=="deposit-limit-zero" or .rule_name=="p2p-domestic-identification-requirement") then .measures=["custom-address-investigation"] else . end) | .custom_measures=$cm') 140 141 # Replace all existing 'postal-registration' measures with 'custom-address-investigation' 142 NEW_RULES=$(echo "$NEW_RULES" | jq --argjson cm "$CUSTOM_MEASURES" '(.rules[].measures[] |= if (. =="postal-registration") then "custom-address-investigation" else . end) | .custom_measures=$cm') 143 144 # Finally, output the new rules. 145 # See https://docs.taler.net/taler-kyc-manual.html#tsref-type-AmlOutcome 146 # for the required output format. 147 148 jq -n \ 149 --argjson et "$EXPIRATION_TIME" \ 150 --argjson sm "$SUCCESSOR_MEASURE" \ 151 --argjson nm '"custom-address-investigation"' \ 152 --argjson cm "$CUSTOM_MEASURES" \ 153 --argjson cma "$CUSTOM_AMEASURES" \ 154 --argjson nr "$NEW_RULES" \ 155 '{"new_measures":$nm,"new_rules":($nr+{"expiration_time":$et,"successor_measure":$sm,"custom_measures":({}+$nr.custom_measures+$cm+$cma)})}|del(..|nulls)' 156 157 exit 0