Skip to content

Instantly share code, notes, and snippets.

@xaelkaz
Created May 5, 2020 18:43
Show Gist options
  • Save xaelkaz/f11278d9dbcd22a282560c29817a997f to your computer and use it in GitHub Desktop.
Save xaelkaz/f11278d9dbcd22a282560c29817a997f to your computer and use it in GitHub Desktop.
override suspend fun validateAndSendCreditCorpPayment(
paymentSecureKey: String,
amountToPayValue: String,
encryptData: String,
dbRef: String,
module: Module
): Result<CreditCardPaymentDTO> {
return try {
database.withTransaction {
// Validate credit card field
val validateCredit =
safePaymentGateway.creditCorpApiGateway("Error validate credit card field with payment gateway") {
restApi.validatePayment(paymentSecureKey, "validate", encryptData)
}
// If there was an error in validate field, notify user
if (validateCredit is Result.Error) {
Result.Error(validateCredit.t)
}
if (validateCredit is Result.Success) {
// First store the payment on database
val resultStorePayment = storePayment(amountToPayValue, dbRef, module)
if (resultStorePayment is Result.Error) {
Result.Error(Exception("Error tratando de guardar el pago. Por favor intente nuevamente."))
}
if (resultStorePayment is Result.Success) {
// If the payment was stored, then notify the module to make the corresponding
val resultModule = moduleIntegration.notifyModulePaymentSaved(dbRef, module)
if (resultModule is Result.Error) {
throw CreditCardException(CreditCardError.DECLINED_CARD, Exception(""))
}
if (resultModule is Result.Success) {
safePaymentGateway.creditCorpApiGateway("Error sending credit card payment") {
restApi.sendPayment(
paymentSecureKey,
"sale",
"0",
encryptData
)
}.thenAsync {
Result.Success(it)
}.otherwiseAsync {
throw CreditCardException(
CreditCardError.DECLINED_CARD,
Exception(it.message)
)
}
}
}
} else {
Result.Error(Exception("Error tratando de guardar el pago. Por favor intente nuevamente."))
}
validateCredit
}
} catch (e: Exception) {
Timber.e(e, "Error tratando de realizar el pago. Por favor intente nuevamente.")
Result.Error(e)
} catch (e: CreditCardException) {
Timber.e(e, "Error tratando de realizar el pago. Por favor intente nuevamente.")
Result.Error(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment