From 423d7ca500a04316dea69144a010338bb22e3be8 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Tue, 5 Nov 2019 22:33:08 +0300 Subject: [PATCH] Ensure that make-processed works w/ started session (#66) --- scripts/make-invoice-payment-processed.sh | 115 +++++++++++++--------- 1 file changed, 71 insertions(+), 44 deletions(-) diff --git a/scripts/make-invoice-payment-processed.sh b/scripts/make-invoice-payment-processed.sh index 62b0f1c..fe785d1 100755 --- a/scripts/make-invoice-payment-processed.sh +++ b/scripts/make-invoice-payment-processed.sh @@ -9,7 +9,7 @@ # schema. # -set -e +set -e -o pipefail CWD="$(dirname $0)" @@ -18,49 +18,90 @@ source "${CWD}/lib/logging" # Actual work is going here INVOICE="${1}" -PAYMENT="${2:-1}" +PAYMENT="${2}" -case ${INVOICE} in - ""|"-h"|"--help" ) - 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), if not specified taken from last invoice event." - echo -e " --force Force execution even when transaction info is missing." - echo -e " -h, --help Show this help message." - echo - echo -e "More information:" - echo -e " https://github.com/rbkmoney/damsel/blob/master/proto/payment_processing.thrift" - exit 0 - ;; - * ) - ;; -esac +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:" + echo -e " https://github.com/rbkmoney/damsel/blob/master/proto/payment_processing.thrift" + 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}\")" +) + +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 LAST_CHANGE=$( "${CWD}/hellgate/get-invoice-events.sh" "${INVOICE}" | jq '.[-1].payload.invoice_changes[-1].invoice_payment_change' ) -if [ \ - "${LAST_CHANGE}" = "null" -o \ - "$(echo "${LAST_CHANGE}" | jq -r '.payload | has("invoice_payment_cash_flow_changed")')" != "true" \ -]; then +if [ "${LAST_CHANGE}" = "null" ]; then err "Last seen change looks wrong for this repair scenario" fi LAST_PAYMENT="$(echo "${LAST_CHANGE}" | jq -r '.id')" - -if [ "$(echo "${LAST_CHANGE}" | jq -r '.id')" != "${PAYMENT}" ]; then +if [ "${LAST_PAYMENT}" != "${PAYMENT}" ]; then err "Last seen change related to another payment with id $(em "${LAST_PAYMENT}")" fi -TRXCHANGE= +if [ "$(echo "${LAST_CHANGE}" | jq -r '.payload | has("invoice_payment_cash_flow_changed")')" == "true" ]; then + +SESSION_CHANGE=$(cat <