Skip to content

Commit 5f38854

Browse files
authored
Merge pull request #5 from OAyomide/finish-modules
finish modules
2 parents 90ff490 + ec25be1 commit 5f38854

14 files changed

+973
-6
lines changed

src/paystack.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
pub mod bulk_charges;
2+
pub mod charge;
3+
pub mod control_panel;
14
pub mod customers;
25
pub mod dedicated_nuban;
6+
pub mod disputes;
37
pub mod invoices;
8+
pub mod miscellaneous;
49
pub mod payment_pages;
510
pub mod plans;
611
pub mod products;
@@ -10,9 +15,18 @@ pub mod subaccounts;
1015
pub mod subscription;
1116
pub mod transactions;
1217
pub mod transactions_split;
18+
pub mod transfer_recipients;
19+
pub mod transfers;
20+
pub mod transfers_control;
21+
pub mod verification;
1322

23+
use bulk_charges::BulkCharges;
24+
use charge::Charge;
25+
use control_panel::ControlPanel;
1426
use dedicated_nuban::DedicatedNuban;
27+
use disputes::Disputes;
1528
use invoices::Invoices;
29+
use miscellaneous::Miscellaneous;
1630
use payment_pages::PaymentPages;
1731
use plans::Plans;
1832
use products::Products;
@@ -22,6 +36,10 @@ use subaccounts::Subaccount;
2236
use subscription::Subscription;
2337
use transactions::Transaction;
2438
use transactions_split::TransactionSplit;
39+
use transfer_recipients::TransferRecipients;
40+
use transfers::Transfers;
41+
use transfers_control::TransfersControl;
42+
use verification::Verification;
2543

2644
#[derive(Default)]
2745
pub struct Paystack {
@@ -36,6 +54,15 @@ pub struct Paystack {
3654
pub payment_pages: PaymentPages,
3755
pub invoices: Invoices,
3856
pub settlements: Settlements,
57+
pub transfer_recipients: TransferRecipients,
58+
pub transfers: Transfers,
59+
pub transfers_control: TransfersControl,
60+
pub bulk_charges: BulkCharges,
61+
pub control_panel: ControlPanel,
62+
pub charge: Charge,
63+
pub disputes: Disputes,
64+
pub verification: Verification,
65+
pub miscellaneous: Miscellaneous,
3966
}
4067

4168
impl Paystack {
@@ -76,6 +103,33 @@ impl Paystack {
76103
settlements: Settlements {
77104
bearer_auth: formatted_bearer.to_string(),
78105
},
106+
transfer_recipients: TransferRecipients {
107+
bearer_auth: formatted_bearer.to_string(),
108+
},
109+
transfers: Transfers {
110+
bearer_auth: formatted_bearer.to_string(),
111+
},
112+
transfers_control: TransfersControl {
113+
bearer_auth: formatted_bearer.to_string(),
114+
},
115+
bulk_charges: BulkCharges {
116+
bearer_auth: formatted_bearer.to_string(),
117+
},
118+
control_panel: ControlPanel {
119+
bearer_auth: formatted_bearer.to_string(),
120+
},
121+
charge: Charge {
122+
bearer_auth: formatted_bearer.to_string(),
123+
},
124+
disputes: Disputes {
125+
bearer_auth: formatted_bearer.to_string(),
126+
},
127+
verification: Verification {
128+
bearer_auth: formatted_bearer.to_string(),
129+
},
130+
miscellaneous: Miscellaneous {
131+
bearer_auth: formatted_bearer.to_string(),
132+
},
79133
}
80134
}
81135
}

src/paystack/bulk_charges.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use crate::utils::make_get_request;
2+
use chrono::{DateTime, Local};
3+
use reqwest::blocking::Response;
4+
use serde::Serialize;
5+
6+
/// The Bulk Charges API allows you create and manage multiple recurring payments from your customers
7+
#[derive(Debug, Default)]
8+
pub struct BulkCharges {
9+
pub(crate) bearer_auth: String,
10+
}
11+
12+
// #[derive(Debug, Serialize)]
13+
// pub struct InitiateBulkChargesBody {
14+
// pub
15+
// }
16+
17+
#[derive(Debug, Serialize)]
18+
#[serde(rename_all = "lowercase")]
19+
pub enum BulkChargesStatus {
20+
FAILED,
21+
SUCCESS,
22+
PENDING,
23+
}
24+
25+
#[derive(Debug, Serialize)]
26+
pub struct ListBulkChargesParams {
27+
/// Specify how many records you want to retrieve per page. If not specify we use a default value of 50.
28+
#[serde(rename = "perPage")]
29+
pub per_page: Option<i128>,
30+
/// Specify exactly what page you want to retrieve. If not specify we use a default value of 1.
31+
pub page: Option<i128>,
32+
/// A timestamp from which to start listing product e.g. 2016-09-24T00:00:05.000Z, 2016-09-21
33+
pub from: Option<DateTime<Local>>,
34+
/// A timestamp at which to stop listing product e.g. 2016-09-24T00:00:05.000Z, 2016-09-21
35+
pub to: Option<DateTime<Local>>,
36+
}
37+
38+
#[derive(Debug, Serialize)]
39+
pub struct FetchChargesInABatchParams {
40+
/// Either one of these values: pending, success or failed
41+
pub status: BulkChargesStatus,
42+
/// Specify how many records you want to retrieve per page. If not specify we use a default value of 50.
43+
#[serde(rename = "perPage")]
44+
pub per_page: Option<i128>,
45+
/// Specify exactly what page you want to retrieve. If not specify we use a default value of 1.
46+
pub page: Option<i128>,
47+
/// A timestamp from which to start listing product e.g. 2016-09-24T00:00:05.000Z, 2016-09-21
48+
pub from: Option<DateTime<Local>>,
49+
/// A timestamp at which to stop listing product e.g. 2016-09-24T00:00:05.000Z, 2016-09-21
50+
pub to: Option<DateTime<Local>>,
51+
}
52+
const BULK_CHARGES_URL: &str = "https://api.paystack.co/bulkcharge";
53+
impl BulkCharges {
54+
// FIXME: the docs dont say what it is here, hence I wont be implementing this method until the docs are clear
55+
// pub fn initiate_bulk_charges(&self, body: InitiateBulkChargesBody) -> Result<Response, String> {
56+
// let res = make_request(
57+
// &self.bearer_auth,
58+
// BULK_CHARGES_URL,
59+
// Some(body),
60+
// REQUEST::POST,
61+
// );
62+
// return res;
63+
// }
64+
65+
/// This lists all bulk charge batches created by the integration. Statuses can be active, paused, or complete.
66+
pub fn list_bulk_charges(
67+
&self,
68+
params: Option<ListBulkChargesParams>,
69+
) -> Result<Response, String> {
70+
let res = make_get_request(&self.bearer_auth, BULK_CHARGES_URL, params);
71+
return res;
72+
}
73+
74+
/// This endpoint retrieves a specific batch code.
75+
/// It also returns useful information on its progress by way of the `total_charges` and `pending_charges` attributes.
76+
/// - id_or_code:
77+
/// An ID or code for the charge whose batches you want to retrieve.
78+
pub fn fetch_bulk_charge_batch(&self, id_or_code: &str) -> Result<Response, String> {
79+
let url = format!("{}/{}", BULK_CHARGES_URL, id_or_code);
80+
let res = make_get_request(&self.bearer_auth, &url, None::<String>);
81+
return res;
82+
}
83+
/// This endpoint retrieves the charges associated with a specified batch code. Pagination parameters are available.
84+
/// You can also filter by status. Charge statuses can be pending, success or failed.
85+
/// - id_or_code:
86+
/// An ID or code for the charge whose batches you want to retrieve.
87+
pub fn fetch_charges_in_a_batch(
88+
&self,
89+
id_or_code: &str,
90+
params: FetchChargesInABatchParams,
91+
) -> Result<Response, String> {
92+
let url = format!("{}/{}", BULK_CHARGES_URL, id_or_code);
93+
let res = make_get_request(&self.bearer_auth, &url, Some(params));
94+
return res;
95+
}
96+
97+
/// Use this endpoint to pause processing a batch
98+
/// - batch_code:
99+
/// The batch code for the bulk charge you want to pause
100+
pub fn pause_bulk_charge_batch(&self, batch_code: &str) -> Result<Response, String> {
101+
let url = format!("{}/pause/{}", BULK_CHARGES_URL, batch_code);
102+
let res = make_get_request(&self.bearer_auth, &url, None::<String>);
103+
return res;
104+
}
105+
106+
/// Use this endpoint to pause processing a batch
107+
/// - batch_code:
108+
/// The batch code for the bulk charge you want to pause
109+
pub fn resume_bulk_charge_batch(&self, batch_code: &str) -> Result<Response, String> {
110+
let url = format!("{}/resume/{}", BULK_CHARGES_URL, batch_code);
111+
let res = make_get_request(&self.bearer_auth, &url, None::<String>);
112+
return res;
113+
}
114+
}

src/paystack/charge.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
use chrono::{DateTime, Local};
2+
use reqwest::blocking::Response;
3+
use serde::Serialize;
4+
use serde_json::Value as JSON;
5+
6+
use crate::utils::{make_get_request, make_request, REQUEST};
7+
8+
#[derive(Debug, Default)]
9+
pub struct Charge {
10+
pub(crate) bearer_auth: String,
11+
}
12+
13+
#[derive(Debug, Serialize)]
14+
pub struct CreateChargeBody<'a> {
15+
/// Customer's email address
16+
pub email: &'a str,
17+
/// Amount should be in kobo if currency is `NGN`, pesewas, if currency is `GHS`, and cents, if currency is `ZAR`
18+
pub amount: &'a str,
19+
/// Bank account to charge (don't send if charging an authorization code)
20+
pub bank: Option<JSON>,
21+
/// An authorization code to charge (don't send if charging a bank account)
22+
pub authorization_code: Option<&'a str>,
23+
/// 4-digit PIN (send with a non-reusable authorization code)
24+
pub pin: Option<&'a str>,
25+
/// A JSON object
26+
pub metadata: Option<JSON>,
27+
/// Unique transaction reference. Only -, .`, = and alphanumeric characters allowed.
28+
pub reference: Option<&'a str>,
29+
/// USSD type to charge (don't send if charging an authorization code, bank or card)
30+
pub ussd: Option<JSON>,
31+
/// Mobile details (don't send if charging an authorization code, bank or card)
32+
pub mobile_money: Option<JSON>,
33+
/// This is the unique identifier of the device a user uses in making payment.
34+
/// Only -, .`, = and alphanumeric characters allowed.
35+
pub device_id: Option<&'a str>,
36+
}
37+
38+
#[derive(Debug, Serialize)]
39+
pub struct SubmitPinBody<'a> {
40+
/// PIN submitted by user
41+
pub pin: &'a str,
42+
/// Reference for transaction that requested pin
43+
pub reference: &'a str,
44+
}
45+
46+
#[derive(Debug, Serialize)]
47+
pub struct SubmitOTPBody<'a> {
48+
/// OTP submitted by user
49+
pub otp: &'a str,
50+
/// Reference for ongoing transaction
51+
pub reference: &'a str,
52+
}
53+
54+
#[derive(Debug, Serialize)]
55+
pub struct SubmitPhoneBody<'a> {
56+
/// Phone submitted by user
57+
pub phone: &'a str,
58+
/// Reference for ongoing transaction
59+
pub reference: &'a str,
60+
}
61+
62+
#[derive(Debug, Serialize)]
63+
pub struct SubmitBirthdayBody<'a> {
64+
/// Birthday submitted by user e.g. 2016-09-21
65+
pub birthday: DateTime<Local>,
66+
/// Reference for ongoing transaction
67+
pub reference: &'a str,
68+
}
69+
70+
#[derive(Debug, Serialize)]
71+
pub struct SubmitAddressBody<'a> {
72+
/// Address submitted by user
73+
pub address: &'a str,
74+
/// Reference for ongoing transaction
75+
pub reference: &'a str,
76+
/// City submitted by user
77+
pub city: &'a str,
78+
/// State submitted by user
79+
pub state: &'a str,
80+
/// Zipcode submitted by user
81+
pub zipcode: &'a str,
82+
}
83+
84+
const CHARGE_URL: &str = "https://api.paystack.co/charge";
85+
impl Charge {
86+
// TODO: link payment channel here
87+
/// Initiate a payment by integrating the [][payment channel] of your choice.
88+
pub fn create_charge(&self, body: CreateChargeBody) -> Result<Response, String> {
89+
let res = make_request(&self.bearer_auth, CHARGE_URL, Some(body), REQUEST::POST);
90+
return res;
91+
}
92+
93+
/// Submit PIN to continue a charge
94+
pub fn submit_pin(&self, body: SubmitPinBody) -> Result<Response, String> {
95+
let url = format!("{}/submit_pin", CHARGE_URL);
96+
let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST);
97+
return res;
98+
}
99+
100+
/// Submit OTP to complete a charge
101+
pub fn submit_otp(&self, body: SubmitOTPBody) -> Result<Response, String> {
102+
let url = format!("{}/submit_otp", CHARGE_URL);
103+
let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST);
104+
return res;
105+
}
106+
107+
/// Submit phone when requested
108+
pub fn submit_phone(&self, body: SubmitPhoneBody) -> Result<Response, String> {
109+
let url = format!("{}/submit_phone", CHARGE_URL);
110+
let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST);
111+
return res;
112+
}
113+
114+
/// Submit birthday when requested
115+
pub fn submit_birthday(&self, body: SubmitBirthdayBody) -> Result<Response, String> {
116+
let url = format!("{}/submit_birthday", CHARGE_URL);
117+
let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST);
118+
return res;
119+
}
120+
121+
/// Submit address to continue a charge
122+
pub fn submit_address(&self, body: SubmitAddressBody) -> Result<Response, String> {
123+
let url = format!("{}/submit_address", CHARGE_URL);
124+
let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST);
125+
return res;
126+
}
127+
128+
/// When you get "pending" as a charge status or if there was an exception when calling any of the /charge endpoints,
129+
/// wait 10 seconds or more, then make a check to see if its status has changed.
130+
/// Don't call too early as you may get a lot more pending than you should.
131+
pub fn check_pending_charge(&self, reference: &str) -> Result<Response, String> {
132+
let url = format!("{}/{}", CHARGE_URL, reference);
133+
let res = make_get_request(&self.bearer_auth, &url, None::<String>);
134+
return res;
135+
}
136+
}

src/paystack/control_panel.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use reqwest::blocking::Response;
2+
use serde::Serialize;
3+
4+
use crate::utils::{make_get_request, make_request, REQUEST};
5+
6+
/// The Control Panel API allows you manage some settings on your integration
7+
#[derive(Debug, Default)]
8+
pub struct ControlPanel {
9+
pub(crate) bearer_auth: String,
10+
}
11+
12+
#[derive(Debug, Serialize)]
13+
pub struct UpdatePaymentSessionTimeoutBody {
14+
/// Time before stopping session (in seconds). Set to 0 to cancel session timeouts
15+
pub timeout: i64,
16+
}
17+
18+
const CONTROL_PANEL_URL: &str = "https://api.paystack.co/integration";
19+
impl ControlPanel {
20+
/// Fetch the payment session timeout on your integration
21+
pub fn fetch_payment_session_timeout(&self) -> Result<Response, String> {
22+
let url = format!("{}/payment_session_timeout", CONTROL_PANEL_URL);
23+
let res = make_get_request(&self.bearer_auth, &url, None::<String>);
24+
return res;
25+
}
26+
27+
/// Update the payment session timeout on your integration
28+
pub fn update_payment_session_timeout(
29+
&self,
30+
body: UpdatePaymentSessionTimeoutBody,
31+
) -> Result<Response, String> {
32+
let url = format!("{}/payment_session_timeout", CONTROL_PANEL_URL);
33+
let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::PUT);
34+
return res;
35+
}
36+
}

0 commit comments

Comments
 (0)