use of com.huawei.hms.iap.entity.PurchaseResultInfo in project Osmand by osmandapp.
the class SubscriptionUtils method getPurchaseResult.
/**
* Parse PurchaseResult data from intent
*
* @param activity Activity
* @param data the intent from onActivityResult
* @return PurchaseResultInfo
*/
public static PurchaseResultInfo getPurchaseResult(Activity activity, Intent data) {
PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(activity).parsePurchaseResultInfoFromIntent(data);
if (null == purchaseResultInfo) {
Log.e(TAG, "PurchaseResultInfo is null");
} else {
int returnCode = purchaseResultInfo.getReturnCode();
String errMsg = purchaseResultInfo.getErrMsg();
switch(returnCode) {
case OrderStatusCode.ORDER_PRODUCT_OWNED:
Log.w(TAG, "you have owned this product");
break;
case OrderStatusCode.ORDER_STATE_SUCCESS:
boolean credible = CipherUtil.doCheck(purchaseResultInfo.getInAppPurchaseData(), purchaseResultInfo.getInAppDataSignature(), CipherUtil.getPublicKey());
if (credible) {
try {
InAppPurchaseData inAppPurchaseData = new InAppPurchaseData(purchaseResultInfo.getInAppPurchaseData());
if (!inAppPurchaseData.isSubValid()) {
return getFailedPurchaseResultInfo();
}
} catch (JSONException e) {
Log.e(TAG, "parse InAppPurchaseData JSONException", e);
return getFailedPurchaseResultInfo();
}
} else {
Log.e(TAG, "check the data signature fail");
return getFailedPurchaseResultInfo();
}
default:
Log.e(TAG, "returnCode: " + returnCode + " , errMsg: " + errMsg);
break;
}
}
return purchaseResultInfo;
}
use of com.huawei.hms.iap.entity.PurchaseResultInfo in project Osmand by osmandapp.
the class InAppPurchaseHelperImpl method onActivityResult.
@Override
public boolean onActivityResult(@NonNull Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQ_CODE_BUY_SUB) {
boolean succeed = false;
if (resultCode == Activity.RESULT_OK) {
PurchaseResultInfo result = SubscriptionUtils.getPurchaseResult(activity, data);
if (result != null) {
switch(result.getReturnCode()) {
case OrderStatusCode.ORDER_STATE_CANCEL:
logDebug("Purchase cancelled");
break;
case OrderStatusCode.ORDER_STATE_FAILED:
inventoryRequestPending = true;
logDebug("Purchase failed");
break;
case OrderStatusCode.ORDER_PRODUCT_OWNED:
inventoryRequestPending = true;
logDebug("Product already owned");
break;
case OrderStatusCode.ORDER_STATE_SUCCESS:
inventoryRequestPending = true;
InAppPurchaseData purchaseData = SubscriptionUtils.getInAppPurchaseData(null, result.getInAppPurchaseData(), result.getInAppDataSignature());
if (purchaseData != null) {
onPurchaseFinished(purchaseData);
succeed = true;
} else {
logDebug("Purchase failed");
}
break;
default:
break;
}
} else {
logDebug("Purchase failed");
}
} else {
logDebug("Purchase cancelled");
}
if (!succeed) {
stop(true);
}
return true;
} else if (requestCode == Constants.REQ_CODE_BUY_INAPP) {
boolean succeed = false;
if (data == null) {
logDebug("data is null");
} else {
PurchaseResultInfo buyResultInfo = Iap.getIapClient(activity).parsePurchaseResultInfoFromIntent(data);
switch(buyResultInfo.getReturnCode()) {
case OrderStatusCode.ORDER_STATE_CANCEL:
logDebug("Order has been canceled");
break;
case OrderStatusCode.ORDER_STATE_FAILED:
inventoryRequestPending = true;
logDebug("Order has been failed");
break;
case OrderStatusCode.ORDER_PRODUCT_OWNED:
inventoryRequestPending = true;
logDebug("Product already owned");
break;
case OrderStatusCode.ORDER_STATE_SUCCESS:
InAppPurchaseData purchaseData = InAppUtils.getInAppPurchaseData(null, buyResultInfo.getInAppPurchaseData(), buyResultInfo.getInAppDataSignature());
if (purchaseData != null) {
onPurchaseFinished(purchaseData);
succeed = true;
} else {
logDebug("Purchase failed");
}
break;
default:
break;
}
}
if (!succeed) {
stop(true);
}
return true;
}
return false;
}
use of com.huawei.hms.iap.entity.PurchaseResultInfo in project Osmand by osmandapp.
the class SubscriptionUtils method getFailedPurchaseResultInfo.
private static PurchaseResultInfo getFailedPurchaseResultInfo() {
PurchaseResultInfo info = new PurchaseResultInfo();
info.setReturnCode(OrderStatusCode.ORDER_STATE_FAILED);
return info;
}
Aggregations