diff --git a/.github/workflows/provision.yml b/.github/workflows/provision.yml index 926a236..3a37976 100644 --- a/.github/workflows/provision.yml +++ b/.github/workflows/provision.yml @@ -63,5 +63,5 @@ jobs: run: exit 1 - name: Terraform Apply - if: github.ref == 'refs/heads/main' && github.event_name == 'push' + #if: github.ref == 'refs/heads/main' && github.event_name == 'push' run: terraform apply -auto-approve -input=false diff --git a/pom.xml b/pom.xml index 5c702bb..4a5544b 100644 --- a/pom.xml +++ b/pom.xml @@ -305,7 +305,7 @@ - com.fiap.payments.PaymentsApplication + com.fiap.payments.PaymentsApiApp diff --git a/src/main/kotlin/com/fiap/payments/PaymentsApp.kt b/src/main/kotlin/com/fiap/payments/PaymentsApiApp.kt similarity index 95% rename from src/main/kotlin/com/fiap/payments/PaymentsApp.kt rename to src/main/kotlin/com/fiap/payments/PaymentsApiApp.kt index ba5fc11..f474bf9 100644 --- a/src/main/kotlin/com/fiap/payments/PaymentsApp.kt +++ b/src/main/kotlin/com/fiap/payments/PaymentsApiApp.kt @@ -32,8 +32,8 @@ import org.springframework.cloud.openfeign.FeignAutoConfiguration ) @EnableFeignClients @ImportAutoConfiguration(value = [FeignAutoConfiguration::class]) -class PaymentsApp +class PaymentsApiApp fun main(args: Array) { - runApplication(*args) + runApplication(*args) } diff --git a/src/main/kotlin/com/fiap/payments/adapter/controller/PaymentController.kt b/src/main/kotlin/com/fiap/payments/adapter/controller/PaymentController.kt index f3e416d..afa4e31 100644 --- a/src/main/kotlin/com/fiap/payments/adapter/controller/PaymentController.kt +++ b/src/main/kotlin/com/fiap/payments/adapter/controller/PaymentController.kt @@ -1,6 +1,5 @@ package com.fiap.payments.adapter.controller -import com.fiap.payments.domain.entities.Order import com.fiap.payments.domain.entities.Payment import com.fiap.payments.domain.entities.PaymentRequest import com.fiap.payments.driver.web.PaymentAPI @@ -57,11 +56,10 @@ class PaymentController( } } - override fun create(orderNumber: Long, order: OrderRequest): ResponseEntity { + override fun create(order: OrderRequest): ResponseEntity { return ResponseEntity.ok(providePaymentRequestUseCase.providePaymentRequest(order.toDomain())); } - enum class IPNType(val ipnType: String) { MERCHANT_ORDER("merchant_order"), PAYMENT("payment"), @@ -69,5 +67,3 @@ class PaymentController( POINT_INTEGRATION_IPN("point_integration_ipn"), } } - - diff --git a/src/main/kotlin/com/fiap/payments/adapter/controller/configuration/ServiceConfig.kt b/src/main/kotlin/com/fiap/payments/adapter/controller/configuration/ServiceConfig.kt index 46f7e21..b50528b 100644 --- a/src/main/kotlin/com/fiap/payments/adapter/controller/configuration/ServiceConfig.kt +++ b/src/main/kotlin/com/fiap/payments/adapter/controller/configuration/ServiceConfig.kt @@ -1,6 +1,6 @@ package com.fiap.payments.adapter.controller.configuration -import com.fiap.payments.PaymentsApp +import com.fiap.payments.PaymentsApiApp import com.fiap.payments.adapter.gateway.OrderGateway import com.fiap.payments.adapter.gateway.PaymentGateway import com.fiap.payments.adapter.gateway.PaymentProviderGateway @@ -14,7 +14,7 @@ import com.fiap.payments.usecases.services.PaymentService import com.fiap.payments.usecases.services.OrderService @Configuration -@ComponentScan(basePackageClasses = [PaymentsApp::class]) +@ComponentScan(basePackageClasses = [PaymentsApiApp::class]) class ServiceConfig { diff --git a/src/main/kotlin/com/fiap/payments/driver/database/configuration/GatewayConfig.kt b/src/main/kotlin/com/fiap/payments/driver/database/configuration/GatewayConfig.kt index 4af6b5c..16bc5ed 100644 --- a/src/main/kotlin/com/fiap/payments/driver/database/configuration/GatewayConfig.kt +++ b/src/main/kotlin/com/fiap/payments/driver/database/configuration/GatewayConfig.kt @@ -1,6 +1,6 @@ package com.fiap.payments.driver.database.configuration -import com.fiap.payments.PaymentsApp +import com.fiap.payments.PaymentsApiApp import com.fiap.payments.adapter.gateway.* import com.fiap.payments.adapter.gateway.impl.* import com.fiap.payments.client.OrderApiClient @@ -10,7 +10,7 @@ import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration @Configuration -@ComponentScan(basePackageClasses = [PaymentsApp::class]) +@ComponentScan(basePackageClasses = [PaymentsApiApp::class]) class GatewayConfig { @Bean("PaymentGateway") diff --git a/src/main/kotlin/com/fiap/payments/driver/database/configuration/PaymentGatewayConfig.kt b/src/main/kotlin/com/fiap/payments/driver/database/configuration/PaymentGatewayConfig.kt index 5ca3b22..edc6fd0 100644 --- a/src/main/kotlin/com/fiap/payments/driver/database/configuration/PaymentGatewayConfig.kt +++ b/src/main/kotlin/com/fiap/payments/driver/database/configuration/PaymentGatewayConfig.kt @@ -1,6 +1,6 @@ package com.fiap.payments.driver.database.configuration -import com.fiap.payments.PaymentsApp +import com.fiap.payments.PaymentsApiApp import com.fiap.payments.adapter.gateway.PaymentProviderGateway import com.fiap.payments.client.MercadoPagoClient import com.fiap.payments.driver.database.provider.MercadoPagoPaymentProvider @@ -12,7 +12,7 @@ import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration @Configuration -@ComponentScan(basePackageClasses = [PaymentsApp::class]) +@ComponentScan(basePackageClasses = [PaymentsApiApp::class]) class PaymentGatewayConfig { @Bean("PaymentProvider") @ConditionalOnProperty("payment-provider.mock", havingValue = "false") diff --git a/src/main/kotlin/com/fiap/payments/driver/web/PaymentAPI.kt b/src/main/kotlin/com/fiap/payments/driver/web/PaymentAPI.kt index 00d1caa..8196dba 100644 --- a/src/main/kotlin/com/fiap/payments/driver/web/PaymentAPI.kt +++ b/src/main/kotlin/com/fiap/payments/driver/web/PaymentAPI.kt @@ -1,6 +1,5 @@ package com.fiap.payments.driver.web -import com.fiap.payments.domain.entities.Order import com.fiap.payments.domain.entities.Payment import com.fiap.payments.domain.entities.PaymentRequest import com.fiap.payments.driver.web.request.OrderRequest @@ -69,10 +68,12 @@ interface PaymentAPI { @RequestParam topic: String, ): ResponseEntity - @PostMapping("/create/{orderNumber}") - fun create( - @Parameter(description = "Número do pedido") @PathVariable orderNumber: Long, - @RequestBody order: OrderRequest - ): ResponseEntity + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "Operação bem-sucedida"), + ], + ) + @PostMapping("/create") + fun create(@RequestBody order: OrderRequest): ResponseEntity } diff --git a/src/main/kotlin/com/fiap/payments/usecases/services/OrderService.kt b/src/main/kotlin/com/fiap/payments/usecases/services/OrderService.kt index cb5bc22..b9ffd10 100644 --- a/src/main/kotlin/com/fiap/payments/usecases/services/OrderService.kt +++ b/src/main/kotlin/com/fiap/payments/usecases/services/OrderService.kt @@ -3,12 +3,15 @@ package com.fiap.payments.usecases.services import com.fiap.payments.adapter.gateway.OrderGateway import com.fiap.payments.domain.entities.Order import com.fiap.payments.usecases.ConfirmOrderUseCase +import org.slf4j.LoggerFactory open class OrderService( private val orderGateway: OrderGateway, ): ConfirmOrderUseCase { + private val log = LoggerFactory.getLogger(javaClass) override fun confirmOrder(orderNumber: Long): Order { + log.info("Requesting order [$orderNumber] to be confirmed") return orderGateway.confirmOrder(orderNumber) } } diff --git a/src/main/kotlin/com/fiap/payments/usecases/services/PaymentService.kt b/src/main/kotlin/com/fiap/payments/usecases/services/PaymentService.kt index 38213e6..ae41d1a 100644 --- a/src/main/kotlin/com/fiap/payments/usecases/services/PaymentService.kt +++ b/src/main/kotlin/com/fiap/payments/usecases/services/PaymentService.kt @@ -10,14 +10,17 @@ import com.fiap.payments.domain.errors.PaymentsException import com.fiap.payments.domain.valueobjects.PaymentStatus import com.fiap.payments.usecases.LoadPaymentUseCase import com.fiap.payments.usecases.ProvidePaymentRequestUseCase +import org.slf4j.LoggerFactory import java.time.LocalDateTime class PaymentService( private val paymentRepository: PaymentGateway, private val paymentProvider: PaymentProviderGateway, -) : - LoadPaymentUseCase, - ProvidePaymentRequestUseCase { +) : LoadPaymentUseCase, + ProvidePaymentRequestUseCase +{ + private val log = LoggerFactory.getLogger(javaClass) + override fun getByOrderNumber(orderNumber: Long): Payment { return paymentRepository.findByOrderNumber(orderNumber) ?: throw PaymentsException( @@ -36,6 +39,8 @@ class PaymentService( override fun providePaymentRequest(order: Order): PaymentRequest { val paymentRequest = paymentProvider.createExternalOrder(order) + log.info("Payment request created for order $order") + val payment = Payment( orderNumber = order.number!!, @@ -48,6 +53,7 @@ class PaymentService( ) paymentRepository.create(payment) + log.info("Payment stored for order $order") return paymentRequest } diff --git a/src/main/kotlin/com/fiap/payments/usecases/services/PaymentSyncService.kt b/src/main/kotlin/com/fiap/payments/usecases/services/PaymentSyncService.kt index 5c4f58e..536d8e6 100644 --- a/src/main/kotlin/com/fiap/payments/usecases/services/PaymentSyncService.kt +++ b/src/main/kotlin/com/fiap/payments/usecases/services/PaymentSyncService.kt @@ -6,6 +6,7 @@ import com.fiap.payments.domain.valueobjects.PaymentStatus import com.fiap.payments.usecases.ConfirmOrderUseCase import com.fiap.payments.usecases.LoadPaymentUseCase import com.fiap.payments.usecases.SyncPaymentUseCase +import org.slf4j.LoggerFactory import java.time.LocalDateTime class PaymentSyncService( @@ -14,6 +15,7 @@ class PaymentSyncService( private val paymentProviderGateway: PaymentProviderGateway, private val confirmOrderUseCase: ConfirmOrderUseCase ): SyncPaymentUseCase { + private val log = LoggerFactory.getLogger(javaClass) override fun syncPayment(orderNumber: Long, externalOrderGlobalId: String) { val payment = loadPaymentUseCase.getByOrderNumber(orderNumber) @@ -23,6 +25,7 @@ class PaymentSyncService( } val newStatus = paymentProviderGateway.checkExternalOrderStatus(externalOrderGlobalId) + log.info("Checked payment status for order [$orderNumber]: $newStatus") if (payment.status != newStatus) { paymentGateway.update( @@ -31,6 +34,7 @@ class PaymentSyncService( statusChangedAt = LocalDateTime.now(), ) ) + log.info("Changed payment status for order [$orderNumber] from ${payment.status} to $newStatus") if (newStatus == PaymentStatus.CONFIRMED) { confirmOrderUseCase.confirmOrder(orderNumber) diff --git a/terraform/dynamodb.tf b/terraform/dynamodb.tf index c3815b2..0ca9547 100644 --- a/terraform/dynamodb.tf +++ b/terraform/dynamodb.tf @@ -4,7 +4,6 @@ module "dynamodb_table" { name = "payments" hash_key = "payment_order_number" - range_key = "payment_created_at" table_class = "STANDARD" attributes = [ @@ -12,11 +11,45 @@ module "dynamodb_table" { name = "payment_order_number" type = "S" }, - { - name = "payment_created_at" - type = "S" - } ] tags = var.tags } + +resource "aws_iam_policy" "payments_dynamodb_table_policy" { + name = "TechChallengePaymentsDynamoDBTablePolicy" + + policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + "Sid" : "ListAndDescribe", + "Effect" : "Allow", + "Action" : [ + "dynamodb:List*", + "dynamodb:DescribeReservedCapacity*", + "dynamodb:DescribeLimits", + "dynamodb:DescribeTimeToLive" + ], + "Resource" : "*" + }, + { + Effect = "Allow" + Action = [ + "dynamodb:BatchGet*", + "dynamodb:DescribeStream", + "dynamodb:DescribeTable", + "dynamodb:Get*", + "dynamodb:Query", + "dynamodb:Scan", + "dynamodb:BatchWrite*", + "dynamodb:CreateTable", + "dynamodb:Delete*", + "dynamodb:Update*", + "dynamodb:PutItem" + ], + Resource = module.dynamodb_table.dynamodb_table_arn + } + ] + }) +} diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 5297caf..6ce28db 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -2,3 +2,8 @@ output "mercado_pago_secrets_read_only_policy_arn" { description = "The ARN of the Mercado Pago secrets" value = aws_iam_policy.mercado_pago_secrets_read_only_policy.arn } + +output "payments_dynamodb_table_policy_arn" { + description = "The ARN of the DynamoDB table for payments" + value = aws_iam_policy.payments_dynamodb_table_policy.arn +}