Cancelar una orden

❗️

Requisitos para realizar una cancelación de orden

Es necesario que la orden de compra tenga los siguientes requisitos:

  • La orden no debe tener cargos pagados.

Cuando se cancela una orden, el estado de la orden puede ser "voided" o "canceled". 

Si la orden tiene sólo combinaciones de pagos con Tarjetas, su estado será "voided" después de la cancelación. 

Si la orden tiene sólo combinaciones de pagos con Transferencia, su estado será "canceled" después de la cancelación. 

Si la orden tiene combinaciones de pagos en spei y tarjeta, su estado también será "canceled" después de la cancelación.

Tipos de estados al cancelar una orden:

  • voided: Solo cuando existen cargos de tarjetas únicamente.
  • canceled: Para pagos en spei-spei y tarjeta-spei.

Para cancelar una orden, se debe realizar una solicitud POST a https://api.conekta.io/orders/ORDER_ID/cancel, donde ORDER_ID es el ID de la orden que se desea cancelar.

Solicitud

curl --location --request POST 'https://api.conekta.io/orders/ORDER_ID/cancel' \
--header 'Accept: application/vnd.conekta-v2.1.0+json' \
--header 'Content-type: application/json' \
--header 'Authorization: Basic ACCESS_TOKEN' \
--data ''
require 'rest-client'
require 'json'

# Define los parámetros de la petición
order_id = 'ORDER_ID'
access_token = 'ACCESS_TOKEN'

headers = {
  'Accept' => 'application/vnd.conekta-v2.1.0+json',
  'Content-type' => 'application/json',
  'Authorization' => "Basic #{access_token}"
}

# Realiza la petición POST para cancelar la orden
begin
  response = RestClient.post(
    "https://api.conekta.io/orders/#{order_id}/cancel",
    nil,
    headers
  )

  puts "La orden #{order_id} se ha cancelado exitosamente"
rescue RestClient::ExceptionWithResponse => e
  error_message = JSON.parse(e.response.body)['message']
  puts "Error al cancelar la orden #{order_id}: #{error_message}"
end

Respuesta

{
    "livemode": false,
    "amount": 50000,
    "currency": "MXN",
    "payment_status": "canceled",
    "amount_refunded": 0,
    "split_payment": true,
    "customer_info": {
        "email": "[email protected]",
        "phone": "5522997233",
        "name": "Nicolás Cedrón",
        "corporate": true,
        "customer_id": null,
        "object": "customer_info"
    },
    "shipping_contact": null,
    "channel": null,
    "fiscal_entity": null,
    "object": "order",
    "id": "ord_2uatiPzpFrYSyfQ5G",
    "metadata": {},
    "is_refundable": false,
    "processing_mode": null,
    "created_at": 1695094068,
    "updated_at": 1695094074,
    "line_items": {
        "object": "list",
        "has_more": false,
        "total": 1,
        "data": [
            {
                "name": "Box of Cohiba S1s",
                "description": null,
                "unit_price": 50000,
                "quantity": 1,
                "sku": null,
                "tags": null,
                "brand": null,
                "type": null,
                "object": "line_item",
                "id": "line_item_2uatiPzpFrYSyfQ5E",
                "parent_id": "ord_2uatiPzpFrYSyfQ5G",
                "metadata": {},
                "antifraud_info": {}
            }
        ]
    },
    "shipping_lines": null,
    "tax_lines": null,
    "discount_lines": null,
    "charges": {
        "object": "list",
        "has_more": false,
        "total": 2,
        "data": [
            {
                "id": "650915358973ba484c99858e",
                "livemode": false,
                "created_at": 1695094069,
                "currency": "MXN",
                "failure_code": null,
                "failure_message": null,
                "monthly_installments": null,
                "device_fingerprint": null,
                "channel": null,
                "payment_method": {
                    "clabe": "646180111812345678",
                    "bank": "STP",
                    "issuing_account_holder_name": null,
                    "issuing_account_tax_id": null,
                    "issuing_account_bank": null,
                    "issuing_account_number": null,
                    "receiving_account_holder_name": null,
                    "receiving_account_tax_id": null,
                    "receiving_account_number": "646180111812345678",
                    "receiving_account_bank": "STP",
                    "reference_number": null,
                    "description": null,
                    "tracking_code": null,
                    "executed_at": null,
                    "payment_attempts": [],
                    "object": "bank_transfer_payment",
                    "type": "spei",
                    "expires_at": 1702867793
                },
                "object": "charge",
                "description": "Payment from order",
                "is_refundable": false,
                "reference_id": null,
                "status": "canceled",
                "amount": 25000,
                "paid_at": null,
                "customer_id": "",
                "order_id": "ord_2uatiPzpFrYSyfQ5G",
                "refunds": null,
                "reference": "646180111812345678"
            },
            {
                "id": "650915348973ba484c998586",
                "livemode": false,
                "created_at": 1695094068,
                "currency": "MXN",
                "failure_code": null,
                "failure_message": null,
                "monthly_installments": null,
                "device_fingerprint": null,
                "channel": null,
                "payment_method": {
                    "clabe": "646180111812345678",
                    "bank": "STP",
                    "issuing_account_holder_name": null,
                    "issuing_account_tax_id": null,
                    "issuing_account_bank": null,
                    "issuing_account_number": null,
                    "receiving_account_holder_name": null,
                    "receiving_account_tax_id": null,
                    "receiving_account_number": "646180111812345678",
                    "receiving_account_bank": "STP",
                    "reference_number": null,
                    "description": null,
                    "tracking_code": null,
                    "executed_at": null,
                    "payment_attempts": [],
                    "object": "bank_transfer_payment",
                    "type": "spei",
                    "expires_at": 1702867793
                },
                "object": "charge",
                "description": "Payment from order",
                "is_refundable": false,
                "reference_id": null,
                "status": "canceled",
                "amount": 25000,
                "paid_at": null,
                "customer_id": "",
                "order_id": "ord_2uatiPzpFrYSyfQ5G",
                "refunds": null,
                "reference": "646180111812345678"
            }
        ]
    }
}

Errores

Si intentas cancelar una orden con cargos pagados recibirás el siguiente error:

{
   "details":[
      {
         "debug_message":"The order has paid charges.",
         "message":"La orden ya tiene cargos pagados.",
         "param":null,
         "code":"conekta.errors.processing.order_operations.charges_already_paid"
      }
   ],
   "object":"error",
   "type":"processing_error",
   "log_id":"64377b7677a374b7a7580ac5"
}

Si la orden no existe, recibirás:

{
    "details": [
        {
            "debug_message": "The object Order \"ORDER_ID\" could not be found.",
            "message": "El recurso no ha sido encontrado.",
            "param": null,
            "code": "conekta.errors.resource_not_found.entity"
        }
    ],
    "object": "error",
    "type": "resource_not_found_error",
    "log_id": "64377bbe77a374b7a7580ac6"
}