{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:sitla:circulation:4:schema:conditional-instruction",
  "$comment": "SPDX-License-Identifier: CC0-1.0; Stripe online adapter, not a new wire/1 record or proof of provider execution.",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "profile": {
      "const": "sitla-conditional-payment-instruction/2"
    },
    "action": {
      "enum": [
        "collection-authorize",
        "refund",
        "settle",
        "deliver"
      ]
    },
    "transactionId": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
    },
    "buyer": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        },
        "controllerDid": {
          "anyOf": [
            {
              "type": "string",
              "minLength": 1,
              "maxLength": 2048
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "id",
        "controllerDid"
      ],
      "additionalProperties": false
    },
    "seller": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        },
        "controllerDid": {
          "anyOf": [
            {
              "type": "string",
              "minLength": 1,
              "maxLength": 2048
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "id",
        "controllerDid"
      ],
      "additionalProperties": false
    },
    "assertingAccount": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "amountMinor": {
      "type": "integer",
      "minimum": 500,
      "maximum": 9007199254740991
    },
    "currency": {
      "const": "JPY"
    },
    "termsDigest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "refundAt": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^\\d{4}-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d(?:\\.\\d{3})?Z$"
        },
        {
          "type": "null"
        }
      ]
    },
    "instruction": {},
    "collectionPolicy": {
      "const": "sitla-collection-agency/1"
    },
    "approval": {
      "type": "object",
      "properties": {
        "operationId": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "evidenceDigest": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        }
      },
      "required": [
        "operationId",
        "evidenceDigest"
      ],
      "additionalProperties": false
    },
    "limitations": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 2048
      },
      "minItems": 1,
      "maxItems": 100
    },
    "collectionAuthority": {
      "type": "object",
      "properties": {
        "agent": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        },
        "processor": {
          "const": "Stripe"
        },
        "scope": {
          "const": "identified-transaction-only"
        },
        "priceDebtDischarge": {
          "const": "valid-stripe-capture"
        },
        "sellerRemittance": {
          "const": "separate-operator-obligation"
        },
        "refunds": {
          "const": "original-payment-method-under-fixed-policy"
        }
      },
      "required": [
        "agent",
        "processor",
        "scope",
        "priceDebtDischarge",
        "sellerRemittance",
        "refunds"
      ],
      "additionalProperties": false
    },
    "inspection": {
      "type": "object",
      "properties": {
        "confirmedByBuyer": {
          "const": true
        },
        "performanceDigest": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "basis": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2048
        }
      },
      "required": [
        "confirmedByBuyer",
        "performanceDigest",
        "basis"
      ],
      "additionalProperties": false
    },
    "resultPolicies": {
      "type": "object",
      "required": [
        "buyer",
        "seller"
      ],
      "properties": {
        "buyer": {
          "$ref": "#/$defs/policy"
        },
        "seller": {
          "$ref": "#/$defs/policy"
        }
      },
      "additionalProperties": false
    },
    "currencyExponent": {
      "const": 0
    },
    "holdSeconds": {
      "type": "integer",
      "minimum": 3600,
      "maximum": 2592000
    },
    "fundedAt": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^\\d{4}-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d(?:\\.\\d{3})?Z$"
        },
        {
          "type": "null"
        }
      ]
    },
    "jobRequestIds": {
      "type": "array",
      "maxItems": 20,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 2048
      }
    }
  },
  "required": [
    "id",
    "profile",
    "action",
    "transactionId",
    "buyer",
    "seller",
    "assertingAccount",
    "amountMinor",
    "currency",
    "termsDigest",
    "refundAt",
    "instruction",
    "collectionPolicy",
    "approval",
    "limitations",
    "resultPolicies",
    "currencyExponent",
    "holdSeconds",
    "fundedAt",
    "jobRequestIds"
  ],
  "additionalProperties": false,
  "oneOf": [
    {
      "properties": {
        "action": {
          "const": "collection-authorize"
        },
        "instruction": {
          "type": "object",
          "properties": {
            "escrowId": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            "termsDigest": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "confirmUnderlyingPriceClaim": {
              "const": true
            },
            "collectionBeforeOrWithPerformance": {
              "const": true
            },
            "authorizeReceipt": {
              "const": true
            },
            "dischargeOnStripeCapture": {
              "const": true
            },
            "authorizeConditionalRefunds": {
              "const": true
            }
          },
          "required": [
            "escrowId",
            "termsDigest",
            "confirmUnderlyingPriceClaim",
            "collectionBeforeOrWithPerformance",
            "authorizeReceipt",
            "dischargeOnStripeCapture",
            "authorizeConditionalRefunds"
          ],
          "additionalProperties": false
        },
        "collectionAuthority": {},
        "fundedAt": {
          "type": "null"
        },
        "refundAt": {
          "type": "null"
        }
      },
      "allOf": [
        {
          "not": {
            "properties": {
              "inspection": {}
            },
            "required": [
              "inspection"
            ]
          }
        }
      ],
      "required": [
        "collectionAuthority"
      ]
    },
    {
      "properties": {
        "action": {
          "const": "refund"
        },
        "instruction": {
          "type": "object",
          "properties": {
            "escrowId": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            "termsDigest": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "depositReference": {
              "type": "string",
              "minLength": 1,
              "maxLength": 2048
            },
            "predecessor": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            }
          },
          "required": [
            "escrowId",
            "termsDigest",
            "depositReference",
            "predecessor"
          ],
          "additionalProperties": false
        },
        "fundedAt": {
          "type": "string"
        },
        "refundAt": {
          "type": "string"
        }
      },
      "not": {
        "properties": {
          "collectionAuthority": {}
        },
        "required": [
          "collectionAuthority"
        ]
      },
      "allOf": [
        {
          "not": {
            "properties": {
              "inspection": {}
            },
            "required": [
              "inspection"
            ]
          }
        }
      ]
    },
    {
      "properties": {
        "action": {
          "const": "settle"
        },
        "instruction": {
          "type": "object",
          "properties": {
            "escrowId": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            "termsDigest": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "depositReference": {
              "type": "string",
              "minLength": 1,
              "maxLength": 2048
            },
            "predecessor": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            "inspectionConfirmed": {
              "const": true
            },
            "performanceDigest": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "resultCredential": {
              "type": "object",
              "properties": {
                "secured": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 32768
                },
                "authority": {
                  "type": "object"
                }
              },
              "required": [
                "secured",
                "authority"
              ],
              "additionalProperties": false
            }
          },
          "required": [
            "escrowId",
            "termsDigest",
            "depositReference",
            "predecessor",
            "inspectionConfirmed",
            "performanceDigest",
            "resultCredential"
          ],
          "additionalProperties": false
        },
        "inspection": {},
        "fundedAt": {
          "type": "string"
        },
        "refundAt": {
          "type": "string"
        }
      },
      "not": {
        "properties": {
          "collectionAuthority": {}
        },
        "required": [
          "collectionAuthority"
        ]
      },
      "required": [
        "inspection"
      ]
    },
    {
      "properties": {
        "action": {
          "const": "deliver"
        },
        "instruction": {
          "type": "object",
          "properties": {
            "escrowId": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            "termsDigest": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "depositReference": {
              "type": "string",
              "minLength": 1,
              "maxLength": 2048
            },
            "predecessor": {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            "resultCredential": {
              "type": "object",
              "properties": {
                "secured": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 32768
                },
                "authority": {
                  "type": "object"
                }
              },
              "required": [
                "secured",
                "authority"
              ],
              "additionalProperties": false
            }
          },
          "required": [
            "escrowId",
            "termsDigest",
            "depositReference",
            "predecessor",
            "resultCredential"
          ],
          "additionalProperties": false
        },
        "fundedAt": {
          "type": "string"
        },
        "refundAt": {
          "type": "string"
        }
      },
      "not": {
        "properties": {
          "collectionAuthority": {}
        },
        "required": [
          "collectionAuthority"
        ]
      },
      "allOf": [
        {
          "not": {
            "properties": {
              "inspection": {}
            },
            "required": [
              "inspection"
            ]
          }
        }
      ]
    }
  ],
  "$defs": {
    "policy": {
      "type": "object",
      "required": [
        "licenses",
        "didAllowList"
      ],
      "properties": {
        "licenses": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string"
          }
        },
        "didAllowList": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "additionalProperties": false
    }
  }
}
