Fuente: https://crm.begraffic.com/docs/api
Índice para agentes: [llms.txt](https://crm.begraffic.com/llms.txt)

# POST /api/v1/payments

URL base: https://crm.begraffic.com. Autenticación: Authorization: Bearer <CLAVE_V1_bcrm_...>.

Definición OpenAPI de la operación (los bloques schema son esquemas, no cuerpos de ejemplo):

```json
{
  "operationId": "payments.upsert",
  "summary": "Registrar un pago",
  "description": "Alta o actualización por `(source, externalId)`: 201 la primera vez, 200 si ya existía (solo cambian `status` y `refundedMinor`; si no cambian, no hace nada). Cada transición emite `payment.succeeded`, `payment.failed` o `payment.refunded` (webhook y automatizaciones) y deja un hito en la ficha del contacto. Un conector oficial registra siempre con su `source`.",
  "tags": [
    "Pagos"
  ],
  "x-scope": "payments:write",
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "parameters": [
    {
      "name": "Idempotency-Key",
      "in": "header",
      "required": false,
      "description": "Clave única por operación (≤ 255 caracteres). Repetir la misma petición devuelve la misma respuesta durante 24 h; con otro cuerpo, 422.",
      "schema": {
        "type": "string",
        "maxLength": 255
      }
    }
  ],
  "responses": {
    "200": {
      "description": "Correcto.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "required": [
              "data"
            ],
            "properties": {
              "data": {
                "type": "object",
                "properties": {
                  "payment": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string"
                      },
                      "externalId": {
                        "type": "string"
                      },
                      "source": {
                        "type": "string",
                        "enum": [
                          "beschedule",
                          "beschool",
                          "besocial",
                          "stripe",
                          "other"
                        ]
                      },
                      "contactId": {
                        "type": "string"
                      },
                      "dealId": {
                        "anyOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "amountMinor": {
                        "type": "integer",
                        "minimum": -9007199254740991,
                        "maximum": 9007199254740991
                      },
                      "currency": {
                        "type": "string"
                      },
                      "status": {
                        "type": "string",
                        "enum": [
                          "succeeded",
                          "pending",
                          "failed",
                          "refunded",
                          "partially_refunded",
                          "disputed"
                        ]
                      },
                      "refundedMinor": {
                        "type": "integer",
                        "minimum": -9007199254740991,
                        "maximum": 9007199254740991
                      },
                      "description": {
                        "anyOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "occurredAt": {
                        "anyOf": [
                          {
                            "type": "string",
                            "format": "date-time"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "externalUrl": {
                        "anyOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "visibility": {
                        "type": "string",
                        "enum": [
                          "workspace",
                          "restricted"
                        ]
                      },
                      "createdAt": {
                        "anyOf": [
                          {
                            "type": "string",
                            "format": "date-time"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "updatedAt": {
                        "anyOf": [
                          {
                            "type": "string",
                            "format": "date-time"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      }
                    },
                    "required": [
                      "id",
                      "externalId",
                      "source",
                      "contactId",
                      "dealId",
                      "amountMinor",
                      "currency",
                      "status",
                      "refundedMinor",
                      "description",
                      "occurredAt",
                      "externalUrl",
                      "visibility",
                      "createdAt",
                      "updatedAt"
                    ]
                  }
                },
                "required": [
                  "payment"
                ]
              }
            }
          }
        }
      }
    },
    "201": {
      "description": "Correcto.",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "required": [
              "data"
            ],
            "properties": {
              "data": {
                "type": "object",
                "properties": {
                  "payment": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string"
                      },
                      "externalId": {
                        "type": "string"
                      },
                      "source": {
                        "type": "string",
                        "enum": [
                          "beschedule",
                          "beschool",
                          "besocial",
                          "stripe",
                          "other"
                        ]
                      },
                      "contactId": {
                        "type": "string"
                      },
                      "dealId": {
                        "anyOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "amountMinor": {
                        "type": "integer",
                        "minimum": -9007199254740991,
                        "maximum": 9007199254740991
                      },
                      "currency": {
                        "type": "string"
                      },
                      "status": {
                        "type": "string",
                        "enum": [
                          "succeeded",
                          "pending",
                          "failed",
                          "refunded",
                          "partially_refunded",
                          "disputed"
                        ]
                      },
                      "refundedMinor": {
                        "type": "integer",
                        "minimum": -9007199254740991,
                        "maximum": 9007199254740991
                      },
                      "description": {
                        "anyOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "occurredAt": {
                        "anyOf": [
                          {
                            "type": "string",
                            "format": "date-time"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "externalUrl": {
                        "anyOf": [
                          {
                            "type": "string"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "visibility": {
                        "type": "string",
                        "enum": [
                          "workspace",
                          "restricted"
                        ]
                      },
                      "createdAt": {
                        "anyOf": [
                          {
                            "type": "string",
                            "format": "date-time"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "updatedAt": {
                        "anyOf": [
                          {
                            "type": "string",
                            "format": "date-time"
                          },
                          {
                            "type": "null"
                          }
                        ]
                      }
                    },
                    "required": [
                      "id",
                      "externalId",
                      "source",
                      "contactId",
                      "dealId",
                      "amountMinor",
                      "currency",
                      "status",
                      "refundedMinor",
                      "description",
                      "occurredAt",
                      "externalUrl",
                      "visibility",
                      "createdAt",
                      "updatedAt"
                    ]
                  }
                },
                "required": [
                  "payment"
                ]
              }
            }
          }
        }
      }
    },
    "400": {
      "description": "`invalid_request`: Petición inválida (cuerpo, parámetros o consulta).",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "401": {
      "description": "`unauthorized`: Falta la clave, no existe, está revocada o caducó.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "402": {
      "description": "`contact_limit_exceeded`: Límite de contactos del plan superado: el workspace está bloqueado hasta subir de plan o de tramo (reintentar no lo arregla).",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "403": {
      "description": "`insufficient_scope`: La clave no tiene el alcance que exige la ruta.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "404": {
      "description": "`not_found`: No existe (o no es de este workspace).",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "409": {
      "description": "`invalid_state`: Operación no válida en el estado actual. `idempotency_in_progress`: Otra petición con la misma Idempotency-Key se está procesando.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "422": {
      "description": "`idempotency_key_reused`: La Idempotency-Key ya se usó con otra petición.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "429": {
      "description": "`rate_limited`: Demasiadas llamadas (límite por clave y minuto). Ver `Retry-After`.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    },
    "500": {
      "description": "`internal`: Error interno.",
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/Error"
          }
        }
      }
    }
  },
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "externalId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200,
              "description": "Id del pago en el sistema de origen (p. ej. `pi_…`). Con `source`, identifica el pago: repetir actualiza."
            },
            "source": {
              "type": "string",
              "enum": [
                "beschedule",
                "beschool",
                "besocial",
                "stripe",
                "other"
              ],
              "description": "Origen. Un conector oficial (Be Schedule, Be School, Be Social) siempre registra con el suyo."
            },
            "contactId": {
              "description": "Contacto existente. Si no, `contact`.",
              "type": "string",
              "minLength": 1,
              "maxLength": 200,
              "pattern": "^[^/]+$"
            },
            "contact": {
              "description": "Se busca por correo (sin distinguir mayúsculas) y luego por teléfono; si no existe, se crea un contacto pasivo (402 `contact_limit_exceeded` si el workspace está bloqueado).",
              "type": "object",
              "properties": {
                "email": {
                  "type": "string",
                  "format": "email"
                },
                "phone": {
                  "type": "object",
                  "properties": {
                    "phone_code": {
                      "type": "string",
                      "maxLength": 8
                    },
                    "phone": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 32
                    }
                  },
                  "required": [
                    "phone"
                  ],
                  "additionalProperties": false
                },
                "name": {
                  "type": "string",
                  "maxLength": 120
                },
                "lastname": {
                  "type": "string",
                  "maxLength": 120
                }
              },
              "additionalProperties": false
            },
            "amountMinor": {
              "type": "integer",
              "minimum": 0,
              "maximum": 10000000000,
              "description": "Importe en unidad menor (10,50 USD = 1050)."
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$",
              "description": "ISO 4217 en MAYÚSCULAS."
            },
            "status": {
              "type": "string",
              "enum": [
                "succeeded",
                "pending",
                "failed",
                "refunded",
                "partially_refunded",
                "disputed"
              ]
            },
            "refundedMinor": {
              "description": "Reembolsado acumulado en unidad menor (por defecto 0).",
              "type": "integer",
              "minimum": 0,
              "maximum": 10000000000
            },
            "description": {
              "description": "Solo el nombre del producto o servicio. Nunca datos clínicos ni el motivo de una consulta.",
              "type": "string",
              "maxLength": 200
            },
            "occurredAt": {
              "description": "Cuándo ocurrió el pago (por defecto, ahora).",
              "type": "string",
              "format": "date-time"
            },
            "dealId": {
              "description": "Negocio del mismo contacto al que se asocia.",
              "type": "string",
              "minLength": 1,
              "maxLength": 200,
              "pattern": "^[^/]+$"
            },
            "externalUrl": {
              "description": "Enlace al pago en el sistema de origen (https).",
              "type": "string",
              "maxLength": 1000,
              "format": "uri"
            },
            "metadata": {
              "description": "Hasta 20 claves; valores de texto de hasta 500.",
              "type": "object",
              "propertyNames": {
                "type": "string",
                "maxLength": 40
              },
              "additionalProperties": {
                "type": "string",
                "maxLength": 500
              }
            },
            "visibility": {
              "description": "`restricted` (p. ej. una cita de salud): sin hito en la línea de tiempo, fuera de los «Pagos» del panel salvo para quien tiene el permiso de las notas restringidas, de `GET /api/v1/payments` salvo para la propia app o una clave con `sensitive:read`, y sus webhooks `payment.*` solo salen a destinos con `sensitive:read`. Se fija en el alta y solo puede cerrarse (`workspace` → `restricted`, que borra sus hitos), nunca abrirse. Un contacto nuevo creado por el pago no guarda el nombre de la app. Por defecto `workspace`.",
              "type": "string",
              "enum": [
                "workspace",
                "restricted"
              ]
            }
          },
          "required": [
            "externalId",
            "source",
            "amountMinor",
            "currency",
            "status"
          ],
          "additionalProperties": false
        }
      }
    }
  }
}
```
