[
  {
    "comment": "create a user account",
    "request": {
      "uri": "users",
      "method": "put",
      "entity": {
        "name": "test_user_credit_refund_restart@example.com",
        "password": "password1!",
        "agreeToTerms": true
      }
    }
  },

  {
    "before": "sleep 22s",  // wait for account objects to be created
    "comment": "login as new user",
    "request": {
      "session": "new",
      "uri": "auth/login",
      "entity": {
        "name": "test_user_credit_refund_restart@example.com",
        "password": "password1!"
      }
    },
    "response": {
      "store": "testAccount",
      "sessionName": "userSession",
      "session": "token"
    }
  },

  {
    "comment": "get payment methods, tokenize a credit card",
    "request": { "uri": "paymentMethods" },
    "response": {
      "store": "paymentMethods"
    },
    "after": "stripe_tokenize_card"
  },

  {
    "before": "sleep 1s",
    "comment": "as root, check email inbox for welcome message",
    "request": {
      "session": "rootSession",
      "uri": "debug/inbox/email/test_user_credit_refund_restart@example.com?type=request&action=welcome&target=account"
    },
    "response": {
      "store": "emailInbox",
      "check": [
        {"condition": "'{{json.[0].ctx.message.messageType}}' == 'request'"},
        {"condition": "'{{json.[0].ctx.message.action}}' == 'welcome'"},
        {"condition": "'{{json.[0].ctx.message.target}}' == 'account'"}
      ]
    }
  },

  {
    "comment": "approve email verification request",
    "request": {
      "session": "userSession",
      "uri": "auth/approve/{{emailInbox.[0].ctx.confirmationToken}}",
      "method": "post"
    }
  },

  {
    "comment": "get plans",
    "request": { "uri": "plans" },
    "response": {
      "store": "plans",
      "check": [{"condition": "json.length >= 1"}]
    }
  },

  {
    "comment": "add plan, using 'credit' payment method with a valid card token, creates a stripe customer",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "paymentMethodType": "credit",
          "paymentInfo": "{{stripeToken}}"
        }
      }
    },
    "response": {
      "store": "accountPlan"
    }
  },

  {
    "before": "sleep 15s",
    "comment": "verify account payment methods, should be one",
    "request": { "uri": "me/paymentMethods" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPaymentMethodType().name() === 'credit'"}
      ]
    }
  },

  {
    "comment": "verify account plans, should be one",
    "request": { "uri": "me/plans" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getName() === accountPlan.getName()"},
        {"condition": "json[0].enabled()"}
      ]
    }
  },

  {
    "comment": "verify account plan payment info",
    "request": { "uri": "me/plans/{{accountPlan.uuid}}/paymentMethod" },
    "response": {
      "store": "savedPaymentMethod",
      "check": [
        {"condition": "json.getPaymentMethodType().name() === 'credit'"},
        {"condition": "json.getMaskedPaymentInfo() === 'XXXX-XXXX-XXXX-4242'"}
      ]
    }
  },

  {
    "comment": "verify bill exists for new service with correct price and has been paid",
    "request": { "uri": "me/bills" },
    "response": {
      "store": "bills",
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'paid'"}
      ]
    }
  },

  {
    "comment": "verify successful payment exists for new service",
    "request": { "uri": "me/payments" },
    "response": {
      "store": "payments",
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getType().name() === 'payment'"},
        {"condition": "json[0].getStatus().name() === 'success'"}
      ]
    }
  },

  {
    "comment": "verify successful payment has paid for the bill above",
    "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getUuid() === '{{bills.[0].uuid}}'"},
        {"condition": "json[0].getStatus().name() === 'paid'"},
        {"condition": "json[0].getRefundedAmount() === 0"}
      ]
    }
  },

  {
    "comment": "delete plan",
    "request": {
      "method": "delete",
      "uri": "me/plans/{{accountPlan.uuid}}"
    }
  },

  {
    "before": "await_url me/payments 1m 2s await_json.length === 2",
    "comment": "verify refund payment has been processed",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 2"},
        {"condition": "json[0].getBill() === '{{bills.[0].uuid}}'"},
        {"condition": "json[0].getType().name() === 'refund'"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getAmount() > 0"},
        {"condition": "json[0].getAmount() < {{bills.[0].total}}"},
        // not sure if current month has 28, 29, 30 or 31 days, so let's make some reasonable bounds
        // we should only have a refund for all but one day
        {"condition": "json[0].getAmount() >= Math.round((bills[0].daysInPeriod() - 1) * (bills[0].getTotal() / bills[0].daysInPeriod()))"},
        {"condition": "json[0].getAmount() <= Math.round(bills[0].getTotal() - (bills[0].getTotal() / bills[0].daysInPeriod()))"}
      ]
    }
  },

  {
    "comment": "verify refund is reflected on the original bill",
    "request": { "uri": "me/plans/{{accountPlan.uuid}}/bills" },
    "response": {
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getUuid() === '{{bills.[0].uuid}}'"},
        {"condition": "json[0].getStatus().name() === 'paid'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getRefundedAmount() > 0"},
        {"condition": "json[0].getRefundedAmount() < {{bills.[0].total}}"},
        // not sure if current month has 28, 29, 30 or 31 days, so let's make some reasonable bounds
        // we should only have a refund for all but one day
        {"condition": "json[0].getRefundedAmount() > Math.round(bills[0].getTotal() - (bills[0].getTotal() / (bills[0].daysInPeriod() - 1)))"},
        {"condition": "json[0].getRefundedAmount() <= Math.round(bills[0].getTotal() - (bills[0].getTotal() / bills[0].daysInPeriod()))"}
      ]
    }
  },

  {
    "comment": "verify no current plans exist",
    "request": { "uri": "me/currentPlans" },
    "response": {
      "check": [ {"condition": "json.length === 0"} ]
    }
  },

  {
    "comment": "add a second plan, using saved payment method",
    "request": {
      "uri": "me/plans",
      "method": "put",
      "entity": {
        "name": "test-net2-{{rand 5}}",
        "domain": "{{defaultDomain}}",
        "locale": "en_US",
        "timezone": "EST",
        "plan": "{{plans.[0].name}}",
        "footprint": "US",
        "paymentMethodObject": {
          "uuid": "{{savedPaymentMethod.uuid}}"
        }
      }
    },
    "response": {
      "store": "accountPlan2"
    }
  },

  {
    "before": "sleep 15s",
    "comment": "verify we still have only one payment method",
    "request": { "uri": "me/paymentMethods" },
    "response": {
      "check": [ {"condition": "json.length === 1"} ]
    }
  },

  {
    "comment": "verify account plan payment info is same as used before",
    "request": { "uri": "me/plans/{{accountPlan2.uuid}}/paymentMethod" },
    "response": {
      "check": [
        {"condition": "json.getPaymentMethodType().name() === 'credit'"},
        {"condition": "json.getMaskedPaymentInfo() === 'XXXX-XXXX-XXXX-4242'"},
        {"condition": "json.getUuid() === '{{savedPaymentMethod.uuid}}'"}
      ]
    }
  },

  {
    "comment": "verify we now have two bills",
    "request": { "uri": "me/bills" },
    "response": {
      "check": [
        {"condition": "json.length === 2"}
      ]
    }
  },

  {
    "comment": "verify bill exists for new service",
    "request": { "uri": "me/plans/{{accountPlan2.uuid}}/bills" },
    "response": {
      "store": "plan2bills",
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan2.uuid}}'"},
        {"condition": "json[0].getTotal() === {{plans.[0].price}}"}
      ]
    }
  },

  {
    "comment": "verify we have now made two successful payments (plus one refund)",
    "request": { "uri": "me/payments" },
    "response": {
      "check": [
        {"condition": "json.length === 3"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan2.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"}
      ]
    }
  },

  {
    "comment": "verify payment exists via plan and is successful",
    "request": { "uri": "me/plans/{{accountPlan2.uuid}}/payments" },
    "response": {
      "store": "plan2payments",
      "check": [
        {"condition": "json.length === 1"},
        {"condition": "json[0].getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getAccountPlan() === '{{accountPlan2.uuid}}'"},
        {"condition": "json[0].getAmount() === {{plans.[0].price}}"},
        {"condition": "json[0].getStatus().name() === 'success'"},
        {"condition": "json[0].getBillObject().getPlan() === '{{plans.[0].uuid}}'"},
        {"condition": "json[0].getBillObject().getAccountPlan() === '{{accountPlan2.uuid}}'"},
        {"condition": "json[0].getBillObject().getTotal() === {{plans.[0].price}}"},
        {"condition": "json[0].getBillObject().getStatus().name() === 'paid'"}
      ]
    }
  }
]