我正在将android-inapp-billing-v3用于我的由kotlin编写的android应用程序。现在可以进行购买了,付款完成后可以转到成功的页面。 但是,当同一用户第二次购买时,该应用程序直接进入成功页面,而无需任何付款和购买过程。 我想让应用程序每次用户单击“付款”按钮时都要求付款。请帮助解决此问题。
class ProductActivity : AppCompatActivity(), BillingProcessor.IBillingHandler {
var ProductID = ""
var ProductName = ""
var ProductDes = ""
var ProductImage = 0
var ProductPrice = ""
val googleKey = "fdsfdsfsddfdsfsfdfs"
lateinit private var bp: BillingProcessor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product)
val bundle: Bundle = intent.extras
ProductID = bundle.getString("ProductID")
ProductName = bundle.getString("ProductName")
ProductDes = bundle.getString("ProductDes")
ProductImage = bundle.getInt("ProductImage")
ProductPrice = bundle.getString("ProductPrice")
bp = BillingProcessor(this, googleKey, this)
bp.consumePurchase(ProductID)
btn_pay.setOnClickListener {
bp.purchase(this, ProductID)
}
}
override fun onResume() {
super.onResume()
val internetStatus:Boolean = ConnectivityUtils.isConnected(this)
if (internetStatus==true) {
}
else {
val msg = getString(R.string.need_internet_for_service)
finish()
}
}
override fun onBillingInitialized() {
}
override fun onPurchaseHistoryRestored() {
}
override fun onProductPurchased(productId: String, details: TransactionDetails?) {
val intent = Intent(this, SearchQponActivity::class.java)
intent.putExtra("ProductID", ProductID)
startActivity(intent)
}
override fun onBillingError(errorCode: Int, error: Throwable?) {
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (!bp.handleActivityResult(requestCode, resultCode, data))
super.onActivityResult(requestCode, resultCode, data)
}
override fun onDestroy() {
if (bp!=null)
bp.release()
super.onDestroy()
}
}
答案 0 :(得分:0)
好吧,我正在为当前应用程序使用该库,并且使用了警报对话框来确认购买或订阅。
此库有很多错误(请确保打开issues页面)。
我在这个库中遇到了类似的问题,并以警告对话框和一个布尔值isConfirmedPurchasing
结束了它。
我认为,这可能是由于以下原因造成的:
onCreate
中消费已购买的物品,这似乎不是在每次活动开始时都消费物品的正确方法onProductPurchased
,如果是,则此分数内的代码将运行。提示:
onProductPurchased
bp
是否为空,您可以将其创建为Late-init,否则使其可以为空activity
或shared preferences
范围内fragment
中使用此库我创建了一个测试应用程序,并在beta测试中发布了该程序,然后我重构了您的代码,并用来检查它的工作方式(使用我的键和id ofc)。
我想这就是您想要的:
您产品的数据类别:
data class MyProduct(
val id: String,
val mame: String,
val des: String,
val image: Int,
val price: String
)
活动:
class ProductActivity : AppCompatActivity(), BillingProcessor.IBillingHandler {
private val googleKey = "your_google_key"
private val keyProductId = "productID"
private var isConfirmedPurchasing = false
private lateinit var bp: BillingProcessor
private lateinit var myProduct: MyProduct
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product)
bp = BillingProcessor(this, googleKey, this)
intent.extras?.let { bundle ->
with (bundle) {
myProduct = MyProduct(
getString("productID"),
getString("productName"),
getString("productDes"),
getInt("productImage"),
getString("productPrice")
)
}
}
btn_pay.setOnClickListener {
createAlertDialog(this) {
isConfirmedPurchasing = true
purchase(myProduct.id)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onDestroy() {
bp.release()
super.onDestroy()
}
override fun onProductPurchased(productId: String, details: TransactionDetails?) {
isConfirmedPurchasing = false
bp.consumePurchase(myProduct.id)
finishPurchaseWorkflow(productId)
}
override fun onBillingInitialized() {
Log.d("Tag", "BillingProcessor was initialized and it's ready to purchase")
bp.consumePurchase(myProduct.id)
}
override fun onPurchaseHistoryRestored() {
Log.d("Tag", "requested PRODUCT ID was successfully purchased")
}
override fun onBillingError(errorCode: Int, error: Throwable?) {
Log.e("Tag", "errorCode: $errorCode, error: $error")
isConfirmedPurchasing = false
}
private fun purchase(id: String) {
if (isConfirmedPurchasing) {
bp.purchase(this, id)
}
}
private fun finishPurchaseWorkflow(googleProductId: String) {
val intent = Intent(this, SearchQponActivity::class.java)
intent.putExtra(keyProductId, googleProductId)
startActivity(intent)
}
private fun createAlertDialog(
context: Context,
title: String? = "Are you sure?",
message: String? = null,
buttonPos: String = "Yes",
buttonNeg: String = "No",
action: () -> Unit
): AlertDialog {
return AlertDialog.Builder(context)
.setTitle(title)
.setMessage(message)
.setNegativeButton(buttonNeg) { dialog, _ -> dialog.cancel() }
.setPositiveButton(buttonPos) { dialog, _ -> action(); dialog.cancel() }
.show()
}
}