Skip to content

Instantly share code, notes, and snippets.

@typebrook
Last active February 4, 2023 02:15
Show Gist options
  • Save typebrook/acbc0c104598076342efdd5a56753913 to your computer and use it in GitHub Desktop.
Save typebrook/acbc0c104598076342efdd5a56753913 to your computer and use it in GitHub Desktop.
Invoice API
CARD_TYPE=
CARD_NO=
APPID=
CARD_ENCRYPT=
.env
*.json
invoice.list
#! /bin/bash
set pipefail
# Get CARD_TYPE, CARD_NO, APPID and CARD_ENCRYPT(password)
source .env
# Decide start date and end date
#
# If 'MONTH' is not specified, then we just query yesterday
# If 'MONTH' is specified, then we query the whole month
# 'YEAR' is optional. If not specify, use this year
YEAR=${YEAR:-$(date +%Y)}
if [[ -n $MONTH ]]; then
START_OF_MONTH=${YEAR}-${MONTH}-01
STARTDATE=$(date --date "$START_OF_MONTH" +%Y/%m/%d)
ENDDATE=$(date --date "$START_OF_MONTH +1month -1day" +%Y/%m/%d )
else
YESTERDAY=`date --date -1day +%Y/%m/%d`
STARTDATE=$YESTERDAY
ENDDATE=$YESTERDAY
fi
echo Time Period: $STARTDATE $ENDDATE >/dev/tty
curl -s https://api.einvoice.nat.gov.tw/PB2CAPIVAN/invServ/InvServ \
-H 'accept: application/json' \
-d version=0.5 \
-d cardType=$CARD_TYPE \
-d cardNo=$CARD_NO \
-d expTimeStamp=`date -d +1minutes +%s` \
-d action=carrierInvChk \
-d timeStamp=`date -d +20seconds +%s` \
-d startDate=$STARTDATE \
-d endDate=$ENDDATE \
-d onlyWinningInv=N \
-d uuid=$(date +%s) \
-d appID=$APPID \
-d cardEncrypt=$CARD_ENCRYPT | \
jq -r '.details[]|"\(.invDate.month)/\(.invDate.date) \(.invNum)"' | \
while read date_not_paded INVNUM; do
echo $YEAR/$(date --date $date_not_paded +%m/%d) $INVNUM
done | sort
exit ${PIPESTATUS[0]}
.ONESHELL:
SHELL := bash
invoice.list:
./get_invoices.sh >$@
details: invoice.list
cat $< | while read date invoice; do
./query_details.sh $$invoice $$date
done
source .env
curl -s https://api.einvoice.nat.gov.tw/PB2CAPIVAN/invServ/InvServ \
-H 'accept: application/json' \
-d version=0.5 \
-d cardType=$CARD_TYPE \
-d cardNo=$CARD_NO \
-d expTimeStamp=`date -d +5minutes +%s` \
-d action=carrierInvDetail \
-d timeStamp=`date -d +20seconds +%s` \
-d invNum=$1 \
-d invDate=$2 \
-d uuid=$(date +%s%3N) \
-d appID=$APPID \
-d cardEncrypt=$CARD_ENCRYPT | \
jq . | \
jq -r '"Expenses:Invoice \(.amount)\t\t\tTWD\n no: \"\(.invNum)\"\n desc: \"\(.details[0].description)\"\n time: \"\(.invoiceTime)\""'
exit ${PIPESTATUS[0]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment