Skip to content

Instantly share code, notes, and snippets.

@fanjavaid
Last active September 5, 2018 18:31
Show Gist options
  • Save fanjavaid/46f2ac56c41bd4a3e65078a42d5aee05 to your computer and use it in GitHub Desktop.
Save fanjavaid/46f2ac56c41bd4a3e65078a42d5aee05 to your computer and use it in GitHub Desktop.
Update Transaction Payment Status
class TransactionService {
fun updateStatus(transactionId: Long, type: String, status: String): Boolean {
// Get Transaction by ID
val transaction = transactionRepository.findById(transactionId)
// If record exists
if (transaction.isPresent) {
transaction.get().apply {
// Check what type of status do you want to update
if (type == "payment") {
this.paymentStatus = status
// Update commision for uplines
if (status == PAID) {
// Recursively get uplines and give them commision
getUplinesAndUpdateCommision(this.member ? .id, transactionId)
}
} else if (type == "shipping") {
this.shippingStatus = status
}
}
transactionRepository.save(transaction.get())
return true
}
return false
}
fun getUplinesAndUpdateCommision(memberId: Long?, transactionId: Long) {
// Get member data
val member = accountService.getAccountInfo(memberId ?: 0)
// If member upline NULL or upline is self, return!
if (null == member?.upline || member.upline?.id == memberId) return
// Update Commision for specific upline with passed transaction ID
updateCommision(member.upline?.id, transactionId)
// Log
println("Member ${member.id}, Upline ${member.upline?.id}")
// Recursive call, until doesn't have upline
getUplinesAndUpdateCommision(member.upline?.id, transactionId)
}
// Update Commisions for upline...
fun updateCommision(uplineId: Long?, transactionId: Long) {
// get transaction data
val transaction = transactionRepository.getOne(transactionId)
// get upline data
val upline = uplineId?.let { accountService.getAccountInfo(it) }
upline?.let {
// get commision rate data
val rate = commisionService.getCurrentRate()?.rate ?: 0
// insert into commision transaction with current rate
val commisionTransaction = CommisionTransaction().apply {
commision = rate.toLong()
currentRate = rate
fromTransaction = transaction
toMember = it
}
// commit
val result = commisionService.addCommision(commisionTransaction)
transaction.commision = result
transactionRepository.save(transaction)
Unit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment