Anonymous Anonymous - 6 months ago
497 0

No description

Bash

bpoint

#!/bin/bash

userHelp(){
 echo "-b = batchId (formatted NNNNNN)"
}

while getopts ":b:h" opt; do
  case $opt in
    b)
      batchId=$OPTARG
      echo batchId set to $batchId
      if [[ ! $batchId =~ [0-9]{6} ]]; then
       echo error: batch format incorrect
       userHelp
       exit 1
      fi
      ;;
    h)
      userHelp
      exit 0
    ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      userHelp
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      userHelp
      exit 1
      ;;
  esac
done

jobName=au_bpoint_ccr_download.sh
log=/var/log/file_transfer_logs/$jobName.log
timeStamp=`date '+%Y%m%d %H:%M:%S'`
date=`date +%Y%m%d`
aDate=`date '+%Y/%m'`
username=*************
password=`cat /root/passwords/au_bpoint.password`
merchantNumber=**************
catalogue=/var/tmp/file_transfer_catalogue/au_commsecure_batch.catalogue
dir=/shared/inbound/facture/payment_event
aDir=/shared/inbound/facture/credit_card/archive/$aDate
echo 'aDir set to '$aDir

#Find the batchID of the file
if [ -z $batchId ]; then
  batchId=`ls /shared/outbound/facture/credit_card/hold/BatchCreditCard_*.csv | sed 's/.*_\([0-9]\+\).csv/\1/'`
fi

if [[ -z $batchId ]]; then
  echo 'No batchID set, exiting...' | tee -a $log
  exit 1
else

if [ ! -d $aDir ]; then
 mkdir -v -p $aDir
fi

echo `date '+%Y%m%d %H:%M:%S'` Starting | tee -a $log


#curl --cookie-jar /tmp/$jobName.cookie -F "username=$username" -F "password=$password" -F "merchantnumber=$merchantNumber" https://www.bpoint.com.au/backoffice/Account/LogOn -L
curl -b /tmp/bpoint.cookie -c /tmp/bpoint.cookie -F "username=automation" -F "password=$password" -F "merchantnumber=5353109291835737" https://www.bpoint.com.au/backoffice/Account/LogOn -L

cookieCode=$?
if [ $cookieCode == 0 ]; then
 echo `date '+%Y%m%d %H:%M:%S'` 'Cookie set successfully' | tee -a $log
 sleep 1
fi

curl -b /tmp/bpoint.cookie -c /tmp/bpoint.cookie -F "batchstate=-1" -F "timespan=30" -F "page=0" -F "currencycode=0" https://www.bpoint.com.au/backoffice/BatchPayment/Index -o /tmp/$jobName.batchidlist
listCode=$?
if [ $listCode == 0 ]; then
 echo `date '+%Y%m%d %H:%M:%S'` 'List downloaded successfully' | tee -a $log
 sleep 1
fi


#curl --cookie /tmp/$jobname.cookie https://www.bpoint.com.au/backoffice/BatchPayment/Index -o /tmp/request.htm

#curl -i -b /tmp/test.cookie -c /tmp/test.cookie https://www.bpoint.com.au/backoffice/BatchPayment/Index -o /tmp/request.htm
curl -i -b /tmp/bpoint.cookie -c /tmp/bpoint.cookie https://www.bpoint.com.au/backoffice/BatchPayment/Index -o /tmp/request.htm
dltoken=`cat /tmp/request.htm | grep __ | awk ' { print $4 } ' | cut -d '"' -f 2 | sed -n 2p`


for i in $batchId; do
 echo ID: $i
 downloadFileId=`cat /tmp/$jobName.batchidlist | sed 's/{"/\n/g' | grep $i | sed 's/BatchFileID":\([0-9]\+\).*/\1/g'`
 echo `date '+%Y%m%d %H:%M:%S'` File name is: BatchCreditCard_$i-R.csv | tee -a $log
 echo `date '+%Y%m%d %H:%M:%S'` DownloadFileId is: $downloadFileId | tee -a $log

 curl -b /tmp/bpoint.cookie -c /tmp/bpoint.cookie -F "downloadfileid=$downloadFileId" -F "returnall=true" -F "zipcontents=false" -F __RequestVerificationToken=$dltoken  https://www.bpoint.com.au/backoffice/BatchPayment/DownloadBatchResult -o $aDir//BatchCreditCard_$i-R.csv
 downloadCode=$?

#Header check
 grep 'CBA-EVOLVE' $aDir/BatchCreditCard_$i-R.csv
 checkCode=$?
#Line check
# uploadLines=`tail -n 1 /shared/outbound/facture/credit_card/hold/BatchCreditCard_$i.csv | cut -d ',' -f 2`
# downloadLines=`tail -n 1 $aDir/BatchCreditCard_$i-R.csv | cut -d ',' -f 2`
# if [ $uploadLines == $downloadLines ]; then lineCode=0; fi
#Sum check
 sumTotal=`tail -n 1 $aDir/BatchCreditCard_$i-R.csv | cut -d ',' -f 3`
 echo sumTotal is: $sumTotal | tee -a $log
 sum=0; for amount in `cat $aDir/BatchCreditCard_$i-R.csv | head -n -1 | tail -n +2 | cut -d ',' -f 6 `; do sum=$(expr $sum + $amount); done
 echo sum is: $sum | tee -a $log
 if [[ $sum == $sumTotal ]]; then
  sumCheck=0
 else
  sumCheck=1
 fi


# if [ $checkCode == 0 ] && [ $downloadCode == 0 ] && [ $lineCode == 0 ] && [ $sumCheck == 0 ]; then
if [ $checkCode == 0 ] && [ $downloadCode == 0 ] && [ $sumCheck == 0 ]; then
  echo `date '+%Y%m%d %H:%M:%S'` 'Bpoint Credit Card Response file downloaded successfully' | tee -a $log
  cp -v $aDir/BatchCreditCard_$i-R.csv cp -v $aDir/BatchCreditCard_$i-R.csv.bku
  sed -i 's^,00/00,^,03/15,^g' $aDir/BatchCreditCard_$i-R.csv
  cp -v $aDir/BatchCreditCard_$i-R.csv /shared/inbound/facture/payment_event/
  mv /shared/outbound/facture/credit_card/hold/BatchCreditCard_$i.csv /shared/outbound/facture/credit_card/archive/$aDate/
 else
  echo `date '+%Y%m%d %H:%M:%S'` checkCode: $checkCode | tee -a $log
  echo `date '+%Y%m%d %H:%M:%S'` downloadCode: $downloadCode | tee -a $log
#  echo `date '+%Y%m%d %H:%M:%S'` lineCode: $lineCode | tee -a $log
  echo `date '+%Y%m%d %H:%M:%S'` sumCheck: $sumCheck | tee -a $log
 fi
done

#rm -v /tmp/$jobName.batchidlist /tmp/$jobName.cookie

fi

echo `date '+%Y%m%d %H:%M:%S'` Finishing | tee -a $log