Search in sources :

Example 1 with PurchaseIntentResult

use of com.huawei.hms.iap.entity.PurchaseIntentResult in project Osmand by osmandapp.

the class InAppPurchaseHelperImpl method getPurchaseSubscriptionCommand.

@Override
protected InAppCommand getPurchaseSubscriptionCommand(final WeakReference<Activity> activity, String sku, String userInfo) throws UnsupportedOperationException {
    return new InAppCommand() {

        @Override
        public void run(InAppPurchaseHelper helper) {
            try {
                Activity a = activity.get();
                ProductInfo productInfo = getProductInfo(sku);
                if (AndroidUtils.isActivityNotDestroyed(a) && productInfo != null) {
                    IapRequestHelper.createPurchaseIntent(getIapClient(), sku, IapClient.PriceType.IN_APP_SUBSCRIPTION, userInfo, new IapApiCallback<PurchaseIntentResult>() {

                        @Override
                        public void onSuccess(PurchaseIntentResult result) {
                            if (result == null) {
                                logError("GetBuyIntentResult is null");
                            } else {
                                Activity a = activity.get();
                                if (AndroidUtils.isActivityNotDestroyed(a)) {
                                    IapRequestHelper.startResolutionForResult(a, result.getStatus(), Constants.REQ_CODE_BUY_SUB);
                                } else {
                                    logError("startResolutionForResult on destroyed activity");
                                }
                            }
                            commandDone();
                        }

                        @Override
                        public void onFail(Exception e) {
                            int errorCode = ExceptionHandle.handle(activity.get(), e);
                            if (ExceptionHandle.SOLVED != errorCode) {
                                logError("createPurchaseIntent, returnCode: " + errorCode);
                                if (OrderStatusCode.ORDER_PRODUCT_OWNED == errorCode) {
                                    logError("already own this product");
                                } else {
                                    logError("unknown error");
                                }
                            }
                            commandDone();
                        }
                    });
                } else {
                    stop(true);
                }
            } catch (Exception e) {
                logError("launchPurchaseFlow Error", e);
                stop(true);
            }
        }
    };
}
Also used : ProductInfo(com.huawei.hms.iap.entity.ProductInfo) PurchaseIntentResult(com.huawei.hms.iap.entity.PurchaseIntentResult) Activity(android.app.Activity) ParseException(java.text.ParseException)

Example 2 with PurchaseIntentResult

use of com.huawei.hms.iap.entity.PurchaseIntentResult in project Osmand by osmandapp.

the class IapRequestHelper method createPurchaseIntent.

public static void createPurchaseIntent(final IapClient iapClient, String productId, int type, String payload, final IapApiCallback callback) {
    Log.i(TAG, "call createPurchaseIntent");
    PurchaseIntentReq req = createPurchaseIntentReq(type, productId);
    req.setDeveloperPayload(payload);
    Task<PurchaseIntentResult> task = iapClient.createPurchaseIntent(req);
    task.addOnSuccessListener(new OnSuccessListener<PurchaseIntentResult>() {

        @Override
        public void onSuccess(PurchaseIntentResult result) {
            Log.i(TAG, "createPurchaseIntent, success");
            callback.onSuccess(result);
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(Exception e) {
            Log.e(TAG, "createPurchaseIntent, fail");
            callback.onFail(e);
        }
    });
}
Also used : PurchaseIntentResult(com.huawei.hms.iap.entity.PurchaseIntentResult) OnSuccessListener(com.huawei.hmf.tasks.OnSuccessListener) PurchaseIntentReq(com.huawei.hms.iap.entity.PurchaseIntentReq) OnFailureListener(com.huawei.hmf.tasks.OnFailureListener) IapApiException(com.huawei.hms.iap.IapApiException)

Aggregations

PurchaseIntentResult (com.huawei.hms.iap.entity.PurchaseIntentResult)2 Activity (android.app.Activity)1 OnFailureListener (com.huawei.hmf.tasks.OnFailureListener)1 OnSuccessListener (com.huawei.hmf.tasks.OnSuccessListener)1 IapApiException (com.huawei.hms.iap.IapApiException)1 ProductInfo (com.huawei.hms.iap.entity.ProductInfo)1 PurchaseIntentReq (com.huawei.hms.iap.entity.PurchaseIntentReq)1 ParseException (java.text.ParseException)1