Skip to content

Commit

Permalink
Correctly query "stray" payments from MercadoPago that we didn't rece…
Browse files Browse the repository at this point in the history
…ive notifications for it
  • Loading branch information
MrPowerGamerBR committed Dec 12, 2024
1 parent ded23b9 commit 4590544
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
)

Expand All @@ -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(
Expand Down

0 comments on commit 4590544

Please sign in to comment.