Skip to content

Instantly share code, notes, and snippets.

@Winwardo
Last active December 29, 2020 11:31
Show Gist options
  • Save Winwardo/8563a7b32ea43e96e4b160c19d423d8f to your computer and use it in GitHub Desktop.
Save Winwardo/8563a7b32ea43e96e4b160c19d423d8f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function fail(makeErrorData = (c, e) => ({ ...e })) {
return {
target: "#payment.failed",
actions: assign({
error_code: (c, e) => e.type,
error_data: makeErrorData,
}),
};
}
const paymentMachine = Machine({
id: "payment",
initial: "creation",
context: {
retries: 0,
error_code: null,
error_data: null,
},
states: {
creation: {
initial: "done",
states: {
done: {
on: {
ADVANCE: "#payment.initial_checks.waiting",
},
},
},
},
initial_checks: {
states: {
waiting: { on: { "": "inprogress" } },
inprogress: {
on: {
SUCCESS_CHECKS_PASSED: "done",
IDEMPOTENCY_KEY_ALREADY_EXISTS: fail((c, e) => ({
idempotency_key: e.idempotency_key,
})),
ZERO_TICKETS_IN_ORDER: fail(),
TICKETS_FROM_SEPARATE_CLIENTS: fail(),
CLIENT_NOT_ACTIVATED: fail(),
CLIENT_EMAIL_NOT_VALIDATED: fail(),
UNKNOWN_ERROR: fail(),
},
},
done: {
on: {
ADVANCE: "#payment.reserve_tickets.waiting",
},
},
},
},
reserve_tickets: {
states: {
waiting: { on: { "": "inprogress" } },
inprogress: {
on: {
SUCCESS_TICKETS_RESERVED: "done",
UNKNOWN_ERROR: fail(),
},
},
reversing: {
on: { SUCCESS_TICKETS_RELEASED: "waiting" },
UNKNOWN_ERROR: fail(),
},
done: {
on: {
ADVANCE: "#payment.stripe_add_payment_intent.waiting",
REVERSE: "reversing",
},
},
},
},
stripe_add_payment_intent: {
states: {
waiting: { on: { "": "inprogress" } },
inprogress: {
on: {
SUCCESS_PAYMENT_INTENT_ADDED: "done",
UNKNOWN_ERROR: fail(),
},
},
done: {
on: {
ADVANCE: "#payment.stripe_waiting_on_3D_secure.waiting",
},
},
},
},
stripe_waiting_on_3D_secure: {
states: {
waiting: {
on: {
START: "inprogress",
},
},
inprogress: {
on: {
SUCCESS_3D_SECURE_FOUND: "done",
SUCCESS_IMMEDIATE_CAPTURE_AVAILABLE: "done",
WAITING_ON_3D_SECURE: "waiting",
UNKNOWN_ERROR: fail(),
},
},
done: {
on: {
ADVANCE: "#payment.stripe_capture_payment_intent.waiting",
},
},
},
},
stripe_capture_payment_intent: {
states: {
waiting: { on: { "": "inprogress" } },
inprogress: {
on: {
SUCCESS_PAYMENT_INTENT_CAPTURED: "done",
INVALID_INTENT_STATUS: fail(),
UNKNOWN_ERROR: fail(),
},
},
done: {
on: {
ADVANCE: "#payment.writing_local_purchases.waiting",
},
},
},
},
writing_local_purchases: {
states: {
waiting: { on: { "": "inprogress" } },
inprogress: {
on: {
SUCCESS_PURCHASES_WRITTEN: "done",
UNKNOWN_ERROR: fail(),
},
},
done: {
on: {
ADVANCE: "#payment.complete",
},
},
},
},
complete: {},
failed: {},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment