{
  "$schema": "./.schemas/page.schema.json",
  "title": "Payments",
  "icon": "creditcard",
  "sidebar": false,
  "period": {
    "step": true,
    "choices": [
      {
        "id": "month",
        "label": "Month",
        "span": "month"
      }
    ]
  },
  "sources": {
    "charges": {
      "url": "https://api.stripe.com",
      "secret": "stripe",
      "every": "15m",
      "headers": {
        "Stripe-Version": "2026-09-30.endive"
      },
      "requests": {
        "charges": {
          "path": "/v1/charges?created[gte]={period.start_unix}&created[lt]={period.end_unix}&limit=100&expand[]=data.customer",
          "follow": {
            "items": "data",
            "while": "has_more",
            "param": "starting_after",
            "from_last": "id",
            "max": 10
          }
        }
      },
      "derive": [
        {
          "name": "pay_rows",
          "value": {
            "each": "charges.data",
            "fields": {
              "id": "id",
              "livemode": "livemode",
              "status": "status",
              "created": "created",
              "pi": {
                "path": "payment_intent",
                "default": {
                  "template": "{id}"
                }
              },
              "who": {
                "if": {
                  "path": "customer.name",
                  "exists": true
                },
                "then": {
                  "template": "{customer.name}"
                },
                "else": {
                  "if": {
                    "path": "billing_details.name",
                    "exists": true
                  },
                  "then": {
                    "template": "{billing_details.name}"
                  },
                  "else": {
                    "if": {
                      "path": "customer.email",
                      "exists": true
                    },
                    "then": {
                      "template": "{customer.email}"
                    },
                    "else": {
                      "if": {
                        "path": "receipt_email",
                        "exists": true
                      },
                      "then": {
                        "template": "{receipt_email}"
                      },
                      "else": {
                        "template": "Payment"
                      }
                    }
                  }
                }
              },
              "what": {
                "if": {
                  "path": "description",
                  "exists": true
                },
                "then": {
                  "template": "{description}"
                },
                "else": {
                  "if": {
                    "path": "payment_method_details.card.last4",
                    "exists": true
                  },
                  "then": {
                    "template": "{payment_method_details.card.brand|title} ····{payment_method_details.card.last4}"
                  },
                  "else": {
                    "template": "{payment_method_details.type|title}"
                  }
                }
              },
              "why": {
                "if": {
                  "path": "outcome.reason",
                  "exists": true
                },
                "then": {
                  "template": "{outcome.reason|title}"
                },
                "else": {
                  "if": {
                    "path": "failure_code",
                    "exists": true
                  },
                  "then": {
                    "template": "{failure_code|title}"
                  },
                  "else": {
                    "template": "Failed"
                  }
                }
              },
              "units": {
                "if": {
                  "path": "captured",
                  "equals": false
                },
                "then": {
                  "multiply": [
                    "amount",
                    {
                      "path": "currency",
                      "values": {
                        "bif": 1,
                        "clp": 1,
                        "djf": 1,
                        "gnf": 1,
                        "jpy": 1,
                        "kmf": 1,
                        "krw": 1,
                        "mga": 1,
                        "pyg": 1,
                        "rwf": 1,
                        "vnd": 1,
                        "vuv": 1,
                        "xaf": 1,
                        "xof": 1,
                        "xpf": 1,
                        "bhd": 0.001,
                        "jod": 0.001,
                        "kwd": 0.001,
                        "omr": 0.001,
                        "tnd": 0.001
                      },
                      "default": 0.01
                    }
                  ],
                  "round": 3
                },
                "else": {
                  "if": {
                    "path": "amount_captured",
                    "exists": true
                  },
                  "then": {
                    "multiply": [
                      "amount_captured",
                      {
                        "path": "currency",
                        "values": {
                          "bif": 1,
                          "clp": 1,
                          "djf": 1,
                          "gnf": 1,
                          "jpy": 1,
                          "kmf": 1,
                          "krw": 1,
                          "mga": 1,
                          "pyg": 1,
                          "rwf": 1,
                          "vnd": 1,
                          "vuv": 1,
                          "xaf": 1,
                          "xof": 1,
                          "xpf": 1,
                          "bhd": 0.001,
                          "jod": 0.001,
                          "kwd": 0.001,
                          "omr": 0.001,
                          "tnd": 0.001
                        },
                        "default": 0.01
                      }
                    ],
                    "round": 3
                  },
                  "else": {
                    "multiply": [
                      "amount",
                      {
                        "path": "currency",
                        "values": {
                          "bif": 1,
                          "clp": 1,
                          "djf": 1,
                          "gnf": 1,
                          "jpy": 1,
                          "kmf": 1,
                          "krw": 1,
                          "mga": 1,
                          "pyg": 1,
                          "rwf": 1,
                          "vnd": 1,
                          "vuv": 1,
                          "xaf": 1,
                          "xof": 1,
                          "xpf": 1,
                          "bhd": 0.001,
                          "jod": 0.001,
                          "kwd": 0.001,
                          "omr": 0.001,
                          "tnd": 0.001
                        },
                        "default": 0.01
                      }
                    ],
                    "round": 3
                  }
                }
              },
              "refund_units": {
                "multiply": [
                  "amount_refunded",
                  {
                    "path": "currency",
                    "values": {
                      "bif": 1,
                      "clp": 1,
                      "djf": 1,
                      "gnf": 1,
                      "jpy": 1,
                      "kmf": 1,
                      "krw": 1,
                      "mga": 1,
                      "pyg": 1,
                      "rwf": 1,
                      "vnd": 1,
                      "vuv": 1,
                      "xaf": 1,
                      "xof": 1,
                      "xpf": 1,
                      "bhd": 0.001,
                      "jod": 0.001,
                      "kwd": 0.001,
                      "omr": 0.001,
                      "tnd": 0.001
                    },
                    "default": 0.01
                  }
                ],
                "round": 3
              },
              "refunded_some": {
                "if": {
                  "path": "amount_refunded",
                  "gt": 0
                },
                "then": {
                  "template": "yes"
                }
              },
              "refunded": "refunded",
              "disputed": "disputed",
              "captured": {
                "path": "captured",
                "default": true
              },
              "sym": {
                "path": "currency",
                "values": {
                  "usd": "$",
                  "eur": "€",
                  "gbp": "£",
                  "jpy": "¥",
                  "inr": "₹",
                  "krw": "₩",
                  "cad": "CA$",
                  "aud": "A$",
                  "nzd": "NZ$",
                  "hkd": "HK$",
                  "sgd": "S$",
                  "mxn": "MX$",
                  "brl": "R$"
                },
                "default": {
                  "template": "{currency|upper} "
                }
              },
              "dec": {
                "path": "currency",
                "values": {
                  "bif": 0,
                  "clp": 0,
                  "djf": 0,
                  "gnf": 0,
                  "jpy": 0,
                  "kmf": 0,
                  "krw": 0,
                  "mga": 0,
                  "pyg": 0,
                  "rwf": 0,
                  "vnd": 0,
                  "vuv": 0,
                  "xaf": 0,
                  "xof": 0,
                  "xpf": 0,
                  "bhd": 3,
                  "jod": 3,
                  "kwd": 3,
                  "omr": 3,
                  "tnd": 3
                },
                "default": 2
              }
            }
          }
        },
        {
          "name": "pay_list",
          "value": {
            "each": "pay_rows",
            "fields": {
              "id": "id",
              "who": "who",
              "what": "what",
              "why": "why",
              "status": "status",
              "disputed": "disputed",
              "refunded": "refunded",
              "captured": "captured",
              "refunded_some": "refunded_some",
              "created": "created",
              "livemode": "livemode",
              "pi": "pi",
              "units": "units",
              "refund_units": "refund_units",
              "sym": "sym",
              "dec": "dec",
              "money": {
                "if": {
                  "path": "dec",
                  "equals": 0
                },
                "then": {
                  "template": "{sym}{units|round:0}"
                },
                "else": {
                  "if": {
                    "path": "dec",
                    "equals": 3
                  },
                  "then": {
                    "template": "{sym}{units|round:3}"
                  },
                  "else": {
                    "template": "{sym}{units|round:2}"
                  }
                }
              },
              "refund_money": {
                "if": {
                  "path": "dec",
                  "equals": 0
                },
                "then": {
                  "template": "{sym}{refund_units|round:0}"
                },
                "else": {
                  "if": {
                    "path": "dec",
                    "equals": 3
                  },
                  "then": {
                    "template": "{sym}{refund_units|round:3}"
                  },
                  "else": {
                    "template": "{sym}{refund_units|round:2}"
                  }
                }
              }
            }
          }
        },
        {
          "name": "paid",
          "value": {
            "count": "pay_list",
            "where": {
              "all": [
                {
                  "path": "status",
                  "equals": "succeeded"
                },
                {
                  "path": "refund_units",
                  "lte": 0
                },
                {
                  "path": "captured",
                  "not": false
                }
              ]
            }
          }
        },
        {
          "name": "paid_rows",
          "value": {
            "count": "pay_list",
            "where": {
              "all": [
                {
                  "path": "status",
                  "in": [
                    "succeeded",
                    "pending"
                  ]
                },
                {
                  "path": "refund_units",
                  "lte": 0
                }
              ]
            }
          }
        },
        {
          "name": "taken",
          "value": {
            "count": "pay_list",
            "where": {
              "path": "status",
              "in": [
                "succeeded",
                "pending"
              ]
            }
          }
        },
        {
          "name": "failed",
          "value": {
            "count": "pay_list",
            "where": {
              "path": "status",
              "equals": "failed"
            }
          }
        },
        {
          "name": "tries",
          "value": {
            "add": [
              "taken",
              "failed"
            ]
          }
        },
        {
          "name": "failed_share",
          "value": {
            "divide": [
              "failed",
              "tries"
            ],
            "default": 0
          }
        },
        {
          "name": "refunds",
          "value": {
            "count": "pay_list",
            "where": {
              "path": "refund_units",
              "gt": 0
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "paid-count",
      "type": "stat",
      "label": "Paid",
      "value": "—",
      "source": {
        "ref": "charges",
        "map": {
          "value": "paid"
        }
      }
    },
    {
      "id": "failed-count",
      "type": "stat",
      "label": "Failed",
      "value": "—",
      "source": {
        "ref": "charges",
        "map": {
          "value": "failed",
          "delta": {
            "if": {
              "path": "tries",
              "gt": 0
            },
            "then": {
              "template": "{failed_share|percent} of tries"
            }
          }
        }
      }
    },
    {
      "id": "cut",
      "type": "text",
      "tone": "info",
      "body": "—",
      "source": {
        "ref": "charges",
        "when": {
          "path": "charges.has_more",
          "equals": true
        },
        "map": {
          "body": {
            "template": "The newest 1,000 payments this month. [All payments in Stripe](https://dashboard.stripe.com/payments)"
          }
        }
      }
    },
    {
      "id": "tabs",
      "type": "tabs",
      "tabs": [
        {
          "label": "Paid",
          "cards": [
            {
              "id": "paid",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "charges",
                "empty": {
                  "text": "No payments",
                  "when": {
                    "path": "paid_rows",
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "pay_list",
                    "where": {
                      "all": [
                        {
                          "path": "status",
                          "in": [
                            "succeeded",
                            "pending"
                          ]
                        },
                        {
                          "path": "refund_units",
                          "lte": 0
                        }
                      ]
                    },
                    "fields": {
                      "key": "id",
                      "text": "who",
                      "subtitle": {
                        "if": {
                          "path": "status",
                          "equals": "failed"
                        },
                        "then": {
                          "template": "Failed · {why}"
                        },
                        "else": {
                          "if": {
                            "path": "disputed",
                            "equals": true
                          },
                          "then": {
                            "template": "Disputed · {what}"
                          },
                          "else": {
                            "if": {
                              "path": "refunded",
                              "equals": true
                            },
                            "then": {
                              "template": "Refunded · {what}"
                            },
                            "else": {
                              "if": {
                                "path": "refunded_some",
                                "exists": true
                              },
                              "then": {
                                "template": "{refund_money} refunded · {what}"
                              },
                              "else": {
                                "if": {
                                  "path": "status",
                                  "equals": "pending"
                                },
                                "then": {
                                  "template": "Pending · {what}"
                                },
                                "else": {
                                  "if": {
                                    "path": "captured",
                                    "equals": false
                                  },
                                  "then": {
                                    "template": "Not captured · {what}"
                                  },
                                  "else": {
                                    "template": "{what}"
                                  }
                                }
                              }
                            }
                          }
                        }
                      },
                      "detail": {
                        "template": "{money} · {created|ago}"
                      },
                      "url": {
                        "if": {
                          "path": "livemode",
                          "equals": false
                        },
                        "then": {
                          "template": "https://dashboard.stripe.com/test/payments/{pi}"
                        },
                        "else": {
                          "template": "https://dashboard.stripe.com/payments/{pi}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Failed",
          "cards": [
            {
              "id": "failed",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "charges",
                "empty": {
                  "text": "No failed payments",
                  "when": {
                    "path": "failed",
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "pay_list",
                    "where": {
                      "path": "status",
                      "equals": "failed"
                    },
                    "fields": {
                      "key": "id",
                      "text": "who",
                      "subtitle": {
                        "if": {
                          "path": "status",
                          "equals": "failed"
                        },
                        "then": {
                          "template": "Failed · {why}"
                        },
                        "else": {
                          "if": {
                            "path": "disputed",
                            "equals": true
                          },
                          "then": {
                            "template": "Disputed · {what}"
                          },
                          "else": {
                            "if": {
                              "path": "refunded",
                              "equals": true
                            },
                            "then": {
                              "template": "Refunded · {what}"
                            },
                            "else": {
                              "if": {
                                "path": "refunded_some",
                                "exists": true
                              },
                              "then": {
                                "template": "{refund_money} refunded · {what}"
                              },
                              "else": {
                                "if": {
                                  "path": "status",
                                  "equals": "pending"
                                },
                                "then": {
                                  "template": "Pending · {what}"
                                },
                                "else": {
                                  "if": {
                                    "path": "captured",
                                    "equals": false
                                  },
                                  "then": {
                                    "template": "Not captured · {what}"
                                  },
                                  "else": {
                                    "template": "{what}"
                                  }
                                }
                              }
                            }
                          }
                        }
                      },
                      "detail": {
                        "template": "{money} · {created|ago}"
                      },
                      "url": {
                        "if": {
                          "path": "livemode",
                          "equals": false
                        },
                        "then": {
                          "template": "https://dashboard.stripe.com/test/payments/{pi}"
                        },
                        "else": {
                          "template": "https://dashboard.stripe.com/payments/{pi}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Refunded",
          "cards": [
            {
              "id": "refunded",
              "type": "list",
              "max_items": 7,
              "items": [],
              "source": {
                "ref": "charges",
                "empty": {
                  "text": "No refunds",
                  "when": {
                    "path": "refunds",
                    "equals": 0
                  }
                },
                "map": {
                  "items": {
                    "each": "pay_list",
                    "where": {
                      "path": "refund_units",
                      "gt": 0
                    },
                    "fields": {
                      "key": "id",
                      "text": "who",
                      "subtitle": {
                        "if": {
                          "path": "status",
                          "equals": "failed"
                        },
                        "then": {
                          "template": "Failed · {why}"
                        },
                        "else": {
                          "if": {
                            "path": "disputed",
                            "equals": true
                          },
                          "then": {
                            "template": "Disputed · {what}"
                          },
                          "else": {
                            "if": {
                              "path": "refunded",
                              "equals": true
                            },
                            "then": {
                              "template": "Refunded · {what}"
                            },
                            "else": {
                              "if": {
                                "path": "refunded_some",
                                "exists": true
                              },
                              "then": {
                                "template": "{refund_money} refunded · {what}"
                              },
                              "else": {
                                "if": {
                                  "path": "status",
                                  "equals": "pending"
                                },
                                "then": {
                                  "template": "Pending · {what}"
                                },
                                "else": {
                                  "if": {
                                    "path": "captured",
                                    "equals": false
                                  },
                                  "then": {
                                    "template": "Not captured · {what}"
                                  },
                                  "else": {
                                    "template": "{what}"
                                  }
                                }
                              }
                            }
                          }
                        }
                      },
                      "detail": {
                        "template": "{money} · {created|ago}"
                      },
                      "url": {
                        "if": {
                          "path": "livemode",
                          "equals": false
                        },
                        "then": {
                          "template": "https://dashboard.stripe.com/test/payments/{pi}"
                        },
                        "else": {
                          "template": "https://dashboard.stripe.com/payments/{pi}"
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}
