Skip to content

Commit

Permalink
chore: move express-request and transaction reversal to builder patte…
Browse files Browse the repository at this point in the history
…rn (#88)

* chore: move express-request to builder pattern

* chore: migrate api, reorganise imports and remove redundant tests

* chore: remove unecessary deps

* chore: fix imports order

* chore: remove vscode files

* Add more tests

* Add validator module and implement phone number validation

* chore: rename method

* Fix phone number validation and remove unnecessary
receiver identifier type

* Add more variants for phone validator

* Fix URL path in express_request.rs and
transaction_reversal.rs

* Fix import order in lib.rs

* Update amount field from f64 to u32 in
express_request.rs and transaction_reversal.rs

* Update dependencies and add receiver identifier
type

* Fix merge conflicts

* Fix doc tests

* Fix dotenvy crate

* Fix: dotenv failure to read .env messages

* fix: uninitialized pass key

* fix: timestamp

* chore: clean up codes

* fix: compilation

* chore: update intiaitor password

* chore: revert removal of identifier receiver

* chore: revert removal of identifier receiver

* chore: revert removal of identifier receiver

* fix: express request tests

* fix: express request tests

* fix: express request tests

* fix: express request tests

* chore: refactor codes

* further investigation
  • Loading branch information
itsyaasir authored Jan 28, 2024
1 parent e7e122d commit b313747
Show file tree
Hide file tree
Showing 31 changed files with 765 additions and 744 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/.idea
Cargo.lock
.env
.DS_Store
.DS_Store

.vscode
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

49 changes: 26 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ repository = "https://github.com/collinsmuriuki/mpesa-rust"
readme = "./README.md"
license = "MIT"

[dependencies]
cached = { version = "0.46", features = ["wasm", "async", "proc_macro"] }
chrono = { version = "0.4", optional = true, default-features = false, features = [
"clock",
"serde",
] }
openssl = { version = "0.10", optional = true }
reqwest = { version = "0.11", features = ["json"] }
derive_builder = "0.12"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_repr = "0.1"
thiserror = "1.0.37"
wiremock = "0.5"
secrecy = "0.8.0"
serde-aux = "4.2.0"

[dev-dependencies]
dotenv = "0.15"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
wiremock = "0.5"

[features]
default = [
"account_balance",
Expand All @@ -42,7 +20,7 @@ default = [
"express_request",
"transaction_reversal",
"transaction_status",
"dynamic_qr"
"dynamic_qr",
]
dynamic_qr = []
account_balance = ["dep:openssl"]
Expand All @@ -54,3 +32,28 @@ c2b_simulate = []
express_request = ["dep:chrono"]
transaction_reversal = ["dep:openssl"]
transaction_status = ["dep:openssl"]


[dependencies]
cached = { version = "0.46", features = ["wasm", "async", "proc_macro"] }
chrono = { version = "0.4", optional = true, default-features = false, features = [
"clock",
"serde",
] }
openssl = { version = "0.10", optional = true }
reqwest = { version = "0.11", features = ["json"] }
derive_builder = "0.12"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_repr = "0.1"
thiserror = "1.0"
secrecy = "0.8"
serde-aux = "4.2"
url = { version = "2", features = ["serde"] }
regex = { version = "1.10", default-features = false, features = ["std"] }


[dev-dependencies]
dotenvy = "0.15.7"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
wiremock = "0.5"
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ use mpesa::{Mpesa, Environment};

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);

Expand All @@ -73,11 +73,11 @@ use std::str::FromStr;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::from_str("sandbox")?, // or
// Environment::try_from("sandbox")?,
);
Expand Down Expand Up @@ -121,11 +121,11 @@ impl ApiEnvironment for CustomEnvironment {

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
CustomEnvironment,
);
}
Expand All @@ -139,11 +139,11 @@ use mpesa::{Mpesa, Environment};

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);

Expand Down Expand Up @@ -177,8 +177,8 @@ The table below shows all the MPESA APIs from Safaricom and those supported by t

**Collins Muriuki**

- Twitter: [@c12i\_](https://twitter.com/c12i_)
- Not affiliated with Safaricom.
- Twitter: [@c12i\_](https://twitter.com/c12i_)
- Not affiliated with Safaricom.

## Contributing

Expand Down
11 changes: 7 additions & 4 deletions docs/client/account_balance.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Account Balance

The Account Balance API is used to request the account balance of a short code. This can be used for both B2C, buy goods and pay bill accounts.

Requires an `initiator_name`.
Returns an `AccountBalanceBuilder` for enquiring the balance on an MPESA BuyGoods.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/AccountBalance)

# Example
## Example

```rust
use mpesa::{Mpesa, Environment};

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);

Expand Down
11 changes: 7 additions & 4 deletions docs/client/b2b.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# B2B

This API enables you to pay bills directly from your business account to a pay bill number, or a paybill store. You can use this API to pay on behalf of a consumer/requester.

The transaction moves money from your MMF/Working account to the recipient’s utility account.
Expand All @@ -7,17 +9,18 @@ Requires an `initiator_name`, the credential/ username used to authenticate the

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BusinessPayBill)

# Example
## Example

```rust
use mpesa::{Mpesa, Environment};

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);

Expand Down
11 changes: 7 additions & 4 deletions docs/client/b2c.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# B2C

Requires an `initiator_name`, the credential/ username used to authenticate the transaction request
Returns a `B2cBuilder` for building a B2C transaction struct.

Safaricom the API docs [reference](https://developer.safaricom.co.ke/APIs/BusinessToCustomer).

# Example
## Example

```rust
use mpesa::{Mpesa, Environment};

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();

let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);

Expand Down
11 changes: 7 additions & 4 deletions docs/client/bill_manager/bulk_invoice.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Bulk Invoice

Creates a `BulkInvoiceBuilder` which allows you to send invoices to your customers in bulk.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BillManager)

# Example
## Example

```rust,ignore
use mpesa::{Mpesa, Environment, Invoice, InvoiceItem};
use chrono::prelude::Utc;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);
Expand Down
11 changes: 7 additions & 4 deletions docs/client/bill_manager/cancel_invoice.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Cancel Invoice

Creates a `CancelInvoiceBuilder` which allows you to recall a sent invoice.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BillManager)

# Example
## Example

```rust,ignore
use mpesa::{Mpesa, Environment, SendRemindersTypes};
use chrono::prelude::Utc;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);
Expand Down
11 changes: 7 additions & 4 deletions docs/client/bill_manager/onboard.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Onboard

Creates a `OnboardBuilder` which allows you to opt in as a biller to the bill manager features.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BillManager)

# Example
## Example

```rust,ignore
use mpesa::{Mpesa, Environment, SendRemindersTypes};
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);
Expand Down
11 changes: 7 additions & 4 deletions docs/client/bill_manager/onboard_modify.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Onboard Modify

Creates a `OnboardModifyBuilder` which allows you to opt in as a biller to the bill manager features.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BillManager)

# Example
## Example

```rust,ignore
use mpesa::{Mpesa, Environment, SendRemindersTypes};
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);
Expand Down
11 changes: 7 additions & 4 deletions docs/client/bill_manager/reconciliation.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Reconciliation

Creates a `ReconciliationBuilder` which enables your customers to receive e-receipts for payments made to your paybill account.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BillManager)

# Example
## Example

```rust,ignore
use mpesa::{Mpesa, Environment};
use chrono::prelude::Utc;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);
Expand Down
11 changes: 7 additions & 4 deletions docs/client/bill_manager/single_invoice.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Single Invoice

Creates a `SingleInvoiceBuilder` which allows you to create and send invoices to your customers.

Safaricom API docs [reference](https://developer.safaricom.co.ke/APIs/BillManager)

# Example
## Example

```rust,ignore
use mpesa::{Mpesa, Environment, InvoiceItem};
use chrono::prelude::Utc;
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let client = Mpesa::new(
env!("CLIENT_KEY"),
env!("CLIENT_SECRET"),
dotenvy::var("CLIENT_KEY").unwrap(),
dotenvy::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
);
Expand Down
Loading

0 comments on commit b313747

Please sign in to comment.