Skip to content

Commit

Permalink
refactor the payment mod
Browse files Browse the repository at this point in the history
  • Loading branch information
nsantiago2719 committed Apr 5, 2023
1 parent 69feb92 commit 250e578
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
53 changes: 2 additions & 51 deletions payment-vault/src/lib.rs
Expand Up @@ -6,54 +6,5 @@ use reqwest::{
};
use serde::Serialize;

pub mod payment_gateway {

use super::*;

#[derive(Debug, Serialize)]
#[allow(non_snake_case)]
pub struct CardDetails {
pub number: String,
pub expYear: String,
pub expMonth: String,
pub cvc: String,
}

#[derive(Debug, Serialize)]
struct Card {
pub card: CardDetails,
}

#[async_trait]
pub trait PaymentGateway {
async fn create_payment_token(
&self,
card_details: CardDetails,
) -> Result<Response, Box<dyn std::error::Error>>;
}

#[async_trait]
impl PaymentGateway for MayaClient {
async fn create_payment_token(
&self,
card_details: CardDetails,
) -> Result<Response, Box<dyn std::error::Error>> {
let url = format!("https://{}/payments/v1/payment-tokens", self.url_domain);
let auth = format!("Basic {}", &self.auth_header);
let request_body = Card { card: card_details };

let response = self
.client
.post(&url)
.header(AUTHORIZATION, auth)
.header(ACCEPT, "application/json")
.header(CONTENT_TYPE, "application/json")
.json(&request_body)
.send()
.await?
.error_for_status()?;

Ok(response)
}
}
}
pub use self::payment::{CardDetails, PaymentGateway};
mod payment;
48 changes: 48 additions & 0 deletions payment-vault/src/payment.rs
@@ -0,0 +1,48 @@
use super::*;

#[derive(Debug, Serialize)]
#[allow(non_snake_case)]
pub struct CardDetails {
pub number: String,
pub expYear: String,
pub expMonth: String,
pub cvc: String,
}

#[derive(Debug, Serialize)]
struct Card {
pub card: CardDetails,
}

#[async_trait]
pub trait PaymentGateway {
async fn create_payment_token(
&self,
card_details: CardDetails,
) -> Result<Response, Box<dyn std::error::Error>>;
}

#[async_trait]
impl PaymentGateway for MayaClient {
async fn create_payment_token(
&self,
card_details: CardDetails,
) -> Result<Response, Box<dyn std::error::Error>> {
let url = format!("https://{}/payments/v1/payment-tokens", self.url_domain);
let auth = format!("Basic {}", &self.auth_header);
let request_body = Card { card: card_details };

let response = self
.client
.post(&url)
.header(AUTHORIZATION, auth)
.header(ACCEPT, "application/json")
.header(CONTENT_TYPE, "application/json")
.json(&request_body)
.send()
.await?
.error_for_status()?;

Ok(response)
}
}
2 changes: 1 addition & 1 deletion payment-vault/tests/payment_gateway/payment_token.rs
@@ -1,5 +1,5 @@
use maya_client_sdk::MayaClient;
use maya_payment_vault_sdk::payment_gateway::{CardDetails, PaymentGateway};
use maya_payment_vault_sdk::{CardDetails, PaymentGateway};

#[cfg(test)]
mod test_payment_token {
Expand Down

0 comments on commit 250e578

Please sign in to comment.