11/*
2- Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
2+ Copyright 2020-2021 . Huawei Technologies Co., Ltd. All rights reserved.
33
44 Licensed under the Apache License, Version 2.0 (the "License")
55 you may not use this file except in compliance with the License.
1717
1818import android .app .Activity ;
1919import android .content .Intent ;
20+ import android .content .IntentSender ;
2021import android .util .Log ;
2122
2223import androidx .annotation .NonNull ;
4546import com .huawei .hms .iap .entity .PurchaseResultInfo ;
4647import com .huawei .hms .iap .entity .StartIapActivityReq ;
4748import com .huawei .hms .iap .entity .StartIapActivityResult ;
49+ import com .huawei .hms .iap .util .IapClientHelper ;
4850import com .huawei .hms .rn .iap .client .Helper ;
4951import com .huawei .hms .rn .iap .client .utils .Constants ;
5052import com .huawei .hms .rn .iap .client .utils .ExceptionHandler ;
5153import com .huawei .hms .rn .iap .client .utils .DataUtils ;
54+ import com .huawei .hms .rn .iap .client .utils .MapUtil ;
5255import com .huawei .hms .rn .iap .client .viewmodel .Service ;
5356import com .huawei .hms .rn .iap .client .viewmodel .ViewModel ;
57+ import com .huawei .hms .support .api .client .Status ;
5458
55- import java .util .Map ;
59+ import org .json .JSONException ;
60+ import org .json .JSONObject ;
5661
62+ import java .util .Map ;
63+ import java .util .Objects ;
5764import static com .huawei .hms .rn .iap .client .Helper .toIAPObject ;
5865import static com .huawei .hms .rn .iap .client .utils .Constants .CONSTANTS ;
5966import static com .huawei .hms .rn .iap .client .utils .Constants .REQ_CODE_PURCHASE_INTENT ;
67+ import static com .huawei .hms .rn .iap .client .utils .DataUtils .getMapFromIsEnvReadyResult ;
6068import static com .huawei .hms .rn .iap .client .utils .MapUtil .createWritableMapWithSuccessStatus ;
6169import static com .huawei .hms .rn .iap .client .utils .MapUtil .getKeyByValue ;
6270import static com .huawei .hms .rn .iap .client .utils .MapUtil .toWritableMap ;
6674 *
6775 * @since v.5.0.0
6876 */
69- public class HmsIapModule extends ReactContextBaseJavaModule implements Service .View {
77+ public class HMSIapModule extends ReactContextBaseJavaModule implements Service .View {
7078
71- private final String TAG = HmsIapModule .class .getSimpleName ();
79+ private final String TAG = HMSIapModule .class .getSimpleName ();
7280 private IapClient iapClient ;
7381 private Service .Presenter viewModel ;
7482 private Promise mPickerPromise ;
@@ -87,36 +95,24 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
8795 PurchaseResultInfo purchaseIntentResult = Iap .getIapClient (getActivity ())
8896 .parsePurchaseResultInfoFromIntent (intent );
8997 WritableMap purchaseIntentResultMap = DataUtils .getMapCreatePurchaseIntent (purchaseIntentResult );
90- switch (purchaseIntentResult .getReturnCode ()) {
91- case OrderStatusCode .ORDER_STATE_CANCEL :
92- String message = getKeyByValue (CONSTANTS , OrderStatusCode .ORDER_STATE_CANCEL );
93- assert message != null ;
94- Log .i (TAG , message );
95- mPickerPromise .resolve (purchaseIntentResultMap );
96- break ;
97- case OrderStatusCode .ORDER_STATE_FAILED :
98- message = getKeyByValue (CONSTANTS , OrderStatusCode .ORDER_STATE_FAILED );
99- assert message != null ;
100- Log .i (TAG , message );
101- mPickerPromise .resolve (purchaseIntentResultMap );
102- break ;
103- case OrderStatusCode .ORDER_PRODUCT_OWNED :
104- message = getKeyByValue (CONSTANTS , OrderStatusCode .ORDER_PRODUCT_OWNED );
105- assert message != null ;
106- Log .i (TAG , message );
107- mPickerPromise .resolve (purchaseIntentResultMap );
108- break ;
109- case OrderStatusCode .ORDER_STATE_SUCCESS :
110- message = getKeyByValue (CONSTANTS , OrderStatusCode .ORDER_STATE_SUCCESS );
111- assert message != null ;
112- Log .i (TAG , message );
113- mPickerPromise .resolve (purchaseIntentResultMap );
114- break ;
115- default :
116- message = "Order unknown error." ;
117- Log .i (TAG , message );
118- mPickerPromise .resolve (purchaseIntentResultMap );
119- break ;
98+ mPickerPromise .resolve (purchaseIntentResultMap );
99+ Log .i (TAG , String .valueOf (purchaseIntentResult .getReturnCode ()));
100+ }
101+ if (requestCode == Constants .REQ_IS_ENVIRONMENT_READY ) {
102+ Log .i (TAG , "onActivityResult from isEnvReady" );
103+ final int accountFlag = IapClientHelper .parseAccountFlagFromIntent (intent );
104+ final int returnCode = IapClientHelper .parseRespCodeFromIntent (intent );
105+ if (returnCode == 0 ) {
106+ WritableMap writableMap = null ;
107+ try {
108+ JSONObject j = new JSONObject ();
109+ j .put ("accountFlag" , accountFlag );
110+ j .put ("returnCode" , returnCode );
111+ writableMap = MapUtil .toWritableMap (j );
112+ mPickerPromise .resolve (writableMap );
113+ } catch (JSONException e ) {
114+ Log .e (TAG , Objects .requireNonNull (e .getMessage ()));
115+ }
120116 }
121117 }
122118 mPickerPromise = null ;
@@ -129,7 +125,7 @@ public void onNewIntent(Intent intent) {
129125 }
130126 };
131127
132- HmsIapModule (ReactApplicationContext context ) {
128+ HMSIapModule (ReactApplicationContext context ) {
133129 super (context );
134130 this .context = context ;
135131 initializeIapClient (this .context );
@@ -195,7 +191,24 @@ public void disableLogger(Promise promise) {
195191 */
196192 @ ReactMethod
197193 public void isEnvironmentReady (final Promise promise ) {
198- viewModel .isEnvironmentReady (iapClient , new IapReqHelper (promise ));
194+ viewModel .isEnvironmentReady (iapClient , new Service .IAPResultListener <IsEnvReadyResult >() {
195+ @ Override
196+ public void onSuccess (IsEnvReadyResult result ) {
197+ promise .resolve (DataUtils .getMapFromIsEnvReadyResult (result ));
198+ }
199+
200+ @ Override
201+ public void onFail (Exception exception ) {
202+ if (exception instanceof IapApiException ) {
203+ IapApiException apiException = (IapApiException ) exception ;
204+ Status status = apiException .getStatus ();
205+ mPickerPromise = promise ;
206+ if (status .getStatusCode () == OrderStatusCode .ORDER_HWID_NOT_LOGIN ) {
207+ Helper .startResolutionForResult (getActivity (), status , Constants .REQ_IS_ENVIRONMENT_READY );
208+ }
209+ }
210+ }
211+ });
199212 }
200213
201214 /**
0 commit comments