Panagiotis Triantafyllou

sm coupons possible fix

...@@ -200,7 +200,8 @@ public class CouponInfoActivity extends Activity implements View.OnClickListener ...@@ -200,7 +200,8 @@ public class CouponInfoActivity extends Activity implements View.OnClickListener
200 // WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel(); 200 // WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel();
201 // couponsChanged.setChanged(true); 201 // couponsChanged.setChanged(true);
202 // EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged)); 202 // EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged));
203 - onBackPressed(); 203 +// onBackPressed();
204 + finish();
204 } 205 }
205 } 206 }
206 } 207 }
......
...@@ -254,7 +254,14 @@ public class CouponShareActivity extends Activity implements View.OnClickListene ...@@ -254,7 +254,14 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
254 dialogPositive.dismiss(); 254 dialogPositive.dismiss();
255 if (mIsFromWallet) { 255 if (mIsFromWallet) {
256 setResult(RESULT_OK, new Intent()); 256 setResult(RESULT_OK, new Intent());
257 - onBackPressed(); 257 +// onBackPressed();
258 + finish();
259 + }
260 + })
261 + .setOnDismissListener(dialog -> {
262 + if (mIsFromWallet) {
263 + setResult(RESULT_OK);
264 + finish();
258 } 265 }
259 }) 266 })
260 .show(); 267 .show();
...@@ -321,16 +328,11 @@ public class CouponShareActivity extends Activity implements View.OnClickListene ...@@ -321,16 +328,11 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
321 .concat(mCoupon.getCouponsetDetails().getName())); 328 .concat(mCoupon.getCouponsetDetails().getName()));
322 mPbLoading.setVisibility(View.VISIBLE); 329 mPbLoading.setVisibility(View.VISIBLE);
323 330
324 - new Thread(() -> { 331 + WarplyManager.cosmoteCouponSharing(
325 - if (!Thread.currentThread().isInterrupted()) { 332 + mCoupon.getCoupon(),
326 - WarplyManager.cosmoteCouponSharing(new CosmoteCouponSharingRequest() 333 + mSender,
327 - .setCoupon(mCoupon.getCoupon()) 334 + mEdtReceiver.getText().toString(),
328 - .setSender(mSender) 335 + mCouponSharingCallback);
329 - .setReceiver(mEdtReceiver.getText().toString())
330 - , mCouponSharingCallback);
331 - }
332 - Thread.currentThread().interrupt();
333 - }).start();
334 }) 336 })
335 .setNegativeButton(R.string.cos_dlg_negative_button2, (dialogNegative, whichNegative) -> { 337 .setNegativeButton(R.string.cos_dlg_negative_button2, (dialogNegative, whichNegative) -> {
336 mIsSharePressed = false; 338 mIsSharePressed = false;
...@@ -355,6 +357,7 @@ public class CouponShareActivity extends Activity implements View.OnClickListene ...@@ -355,6 +357,7 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
355 private final CallbackReceiver<JSONObject> mCouponSharingCallback = new CallbackReceiver<JSONObject>() { 357 private final CallbackReceiver<JSONObject> mCouponSharingCallback = new CallbackReceiver<JSONObject>() {
356 @Override 358 @Override
357 public void onSuccess(JSONObject result) { 359 public void onSuccess(JSONObject result) {
360 + mPbLoading.setVisibility(View.GONE);
358 mIsSharePressed = false; 361 mIsSharePressed = false;
359 int status = result.optInt("status", 2); 362 int status = result.optInt("status", 2);
360 String message = result.optString("msg", ""); 363 String message = result.optString("msg", "");
...@@ -363,31 +366,24 @@ public class CouponShareActivity extends Activity implements View.OnClickListene ...@@ -363,31 +366,24 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
363 // WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver); 366 // WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver);
364 WarplyManagerHelper.removeCouponItem(mCoupon); 367 WarplyManagerHelper.removeCouponItem(mCoupon);
365 /* New Addition end */ 368 /* New Addition end */
366 - }
367 369
368 - runOnUiThread(() -> { 370 + WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel();
369 - mPbLoading.setVisibility(View.GONE); 371 + couponsChanged.setChanged(true);
370 - if (status == 1) { 372 + EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged));
371 - WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel(); 373 +
372 - couponsChanged.setChanged(true); 374 + acceptSharingDialog();
373 - EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged)); 375 + } else if (status == 4)
374 - 376 + wrongNumberDialog(message);
375 - acceptSharingDialog(); 377 + else
376 - } else if (status == 4) 378 + errorSharingDialog();
377 - wrongNumberDialog(message);
378 - else
379 - errorSharingDialog();
380 - });
381 } 379 }
382 380
383 @Override 381 @Override
384 public void onFailure(int errorCode) { 382 public void onFailure(int errorCode) {
385 mIsSharePressed = false; 383 mIsSharePressed = false;
386 Log.v("COUPON_SHARE", String.valueOf(errorCode)); 384 Log.v("COUPON_SHARE", String.valueOf(errorCode));
387 - runOnUiThread(() -> { 385 + mPbLoading.setVisibility(View.GONE);
388 - mPbLoading.setVisibility(View.GONE); 386 + errorSharingDialog();
389 - errorSharingDialog();
390 - });
391 } 387 }
392 }; 388 };
393 389
......
...@@ -124,6 +124,18 @@ public interface ApiService { ...@@ -124,6 +124,18 @@ public interface ApiService {
124 @Header(WarpConstants.HEADER_AUTHORIZATION) String bearer); 124 @Header(WarpConstants.HEADER_AUTHORIZATION) String bearer);
125 125
126 @Headers("Content-Type: application/json") 126 @Headers("Content-Type: application/json")
127 + @POST("/oauth/{appUuid}/context")
128 + Call<ResponseBody> shareCoupon(@Path("appUuid") String appUuid,
129 + @Body RequestBody request,
130 + @Header(WarpConstants.HEADER_DATE) String timeStamp,
131 + @Header(WarpConstants.HEADER_LOYALTY_BUNDLE_ID) String bundleId,
132 + @Header(WarpConstants.HEADER_UNIQUE_DEVICE_ID) String deviceId,
133 + @Header(WarpConstants.HEADER_CHANNEL) String channel,
134 + @Header(WarpConstants.HEADER_WEB_ID) String webId,
135 + @Header(WarpConstants.HEADER_SIGNATURE) String signature,
136 + @Header(WarpConstants.HEADER_AUTHORIZATION) String bearer);
137 +
138 + @Headers("Content-Type: application/json")
127 @POST("/api/mobile/v2/{appUuid}/context/") 139 @POST("/api/mobile/v2/{appUuid}/context/")
128 Call<ResponseBody> getMerchants(@Path("appUuid") String appUuid, 140 Call<ResponseBody> getMerchants(@Path("appUuid") String appUuid,
129 @Body RequestBody request, 141 @Body RequestBody request,
......
...@@ -1880,6 +1880,149 @@ public class WarplyManager { ...@@ -1880,6 +1880,149 @@ public class WarplyManager {
1880 return campaignLoyaltyList; 1880 return campaignLoyaltyList;
1881 } 1881 }
1882 1882
1883 + public static void cosmoteCouponSharing(String coupon, String sender, String receiverMsisdn, final CallbackReceiver<JSONObject> receiver) {
1884 + WarpUtils.log("************* WARPLY Cosmote Coupon Sharing Request ********************");
1885 + WarpUtils.log("[WARP Trace] WARPLY Cosmote Coupon Sharing Request is active");
1886 + WarpUtils.log("**************************************************");
1887 +
1888 + ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class);
1889 + ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
1890 +
1891 + ListenableFuture<JSONObject> futureSharing = cosmoteSharingRetro(service, coupon, sender, receiverMsisdn);
1892 +
1893 + ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureSharing);
1894 + ListenableFuture<JSONObject> mergedResultFuture = Futures.transformAsync(
1895 + allResultsFuture,
1896 + results -> {
1897 + JSONObject resultCoupons = (JSONObject) results.get(0);
1898 + return executorService.submit(() -> resultCoupons);
1899 + },
1900 + executorService
1901 + );
1902 +
1903 + Futures.addCallback(mergedResultFuture, new FutureCallback<JSONObject>() {
1904 + @Override
1905 + public void onSuccess(JSONObject mergedResult) {
1906 + executorService.shutdownNow();
1907 + new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(mergedResult));
1908 + }
1909 +
1910 + @Override
1911 + public void onFailure(Throwable throwable) {
1912 + executorService.shutdownNow();
1913 + new Handler(Looper.getMainLooper()).post(() -> receiver.onFailure(2));
1914 + }
1915 + }, executorService);
1916 + }
1917 +
1918 + private static /*void*/ ListenableFuture<JSONObject> cosmoteSharingRetro(ApiService service, String coupon, String sender, String receiver) {
1919 + SettableFuture<JSONObject> future = SettableFuture.create();
1920 +
1921 + String timeStamp = DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
1922 + String apiKey = WarpUtils.getApiKey(Warply.getWarplyContext());
1923 + String webId = WarpUtils.getWebId(Warply.getWarplyContext());
1924 +
1925 + Map<String, Object> jsonParamsShare = new ArrayMap<>();
1926 + Map<String, Object> jsonParams = new ArrayMap<>();
1927 + jsonParams.put("action", "share");
1928 + jsonParams.put("coupon", coupon);
1929 + jsonParams.put("sender_msisdn", sender);
1930 + jsonParams.put("receiver_msisdn", receiver);
1931 +
1932 + jsonParamsShare.put("coupon", jsonParams);
1933 + RequestBody merchantsRequest = RequestBody.create(MediaType.get("application/json; charset=utf-8"), (new JSONObject(jsonParamsShare)).toString());
1934 +
1935 + Call<ResponseBody> shareCall = service.shareCoupon(
1936 + WarplyProperty.getAppUuid(Warply.getWarplyContext()),
1937 + merchantsRequest,
1938 + timeStamp,
1939 + "android:" + Warply.getWarplyContext().getPackageName(),
1940 + new WarplyDeviceInfoCollector(Warply.getWarplyContext()).getUniqueDeviceId(),
1941 + "mobile",
1942 + webId,
1943 + WarpUtils.produceSignature(apiKey + timeStamp),
1944 + "Bearer " + WarplyDBHelper.getInstance(Warply.getWarplyContext()).getAuthValue("access_token")
1945 + );
1946 +
1947 + shareCall.enqueue(new Callback<ResponseBody>() {
1948 + @Override
1949 + public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
1950 + if (response.code() == 200 && response.body() != null) {
1951 + JSONObject jobjSharingResponse = null;
1952 + try {
1953 + jobjSharingResponse = new JSONObject(response.body().string());
1954 + } catch (Exception e) {
1955 + e.printStackTrace();
1956 + }
1957 +
1958 + if (jobjSharingResponse != null && jobjSharingResponse.has("status")) {
1959 + int status = jobjSharingResponse.optInt("status", 2);
1960 + JSONObject jResponse = new JSONObject();
1961 +
1962 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
1963 + if (status == 1 || status == 3 || status == 4) {
1964 + dynatraceEvent.setEventName("custom_success_coupon_sharing_loyalty");
1965 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
1966 + try {
1967 + jResponse.putOpt("status", status);
1968 + jResponse.putOpt("msg", jobjSharingResponse.optString("msg", ""));
1969 + } catch (JSONException e) {
1970 + throw new RuntimeException(e);
1971 + }
1972 + } else {
1973 + dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
1974 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
1975 + try {
1976 + jResponse.putOpt("status", 2);
1977 + jResponse.putOpt("msg", jobjSharingResponse.optString("msg", ""));
1978 + } catch (JSONException e) {
1979 + throw new RuntimeException(e);
1980 + }
1981 + }
1982 + future.set(jResponse);
1983 + } else {
1984 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
1985 + dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
1986 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
1987 + future.set(new JSONObject());
1988 + }
1989 + } else if (response.code() == 401) {
1990 + refreshToken(new WarplyRefreshTokenRequest(), new CallbackReceiver<JSONObject>() {
1991 + @Override
1992 + public void onSuccess(JSONObject result) {
1993 + int status = result.optInt("status", 2);
1994 + if (status == 1)
1995 + cosmoteSharingRetro(service, coupon, sender, receiver);
1996 + else {
1997 + future.setException(new Throwable());
1998 + }
1999 + }
2000 +
2001 + @Override
2002 + public void onFailure(int errorCode) {
2003 + future.setException(new Throwable());
2004 + }
2005 + });
2006 + } else {
2007 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
2008 + dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
2009 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
2010 + future.setException(new Throwable());
2011 + }
2012 + }
2013 +
2014 + @Override
2015 + public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
2016 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
2017 + dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
2018 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
2019 + future.setException(new Throwable());
2020 + }
2021 + });
2022 +
2023 + return future;
2024 + }
2025 +
1883 public static void getUserCouponsWithCouponsets(final CallbackReceiver<CouponList> receiver) { 2026 public static void getUserCouponsWithCouponsets(final CallbackReceiver<CouponList> receiver) {
1884 WarpUtils.log("************* WARPLY User Coupons Request ********************"); 2027 WarpUtils.log("************* WARPLY User Coupons Request ********************");
1885 WarpUtils.log("[WARP Trace] WARPLY User Coupons Request is active"); 2028 WarpUtils.log("[WARP Trace] WARPLY User Coupons Request is active");
......