use serde::{Deserialize, Serialize};
use crate::client::RazorpayClient;
use crate::error::RazorpayError;
use crate::resources::common::{Notes, NotesUpdate};
use crate::resources::payments::Payment;
use crate::util::pagination::{Collection, ListOptions};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OrderStatus {
Created,
Attempted,
Paid,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct Order {
pub id: String,
#[serde(default)]
pub entity: String,
pub amount: i64,
#[serde(default)]
pub amount_paid: i64,
#[serde(default)]
pub amount_due: i64,
pub currency: String,
#[serde(default)]
pub receipt: Option<String>,
pub status: OrderStatus,
#[serde(default)]
pub attempts: u32,
#[serde(default)]
pub notes: Notes,
pub created_at: i64,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CreateOrderParams {
pub amount: i64,
pub currency: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub receipt: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notes: Option<Notes>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_capture: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub method: Option<String>,
}
impl CreateOrderParams {
pub fn new(amount: i64, currency: impl Into<String>) -> Self {
Self {
amount,
currency: currency.into(),
receipt: None,
notes: None,
payment_capture: None,
method: None,
}
}
pub fn receipt(mut self, receipt: impl Into<String>) -> Self {
self.receipt = Some(receipt.into());
self
}
pub fn notes(mut self, notes: Notes) -> Self {
self.notes = Some(notes);
self
}
pub fn manual_capture(mut self) -> Self {
self.payment_capture = Some(0);
self
}
pub fn method(mut self, method: impl Into<String>) -> Self {
self.method = Some(method.into());
self
}
}
pub struct OrdersClient<'a> {
pub(crate) client: &'a RazorpayClient,
}
impl<'a> OrdersClient<'a> {
pub async fn create(&self, params: CreateOrderParams) -> Result<Order, RazorpayError> {
self.client.post("orders", Some(¶ms)).await
}
pub async fn fetch(&self, id: &str) -> Result<Order, RazorpayError> {
self.client.get::<(), _>(&format!("orders/{id}"), None).await
}
pub async fn all(&self, options: ListOptions) -> Result<Collection<Order>, RazorpayError> {
self.client.get("orders", Some(&options)).await
}
pub async fn fetch_payments(&self, id: &str) -> Result<Collection<Payment>, RazorpayError> {
self.client
.get::<(), _>(&format!("orders/{id}/payments"), None)
.await
}
pub async fn edit(&self, id: &str, notes: Notes) -> Result<Order, RazorpayError> {
self.client
.patch(&format!("orders/{id}"), Some(&NotesUpdate::new(notes)))
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unset_optional_params_are_omitted() {
let json = serde_json::to_string(&CreateOrderParams::new(100, "INR")).unwrap();
assert_eq!(json, r#"{"amount":100,"currency":"INR"}"#);
}
#[test]
fn builder_sets_fields() {
let p = CreateOrderParams::new(100, "INR").receipt("r1").manual_capture();
assert_eq!(p.receipt.as_deref(), Some("r1"));
assert_eq!(p.payment_capture, Some(0));
}
#[test]
fn unknown_status_does_not_fail_decoding() {
let order: Order = serde_json::from_str(
r#"{"id":"order_1","amount":1,"currency":"INR","status":"brand_new","created_at":1}"#,
)
.unwrap();
assert_eq!(order.status, OrderStatus::Unknown);
}
}