forked from thijooree/android
add support to detect dhiraagu and ooredoo packages
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package sh.sar.basedbank.api.fahipay
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.json.JSONObject
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class OoredooClient {
|
||||
|
||||
private val client = OkHttpClient.Builder()
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.readTimeout(15, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
enum class CustType { PRE, POST, HYBRID, UNSUPPORTED }
|
||||
|
||||
/**
|
||||
* Validates an Ooredoo number and returns which services are supported.
|
||||
* @param number 7-digit Maldivian phone number (without country code)
|
||||
*/
|
||||
fun validateNumber(number: String): CustType {
|
||||
val resp = client.newCall(
|
||||
Request.Builder()
|
||||
.url("https://www.ooredoo.mv/ooredoo-prod/QuickPayPackage/v1/numberTypeValidation?action=cust_details&msisdn=960$number")
|
||||
.get()
|
||||
.build()
|
||||
).execute()
|
||||
val json = resp.body?.string() ?: return CustType.UNSUPPORTED
|
||||
resp.close()
|
||||
return try {
|
||||
val custType = JSONObject(json)
|
||||
.optJSONObject("data")
|
||||
?.optString("custType")
|
||||
?.takeIf { it.isNotBlank() && it != "null" }
|
||||
when (custType) {
|
||||
"PRE" -> CustType.PRE
|
||||
"POST" -> CustType.POST
|
||||
"HYBRID" -> CustType.HYBRID
|
||||
else -> CustType.UNSUPPORTED
|
||||
}
|
||||
} catch (_: Exception) { CustType.UNSUPPORTED }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user