1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
//! Transaction Split
//! =================
//! The Transaction Splits API enables merchants split the settlement for a transaction
//! across their payout account, and one or more subaccounts.
use crate::{
get_request, post_request, put_request, BearerType, Currency, Error, PaystackResult,
ResponseWithoutData, SplitType, TransactionSplitListResponse, TransactionSplitResponse,
};
use derive_builder::Builder;
use reqwest::StatusCode;
use serde::Serialize;
/// This struct is used to create a split payment on your integration.
/// The struct is constructed using the `CreateTransactionSplitBodyBuilder`
#[derive(Serialize, Debug, Default, Builder)]
pub struct CreateTransactionSplitBody {
/// Name of the transaction split
name: String,
/// The type of transaction split you want to create
#[serde(rename = "type")]
split_type: SplitType,
/// Any of the supported currency
currency: Currency,
/// A list of object containing subaccount code and number of shares: `[{subaccount: ‘ACT_xxxxxxxxxx’, share: xxx},{...}]`
subaccounts: Vec<SubaccountBody>,
/// Any of subaccount
bearer_type: BearerType,
/// Subaccount code
bearer_subaccount: String,
}
/// This struct represents the subaccount.
/// It can be used as the payload for the API end points that require a subaccount as a payload.
/// It is also possible to extract a single field from this struct to use as well.
/// The Struct is constructed using the `SubaccountBuilder`
#[derive(Serialize, Debug, Clone, Builder)]
pub struct SubaccountBody {
/// This is the sub account code
pub subaccount: String,
/// This is the transaction share for the subaccount
pub share: u32,
}
/// This struct is used to update a transaction split details on your integration.
/// The struct is constructed using the `UpdateTransactionSplitBodyBuilder`
#[derive(Serialize, Debug, Builder)]
pub struct UpdateTransactionSplitBody {
/// Name of the transaction split
pub name: String,
/// True or False
pub active: bool,
/// Any of subaccount
#[builder(default = "None")]
pub bearer_type: Option<BearerType>,
/// Subaccount code of a subaccount in the split group. This should be specified only if the `bearer_type is subaccount
#[builder(default = "None")]
pub bearer_subaccount: Option<SubaccountBody>,
}
/// A struct to hold all the functions of the transaction split API route
#[derive(Debug, Clone)]
pub struct TransactionSplit<'a> {
/// Paystack API key
api_key: &'a str,
}
static BASE_URL: &str = "https://api.paystack.co";
impl<'a> TransactionSplit<'a> {
/// Constructor for the Transaction Split object
pub fn new(key: &'a str) -> TransactionSplit<'a> {
TransactionSplit { api_key: key }
}
/// Create a split payment on your integration.
///
/// This method takes a TransactionSplit object as a parameter.
pub async fn create_transaction_split(
&self,
split_body: CreateTransactionSplitBody,
) -> PaystackResult<TransactionSplitResponse> {
let url = format!("{}/split", BASE_URL);
match post_request(self.api_key, &url, split_body).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<TransactionSplitResponse>().await {
Ok(content) => Ok(content),
Err(err) => Err(Error::TransactionSplit(err.to_string())),
},
_ => Err(Error::RequestNotSuccessful(
response.status().to_string(),
response.text().await?,
)),
},
Err(err) => Err(Error::FailedRequest(err.to_string())),
}
}
/// List the transaction splits available on your integration
///
/// Takes in the following parameters:
/// - `split_name`: (Optional) name of the split to retrieve.
/// - `split_active`: (Optional) status of the split to retrieve.
pub async fn list_transaction_splits(
&self,
split_name: Option<&str>,
split_active: Option<bool>,
) -> PaystackResult<TransactionSplitListResponse> {
let url = format!("{}/split", BASE_URL);
// Specify a default option for active splits
let split_active = match split_active {
Some(active) => active.to_string(),
None => "".to_string(),
};
let query = vec![
("name", split_name.unwrap_or("")),
("active", &split_active),
];
match get_request(self.api_key, &url, Some(query)).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<TransactionSplitListResponse>().await {
Ok(content) => Ok(content),
Err(err) => Err(Error::TransactionSplit(err.to_string())),
},
_ => Err(Error::RequestNotSuccessful(
response.status().to_string(),
response.text().await?,
)),
},
Err(err) => Err(Error::FailedRequest(err.to_string())),
}
}
/// Get details of a split on your integration.
///
/// Takes in the following parameter:
/// - `split_id`: Id of the transaction split.
pub async fn fetch_transaction_split(
&self,
split_id: &str,
) -> PaystackResult<TransactionSplitResponse> {
let url = format!("{}/split{}", BASE_URL, split_id);
match get_request(self.api_key, &url, None).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<TransactionSplitResponse>().await {
Ok(content) => Ok(content),
Err(err) => Err(Error::TransactionSplit(err.to_string())),
},
_ => Err(Error::RequestNotSuccessful(
response.status().to_string(),
response.text().await?,
)),
},
Err(err) => Err(Error::FailedRequest(err.to_string())),
}
}
/// Update a transaction split details on your integration.
///
/// Takes in the following parameters:
/// - `split_id`: Id of the transaction split.
/// - `split_name`: updated name for the split.
/// - `split_status`: updated states for the split.
/// - `bearer_type`: (Optional) updated bearer type for the split.
/// - `bearer_subaccount`: (Optional) updated bearer subaccount for the split
pub async fn update_transaction_split(
&self,
split_id: &str,
body: UpdateTransactionSplitBody,
) -> PaystackResult<TransactionSplitResponse> {
let url = format!("{}/split/{}", BASE_URL, split_id);
match put_request(self.api_key, &url, body).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<TransactionSplitResponse>().await {
Ok(content) => Ok(content),
Err(err) => Err(Error::TransactionSplit(err.to_string())),
},
_ => Err(Error::RequestNotSuccessful(
response.status().to_string(),
response.text().await?,
)),
},
Err(err) => Err(Error::FailedRequest(err.to_string())),
}
}
/// Add a Subaccount to a Transaction Split, or update the share of an existing Subaccount in a Transaction Split
///
/// Takes in the following parameters:
/// - `split_id`: Id of the transaction split to update.
/// - `body`: Subaccount to add to the transaction split.
pub async fn add_or_update_subaccount_split(
&self,
split_id: &str,
body: SubaccountBody,
) -> PaystackResult<TransactionSplitResponse> {
let url = format!("{}/split/{}/subaccount/add", BASE_URL, split_id);
match post_request(self.api_key, &url, body).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<TransactionSplitResponse>().await {
Ok(content) => Ok(content),
Err(err) => Err(Error::TransactionSplit(err.to_string())),
},
_ => Err(Error::RequestNotSuccessful(
response.status().to_string(),
response.text().await?,
)),
},
Err(err) => Err(Error::FailedRequest(err.to_string())),
}
}
/// Remove a subaccount from a transaction split.
///
/// Takes in the following parameters
/// - split_id: Id of the transaction split
/// - subaccount: subaccount code to remove
pub async fn remove_subaccount_from_transaction_split(
&self,
split_id: &str,
subaccount: &str,
) -> PaystackResult<ResponseWithoutData> {
let url = format!("{}/split/{}/subaccount/remove", BASE_URL, split_id);
match post_request(self.api_key, &url, subaccount).await {
Ok(response) => match response.status() {
StatusCode::OK => match response.json::<ResponseWithoutData>().await {
Ok(content) => Ok(content),
Err(err) => Err(Error::TransactionSplit(err.to_string())),
},
_ => Err(Error::RequestNotSuccessful(
response.status().to_string(),
response.text().await?,
)),
},
Err(err) => Err(Error::FailedRequest(err.to_string())),
}
}
}