2019-11-05 18:34:31 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# This little guy repairs an invoice which have failed because of an unexpected
|
|
|
|
# error while trying to cancel payment and we need to make it look like it was
|
|
|
|
# captured.
|
|
|
|
#
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
CWD="$(dirname $0)"
|
|
|
|
|
|
|
|
source "${CWD}/lib/logging"
|
|
|
|
|
|
|
|
# Actual work is going here
|
|
|
|
|
|
|
|
INVOICE="${1}"
|
2019-11-08 08:13:53 +00:00
|
|
|
REASON="${2:-Manually captured payment}"
|
2019-11-05 18:34:31 +00:00
|
|
|
|
|
|
|
case ${INVOICE} in
|
|
|
|
""|"-h"|"--help" )
|
|
|
|
echo -ne "Given ID of an invoice make it look like last payment was captured after failed cancel "
|
|
|
|
echo -ne "attempt."
|
|
|
|
echo
|
|
|
|
echo
|
2019-11-08 08:13:53 +00:00
|
|
|
echo -e "Usage: ${SCRIPTNAME} invoice_id [reason]"
|
2019-11-05 18:34:31 +00:00
|
|
|
echo -e " invoice_id Invoice ID (string)."
|
2019-11-08 08:13:53 +00:00
|
|
|
echo -e " reason Reason for manually reconstructed session failure (string)."
|
2019-11-05 18:34:31 +00:00
|
|
|
echo -e " -h, --help Show this help message."
|
|
|
|
echo
|
|
|
|
echo -e "More information:"
|
2022-05-27 15:46:58 +00:00
|
|
|
echo -e " https://github.com/valitydev/damsel/blob/master/proto/payment_processing.thrift"
|
2019-11-05 18:34:31 +00:00
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
* )
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
INVOICE_EVENTS=$("${CWD}/hellgate/get-invoice-events.sh" "${INVOICE}")
|
|
|
|
LAST_CHANGE=$(echo "${INVOICE_EVENTS}" | jq '.[-1].payload.invoice_changes[-1].invoice_payment_change')
|
|
|
|
|
|
|
|
PAYMENT=$(echo "${LAST_CHANGE}" | jq -r '.id')
|
|
|
|
SESSION=$(echo "${LAST_CHANGE}" | jq -r '.payload.invoice_payment_session_change')
|
|
|
|
TARGET=$(echo "${SESSION}" | jq -r '.target')
|
|
|
|
|
|
|
|
if [ \
|
|
|
|
"${PAYMENT}" = "null" -o \
|
|
|
|
"$(echo "${TARGET}" | jq -r '.cancelled')" = "null" -o \
|
2020-04-21 14:19:44 +00:00
|
|
|
"$(echo "${SESSION}" | jq -r '.payload')" = "null" -o \
|
2020-04-06 08:28:24 +00:00
|
|
|
"$(echo "${SESSION}" | jq -r '.payload.session_finished')" != "null" \
|
2019-11-05 18:34:31 +00:00
|
|
|
]; then
|
|
|
|
err "Last seen change looks wrong for this repair scenario"
|
|
|
|
fi
|
|
|
|
|
|
|
|
CHANGES=$(cat <<END
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"invoice_payment_change": {
|
|
|
|
"id": "${PAYMENT}",
|
|
|
|
"payload": {
|
|
|
|
"invoice_payment_session_change": {
|
|
|
|
"target": {
|
|
|
|
"cancelled": []
|
|
|
|
},
|
|
|
|
"payload": {
|
|
|
|
"session_finished": {
|
|
|
|
"result": {
|
2019-11-08 08:13:53 +00:00
|
|
|
"failed": {
|
|
|
|
"failure": {
|
|
|
|
"failure": {
|
|
|
|
"code": "authorization_failed",
|
|
|
|
"reason": "${REASON}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 18:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invoice_payment_change": {
|
|
|
|
"id": "${PAYMENT}",
|
|
|
|
"payload": {
|
|
|
|
"invoice_payment_session_change": {
|
|
|
|
"target": {
|
|
|
|
"captured": []
|
|
|
|
},
|
|
|
|
"payload": {
|
|
|
|
"session_started": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invoice_payment_change": {
|
|
|
|
"id": "${PAYMENT}",
|
|
|
|
"payload": {
|
|
|
|
"invoice_payment_session_change": {
|
|
|
|
"target": {
|
|
|
|
"captured": []
|
|
|
|
},
|
|
|
|
"payload": {
|
|
|
|
"session_finished": {
|
|
|
|
"result": {
|
|
|
|
"succeeded": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
END
|
|
|
|
)
|
|
|
|
|
|
|
|
# Then we should stuff it with previously reconstructed history
|
|
|
|
"${CWD}/repair-invoice.sh" "${INVOICE}" "${CHANGES}"
|