paypal_rust/resources/
webhooks.rs

1use std::borrow::Cow;
2
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7use crate::client::endpoint::Endpoint;
8use crate::client::error::PayPalError;
9use crate::client::paypal::Client;
10use crate::resources::enums::verification_status::VerificationStatus;
11
12pub struct Webhook;
13
14impl Webhook {
15    /// Verifies a webhook signature.
16    pub async fn verify(
17        client: &Client,
18        dto: VerifyWebhookSignatureDto,
19    ) -> Result<VerifyWebhookSignatureResponse, PayPalError> {
20        client.post(&VerifyWebhookSignature::new(dto)).await
21    }
22}
23
24#[skip_serializing_none]
25#[derive(Clone, Debug, Serialize)]
26pub struct VerifyWebhookSignatureDto {
27    /// The algorithm that PayPal uses to generate the signature and that you can use to verify the signature.
28    /// Extract this value from the `PAYPAL-AUTH-ALGO` response header, which is received with the webhook notification.
29    pub auth_algo: String,
30
31    /// The X.509 public key certificate. Download the certificate from this URL and use it to verify the signature.
32    /// Extract this value from the `PAYPAL-CERT-URL` response header, which is received with the webhook notification.
33    pub cert_url: String,
34
35    /// The ID of the HTTP transmission. Contained in the `PAYPAL-TRANSMISSION-ID` header of the notification message.
36    pub transmission_id: String,
37
38    /// The PayPal-generated asymmetric signature. Appears in the `PAYPAL-TRANSMISSION-SIG` header of the notification message.
39    pub transmission_sig: String,
40
41    /// The date and time of the HTTP transmission, in Internet date and time format.
42    /// Appears in the `PAYPAL-TRANSMISSION-TIME` header of the notification message.
43    pub transmission_time: String,
44
45    /// A webhook event notification.
46    /// @Note: In this case, the request body.
47    pub webhook_event: serde_json::Value,
48
49    /// The ID of the webhook as configured in your Developer Portal account.
50    pub webhook_id: String,
51}
52
53#[derive(Clone, Debug, Deserialize)]
54pub struct VerifyWebhookSignatureResponse {
55    /// The status of the signature verification.
56    pub verification_status: VerificationStatus,
57}
58
59#[derive(Debug)]
60struct VerifyWebhookSignature {
61    pub body: VerifyWebhookSignatureDto,
62}
63
64impl VerifyWebhookSignature {
65    pub const fn new(body: VerifyWebhookSignatureDto) -> Self {
66        Self { body }
67    }
68}
69
70impl Endpoint for VerifyWebhookSignature {
71    type QueryParams = ();
72    type RequestBody = VerifyWebhookSignatureDto;
73    type ResponseBody = VerifyWebhookSignatureResponse;
74
75    fn path(&self) -> Cow<str> {
76        Cow::Borrowed("v1/notifications/verify-webhook-signature")
77    }
78
79    fn request_body(&self) -> Option<Self::RequestBody> {
80        Some(self.body.clone())
81    }
82
83    fn request_method(&self) -> Method {
84        Method::POST
85    }
86}