pix_api_client/
webhook.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{ApiRequest, PixClient};
5
6pub struct WebhookEndpoint<'a> {
7    inner: &'a PixClient,
8}
9
10#[derive(Debug, Serialize, Deserialize)]
11struct WebHookPayload {
12    #[serde(rename = "webhookUrl")]
13    webhook_url: String,
14}
15
16impl WebHookPayload {
17    pub fn new(webhook_url: String) -> WebHookPayload {
18        Self { webhook_url }
19    }
20}
21
22impl PixClient {
23    pub fn webhook(&self) -> WebhookEndpoint {
24        WebhookEndpoint { inner: self }
25    }
26}
27
28#[derive(Debug, Serialize, Deserialize)]
29pub struct WebHookResponse {}
30
31/// Base response object used by the `WebHook` for any transaction.
32#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
33pub struct WebHookCallbackResponse {
34    pub pix: Vec<PixInput>,
35}
36
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38pub struct PixInput {
39    #[serde(rename = "endToEndId")]
40    pub end_to_end_id: String,
41    /// Transaction id
42    pub txid: Option<String>,
43    /// Beneficiary's Pix Key
44    pub chave: String,
45    pub valor: String,
46    pub horario: String,
47
48    #[serde(rename = "infoPagador")]
49    pub info_pagador: Option<String>,
50    pub devolucoes: Option<Vec<Devolucoes>>,
51    pub tipo: Option<String>,
52    pub status: Option<String>,
53}
54
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56pub struct Devolucoes {
57    /// Id gerado pelo cliente para representar unicamente uma devolução.
58    pub id: String,
59    /// ReturnIdentification que transita na PACS004.
60    #[serde(rename = "rtrId")]
61    pub rtr_id: String,
62    pub valor: String,
63    pub horario: Horario,
64    pub status: String,
65}
66
67#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
68pub struct Horario {
69    pub solicitacao: String,
70}
71
72impl<'a> WebhookEndpoint<'a> {
73    pub fn criar_por_chave(&self, chave_pix: String, webhook_url: String) -> ApiRequest<WebHookResponse> {
74        let endpoint = format!("{}/webhook/{}", &*self.inner.base_endpoint, chave_pix);
75        let payload = WebHookPayload::new(webhook_url);
76        self.inner.request_with_headers(Method::PUT, &endpoint, payload)
77    }
78
79    pub fn consultar_por_chave(&self, chave_pix: String, webhook_url: String) -> ApiRequest<WebHookResponse> {
80        let endpoint = format!("{}/webhook/{}", &*self.inner.base_endpoint, chave_pix);
81        let payload = WebHookPayload::new(webhook_url);
82        self.inner.request_with_headers(Method::GET, &endpoint, payload)
83    }
84    pub fn cancelar_por_chave(&self, chave_pix: String, webhook_url: String) -> ApiRequest<WebHookResponse> {
85        let endpoint = format!("{}/webhook/{}", &*self.inner.base_endpoint, chave_pix);
86        let payload = WebHookPayload::new(webhook_url);
87        self.inner.request_with_headers(Method::DELETE, &endpoint, payload)
88    }
89
90    /// Criar uma cobrança imediata.
91    /// Diferente de `criar_cobranca_imediata`, o `txid` é definido pelo PSP.
92    pub fn consultar_todos(&self) -> ApiRequest<WebHookResponse> {
93        let endpoint = format!("{}/webhook", &*self.inner.base_endpoint);
94        self.inner.request_with_headers(Method::GET, &endpoint, None::<&str>)
95    }
96}