Skip to content

Commit

Permalink
push new thing
Browse files Browse the repository at this point in the history
  • Loading branch information
princefr committed Sep 6, 2024
1 parent 37cffe9 commit e9cbbc5
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 131 deletions.
14 changes: 14 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@



[env]
MTN_API_KEY = "fad2514dbf894c66a91facd483e2cfbf"
MTN_API_USER = "4fcfcdc4-1099-4a3d-9f2f-803940572dc5"
MTN_COLLECTION_PRIMARY_KEY = "c19aabaac34a43a8a3786909ed0d6b31"
MTN_COLLECTION_SECONDARY_KEY = "98b4b6300b6242059a9029f00f6afe54"
MTN_DISBURSEMENT_PRIMARY_KEY = "c1861c073bf54addbb56481d6977eebf"
MTN_DISBURSEMENT_SECONDARY_KEY = "c58b67e3934b44bbabe65fb88fa33b96"
MTN_REMITTANCE_PRIMARY_KEY = "62b479e1f41441cda28eb24dff4fc067"
MTN_REMITTANCE_SECONDARY_KEY = "3766f4a178534847a2cce411041ffaac"
MTN_URL = "https://sandbox.momodeveloper.mtn.com"
MTN_CALLBACK_HOST = "ngrok.boursenumeriquedafrique.com"
22 changes: 18 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@ keywords = ["momo", "money", "africa", "payment", "mtn"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = { version = "0.4.31", features = ["serde"]}
chrono = { version = "0.4.31", features = ["serde"] }
dotenv = "0.15.0"
once_cell = "1.19.0"
poem = "3.0.4"
poem = { version = "3.0.4", features = [
"rustls",
"compression",
"sse",
"requestid",
] }
reqwest = "0.11.22"
serde = {version = "1.0", features = ["derive"]}
rustls = "0.23.12"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.108"
tokio = {version = "1.33.0", features = ["full"] }
tokio = { version = "1.33.0", features = ["full"] }
tokio-rustls = "0.26.0"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
webpki-roots = "0.26.5"


[dev-dependencies]
once_cell = "1.18.0"


[dependencies.uuid]
Expand Down
6 changes: 5 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
integration_test:
cargo test --test '*' -- --test-threads=1 --nocapture
curl:
curl -X POST https://ngrok.boursenumeriquedafrique.com/mtn -H "Content-Type: application/json" -d '{"key":"value"}'

push_new_version:
chmod +x new_version.sh
./new_version.sh
./new_version.sh
56 changes: 53 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@
#[doc(hidden)]
use std::error::Error;

use poem::{listener::TcpListener, post, EndpointExt};
use uuid::Uuid;

use poem::Result;
#[doc(hidden)]
use poem::{handler, Route, Server};

pub mod enums;
pub mod errors;
pub mod products;
Expand Down Expand Up @@ -204,6 +209,47 @@ impl DepositId {
}
}

#[handler]
fn mtn_callback(req: &poem::Request) -> poem::Response {

Check failure on line 213 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `req`
poem::Response::builder()
.status(poem::http::StatusCode::OK)
.body("Callback received successfully")
}

#[handler]
fn mtn_put_calback(req: &poem::Request) -> poem::Response {

Check failure on line 220 in src/lib.rs

View workflow job for this annotation

GitHub Actions / build

unused variable: `req`
println!("yes put boatch");
poem::Response::builder()
.status(poem::http::StatusCode::OK)
.body("Callback received successfully")
}

pub struct MomoCallbackListener;

impl MomoCallbackListener {
pub async fn serve(port: String) -> Result<(), Box<dyn Error>> {
use tracing_subscriber;

tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();

std::env::set_var("RUST_BACKTRACE", "1");
let app = Route::new()
.at("/mtn", post(mtn_callback).put(mtn_put_calback))
.with(poem::middleware::Tracing::default())
.with(poem::middleware::Cors::new())
.with(poem::middleware::Compression::default())
.with(poem::middleware::RequestId::default());

Server::new(TcpListener::bind(format!("0.0.0.0:{}", port)))
.run(app)
.await
.expect("the server failed to start");
Ok(())
}
}

#[doc(hidden)]
#[derive(Debug)]
pub struct Momo {
Expand Down Expand Up @@ -239,16 +285,20 @@ impl Momo {
/// # Parameters
/// * 'url' the momo instance url to use
/// * 'subscription_key' the subscription key to use
/// * 'provider_callback_host', the callback host that will be used to send momo updates (ex: google.com)
///
/// #Returns
/// Result<Momo, Box<dyn Error>>
pub async fn new_with_provisioning(
url: String,
subscription_key: String,
provider_callback_host: &str,
) -> Result<Momo, Box<dyn Error>> {
let provisioning = MomoProvisioning::new(url.clone(), subscription_key.clone());
let reference_id = Uuid::new_v4().to_string();
let _create_sandbox = provisioning.create_sandox(&reference_id).await?;
let _create_sandbox = provisioning
.create_sandox(&reference_id, provider_callback_host)
.await?;
let api = provisioning.create_api_information(&reference_id).await?;
return Ok(Momo {
url,
Expand Down Expand Up @@ -338,7 +388,7 @@ mod tests {
let primary_key = env::var("MTN_COLLECTION_PRIMARY_KEY").expect("PRIMARY_KEY must be set");
let secondary_key =
env::var("MTN_COLLECTION_SECONDARY_KEY").expect("SECONDARY_KEY must be set");
let momo = Momo::new_with_provisioning(mtn_url, primary_key.clone())
let momo = Momo::new_with_provisioning(mtn_url, primary_key.clone(), "test")
.await
.unwrap();
let collection = momo.collection(primary_key, secondary_key);
Expand All @@ -356,7 +406,7 @@ mod tests {
"test_payer_message".to_string(),
"test_payee_note".to_string(),
);
let result = collection.request_to_pay(request).await;
let result = collection.request_to_pay(request, None).await;
assert!(result.is_ok());
}
}
13 changes: 2 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
use poem::{get, handler, listener::TcpListener, web::Path, Route, Server};

#[handler]
fn callback(Path(name): Path<String>) -> String {
format!("hello")
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let app = Route::new().at("callback", get(callback));
Server::new(TcpListener::bind("0.0.0.0:3000"))
.run(app)
.await
let _ = mtnmomo::MomoCallbackListener::serve("3000".to_string()).await;
Ok(())
}
Loading

0 comments on commit e9cbbc5

Please sign in to comment.