Skip to content

Commit

Permalink
feature: create payment fn send request
Browse files Browse the repository at this point in the history
  • Loading branch information
nsantiago2719 committed Dec 9, 2023
1 parent 7b342db commit e6eacfc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion payment-vault/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use maya_client_sdk::MayaClient;
use reqwest::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE};
use serde::Serialize;
use serde::{Deserialize, Serialize};

pub use self::payment::CardDetails;

Expand Down
22 changes: 16 additions & 6 deletions payment-vault/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,20 @@ pub struct MetaData {
#[derive(Debug, Serialize)]
#[allow(non_snake_case)]
pub struct Payment {
paymentTokenId: String,
totalAmount: TotalAmount,
buyer: Buyer,
redirectUrl: RedirectUrl,
requestReferenceNumber: String,
metadata: Option<MetaData>,
pub paymentTokenId: String,
pub totalAmount: TotalAmount,
pub buyer: Option<Buyer>,
pub redirectUrl: Option<RedirectUrl>,
pub requestReferenceNumber: Option<String>,
pub metadata: Option<MetaData>,
}

#[derive(Debug, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct PaymentToken {
pub paymentTokenId: String,
pub state: String,
pub createdAt: String,
pub updatedAt: String,
pub issuer: String,
}
7 changes: 5 additions & 2 deletions payment-vault/src/payment/payment_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ impl PaymentGateway for MayaClient {
let auth = format!("Basic {}", &self.auth_header);
let request_body = Card { card: card_details };

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

Ok(res)
}

async fn create_payment(&self, payment: Payment) -> Result<Response, Error> {
Expand Down
40 changes: 37 additions & 3 deletions payment-vault/tests/payment_gateway/payment_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ mod test_payment_token {
email: "sample@google.com".to_string(),
};

let _redirect_url: RedirectUrl = RedirectUrl {
let redirect_url: RedirectUrl = RedirectUrl {
success: "https://www.merchantsite.com/success".to_string(),
failure: "https://www.merchantsite.com/failure".to_string(),
cancel: "https://www.merchantsite.com/cancel".to_string(),
};

let _total_amount: TotalAmount = TotalAmount {
let total_amount: TotalAmount = TotalAmount {
amount: "100".to_string(),
currency: "PHP".to_string(),
};

let _buyer_user: Buyer = Buyer {
let buyer_user: Buyer = Buyer {
billingAddress: billing_address,
shippingAddress: shipping_address,
firstName: "John".to_string(),
Expand All @@ -89,5 +89,39 @@ mod test_payment_token {
contact: contact,
sex: Sex::M,
};

let card_details = CardDetails {
number: NUMBER.to_string(),
expYear: EXP_YEAR.to_string(),
expMonth: EXP_MONTH.to_string(),
cvc: CVC.to_string(),
};

let maya_client = MayaClient::new(
env!("ACCESS_TOKEN").to_string(),
env!("SECRET_TOKEN").to_string(),
None,
);

let payment_token = maya_client
.create_payment_token(card_details)
.await
.unwrap()
.json::<PaymentToken>()
.await
.unwrap();

let payment = Payment {
paymentTokenId: payment_token.paymentTokenId,
totalAmount: total_amount,
buyer: Some(buyer_user),
redirectUrl: Some(redirect_url),
metadata: None,
requestReferenceNumber: Some("332211".to_string()),
};
let create_payment = maya_client.create_payment(payment).await.unwrap();
println!("Created payment {:?}", create_payment);

// assert_eq!(create_payment.json(), 200);
}
}

0 comments on commit e6eacfc

Please sign in to comment.