Skip to content

Commit

Permalink
Moving around some fields and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirch committed Sep 3, 2023
1 parent 13b2052 commit e97bf40
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
3 changes: 0 additions & 3 deletions infrastructure/iam.tf
Expand Up @@ -20,7 +20,6 @@ data "aws_iam_policy_document" "initiate_payment_policy_document" {
]
resources = [
aws_dynamodb_table.payments.arn,
"${aws_dynamodb_table.payments.arn}/*",
]
}
statement {
Expand Down Expand Up @@ -48,7 +47,6 @@ resource "aws_iam_role_policy_attachment" "initiate_payment_policy_attachment" {
}

# FINISH PAYMENT LAMBDA ROLE/POLICIES

resource "aws_iam_role" "finish_payment" {
assume_role_policy = data.aws_iam_policy_document.finish_payment_assume_policy.json
}
Expand All @@ -72,7 +70,6 @@ data "aws_iam_policy_document" "finish_payment_policy_document" {
]
resources = [
aws_dynamodb_table.payments.arn,
"${aws_dynamodb_table.payments.arn}/*",
]
}

Expand Down
2 changes: 1 addition & 1 deletion infrastructure/lambda.tf
Expand Up @@ -2,8 +2,8 @@
resource "aws_lambda_function" "initiate_payment_lambda" {
function_name = "InitiatePayment"

source_code_hash = filebase64sha256("data/lambdas/initiate_payment.zip")
filename = "data/lambdas/initiate_payment.zip"
source_code_hash = filebase64sha256("data/lambdas/initiate_payment.zip")

handler = "handler"
runtime = "provided.al2"
Expand Down
4 changes: 2 additions & 2 deletions src/payment.rs
@@ -1,13 +1,13 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub enum PaymentStatus {
Pending,
Failed,
Completed,
}

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Payment {
pub id: String,
pub amount: i64,
Expand Down
15 changes: 10 additions & 5 deletions src/payment_client.rs
@@ -1,14 +1,14 @@
use crate::{
environment::{DOMAIN, STRIPE_SECRET_KEY},
payment::Payment,
};
use crate::payment::Payment;
use stripe::{
CheckoutSession, CheckoutSessionMode, Client, CreateCheckoutSession,
CreateCheckoutSessionLineItems, CreateCheckoutSessionLineItemsPriceData,
CreateCheckoutSessionLineItemsPriceDataProductData, CreateCheckoutSessionPaymentIntentData,
Currency, Metadata,
};

pub const STRIPE_SECRET_KEY: &str = "STRIPE_SECRET_KEY";
pub const DOMAIN: &str = "DOMAIN";

pub struct PaymentClient {
stripe_client: stripe::Client,
}
Expand All @@ -28,10 +28,12 @@ impl PaymentClient {
}
}

#[tracing::instrument(skip(self, payment), fields(sender = %payment.sender, amount = %payment.amount))]
#[tracing::instrument(skip(self))]
pub async fn initiate_payment(&self, payment: &Payment) -> Result<String, String> {
// Get the website domain from the environment
let domain = std::env::var(DOMAIN).map_err(|e| e.to_string())?;

// Create the Stripe Checkout Session parameters
let mut create_session_params = CreateCheckoutSession::new(&domain);
create_session_params.line_items = Some(vec![CreateCheckoutSessionLineItems {
price_data: Some(CreateCheckoutSessionLineItemsPriceData {
Expand All @@ -46,15 +48,18 @@ impl PaymentClient {
quantity: Some(1),
..Default::default()
}]);
// Set the mode to payment
create_session_params.mode = Some(CheckoutSessionMode::Payment);

// Add the payment id to the metadata
let mut metadata = Metadata::new();
metadata.insert("payment_id".to_string(), payment.id.to_string());
create_session_params.payment_intent_data = Some(CreateCheckoutSessionPaymentIntentData {
metadata,
..Default::default()
});

// Create the Stripe Checkout Session
let session = CheckoutSession::create(&self.stripe_client, create_session_params)
.await
.map_err(|e| e.to_string())?;
Expand Down
11 changes: 4 additions & 7 deletions src/payments_repository.rs
@@ -1,11 +1,9 @@
use aws_sdk_dynamodb::{types::AttributeValue, Client};

use crate::{
environment::PAYMENTS_TABLE,
payment::{Payment, PaymentStatus},
};
use crate::payment::{Payment, PaymentStatus};

pub const PAYMENTS_TABLE: &str = "PAYMENTS_TABLE_NAME";

#[derive(Clone)]
pub struct PaymentsRepository {
client: Client,
table_name: String,
Expand All @@ -25,8 +23,7 @@ impl PaymentsRepository {
let sender = AttributeValue::S(payment.sender);
let status = AttributeValue::N((payment.status as i8).to_string());

let _request = self
.client
self.client
.put_item()
.table_name(&self.table_name)
.item("id", id)
Expand Down

0 comments on commit e97bf40

Please sign in to comment.