use reqwest::Method;
use crate::{
Client, Payment, PaymentIntent, PaymentIntentRequest, PaymentListOptions, PaymentStats,
PaymentStatsOptions, PaymentSummary, Refund, RefundRequest, RequestOptions, Result,
UpdatePaymentIntentRequest,
};
use super::path_segment;
#[derive(Debug, Clone)]
pub struct Payments {
client: Client,
}
impl Payments {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn create_intent(
&self,
input: &PaymentIntentRequest,
options: RequestOptions,
) -> Result<PaymentIntent> {
let request = self
.client
.request(Method::POST, "/v1/payments/intents")?
.json(input);
self.client.send(request, options).await
}
pub async fn list(&self, options: &PaymentListOptions) -> Result<Vec<PaymentSummary>> {
let request = self
.client
.request(Method::GET, "/v1/payments")?
.query(options);
self.client.send_collection(request, "payments").await
}
pub async fn get(&self, id: &str) -> Result<Payment> {
let request = self
.client
.request(Method::GET, &format!("/v1/payments/{}", path_segment(id)))?;
self.client.send(request, RequestOptions::default()).await
}
pub async fn update_intent(
&self,
id: &str,
input: &UpdatePaymentIntentRequest,
options: RequestOptions,
) -> Result<Payment> {
let request = self
.client
.request(
Method::PATCH,
&format!("/v1/payments/intents/{}", path_segment(id)),
)?
.json(input);
self.client.send(request, options).await
}
pub async fn confirm(&self, id: &str, options: RequestOptions) -> Result<Payment> {
self.client
.post_empty(
&format!("/v1/payments/{}/confirm", path_segment(id)),
options,
)
.await
}
pub async fn confirm_intent(
&self,
id: &str,
client_secret: &str,
options: RequestOptions,
) -> Result<Payment> {
let request = self
.client
.request(
Method::POST,
&format!("/v1/payments/{}/confirm-intent", path_segment(id)),
)?
.query(&[("client_secret", client_secret)])
.json(&serde_json::json!({}));
self.client.send(request, options).await
}
pub async fn cancel(&self, id: &str, options: RequestOptions) -> Result<Payment> {
self.client
.post_empty(
&format!("/v1/payments/{}/cancel", path_segment(id)),
options,
)
.await
}
pub async fn retry(&self, id: &str, options: RequestOptions) -> Result<Payment> {
self.client
.post_empty(&format!("/v1/payments/{}/retry", path_segment(id)), options)
.await
}
pub async fn refund(
&self,
id: &str,
input: &RefundRequest,
options: RequestOptions,
) -> Result<Refund> {
let request = self
.client
.request(
Method::POST,
&format!("/v1/payments/{}/refund", path_segment(id)),
)?
.json(input);
self.client.send(request, options).await
}
pub async fn stats(&self, options: &PaymentStatsOptions) -> Result<PaymentStats> {
let request = self
.client
.request(Method::GET, "/v1/payments/stats")?
.query(options);
self.client.send(request, RequestOptions::default()).await
}
}