2019-01-25 12:18:30 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# This little guy repairs an invoice which have failed because of an unexpected
|
|
|
|
# error while trying to process payment though provider processed it successfully
|
|
|
|
# in the meantime.
|
|
|
|
#
|
|
|
|
# Attention! To attach some transaction info to the payment you must have a file
|
|
|
|
# name `trx.{invoice_id}.json' in the workdir which follows `domain.TransactionInfo'
|
|
|
|
# schema.
|
|
|
|
#
|
|
|
|
|
2019-11-05 19:33:08 +00:00
|
|
|
set -e -o pipefail
|
2019-01-25 12:18:30 +00:00
|
|
|
|
2019-01-28 12:24:24 +00:00
|
|
|
CWD="$(dirname $0)"
|
|
|
|
|
|
|
|
source "${CWD}/lib/logging"
|
|
|
|
|
2019-01-25 12:18:30 +00:00
|
|
|
# Actual work is going here
|
|
|
|
|
|
|
|
INVOICE="${1}"
|
2019-11-05 19:33:08 +00:00
|
|
|
PAYMENT="${2}"
|
|
|
|
|
|
|
|
function usage {
|
|
|
|
echo -ne "Given ID of an invoice and a payment make it look like the payment processed successfully. "
|
|
|
|
echo -ne "You can bind transaction info if you should, just place a file named "
|
|
|
|
echo -ne "'trx.{invoice_id}.{payment_id}.json' under the feet."
|
|
|
|
echo
|
|
|
|
echo
|
|
|
|
echo -e "Usage: ${SCRIPTNAME} invoice_id payment_id [--force]"
|
|
|
|
echo -e " invoice_id Invoice ID (string)."
|
|
|
|
echo -e " payment_id Payment ID (string)."
|
|
|
|
echo -e " --force Force execution even when transaction info is missing."
|
|
|
|
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 19:33:08 +00:00
|
|
|
exit 127
|
|
|
|
}
|
|
|
|
|
|
|
|
[ -z "${INVOICE}" -o -z "${PAYMENT}" ] && usage
|
|
|
|
|
|
|
|
INVOICE_STATE=$(
|
|
|
|
"${CWD}/hellgate/get-invoice-state.sh" "${INVOICE}"
|
|
|
|
)
|
|
|
|
|
|
|
|
PAYMENT_STATE=$(
|
|
|
|
echo "${INVOICE_STATE}" | jq ".payments[].payment | select(.id == \"${PAYMENT}\")"
|
|
|
|
)
|
2019-01-25 12:18:30 +00:00
|
|
|
|
2019-11-05 19:33:08 +00:00
|
|
|
if [ \
|
|
|
|
"${INVOICE_STATE}" = "" -o \
|
|
|
|
"${PAYMENT_STATE}" = "" -o \
|
|
|
|
"$(echo "${INVOICE_STATE}" | jq -r '.invoice.status | has("unpaid")')" != "true" -o \
|
|
|
|
"$(echo "${PAYMENT_STATE}" | jq -r '.status | has("pending")')" != "true" \
|
|
|
|
]; then
|
|
|
|
err "Invoice looks wrong for this repair scenario"
|
|
|
|
fi
|
2019-01-28 12:24:24 +00:00
|
|
|
|
|
|
|
LAST_CHANGE=$(
|
2019-07-11 09:12:14 +00:00
|
|
|
"${CWD}/hellgate/get-invoice-events.sh" "${INVOICE}" |
|
2019-01-28 12:24:24 +00:00
|
|
|
jq '.[-1].payload.invoice_changes[-1].invoice_payment_change'
|
|
|
|
)
|
|
|
|
|
2020-04-23 08:43:29 +00:00
|
|
|
SESSION=$(echo "${LAST_CHANGE}" | jq -r '.payload.invoice_payment_session_change')
|
|
|
|
|
2019-11-05 19:33:08 +00:00
|
|
|
if [ "${LAST_CHANGE}" = "null" ]; then
|
2019-01-28 12:24:24 +00:00
|
|
|
err "Last seen change looks wrong for this repair scenario"
|
|
|
|
fi
|
|
|
|
|
|
|
|
LAST_PAYMENT="$(echo "${LAST_CHANGE}" | jq -r '.id')"
|
2019-11-05 19:33:08 +00:00
|
|
|
if [ "${LAST_PAYMENT}" != "${PAYMENT}" ]; then
|
2019-01-28 12:24:24 +00:00
|
|
|
err "Last seen change related to another payment with id $(em "${LAST_PAYMENT}")"
|
|
|
|
fi
|
|
|
|
|
2019-11-05 19:33:08 +00:00
|
|
|
if [ "$(echo "${LAST_CHANGE}" | jq -r '.payload | has("invoice_payment_cash_flow_changed")')" == "true" ]; then
|
|
|
|
|
|
|
|
SESSION_CHANGE=$(cat <<END
|
|
|
|
{
|
|
|
|
"invoice_payment_change": {
|
|
|
|
"id": "${PAYMENT}",
|
|
|
|
"payload": {
|
|
|
|
"invoice_payment_session_change": {
|
|
|
|
"target": {
|
|
|
|
"processed": []
|
|
|
|
},
|
|
|
|
"payload": {
|
|
|
|
"session_started": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
END
|
|
|
|
)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
2020-04-23 10:21:42 +00:00
|
|
|
if [ "$(echo "${SESSION}")" != "null" ] &&
|
2020-04-23 08:43:29 +00:00
|
|
|
[ "$(echo "${SESSION}" | jq -r '.payload | has("session_finished")')" != "true" ]; then
|
|
|
|
SESSION_CHANGE=""
|
|
|
|
else
|
|
|
|
err "Last seen change looks wrong for this repair scenario"
|
|
|
|
fi
|
2019-11-05 19:33:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
TRXCHANGE=""
|
2019-01-28 12:24:24 +00:00
|
|
|
TRXFILE="trx.${INVOICE}.${PAYMENT}.json"
|
2019-01-25 12:18:30 +00:00
|
|
|
|
2019-01-28 12:24:24 +00:00
|
|
|
if [ -f "${TRXFILE}" ]; then
|
|
|
|
|
|
|
|
TRXCHANGE=$(cat <<END
|
2019-01-25 12:18:30 +00:00
|
|
|
{
|
|
|
|
"invoice_payment_change": {
|
|
|
|
"id": "${PAYMENT}",
|
|
|
|
"payload": {
|
|
|
|
"invoice_payment_session_change": {
|
|
|
|
"target": {
|
|
|
|
"processed": []
|
|
|
|
},
|
|
|
|
"payload": {
|
|
|
|
"session_transaction_bound": {
|
2019-01-28 12:24:24 +00:00
|
|
|
"trx": $(cat "${TRXFILE}")
|
2019-01-25 12:18:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 12:24:24 +00:00
|
|
|
},
|
2019-01-25 12:18:30 +00:00
|
|
|
END
|
2019-01-28 12:24:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
warn "No transaction info to bound, file $(em "${TRXFILE}") is missing"
|
|
|
|
if [ "${3}" != "--force" ]; then
|
|
|
|
err "Rerun with $(em --force) to proceed anyway"
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
2019-01-25 12:18:30 +00:00
|
|
|
|
|
|
|
# Essentially we have to simulate the failed session has been restarted and then
|
|
|
|
# finished successfully.
|
|
|
|
CHANGES=$(cat <<END
|
|
|
|
[
|
2019-11-05 19:33:08 +00:00
|
|
|
${SESSION_CHANGE}
|
2019-01-28 12:24:24 +00:00
|
|
|
${TRXCHANGE}
|
2019-01-25 12:18:30 +00:00
|
|
|
{
|
|
|
|
"invoice_payment_change": {
|
|
|
|
"id": "${PAYMENT}",
|
|
|
|
"payload": {
|
|
|
|
"invoice_payment_session_change": {
|
|
|
|
"target": {
|
|
|
|
"processed": []
|
|
|
|
},
|
|
|
|
"payload": {
|
|
|
|
"session_finished": {
|
|
|
|
"result": {
|
|
|
|
"succeeded": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
END
|
|
|
|
)
|
|
|
|
|
|
|
|
# Then we should stuff it with previously reconstructed history
|
2019-07-11 09:12:14 +00:00
|
|
|
"${CWD}/repair-invoice.sh" "${INVOICE}" "${CHANGES}"
|