<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:modeler="http://camunda.org/schema/modeler/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="@bpmnkit/core" exporterVersion="0.0.23" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.9.0">
  <bpmn:error id="Error_PAYMENT_DECLINED" name="PAYMENT_DECLINED" errorCode="PAYMENT_DECLINED"/>
  <bpmn:process id="payment-saga" name="Checkout Payment Saga" isExecutable="true">
    <bpmn:extensionElements>
      <zeebe:versionTag value="1.0.0"/>
    </bpmn:extensionElements>
    <bpmn:startEvent id="checkout-completed" name="Checkout completed">
      <bpmn:outgoing>Flow_checkout-completed_reserve-stock</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:serviceTask id="reserve-stock" name="Reserve stock">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="inventory-reserve"/>
        <zeebe:ioMapping>
          <zeebe:output source="=reservationId" target="reservationId"/>
        </zeebe:ioMapping>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_checkout-completed_reserve-stock</bpmn:incoming>
      <bpmn:outgoing>Flow_reserve-stock_take-payment</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:subProcess id="take-payment" name="Take payment">
      <bpmn:incoming>Flow_reserve-stock_take-payment</bpmn:incoming>
      <bpmn:outgoing>Flow_take-payment_create-shipment</bpmn:outgoing>
      <bpmn:startEvent id="payment-start">
        <bpmn:outgoing>Flow_payment-start_authorise-payment</bpmn:outgoing>
      </bpmn:startEvent>
      <bpmn:serviceTask id="authorise-payment" name="Authorise payment">
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="payment-authorise"/>
          <zeebe:ioMapping>
            <zeebe:output source="=authorised" target="paymentAuthorised"/>
            <zeebe:output source="=authorisationId" target="authorisationId"/>
          </zeebe:ioMapping>
        </bpmn:extensionElements>
        <bpmn:incoming>Flow_payment-start_authorise-payment</bpmn:incoming>
        <bpmn:outgoing>Flow_authorise-payment_authorised</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:exclusiveGateway id="authorised" name="Authorised?" default="Flow_authorised_payment-declined">
        <bpmn:incoming>Flow_authorise-payment_authorised</bpmn:incoming>
        <bpmn:outgoing>Flow_authorised_capture-payment</bpmn:outgoing>
        <bpmn:outgoing>Flow_authorised_payment-declined</bpmn:outgoing>
      </bpmn:exclusiveGateway>
      <bpmn:serviceTask id="capture-payment" name="Capture payment">
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="payment-capture"/>
          <zeebe:ioMapping>
            <zeebe:output source="=paymentId" target="paymentId"/>
          </zeebe:ioMapping>
        </bpmn:extensionElements>
        <bpmn:incoming>Flow_authorised_capture-payment</bpmn:incoming>
        <bpmn:outgoing>Flow_capture-payment_payment-taken</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:endEvent id="payment-taken">
        <bpmn:incoming>Flow_capture-payment_payment-taken</bpmn:incoming>
      </bpmn:endEvent>
      <bpmn:endEvent id="payment-declined">
        <bpmn:incoming>Flow_authorised_payment-declined</bpmn:incoming>
        <bpmn:errorEventDefinition errorRef="Error_PAYMENT_DECLINED"/>
      </bpmn:endEvent>
      <bpmn:sequenceFlow id="Flow_payment-start_authorise-payment" sourceRef="payment-start" targetRef="authorise-payment"/>
      <bpmn:sequenceFlow id="Flow_authorise-payment_authorised" sourceRef="authorise-payment" targetRef="authorised"/>
      <bpmn:sequenceFlow id="Flow_authorised_capture-payment" sourceRef="authorised" targetRef="capture-payment" name="yes">
        <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">=paymentAuthorised</bpmn:conditionExpression>
      </bpmn:sequenceFlow>
      <bpmn:sequenceFlow id="Flow_capture-payment_payment-taken" sourceRef="capture-payment" targetRef="payment-taken"/>
      <bpmn:sequenceFlow id="Flow_authorised_payment-declined" sourceRef="authorised" targetRef="payment-declined" name="no"/>
    </bpmn:subProcess>
    <bpmn:boundaryEvent id="on-payment-declined" name="Payment declined" attachedToRef="take-payment">
      <bpmn:outgoing>Flow_on-payment-declined_release-stock</bpmn:outgoing>
      <bpmn:errorEventDefinition errorRef="Error_PAYMENT_DECLINED"/>
    </bpmn:boundaryEvent>
    <bpmn:serviceTask id="release-stock" name="Release stock">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="inventory-release"/>
        <zeebe:ioMapping>
          <zeebe:input source="=reservationId" target="releaseReservationId"/>
        </zeebe:ioMapping>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_on-payment-declined_release-stock</bpmn:incoming>
      <bpmn:outgoing>Flow_release-stock_notify-declined</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="notify-declined" name="Notify customer: payment declined">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="customer-notify"/>
        <zeebe:taskHeaders>
          <zeebe:header key="template" value="payment-declined"/>
        </zeebe:taskHeaders>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_release-stock_notify-declined</bpmn:incoming>
      <bpmn:outgoing>Flow_notify-declined_order-failed</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="order-failed" name="Order failed">
      <bpmn:incoming>Flow_notify-declined_order-failed</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:serviceTask id="create-shipment" name="Create shipment">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="shipment-create"/>
        <zeebe:ioMapping>
          <zeebe:output source="=trackingNumber" target="trackingNumber"/>
        </zeebe:ioMapping>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_take-payment_create-shipment</bpmn:incoming>
      <bpmn:outgoing>Flow_create-shipment_order-shipped</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="order-shipped" name="Order shipped">
      <bpmn:incoming>Flow_create-shipment_order-shipped</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_checkout-completed_reserve-stock" sourceRef="checkout-completed" targetRef="reserve-stock"/>
    <bpmn:sequenceFlow id="Flow_reserve-stock_take-payment" sourceRef="reserve-stock" targetRef="take-payment"/>
    <bpmn:sequenceFlow id="Flow_on-payment-declined_release-stock" sourceRef="on-payment-declined" targetRef="release-stock"/>
    <bpmn:sequenceFlow id="Flow_release-stock_notify-declined" sourceRef="release-stock" targetRef="notify-declined"/>
    <bpmn:sequenceFlow id="Flow_notify-declined_order-failed" sourceRef="notify-declined" targetRef="order-failed"/>
    <bpmn:sequenceFlow id="Flow_take-payment_create-shipment" sourceRef="take-payment" targetRef="create-shipment"/>
    <bpmn:sequenceFlow id="Flow_create-shipment_order-shipped" sourceRef="create-shipment" targetRef="order-shipped"/>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="payment-saga_di">
    <bpmndi:BPMNPlane id="payment-saga_di_plane" bpmnElement="payment-saga">
      <bpmndi:BPMNShape id="checkout-completed_di" bpmnElement="checkout-completed">
        <dc:Bounds x="65" y="186" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="20" y="226" width="126" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="reserve-stock_di" bpmnElement="reserve-stock">
        <dc:Bounds x="201" y="164" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="take-payment_di" bpmnElement="take-payment" isExpanded="true">
        <dc:Bounds x="401" y="20" width="822" height="368"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="on-payment-declined_di" bpmnElement="on-payment-declined">
        <dc:Bounds x="794" y="370" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="756" y="410" width="112" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="release-stock_di" bpmnElement="release-stock">
        <dc:Bounds x="1323" y="468" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="notify-declined_di" bpmnElement="notify-declined">
        <dc:Bounds x="1523" y="468" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="order-failed_di" bpmnElement="order-failed">
        <dc:Bounds x="1723" y="490" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1699" y="530" width="84" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="create-shipment_di" bpmnElement="create-shipment">
        <dc:Bounds x="1323" y="164" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="order-shipped_di" bpmnElement="order-shipped">
        <dc:Bounds x="1555" y="186" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1528" y="226" width="91" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="payment-start_di" bpmnElement="payment-start">
        <dc:Bounds x="451" y="120" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="authorise-payment_di" bpmnElement="authorise-payment">
        <dc:Bounds x="587" y="98" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="authorised_di" bpmnElement="authorised">
        <dc:Bounds x="787" y="113" width="50" height="50"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="774" y="167" width="77" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="capture-payment_di" bpmnElement="capture-payment">
        <dc:Bounds x="937" y="258" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="payment-taken_di" bpmnElement="payment-taken">
        <dc:Bounds x="1137" y="280" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="payment-declined_di" bpmnElement="payment-declined">
        <dc:Bounds x="969" y="120" width="36" height="36"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="Flow_checkout-completed_reserve-stock_di" bpmnElement="Flow_checkout-completed_reserve-stock">
        <di:waypoint x="101" y="204"/>
        <di:waypoint x="201" y="204"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_reserve-stock_take-payment_di" bpmnElement="Flow_reserve-stock_take-payment">
        <di:waypoint x="301" y="204"/>
        <di:waypoint x="401" y="204"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_on-payment-declined_release-stock_di" bpmnElement="Flow_on-payment-declined_release-stock">
        <di:waypoint x="812" y="406"/>
        <di:waypoint x="812" y="508"/>
        <di:waypoint x="1323" y="508"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_release-stock_notify-declined_di" bpmnElement="Flow_release-stock_notify-declined">
        <di:waypoint x="1423" y="508"/>
        <di:waypoint x="1523" y="508"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_notify-declined_order-failed_di" bpmnElement="Flow_notify-declined_order-failed">
        <di:waypoint x="1623" y="508"/>
        <di:waypoint x="1723" y="508"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_take-payment_create-shipment_di" bpmnElement="Flow_take-payment_create-shipment">
        <di:waypoint x="1223" y="204"/>
        <di:waypoint x="1323" y="204"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_create-shipment_order-shipped_di" bpmnElement="Flow_create-shipment_order-shipped">
        <di:waypoint x="1423" y="204"/>
        <di:waypoint x="1555" y="204"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_payment-start_authorise-payment_di" bpmnElement="Flow_payment-start_authorise-payment">
        <di:waypoint x="487" y="138"/>
        <di:waypoint x="587" y="138"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_authorise-payment_authorised_di" bpmnElement="Flow_authorise-payment_authorised">
        <di:waypoint x="687" y="138"/>
        <di:waypoint x="787" y="138"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_authorised_capture-payment_di" bpmnElement="Flow_authorised_capture-payment">
        <di:waypoint x="812" y="163"/>
        <di:waypoint x="812" y="298"/>
        <di:waypoint x="937" y="298"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="788" y="224" width="40" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_capture-payment_payment-taken_di" bpmnElement="Flow_capture-payment_payment-taken">
        <di:waypoint x="1037" y="298"/>
        <di:waypoint x="1137" y="298"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_authorised_payment-declined_di" bpmnElement="Flow_authorised_payment-declined">
        <di:waypoint x="837" y="138"/>
        <di:waypoint x="969" y="138"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="883" y="114" width="40" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

