Panagiotis Triantafyllou

sm coupons possible fix

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="21" />
<bytecodeTargetLevel target="17" />
</component>
</project>
\ No newline at end of file
......
......@@ -184,7 +184,7 @@
</map>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="zulu-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -326,16 +326,11 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
.concat(mCoupon.getCouponsetDetails().getName()));
mPbLoading.setVisibility(View.VISIBLE);
new Thread(() -> {
if (!Thread.currentThread().isInterrupted()) {
WarplyManager.cosmoteCouponSharing(new CosmoteCouponSharingRequest()
.setCoupon(mCoupon.getCoupon())
.setSender(mSender)
.setReceiver(mEdtReceiver.getText().toString())
, mCouponSharingCallback);
}
Thread.currentThread().interrupt();
}).start();
WarplyManager.cosmoteCouponSharing(
mCoupon.getCoupon(),
mSender,
mEdtReceiver.getText().toString(),
mCouponSharingCallback);
})
.setNegativeButton(R.string.cos_dlg_negative_button2, (dialogNegative, whichNegative) -> {
mIsSharePressed = false;
......@@ -360,6 +355,7 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
private final CallbackReceiver<JSONObject> mCouponSharingCallback = new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
mPbLoading.setVisibility(View.GONE);
mIsSharePressed = false;
int status = result.optInt("status", 2);
String message = result.optString("msg", "");
......@@ -368,31 +364,24 @@ public class CouponShareActivity extends Activity implements View.OnClickListene
// WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver);
WarplyManagerHelper.removeCouponItem(mCoupon);
/* New Addition end */
}
runOnUiThread(() -> {
mPbLoading.setVisibility(View.GONE);
if (status == 1) {
WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel();
couponsChanged.setChanged(true);
EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged));
acceptSharingDialog();
} else if (status == 4)
wrongNumberDialog(message);
else
errorSharingDialog();
});
WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel();
couponsChanged.setChanged(true);
EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged));
acceptSharingDialog();
} else if (status == 4)
wrongNumberDialog(message);
else
errorSharingDialog();
}
@Override
public void onFailure(int errorCode) {
mIsSharePressed = false;
Log.v("COUPON_SHARE", String.valueOf(errorCode));
runOnUiThread(() -> {
mPbLoading.setVisibility(View.GONE);
errorSharingDialog();
});
mPbLoading.setVisibility(View.GONE);
errorSharingDialog();
}
};
......
......@@ -135,6 +135,18 @@ public interface ApiService {
@Header(WarpConstants.HEADER_SIGNATURE) String signature);
@Headers("Content-Type: application/json")
@POST("/oauth/{appUuid}/context")
Call<ResponseBody> shareCoupon(@Path("appUuid") String appUuid,
@Body RequestBody request,
@Header(WarpConstants.HEADER_DATE) String timeStamp,
@Header(WarpConstants.HEADER_LOYALTY_BUNDLE_ID) String bundleId,
@Header(WarpConstants.HEADER_UNIQUE_DEVICE_ID) String deviceId,
@Header(WarpConstants.HEADER_CHANNEL) String channel,
@Header(WarpConstants.HEADER_WEB_ID) String webId,
@Header(WarpConstants.HEADER_SIGNATURE) String signature,
@Header(WarpConstants.HEADER_AUTHORIZATION) String bearer);
@Headers("Content-Type: application/json")
@POST("/api/mobile/v2/{appUuid}/context/")
Call<ResponseBody> getCampaigns(@Path("appUuid") String appUuid,
@Body RequestBody request,
......
......@@ -5301,4 +5301,147 @@ public class WarplyManager {
}
}, executorService);
}
public static void cosmoteCouponSharing(String coupon, String sender, String receiverMsisdn, final CallbackReceiver<JSONObject> receiver) {
WarpUtils.log("************* WARPLY Cosmote Coupon Sharing Request ********************");
WarpUtils.log("[WARP Trace] WARPLY Cosmote Coupon Sharing Request is active");
WarpUtils.log("**************************************************");
ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
ListenableFuture<JSONObject> futureSharing = cosmoteSharingRetro(service, coupon, sender, receiverMsisdn);
ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureSharing);
ListenableFuture<JSONObject> mergedResultFuture = Futures.transformAsync(
allResultsFuture,
results -> {
JSONObject resultCoupons = (JSONObject) results.get(0);
return executorService.submit(() -> resultCoupons);
},
executorService
);
Futures.addCallback(mergedResultFuture, new FutureCallback<JSONObject>() {
@Override
public void onSuccess(JSONObject mergedResult) {
executorService.shutdownNow();
new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(mergedResult));
}
@Override
public void onFailure(Throwable throwable) {
executorService.shutdownNow();
new Handler(Looper.getMainLooper()).post(() -> receiver.onFailure(2));
}
}, executorService);
}
private static /*void*/ ListenableFuture<JSONObject> cosmoteSharingRetro(ApiService service, String coupon, String sender, String receiver) {
SettableFuture<JSONObject> future = SettableFuture.create();
String timeStamp = DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
String apiKey = WarpUtils.getApiKey(Warply.getWarplyContext());
String webId = WarpUtils.getWebId(Warply.getWarplyContext());
Map<String, Object> jsonParamsShare = new ArrayMap<>();
Map<String, Object> jsonParams = new ArrayMap<>();
jsonParams.put("action", "share");
jsonParams.put("coupon", coupon);
jsonParams.put("sender_msisdn", sender);
jsonParams.put("receiver_msisdn", receiver);
jsonParamsShare.put("coupon", jsonParams);
RequestBody merchantsRequest = RequestBody.create(MediaType.get("application/json; charset=utf-8"), (new JSONObject(jsonParamsShare)).toString());
Call<ResponseBody> shareCall = service.shareCoupon(
WarplyProperty.getAppUuid(Warply.getWarplyContext()),
merchantsRequest,
timeStamp,
"android:" + Warply.getWarplyContext().getPackageName(),
new WarplyDeviceInfoCollector(Warply.getWarplyContext()).getUniqueDeviceId(),
"mobile",
webId,
WarpUtils.produceSignature(apiKey + timeStamp),
"Bearer " + WarplyDBHelper.getInstance(Warply.getWarplyContext()).getAuthValue("access_token")
);
shareCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
if (response.code() == 200 && response.body() != null) {
JSONObject jobjSharingResponse = null;
try {
jobjSharingResponse = new JSONObject(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
if (jobjSharingResponse != null && jobjSharingResponse.has("status")) {
int status = jobjSharingResponse.optInt("status", 2);
JSONObject jResponse = new JSONObject();
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
if (status == 1 || status == 3 || status == 4) {
dynatraceEvent.setEventName("custom_success_coupon_sharing_loyalty");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
try {
jResponse.putOpt("status", status);
jResponse.putOpt("msg", jobjSharingResponse.optString("msg", ""));
} catch (JSONException e) {
throw new RuntimeException(e);
}
} else {
dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
try {
jResponse.putOpt("status", 2);
jResponse.putOpt("msg", jobjSharingResponse.optString("msg", ""));
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
future.set(jResponse);
} else {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.set(new JSONObject());
}
} else if (response.code() == 401) {
refreshToken(new WarplyRefreshTokenRequest(), new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
int status = result.optInt("status", 2);
if (status == 1)
cosmoteSharingRetro(service, coupon, sender, receiver);
else {
future.setException(new Throwable());
}
}
@Override
public void onFailure(int errorCode) {
future.setException(new Throwable());
}
});
} else {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.setException(new Throwable());
}
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_coupon_sharing_loyalty");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.setException(new Throwable());
}
});
return future;
}
}
......