-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from FIAP-3SOAT-G15/feature/hello-payments-api
hellp payments api
- Loading branch information
Showing
106 changed files
with
1,237 additions
and
1,795 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,39 @@ | ||
version: '3.7' | ||
|
||
services: | ||
#api: | ||
# image: stock-api:latest | ||
# container_name: app | ||
# build: | ||
# context: . | ||
# dockerfile: Dockerfile | ||
# depends_on: | ||
# - stock_db | ||
# environment: | ||
# - DB_ENDPOINT=db:5432 | ||
# - DB_NAME=selforder | ||
# - DB_USERNAME=selforder | ||
# - DB_PASSWORD=self@Order123! | ||
# - ADMIN_ACCESS_TOKEN=token | ||
# - MOCK_PAYMENT_PROVIDER=true | ||
# - MP_TOKEN=token | ||
# - MP_USER_ID=userId | ||
# - MP_POS_ID=postId | ||
# - MP_WEBHOOK_BASE_URL=webhookBaseUrl | ||
# ports: | ||
# - "8080:8080" | ||
# restart: always | ||
# api: | ||
# image: tech-challenge:latest | ||
# container_name: app | ||
# build: | ||
# context: . | ||
# dockerfile: Dockerfile | ||
# depends_on: | ||
# - db | ||
# environment: | ||
# - DB_ENDPOINT=db:5432 | ||
# - DB_NAME=selforder | ||
# - DB_USERNAME=selforder | ||
# - DB_PASSWORD=self@Order123! | ||
# - ADMIN_ACCESS_TOKEN=token | ||
# - MOCK_PAYMENT_PROVIDER=true | ||
# - MP_TOKEN=token | ||
# - MP_USER_ID=userId | ||
# - MP_POS_ID=postId | ||
# - MP_WEBHOOK_BASE_URL=webhookBaseUrl | ||
# ports: | ||
# - "8080:8080" | ||
# restart: always | ||
|
||
stock_db: | ||
image: postgres:15.4 | ||
container_name: stock_db | ||
volumes: | ||
- stock_db:/var/lib/postgresql/data | ||
environment: | ||
- POSTGRES_DB=stock | ||
- POSTGRES_USER=selforder | ||
- POSTGRES_PASSWORD=self@Order123! | ||
payment_db: | ||
image: amazon/dynamodb-local:latest | ||
ports: | ||
- "5432:5432" | ||
restart: always | ||
- "54000:8000" | ||
environment: | ||
AWS_ACCESS_KEY_ID: "fakekey" | ||
AWS_SECRET_ACCESS_KEY: "fakeaccesskey" | ||
AWS_REGION: us-east-2 | ||
command: ["-D\"java.library.path\"=./DynamoDBLocal_lib", "-jar", "DynamoDBLocal.jar", "-inMemory", "-sharedDb"] | ||
|
||
volumes: | ||
stock_db: | ||
db: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/kotlin/com/fiap/payments/adapter/controller/PaymentController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.fiap.payments.adapter.controller | ||
|
||
import com.fiap.payments.domain.entities.Payment | ||
import com.fiap.payments.driver.web.PaymentAPI | ||
import com.fiap.payments.usecases.LoadPaymentUseCase | ||
import com.fiap.payments.usecases.SyncPaymentUseCase | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
class PaymentController( | ||
private val loadPaymentUseCase: LoadPaymentUseCase, | ||
private val syncPaymentUseCase: SyncPaymentUseCase | ||
) : PaymentAPI { | ||
private val log = LoggerFactory.getLogger(javaClass) | ||
|
||
override fun findAll(): ResponseEntity<List<Payment>> { | ||
return ResponseEntity.ok(loadPaymentUseCase.findAll()) | ||
} | ||
|
||
override fun getByOrderNumber(orderNumber: Long): ResponseEntity<Payment> { | ||
return ResponseEntity.ok(loadPaymentUseCase.getByOrderNumber(orderNumber)) | ||
} | ||
|
||
/** | ||
* The server response is important to flag the provider for retries | ||
*/ | ||
override fun notify(orderNumber: Long, resourceId: String, topic: String): ResponseEntity<Any> { | ||
// TODO: verify x-signature header by Mercado Pago | ||
log.info("Notification received for order ${orderNumber}: type=${topic} externalId=${resourceId}") | ||
|
||
when (topic) { | ||
IPNType.MERCHANT_ORDER.ipnType -> { | ||
syncPaymentUseCase.syncPayment(orderNumber, resourceId) | ||
return ResponseEntity.ok().build() | ||
} | ||
IPNType.PAYMENT.ipnType -> { | ||
val payment = loadPaymentUseCase.getByOrderNumber(orderNumber) | ||
payment.externalOrderGlobalId?.let { | ||
syncPaymentUseCase.syncPayment(orderNumber, it) | ||
return ResponseEntity.ok().build() | ||
} | ||
// returns server error because external order global ID was not previously saved, | ||
// which does not conform with the usual application flow | ||
return ResponseEntity.internalServerError().build() | ||
} | ||
else -> { | ||
// returns bad request because application does not accept this kind of IPN types | ||
return ResponseEntity.badRequest().build() | ||
} | ||
} | ||
} | ||
|
||
enum class IPNType(val ipnType: String) { | ||
MERCHANT_ORDER("merchant_order"), | ||
PAYMENT("payment"), | ||
CHARGEBACK("chargebacks"), | ||
POINT_INTEGRATION_IPN("point_integration_ipn"), | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
...nfiguration/ControllerExceptionHandler.kt → ...nfiguration/ControllerExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/com/fiap/payments/adapter/controller/configuration/ServiceConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.fiap.payments.adapter.controller.configuration | ||
|
||
import com.fiap.payments.PaymentsApp | ||
import com.fiap.payments.adapter.gateway.PaymentGateway | ||
import com.fiap.payments.adapter.gateway.PaymentProviderGateway | ||
import com.fiap.payments.usecases.LoadPaymentUseCase | ||
import com.fiap.payments.usecases.services.PaymentSyncService | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.ComponentScan | ||
import org.springframework.context.annotation.Configuration | ||
import services.PaymentService | ||
|
||
@Configuration | ||
@ComponentScan(basePackageClasses = [PaymentsApp::class]) | ||
class ServiceConfig { | ||
|
||
|
||
@Bean | ||
fun createPaymentService( | ||
paymentRepository: PaymentGateway, | ||
paymentProvider: PaymentProviderGateway, | ||
): PaymentService { | ||
return PaymentService( | ||
paymentRepository, | ||
paymentProvider | ||
) | ||
} | ||
|
||
@Bean | ||
fun paymentSyncService( | ||
loadPaymentUseCase: LoadPaymentUseCase, | ||
paymentGateway: PaymentGateway, | ||
paymentProvider: PaymentProviderGateway, | ||
): PaymentSyncService { | ||
return PaymentSyncService( | ||
loadPaymentUseCase, | ||
paymentGateway, | ||
paymentProvider, | ||
) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/fiap/payments/adapter/gateway/PaymentGateway.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fiap.payments.adapter.gateway | ||
|
||
import com.fiap.payments.domain.entities.Payment | ||
|
||
interface PaymentGateway { | ||
fun findByOrderNumber(orderNumber: Long): Payment? | ||
|
||
fun findAll(): List<Payment> | ||
|
||
fun create(payment: Payment): Payment | ||
|
||
fun update(payment: Payment): Payment | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/fiap/payments/adapter/gateway/PaymentProviderGateway.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.fiap.payments.adapter.gateway | ||
|
||
import com.fiap.payments.domain.entities.Order | ||
import com.fiap.payments.domain.entities.PaymentRequest | ||
import com.fiap.payments.domain.valueobjects.PaymentStatus | ||
|
||
|
||
interface PaymentProviderGateway { | ||
fun createExternalOrder(order: Order): PaymentRequest | ||
|
||
fun checkExternalOrderStatus(externalOrderGlobalId: String): PaymentStatus | ||
} |
2 changes: 1 addition & 1 deletion
2
...k/adapter/gateway/TransactionalGateway.kt → ...s/adapter/gateway/TransactionalGateway.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.