Created
May 5, 2020 18:43
-
-
Save xaelkaz/f11278d9dbcd22a282560c29817a997f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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