Last active
November 30, 2018 08:41
-
-
Save stuchl4n3k/af0178701122e501d6a3a33a117d7d04 to your computer and use it in GitHub Desktop.
Simple curl script for POSTing to Uctenkovna API (Tax Lottery app by Czech Ministry of Finance)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## | |
# UCTENKOVKA SENDER | |
# | |
# Simple curl script for POSTing to Uctenkovna API (Tax Lottery app by Czech Ministry of Finance). | |
# | |
# @todo: add support for BKP and phone fields | |
# | |
# | |
# Notice: | |
# | |
# If the following attribute conditions are met, then "REJECTED" status is usually returned and info email is sent. | |
# This allows anyone to use this service as a simple email-bomber. You've been warned. | |
# | |
# | |
# AMOUNT: | |
# - arbitrary | |
# | |
# FIK: | |
# - 3rd number must probably start with a 4 | |
# - must be unique per VATID | |
# | |
# DATE | |
# - must have the month in a month preceding the next lottery event (future date is OK) | |
# | |
# - arbitrary, but unique per (VATID & DATE) | |
# | |
# TIME | |
# - arbitrary (future time is OK) | |
# | |
# VATID | |
# - must be unique per (EMAIL & DATE) | |
# - must be valid (probably even existing) otherwise only first player mail is sent (with status "BASIC_PLAYER_CREATED") | |
## | |
# Come as no surprise, prints the usage. | |
# | |
usage() { | |
cat <<EOF | |
Usage: ${PROGNAME} [options] | |
Simple curl script for POSTing to Uctenkovna API (Tax Lottery app by Czech Ministry of Finance). | |
Options: | |
EOF | |
cat <<EOF | column -s\& -t | |
-h, --help & shows this help and terminates | |
-e, --email & email of the player (e.g. foo@bar.baz) | |
-a, --amount & paid amount in CZK (e.g. 3.50) | |
-f, --fik & first 16 digits of the FIK number (e.g. 12345678-1234-4321) | |
-v, --vatid & VAT ID (VATIN) of the subject (e.g. CZ12345678) | |
-s, --simple & simplified tax mode (true/false, defaults to false) | |
-d, --date & date of the purchase in YYYY-MM-DD format (e.g. 2017-12-24) | |
-t, --time & time of the purchase in HH:mm format (e.g. 12:34) | |
EOF | |
} | |
## | |
# Checks if a given argument is a (decimal) number. | |
# | |
is_number() { | |
case $1 in ''|*[!0-9]*) false;; esac; | |
} | |
## | |
# Validates input variables. | |
# | |
validate_input() { | |
VALID=1 | |
if [[ ${EMAIL} == "" ]]; then | |
>&2 printf "error: no email given\n" | |
VALID=0 | |
fi | |
if ! is_number ${AMOUNT} || ! [ ${AMOUNT} -gt 0 ]; then | |
>&2 printf "error: invalid amount given\n" | |
VALID=0 | |
fi | |
if [[ ${FIK} == "" ]]; then | |
>&2 printf "error: no FIK given\n" | |
VALID=0 | |
elif [ ${#FIK} -ne 18 ]; then | |
>&2 printf "error: FIK has invalid length\n" | |
VALID=0 | |
fi | |
if [[ ${VATID} == "" ]]; then | |
>&2 printf "error: no VAT ID given\n" | |
VALID=0 | |
fi | |
if [[ ${SIMPLE} != "false" ]] && [[ ${SIMPLE} != "true" ]]; then | |
>&2 printf "error: invalid simple given\n" | |
VALID=0 | |
fi | |
date "+%Y-%m-%d" -d ${DATE} > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
>&2 printf "error: invalid date given\n" | |
VALID=0 | |
fi | |
date "+%H:%M" -d ${TIME} > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
>&2 printf "error: invalid time given\n" | |
VALID=0 | |
fi | |
if [ ${VALID} -ne 1 ]; then | |
exit 1 | |
fi | |
} | |
## | |
# Main function. Does the actual HTTP request via curl. | |
# | |
main() { | |
JSON_TEMPLATE=' | |
{ | |
"email": "%s", | |
"amount": %s, | |
"fik": "%s", | |
"vatId": "%s", | |
"simpleMode": %s, | |
"date": "%s", | |
"time": "%s" | |
}' | |
JSON=`printf "${JSON_TEMPLATE}" ${EMAIL} ${AMOUNT} ${FIK} ${VATID} ${SIMPLE} ${DATE} ${TIME}` | |
printf "Sending payload:\n ${JSON}\n\n" | |
RESPONSE=$(echo ${JSON} | curl -sS -H "Content-Type: application/json" \ | |
-X POST \ | |
-d @- \ | |
${API_URL}) | |
printf "Got response:\n\n${RESPONSE}\n" | |
} | |
VERSION="1.0" | |
PROGNAME=${0##*/} | |
SHORTOPTS="h,e:,p:,a:,f:,v:,s:,d:,t:" | |
LONGOPTS="help,email:,phone:,amount:,fik:,vatid:,simple:,date:,time:" | |
API_URL="https://api.uctenkovka.cz/m/receipts" | |
EMAIL=""; | |
AMOUNT=0; | |
FIK=0; | |
VATID=0; | |
SIMPLE="false"; | |
DATE=""; | |
TIME=""; | |
echo -e "--- uctenkovka shell sender ${VERSION} by stuchl4n3k\n" | |
ARGS=$(getopt -s bash --options ${SHORTOPTS} --longoptions ${LONGOPTS} --name ${PROGNAME} -- "$@") | |
if [ $? -ne 0 ]; then | |
usage | |
exit 1 | |
elif [ $# -eq 0 ]; then | |
>&2 printf "${PROGNAME}: no arguments supplied\n" | |
usage | |
exit 1 | |
fi | |
eval set -- "${ARGS}" | |
unset ARGS | |
while true; do | |
case $1 in | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-e|--email) | |
shift | |
EMAIL=$1 | |
;; | |
-a|--amount) | |
shift | |
AMOUNT=`echo "scale = 2; ${1} * 100" | bc 2> /dev/null` | |
AMOUNT=${AMOUNT%.*} | |
;; | |
-f|--fik) | |
shift | |
FIK=$1 | |
;; | |
-v|--vatid) | |
shift | |
VATID=$1 | |
;; | |
-s|--simple) | |
shift | |
SIMPLE=$1 | |
;; | |
-d|--date) | |
shift | |
DATE=$1 | |
;; | |
-t|--time) | |
shift | |
TIME=$1 | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
validate_input | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zdravim, velice rad bych pouzival Vas script, ale pokazde kdyz dam prikaz napr ./uctenkovka.sh -a xx napr 150 nebo 150.00 ci "150" apod, pokazde mi to vyhodi error: invalid amount given... nejaky tip, v cem bych mohl delat chybu?
Diky