|
@@ -1,14 +1,10 @@
|
|
package cn.yyxx.eyuancomm.impl.channel.huawei
|
|
package cn.yyxx.eyuancomm.impl.channel.huawei
|
|
|
|
|
|
import android.app.Activity
|
|
import android.app.Activity
|
|
-import android.content.Context
|
|
|
|
import android.content.Intent
|
|
import android.content.Intent
|
|
import android.content.IntentSender
|
|
import android.content.IntentSender
|
|
-import cn.yyxx.eyuancomm.comm.entity.ResultInfo
|
|
|
|
import cn.yyxx.eyuancomm.comm.entity.SdkChargeInfo
|
|
import cn.yyxx.eyuancomm.comm.entity.SdkChargeInfo
|
|
-import cn.yyxx.eyuancomm.comm.network.SdkRequest
|
|
|
|
import cn.yyxx.eyuancomm.comm.utils.Logger
|
|
import cn.yyxx.eyuancomm.comm.utils.Logger
|
|
-import cn.yyxx.eyuancomm.comm.widget.ScaleLoadingDialog
|
|
|
|
import cn.yyxx.support.JsonUtils
|
|
import cn.yyxx.support.JsonUtils
|
|
import com.huawei.hms.iap.Iap
|
|
import com.huawei.hms.iap.Iap
|
|
import com.huawei.hms.iap.IapApiException
|
|
import com.huawei.hms.iap.IapApiException
|
|
@@ -25,8 +21,7 @@ import org.json.JSONObject
|
|
*/
|
|
*/
|
|
internal class HuaweiInAppPay {
|
|
internal class HuaweiInAppPay {
|
|
|
|
|
|
- private var loadingDialog: ScaleLoadingDialog? = null
|
|
|
|
- private var callback: InAppPayCallback? = null
|
|
|
|
|
|
+ private lateinit var callback: InAppPayCallback
|
|
|
|
|
|
companion object {
|
|
companion object {
|
|
val instance: HuaweiInAppPay by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
|
val instance: HuaweiInAppPay by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
|
@@ -35,14 +30,12 @@ internal class HuaweiInAppPay {
|
|
}
|
|
}
|
|
|
|
|
|
fun charge(activity: Activity, chargeInfo: SdkChargeInfo, callback: InAppPayCallback) {
|
|
fun charge(activity: Activity, chargeInfo: SdkChargeInfo, callback: InAppPayCallback) {
|
|
- showLoadingDialog(activity)
|
|
|
|
this.callback = callback
|
|
this.callback = callback
|
|
Iap.getIapClient(activity).isEnvReady.addOnSuccessListener {
|
|
Iap.getIapClient(activity).isEnvReady.addOnSuccessListener {
|
|
// 获取接口请求的结果
|
|
// 获取接口请求的结果
|
|
-// obtainProductInfo(activity, chargeInfo, callback)
|
|
|
|
obtainOwnedPurchases(activity, chargeInfo)
|
|
obtainOwnedPurchases(activity, chargeInfo)
|
|
}.addOnFailureListener { e ->
|
|
}.addOnFailureListener { e ->
|
|
- dismissLoadingDialog()
|
|
|
|
|
|
+ e.log()
|
|
if (e is IapApiException) {
|
|
if (e is IapApiException) {
|
|
val status: Status = e.status
|
|
val status: Status = e.status
|
|
when (status.statusCode) {
|
|
when (status.statusCode) {
|
|
@@ -61,15 +54,18 @@ internal class HuaweiInAppPay {
|
|
OrderStatusCode.ORDER_ACCOUNT_AREA_NOT_SUPPORTED -> {
|
|
OrderStatusCode.ORDER_ACCOUNT_AREA_NOT_SUPPORTED -> {
|
|
// 用户当前登录的华为帐号所在的服务地不在华为IAP支持结算的国家/地区中
|
|
// 用户当前登录的华为帐号所在的服务地不在华为IAP支持结算的国家/地区中
|
|
Logger.e("用户当前登录的华为帐号所在的服务地不在华为IAP支持结算的国家/地区中")
|
|
Logger.e("用户当前登录的华为帐号所在的服务地不在华为IAP支持结算的国家/地区中")
|
|
|
|
+ callback.onResult(-1, "用户当前登录的华为帐号所在的服务地不在华为IAP支持结算的国家/地区中")
|
|
}
|
|
}
|
|
else -> {
|
|
else -> {
|
|
// 其他外部错误
|
|
// 其他外部错误
|
|
Logger.e("其他外部错误")
|
|
Logger.e("其他外部错误")
|
|
|
|
+ callback.onResult(-1, "其他外部错误")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
// 其他外部错误
|
|
// 其他外部错误
|
|
Logger.e("其他外部错误")
|
|
Logger.e("其他外部错误")
|
|
|
|
+ callback.onResult(-1, "其他外部错误")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -91,31 +87,27 @@ internal class HuaweiInAppPay {
|
|
// 永远保持只有一笔未消耗订单
|
|
// 永远保持只有一笔未消耗订单
|
|
val purchaseData = result.inAppPurchaseDataList[0]
|
|
val purchaseData = result.inAppPurchaseDataList[0]
|
|
val signature = result.inAppSignature[0]
|
|
val signature = result.inAppSignature[0]
|
|
- notifyOrder2Backend(activity, purchaseData, signature) { resultInfo ->
|
|
|
|
- dismissLoadingDialog()
|
|
|
|
- if (resultInfo.code == 1) {
|
|
|
|
- // 重新消耗成功,继续发起支付
|
|
|
|
- obtainProductInfo(activity, chargeInfo)
|
|
|
|
|
|
+ val jsonObject = JSONObject()
|
|
|
|
+ with(JSONObject(purchaseData)) {
|
|
|
|
+ if (JsonUtils.hasJsonKey(this, "developerPayload")) {
|
|
|
|
+ jsonObject.put("order_id", getString("developerPayload"))
|
|
} else {
|
|
} else {
|
|
- // 消耗失败
|
|
|
|
- callback?.onResult(-1, "obtainOwnedPurchases: 支付发生异常")
|
|
|
|
|
|
+ jsonObject.put("order_id", "")
|
|
}
|
|
}
|
|
|
|
+ val notifyData = JSONObject()
|
|
|
|
+ notifyData.put("purchase_data", purchaseData)
|
|
|
|
+ notifyData.put("signature", signature)
|
|
|
|
+ jsonObject.put("order_notify_data", notifyData.toString())
|
|
}
|
|
}
|
|
|
|
+ callback.onResult(1, jsonObject.toString())
|
|
}
|
|
}
|
|
}.addOnFailureListener { e ->
|
|
}.addOnFailureListener { e ->
|
|
- dismissLoadingDialog()
|
|
|
|
- callback?.onResult(-1, "obtainOwnedPurchases: 支付发生异常")
|
|
|
|
- if (e is IapApiException) {
|
|
|
|
- val apiException = e as IapApiException
|
|
|
|
- val status: Status = apiException.status
|
|
|
|
- val returnCode = apiException.statusCode
|
|
|
|
- } else {
|
|
|
|
- // 其他外部错误
|
|
|
|
- }
|
|
|
|
|
|
+ e.log()
|
|
|
|
+ callback.onResult(-1, "查询未消耗订单异常")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private fun obtainProductInfo(activity: Activity, chargeInfo: SdkChargeInfo) {
|
|
|
|
|
|
+ fun obtainProductInfo(activity: Activity, chargeInfo: SdkChargeInfo) {
|
|
val productIdList: MutableList<String> = ArrayList()
|
|
val productIdList: MutableList<String> = ArrayList()
|
|
// 查询的商品必须是您在AppGallery Connect网站配置的商品
|
|
// 查询的商品必须是您在AppGallery Connect网站配置的商品
|
|
productIdList.add(chargeInfo.productId)
|
|
productIdList.add(chargeInfo.productId)
|
|
@@ -129,15 +121,8 @@ internal class HuaweiInAppPay {
|
|
// 获取接口请求成功时返回的商品详情信息
|
|
// 获取接口请求成功时返回的商品详情信息
|
|
createPurchaseIntent(activity, chargeInfo)
|
|
createPurchaseIntent(activity, chargeInfo)
|
|
}.addOnFailureListener { e ->
|
|
}.addOnFailureListener { e ->
|
|
- dismissLoadingDialog()
|
|
|
|
- Logger.e(e.localizedMessage)
|
|
|
|
- callback?.onResult(-1, "obtainProductInfo: 支付发生异常")
|
|
|
|
- if (e is IapApiException) {
|
|
|
|
- val apiException = e as IapApiException
|
|
|
|
- val returnCode = apiException.statusCode
|
|
|
|
- } else {
|
|
|
|
- // 其他外部错误
|
|
|
|
- }
|
|
|
|
|
|
+ e.log()
|
|
|
|
+ callback.onResult(-1, "查询商品信息发生异常")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -158,39 +143,16 @@ internal class HuaweiInAppPay {
|
|
// 启动IAP返回的收银台页面
|
|
// 启动IAP返回的收银台页面
|
|
result.status.startResolutionForResult(activity, 8888)
|
|
result.status.startResolutionForResult(activity, 8888)
|
|
} catch (exp: IntentSender.SendIntentException) {
|
|
} catch (exp: IntentSender.SendIntentException) {
|
|
- callback?.onResult(-1, "支付发生异常")
|
|
|
|
|
|
+ callback.onResult(-1, "调用支付接口异常")
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- callback?.onResult(-1, "支付发生异常")
|
|
|
|
|
|
+ callback.onResult(-1, "调用支付接口异常")
|
|
}
|
|
}
|
|
}.addOnFailureListener { e ->
|
|
}.addOnFailureListener { e ->
|
|
- dismissLoadingDialog()
|
|
|
|
- Logger.d(e.localizedMessage)
|
|
|
|
- callback?.onResult(-1, "createPurchaseIntent: 支付发生异常")
|
|
|
|
- if (e is IapApiException) {
|
|
|
|
- val apiException = e as IapApiException
|
|
|
|
- val status: Status = apiException.status
|
|
|
|
- val returnCode = apiException.statusCode
|
|
|
|
- } else {
|
|
|
|
- // 其他外部错误
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private fun notifyOrder2Backend(activity: Activity, purchaseData: String, signature: String, onResponse: (ResultInfo) -> Unit) {
|
|
|
|
- val jsonObject = JSONObject()
|
|
|
|
- with(JSONObject(purchaseData)) {
|
|
|
|
- if (JsonUtils.hasJsonKey(this, "developerPayload")) {
|
|
|
|
- jsonObject.put("order_id", getString("developerPayload"))
|
|
|
|
- } else {
|
|
|
|
- jsonObject.put("order_id", "")
|
|
|
|
- }
|
|
|
|
- val notifyData = JSONObject()
|
|
|
|
- notifyData.put("purchase_data", purchaseData)
|
|
|
|
- notifyData.put("signature", signature)
|
|
|
|
- jsonObject.put("order_notify_data", notifyData.toString())
|
|
|
|
|
|
+// dismissLoadingDialog()
|
|
|
|
+ e.log()
|
|
|
|
+ callback.onResult(-1, "调用支付接口异常")
|
|
}
|
|
}
|
|
- SdkRequest.instance.notifyOrder(activity, jsonObject, onResponse)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {
|
|
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {
|
|
@@ -202,52 +164,43 @@ internal class HuaweiInAppPay {
|
|
val purchaseResultInfo = Iap.getIapClient(activity).parsePurchaseResultInfoFromIntent(data)
|
|
val purchaseResultInfo = Iap.getIapClient(activity).parsePurchaseResultInfoFromIntent(data)
|
|
when (purchaseResultInfo.returnCode) {
|
|
when (purchaseResultInfo.returnCode) {
|
|
OrderStatusCode.ORDER_STATE_CANCEL -> {
|
|
OrderStatusCode.ORDER_STATE_CANCEL -> {
|
|
- Logger.d("cancel")
|
|
|
|
- dismissLoadingDialog()
|
|
|
|
- callback?.onResult(-1, "支付发生异常")
|
|
|
|
|
|
+ callback.onResult(-1, "取消支付")
|
|
}
|
|
}
|
|
OrderStatusCode.ORDER_STATE_FAILED, OrderStatusCode.ORDER_PRODUCT_OWNED, OrderStatusCode.ORDER_STATE_DEFAULT_CODE -> {
|
|
OrderStatusCode.ORDER_STATE_FAILED, OrderStatusCode.ORDER_PRODUCT_OWNED, OrderStatusCode.ORDER_STATE_DEFAULT_CODE -> {
|
|
- dismissLoadingDialog()
|
|
|
|
- callback?.onResult(-1, "支付发生异常")
|
|
|
|
|
|
+ callback.onResult(-1, "支付发生异常")
|
|
}
|
|
}
|
|
OrderStatusCode.ORDER_STATE_SUCCESS -> {
|
|
OrderStatusCode.ORDER_STATE_SUCCESS -> {
|
|
// 支付成功
|
|
// 支付成功
|
|
|
|
+ Logger.d("支付成功")
|
|
with(purchaseResultInfo) {
|
|
with(purchaseResultInfo) {
|
|
// consumeOwnedPurchase(activity, inAppPurchaseData, inAppDataSignature)
|
|
// consumeOwnedPurchase(activity, inAppPurchaseData, inAppDataSignature)
|
|
// 通知服务端支付流程完成,验证订单并确认交易
|
|
// 通知服务端支付流程完成,验证订单并确认交易
|
|
- notifyOrder2Backend(activity, inAppPurchaseData, inAppDataSignature) { resultInfo ->
|
|
|
|
- dismissLoadingDialog()
|
|
|
|
- if (resultInfo.code == 1) {
|
|
|
|
- callback?.onResult(0, "支付流程完成")
|
|
|
|
|
|
+ val jsonObject = JSONObject()
|
|
|
|
+ with(JSONObject(inAppPurchaseData)) {
|
|
|
|
+ if (JsonUtils.hasJsonKey(this, "developerPayload")) {
|
|
|
|
+ jsonObject.put("order_id", getString("developerPayload"))
|
|
} else {
|
|
} else {
|
|
- callback?.onResult(-1, "支付发生异常")
|
|
|
|
|
|
+ jsonObject.put("order_id", "")
|
|
}
|
|
}
|
|
|
|
+ val notifyData = JSONObject()
|
|
|
|
+ notifyData.put("purchase_data", inAppPurchaseData)
|
|
|
|
+ notifyData.put("signature", inAppDataSignature)
|
|
|
|
+ jsonObject.put("order_notify_data", notifyData.toString())
|
|
}
|
|
}
|
|
|
|
+ callback.onResult(0, jsonObject.toString())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else -> {
|
|
else -> {
|
|
- dismissLoadingDialog()
|
|
|
|
- callback?.onResult(-1, "支付发生异常")
|
|
|
|
|
|
+// dismissLoadingDialog()
|
|
|
|
+ callback.onResult(-1, "解析支付结果异常")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private fun showLoadingDialog(context: Context) {
|
|
|
|
- loadingDialog?.apply {
|
|
|
|
- dismiss()
|
|
|
|
- loadingDialog = null
|
|
|
|
- }
|
|
|
|
- loadingDialog = ScaleLoadingDialog(context, "")
|
|
|
|
- loadingDialog?.show()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private fun dismissLoadingDialog() {
|
|
|
|
- loadingDialog?.apply {
|
|
|
|
- dismiss()
|
|
|
|
- loadingDialog = null
|
|
|
|
- }
|
|
|
|
|
|
+ private fun Exception.log() {
|
|
|
|
+ Logger.d("华为支付发生异常, msg: $message")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|