#!/usr/bin/env bash
#------------------------------------------------------------------------------
:<<'__comments__'
    Name: get-express-checkout-details
   Usage: get-express-checkout-details TOKEN
Synopsis: Retrieve details of a PayPal sandbox Express Checkout.
   Notes: See paypal-nvp-express-checkout-how-to.md
  Author: G. D. LaBossiere, Xview Solutions Inc. <code@XviewSolutions.com>
 Release: 1
    Date: 2022-01-25
     RCS: $Id$
 License: GNU GPL v3 <http://www.gnu.org/licenses/gpl-3.0.txt>
__comments__
#------------------------------------------------------------------------------
#  bash environment
#------------------------------------------------------------------------------
#  BASH_VERSINFO[@]
#  FUNCNAME
#------------------------------------------------------------------------------
#  constants
#------------------------------------------------------------------------------
PAYPAL_METHOD="GetExpressCheckoutDetails"
PAYPAL_NVP_URL="https://api-3t.sandbox.paypal.com/nvp"
PAYPAL_PWD=""
PAYPAL_SIGNATURE=""
PAYPAL_USER=""
#------------------------------------------------------------------------------
#  error codes
#------------------------------------------------------------------------------
ERROR_10="Requires bash 4.2+"
ERROR_15="Missing required program(s)"
ERROR_17="Required global(s) unset"
ERROR_18="Bad or missing required parameter(s)"
#------------------------------------------------------------------------------
#  variables
#------------------------------------------------------------------------------
argList=()
errFuncName=""
errorNumber=""
paypalReply=""
#------------------------------------------------------------------------------
exit_handler() {
  errorNumber="$?"
  [[ $errorNumber -eq 0 ]] && return 0
  local errtxt="ERROR_${errorNumber}"
  local errmsg="Error ${errorNumber}: No description available"
  [[ ${!errtxt} ]] && errmsg="Error ${errorNumber}: ${!errtxt}"
  [[ ${errFuncName} ]] && errmsg="${errFuncName}: ${errmsg}"
  errmsg="${0##*/}: ${errmsg}"
  printf '%s\n' "${errmsg}"
  return $errorNumber
}
#------------------------------------------------------------------------------
#  functions
#------------------------------------------------------------------------------
generate_reply() {
  errFuncName="$FUNCNAME"
  type -p curl &>/dev/null || exit 15
  local args token
  token="${argList[0]}"
  [[ ${#token} -eq 20 ]] || exit 18
  args="-d METHOD=${PAYPAL_METHOD} -d TOKEN=${token} -d VERSION=100"
  args="${args} -d PWD=${PAYPAL_PWD}"
  args="${args} -d SIGNATURE=${PAYPAL_SIGNATURE}"
  args="${args} -d USER=${PAYPAL_USER}"
  paypalReply=$(curl ${PAYPAL_NVP_URL} -s -k -m 10 ${args})
  return 0
}
#------------------------------------------------------------------------------
print_reply() {
  errFuncName="$FUNCNAME"
  [[ $paypalReply ]] || exit 17
  type -p cgi2shell &>/dev/null || exit 15
#  replace ampersands with newlines; read newline-delimited string into array
  readarray -t paypalReply <<< "${paypalReply//\&/$'\n'}"
#  convert the url-encoded array elements to name="value" shell format; save
#+ the result as a string containing newlines
  paypalReply=$(cgi2shell "${paypalReply[@]}")
#  read the string contents back into an array
  readarray -t paypalReply <<< "${paypalReply}"
#  print each element of the array in name="value" shell format
  printf '%s\n' "${paypalReply[@]}"
  return 0
}
#------------------------------------------------------------------------------
#  main
#------------------------------------------------------------------------------
main() {
  trap exit_handler EXIT
  argList=("$@")
  [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -ge 2 ]] || \
    [[ ${BASH_VERSINFO[0]} -gt 4 ]] || exit 10
  generate_reply
  print_reply
  exit 0
}
#------------------------------------------------------------------------------
#  run program
#------------------------------------------------------------------------------
main "$@"

