From 45905447a8706d62d64996b8a5dbe4996bf33b25 Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Wed, 11 Dec 2024 23:16:53 -0300 Subject: [PATCH] Correctly query "stray" payments from MercadoPago that we didn't receive notifications for it --- .../utils/UpdateMercadoPagoPaymentsTask.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/src/main/kotlin/net/perfectdreams/perfectpayments/backend/utils/UpdateMercadoPagoPaymentsTask.kt b/backend/src/main/kotlin/net/perfectdreams/perfectpayments/backend/utils/UpdateMercadoPagoPaymentsTask.kt index ce96cc0..78ebf70 100644 --- a/backend/src/main/kotlin/net/perfectdreams/perfectpayments/backend/utils/UpdateMercadoPagoPaymentsTask.kt +++ b/backend/src/main/kotlin/net/perfectdreams/perfectpayments/backend/utils/UpdateMercadoPagoPaymentsTask.kt @@ -11,9 +11,11 @@ import net.perfectdreams.perfectpayments.backend.dao.Payment import net.perfectdreams.perfectpayments.backend.routes.api.v1.callbacks.PostPagSeguroCallbackRoute import net.perfectdreams.perfectpayments.backend.utils.extensions.respondEmptyJson import org.jsoup.Jsoup +import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter +import java.time.temporal.ChronoUnit /** * Queries MercadoPago payments and updates their status if the payment is approved @@ -30,12 +32,27 @@ class UpdateMercadoPagoPaymentsTask(val m: PerfectPayments) : RunnableCoroutine override suspend fun run() { logger.info { "Querying MercadoPago payments manually..." } + val now = Instant.now() // This is so weird because the API that this SDK calls does not match the API reference val result = paymentClient.search( MPSearchRequest.builder() .offset(0) .limit(1000) - .filters(mapOf()) + // By default, the search API searches from the beginning of the account + // So we need to filter it ourselves! + .filters( + mapOf( + "range" to "date_created", + // 30 days + // Yes, the .toString() is a ISO8601 representation of the Instant + // And yes, we need to truncate it to SECONDS, if not MercadoPago will complain that the date is invalid + // {"message":"Params Error: Invalid date parameter","error":"bad_request","status":400,"cause":[{"code":1,"description":"Params Error","data":"12-12-2024T02:15:25UTC;71d0f6f7-0a81-42f8-ae93-e789d31e4986"}]} + "begin_date" to now.minusSeconds(86_400 * 30).truncatedTo(ChronoUnit.SECONDS).toString(), + "end_date" to now.truncatedTo(ChronoUnit.SECONDS).toString(), + "sort" to "date_created", + "criteria" to "desc" + ) + ) .build() ) @@ -59,7 +76,7 @@ class UpdateMercadoPagoPaymentsTask(val m: PerfectPayments) : RunnableCoroutine val paymentStatus = MercadoPagoUtils.getPaymentStatusFromMercadoPagoPaymentStatus(status) - logger.info { "MercadoPago payment $reference status is $paymentStatus" } + logger.info { "MercadoPago payment $reference status is $status (mapped to PerfectPayments status: $paymentStatus)" } if (paymentStatus != null) { PaymentUtils.updatePaymentStatus(