Para mas automatizaciones ayuda@afipsdk.com
Automatizaciones
Delegar un web service

Delegar un web service

Esta automatización sirve para que un CUIT autorice a otro a utilizar un web service en su nombre. Es útil cuando tenés un software con múltiples clientes y querés usar un solo certificado para todos. Ellos te delegan el web service a tu CUIT, vos aceptás la delegación y luego lo vinculás a tu certificado.


Requisitos previos

Para poder usar esta automatización, primero necesitarás:


Nombre

delegate-web-service

Parametros

cuitstring
CUIT a usar en la página de ARCA.
usernamestring
CUIT para loguearse en la página de ARCA. Normalmente es el mismo CUIT que el parámetro 'cuit', pero si administrás una sociedad, el CUIT que usás para loguearte es tu propio CUIT.
passwordstring
Contraseña para loguearse en la página de ARCA.
servicestring
ID del web service a delegar.
delegate_tostring
CUIT al cual delegar el web service.
Solicitud
import (
    "fmt"
    "strings"
    "net/http"
    "io/ioutil"
    "encoding/json"
	"time"
)

// Aqui deben cambiar los datos de params por los que correspondan. 
// Esta request de ejemplo incluye todos posibles 
// valores para ejecutar la automatizacion delegate-web-service, 
// puede que algun valor sea opcional.
requestData, _ := json.Marshal(map[string]any {
    "automation" : "delegate-web-service",
    "params" : map[string]any {
        "cuit" : "20111111112",
        "username" : "20111111112",
        "password" : "contraseña#segura?",
        "service" : "wsfe",
        "delegate_to" : "20111111112",
    },
})

// Creamos un cliente HTTP para llamar a la API
clientRequest := &http.Client {}
urlRequest := "https://app.afipsdk.com/api/v1/automations"
methodRequest := "POST"
reqRequest, _ := http.NewRequest(methodRequest, urlRequest, strings.NewReader(string(requestData)))

// Reemplazar con tu access_token obtenido de https://app.afipsdk.com
accessToken := "TU_TOKEN_AQUI"
reqRequest.Header.Add("Authorization", "Bearer " + accessToken)

// Realizamos la llamada a la API
reqRequest.Header.Add("Content-Type", "application/json")
resRequest, _ := clientRequest.Do(reqRequest)
defer resRequest.Body.Close()
bodyRequest, _ := ioutil.ReadAll(resRequest.Body)

// En caso de error lo mostramos por consola
if resRequest.StatusCode >= 400 {
    var responseError map[string]any
    json.Unmarshal([]byte(string(bodyRequest)), &responseError)
    responseErrorString, _ := json.MarshalIndent(responseError, "", "    ")
    fmt.Println(string(responseErrorString))
    return
}

// Convertimos la respuesta en un objeto
var response map[string]any
json.Unmarshal([]byte(string(bodyRequest)), &response)

// Mostramos la respuesta por consola
responseString, _ := json.MarshalIndent(response, "", "    ")
fmt.Println(string(responseString))

// Expera maxima de 120 segundos (5 segundos * 24)
retries := 24

for retries >= 0 {
    retries--

    reqRequest, _ := http.NewRequest("GET", "https://app.afipsdk.com/api/v1/automations/" + response["id"].(string), nil)
    
    // Realizamos la llamada a la API
    reqRequest.Header.Add("Authorization", "Bearer " + accessToken)
    reqRequest.Header.Add("Content-Type", "application/json")
    resRequest, _ := clientRequest.Do(reqRequest)
    defer resRequest.Body.Close()
    bodyRequest, _ := ioutil.ReadAll(resRequest.Body)

    // En caso de error lo mostramos por consola
    if resRequest.StatusCode >= 400 {
        var responseError map[string]any
        json.Unmarshal([]byte(string(bodyRequest)), &responseError)
        responseErrorString, _ := json.MarshalIndent(responseError, "", "    ")
        fmt.Println(string(responseErrorString))
        return
    }

    // Convertimos la respuesta en un objeto
    var response map[string]any
    json.Unmarshal([]byte(string(bodyRequest)), &response)

    if(response["status"] == "complete"){
        // Mostramos la respuesta por consola
        responseString, _ := json.MarshalIndent(response, "", "    ")
        fmt.Println(string(responseString))
        break
    }

    time.Sleep(5 * time.Second)
}
Respuesta
{
    "id": "0c31d74f-d672-4677-a00b-7dc865396c69",
    "status": "complete",
    "data": {
        "status": "created"
    }
}