diff --git a/src/main/kotlin/dev/solsynth/snConnect/commands/SnCommand.kt b/src/main/kotlin/dev/solsynth/snConnect/commands/SnCommand.kt index 506fd5f..b7977b5 100644 --- a/src/main/kotlin/dev/solsynth/snConnect/commands/SnCommand.kt +++ b/src/main/kotlin/dev/solsynth/snConnect/commands/SnCommand.kt @@ -2,9 +2,11 @@ package dev.solsynth.snConnect.commands import dev.solsynth.snConnect.services.SnOrderService import dev.solsynth.snConnect.services.SnService +import dev.solsynth.snConnect.services.SnTransactionService import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.TextComponent import net.milkbowl.vault.economy.Economy +import net.milkbowl.vault.economy.EconomyResponse import org.bukkit.ChatColor import org.bukkit.Sound import org.bukkit.command.Command @@ -44,8 +46,9 @@ class SnCommand(private val sn: SnService, private val eco: Economy?) : CommandE eco?.depositPlayer(p0.player, bal) val doneComponent = TextComponent(ChatColor.GREEN.toString() + "Done!") - val moneyComponent = TextComponent(ChatColor.GOLD.toString()+ChatColor.BOLD + " $bal$") - val suffixComponent = TextComponent(ChatColor.GREEN.toString() +" has been added to your balance.") + val moneyComponent = TextComponent(ChatColor.GOLD.toString() + ChatColor.BOLD + " $bal$") + val suffixComponent = + TextComponent(ChatColor.GREEN.toString() + " has been added to your balance.") p0.playSound(p0.player!!, Sound.BLOCK_ANVIL_PLACE, 1.0F, 1.0F) p0.spigot().sendMessage(doneComponent, moneyComponent, suffixComponent) @@ -81,6 +84,48 @@ class SnCommand(private val sn: SnService, private val eco: Economy?) : CommandE p0.spigot().sendMessage(afterPaidComponent); } + "withdraw" -> { + if (p3.size < 2) { + p0.sendMessage(ChatColor.RED.toString() + "You need to specific an amount to deposit.") + return true; + } + + val amount = p3[1].toDoubleOrNull(); + if (amount == null) { + p0.sendMessage(ChatColor.RED.toString() + "You need to specific an amount of number to deposit.") + return true; + } + + if (p3.size < 3) { + p0.sendMessage(ChatColor.RED.toString() + "You need to specific a wallet account to deposit.") + return true; + } + + val walletId = p3[2].toLongOrNull(); + if (walletId == null) { + p0.sendMessage(ChatColor.RED.toString() + "You need to specific a wallet account to deposit.") + return true; + } + + p0.sendMessage(ChatColor.GRAY.toString() + "Making transaction, please stand by..."); + + val bal = amount / 100; + val resp = eco?.withdrawPlayer(p0.player, "Withdraw to Source Point - $bal SRC", amount); + if (resp?.type != EconomyResponse.ResponseType.SUCCESS) { + p0.sendMessage(ChatColor.RED.toString() + "Your in game account has no enough money for that.") + return true; + } + + val transactionSrv = SnTransactionService(sn); + val transaction = transactionSrv.makeTransaction(bal, "Withdraw from Highland MC", walletId); + + val transactionHintComponent = + TextComponent(ChatColor.GREEN.toString() + "Done! transaction number " + ChatColor.WHITE + ChatColor.BOLD + "#${transaction.id}") + + p0.playSound(p0.player!!, Sound.BLOCK_ANVIL_PLACE, 1.0F, 1.0F) + p0.spigot().sendMessage(transactionHintComponent) + } + else -> { return false } diff --git a/src/main/kotlin/dev/solsynth/snConnect/services/SnOrderService.kt b/src/main/kotlin/dev/solsynth/snConnect/services/SnOrderService.kt index eef3b51..82ec0e8 100644 --- a/src/main/kotlin/dev/solsynth/snConnect/services/SnOrderService.kt +++ b/src/main/kotlin/dev/solsynth/snConnect/services/SnOrderService.kt @@ -10,7 +10,7 @@ import okhttp3.RequestBody.Companion.toRequestBody import java.io.IOException @Serializable -class SnOrderRequest( +data class SnOrderRequest( @SerialName("client_id") val clientId: String, @SerialName("client_secret") diff --git a/src/main/kotlin/dev/solsynth/snConnect/services/SnTransactionService.kt b/src/main/kotlin/dev/solsynth/snConnect/services/SnTransactionService.kt new file mode 100644 index 0000000..4fac6c5 --- /dev/null +++ b/src/main/kotlin/dev/solsynth/snConnect/services/SnTransactionService.kt @@ -0,0 +1,83 @@ +package dev.solsynth.snConnect.services + +import kotlinx.datetime.Instant +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import java.io.IOException + +@Serializable +data class SnTransaction ( + val id: Long, + @SerialName("created_at") + val createdAt: Instant, + @SerialName("updated_at") + val updatedAt: Instant, + @SerialName("deleted_at") + val deletedAt: Instant? = null, + val remark: String, + val amount: String, + @SerialName("payer_id") + val payerID: Long? = null, + @SerialName("payee_id") + val payeeID: Long? = null +) + +@Serializable +data class SnTransactionRequest( + @SerialName("client_id") + val clientId: String, + @SerialName("client_secret") + val clientSecret: String, + @SerialName("payee_id") + val payeeID: Long? = null, + @SerialName("payer_id") + val payerID: Long? = null, + val remark: String, + val amount: Double +) + +class SnTransactionService(private val sn: SnService) { + private val json = Json { + ignoreUnknownKeys = true + } + + fun makeTransaction(amount: Double, remark: String, payeeID: Long): SnTransaction { + val body = SnTransactionRequest( + sn.clientId, + sn.clientSecret, + amount = amount, + remark = remark, + payeeID = payeeID, + ); + val request = Request.Builder() + .url(sn.getUrl("wa", "/transactions")) + .post(Json.encodeToString(body).toRequestBody("application/json".toMediaTypeOrNull())) + .build() + + sn.client.newCall(request).execute().use { response -> + val responseBody = response.body!!.string() + if (!response.isSuccessful) throw IOException("Unexpected code $response - $responseBody") + val out = json.decodeFromString(responseBody) + + return out + } + } + + fun getTransaction(id: Long): SnTransaction { + val request = Request.Builder() + .url(sn.getUrl("wa", "/transactions/$id")) + .get() + .build() + sn.client.newCall(request).execute().use { response -> + if (!response.isSuccessful) throw IOException("Unexpected code $response") + val responseBody = response.body!!.string() + val out = json.decodeFromString(responseBody) + + return out + } + } +} \ No newline at end of file