-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task to keep checking if there were new MercadoPago payments that…
… the system may have failed to receive via webhooks
- Loading branch information
1 parent
c8d9acc
commit ef4bf37
Showing
4 changed files
with
96 additions
and
22 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
14 changes: 14 additions & 0 deletions
14
backend/src/main/kotlin/net/perfectdreams/perfectpayments/backend/utils/MercadoPagoUtils.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,14 @@ | ||
package net.perfectdreams.perfectpayments.backend.utils | ||
|
||
import net.perfectdreams.perfectpayments.backend.payments.PaymentStatus | ||
|
||
object MercadoPagoUtils { | ||
fun getPaymentStatusFromMercadoPagoPaymentStatus(status: String): PaymentStatus? { | ||
return when (status) { | ||
"approved" -> PaymentStatus.APPROVED | ||
"in_mediation" -> PaymentStatus.CHARGED_BACK | ||
"charged_back" -> PaymentStatus.CHARGED_BACK | ||
else -> null | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...n/kotlin/net/perfectdreams/perfectpayments/backend/utils/UpdateMercadoPagoPaymentsTask.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,73 @@ | ||
package net.perfectdreams.perfectpayments.backend.utils | ||
|
||
import com.mercadopago.client.payment.PaymentClient | ||
import com.mercadopago.net.MPSearchRequest | ||
import io.ktor.client.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import mu.KotlinLogging | ||
import net.perfectdreams.perfectpayments.backend.PerfectPayments | ||
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.LocalDateTime | ||
import java.time.ZoneId | ||
import java.time.format.DateTimeFormatter | ||
|
||
/** | ||
* Queries MercadoPago payments and updates their status if the payment is approved | ||
* | ||
* While not really needed, this is only as a fallback because "what if the webhooks fail"... | ||
*/ | ||
class UpdateMercadoPagoPaymentsTask(val m: PerfectPayments) : RunnableCoroutine { | ||
companion object { | ||
private val logger = KotlinLogging.logger {} | ||
} | ||
|
||
private val paymentClient = PaymentClient() | ||
|
||
override suspend fun run() { | ||
logger.info { "Querying MercadoPago payments manually..." } | ||
|
||
// 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()) | ||
.build() | ||
) | ||
|
||
result.results.forEach { | ||
val status = it.status | ||
val reference = it.externalReference | ||
|
||
// This code is from PostMercadoPagoCallbackRoute | ||
logger.info { "MercadoPago payment $reference status is $status" } | ||
|
||
val internalTransactionId = reference.split("-").last() | ||
|
||
val internalPayment = m.newSuspendedTransaction { | ||
Payment.findById(internalTransactionId.toLong()) | ||
} | ||
|
||
if (internalPayment == null) { | ||
logger.warn { "MercadoPago Payment with Reference ID: $reference ($internalTransactionId) doesn't have a matching internal ID! Bug?" } | ||
return | ||
} | ||
|
||
val paymentStatus = MercadoPagoUtils.getPaymentStatusFromMercadoPagoPaymentStatus(status) | ||
|
||
logger.info { "MercadoPago payment $reference status is $paymentStatus" } | ||
|
||
if (paymentStatus != null) { | ||
PaymentUtils.updatePaymentStatus( | ||
m, | ||
internalPayment, | ||
paymentStatus | ||
) | ||
} | ||
} | ||
} | ||
} |