hasancse1991

Untitled

Nov 25th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.11 KB | None | 0 0
  1. package com.bs.mageapp23.ui.fragment.Checkout;
  2.  
  3.  
  4. import android.app.Dialog;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.Typeface;
  8. import android.net.Uri;
  9. import android.net.http.SslError;
  10. import android.os.Bundle;
  11. import android.support.annotation.NonNull;
  12. import android.support.annotation.Nullable;
  13. import android.support.v7.widget.AppCompatButton;
  14. import android.support.v7.widget.AppCompatTextView;
  15. import android.support.v7.widget.CardView;
  16. import android.support.v7.widget.LinearLayoutManager;
  17. import android.support.v7.widget.RecyclerView;
  18. import android.util.Log;
  19. import android.view.LayoutInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.view.Window;
  23. import android.webkit.SslErrorHandler;
  24. import android.webkit.WebChromeClient;
  25. import android.webkit.WebSettings;
  26. import android.webkit.WebView;
  27. import android.webkit.WebViewClient;
  28. import android.widget.LinearLayout;
  29.  
  30. import com.bs.mageapp23.R;
  31. import com.bs.mageapp23.constant.CheckoutStepTag;
  32. import com.bs.mageapp23.constant.LogEventConstant;
  33. import com.bs.mageapp23.constant.ResponseCode;
  34. import com.bs.mageapp23.constant.StringValue;
  35. import com.bs.mageapp23.event.CheckoutFragmentChangeEvent;
  36. import com.bs.mageapp23.event.ConfirmOrderEvent;
  37. import com.bs.mageapp23.model.Checkout.response.Address;
  38. import com.bs.mageapp23.model.Checkout.response.OrderResponse;
  39. import com.bs.mageapp23.model.Checkout.response.PaymentMethodList;
  40. import com.bs.mageapp23.model.Checkout.response.RedirectUrlResponse;
  41. import com.bs.mageapp23.model.Checkout.response.ShippingMethodData;
  42. import com.bs.mageapp23.model.ProductModel.response.CartItemResponse;
  43. import com.bs.mageapp23.service.PreferenceService;
  44. import com.bs.mageapp23.util.CircularLoaderDialog;
  45. import com.bs.mageapp23.util.bkash.JavaScriptInterface;
  46. import com.bs.mageapp23.util.bkash.PaymentRequest;
  47. import com.bs.mageapp23.network.NetworkServiceHandler;
  48. import com.bs.mageapp23.network.RestClient;
  49. import com.bs.mageapp23.ui.activity.HomePageActivity;
  50. import com.bs.mageapp23.ui.adapter.Checkout.ConfirmOrderProductAdapter;
  51. import com.bs.mageapp23.ui.adapter.interface_pack.OnItemClickListener;
  52. import com.bs.mageapp23.ui.fragment.BaseFragment;
  53. import com.facebook.appevents.AppEventsLogger;
  54. import com.google.firebase.analytics.FirebaseAnalytics;
  55. import com.google.gson.Gson;
  56. import com.orhanobut.logger.AndroidLogAdapter;
  57. import com.orhanobut.logger.Logger;
  58. import com.sslcommerz.library.payment.Classes.PayUsingSSLCommerz;
  59. import com.sslcommerz.library.payment.Listener.OnPaymentResultListener;
  60. import com.sslcommerz.library.payment.Util.ConstantData.CurrencyType;
  61. import com.sslcommerz.library.payment.Util.ConstantData.ErrorKeys;
  62. import com.sslcommerz.library.payment.Util.ConstantData.SdkCategory;
  63. import com.sslcommerz.library.payment.Util.ConstantData.SdkType;
  64. import com.sslcommerz.library.payment.Util.JsonModel.TransactionInfo;
  65. import com.sslcommerz.library.payment.Util.Model.MandatoryFieldModel;
  66.  
  67.  
  68. import java.util.HashMap;
  69. import java.util.Map;
  70.  
  71. import butterknife.BindView;
  72. import butterknife.ButterKnife;
  73. import butterknife.Unbinder;
  74. import de.greenrobot.event.EventBus;
  75. import me.relex.photodraweeview.PhotoDraweeView;
  76. import retrofit2.Call;
  77.  
  78. import static com.bs.mageapp23.constant.CheckoutStepTag.BKASH_PAYMENT_CREATE;
  79. import static com.bs.mageapp23.constant.CheckoutStepTag.BKASH_PAYMENT_EXECUTE;
  80. import static com.bs.mageapp23.service.PreferenceService.TOKEN_KEY;
  81.  
  82. public class ConfirmOrderFragment extends BaseFragment {
  83.  
  84.     @BindView(R.id.rvProductList)
  85.     RecyclerView productListRecyclerView;
  86.  
  87.     @BindView(R.id.shippingName)
  88.     AppCompatTextView shippingNameView;
  89.  
  90.     @BindView(R.id.shippingAddress)
  91.     AppCompatTextView shippingAddressView;
  92.  
  93.     @BindView(R.id.billingNameView)
  94.     AppCompatTextView billingNameView;
  95.  
  96.     @BindView(R.id.billingAddressView)
  97.     AppCompatTextView billingAddressView;
  98.  
  99.     @BindView(R.id.shippingMethodView)
  100.     AppCompatTextView shippingMethodView;
  101.  
  102.     @BindView(R.id.paymentMethodView)
  103.     AppCompatTextView paymentMethodView;
  104.  
  105.     @BindView(R.id.totalTextView)
  106.     AppCompatTextView grandTotalTextView;
  107.  
  108.     @BindView(R.id.subTotalTextView)
  109.     AppCompatTextView subTotalTextView;
  110.  
  111.     @BindView(R.id.discountLabel)
  112.     AppCompatTextView discountLabel;
  113.  
  114.     @BindView(R.id.discountTextView)
  115.     AppCompatTextView discountTextView;
  116.  
  117.     @BindView(R.id.discountLayout)
  118.     LinearLayout discountLayout;
  119.  
  120.     @BindView(R.id.confirmOrderButton)
  121.     CardView confirmOrderButton;
  122.  
  123.     @BindView(R.id.clubPointLayout)
  124.     LinearLayout clubPointLayout;
  125.  
  126.     @BindView(R.id.clubPointTextView)
  127.     AppCompatTextView clubPointTextView;
  128.  
  129.     @BindView(R.id.interestLayout)
  130.     LinearLayout interestLayout;
  131.  
  132.     @BindView(R.id.interestTextView)
  133.     AppCompatTextView interestTextView;
  134.  
  135.     @BindView(R.id.shippingMethodLabelTextView)
  136.     AppCompatTextView shippingMethodLabelTextView;
  137.  
  138.     @BindView(R.id.shippingMethodValueTextView)
  139.     AppCompatTextView shippingMethodValueTextView;
  140.  
  141.     @BindView(R.id.rootLayout)
  142.     LinearLayout rootLayout;
  143.  
  144.     @BindView(R.id.paymentWebView)
  145.     WebView paymentWebView;
  146.  
  147.     @BindView(R.id.bkashPaymentImageView)
  148.     PhotoDraweeView bkashPaymentImageView;
  149.  
  150.     @BindView(R.id.bkashOkButton)
  151.     AppCompatButton bkashOkButton;
  152.  
  153.     @BindView(R.id.bkashPaymentView)
  154.     LinearLayout bkashPaymentView;
  155.  
  156.     private Unbinder unbinder;
  157.  
  158.     private AppEventsLogger logger;
  159.     private FirebaseAnalytics mFirebaseAnalytics;
  160.     Typeface customFontSemiBold;
  161.     Typeface customFontRegular;
  162.     public static boolean isShowPaymentPage;
  163.     public static boolean isPaymentByBKash = false;
  164.  
  165.     private String orderIdForCityBankPayment = "";
  166.     private String request = "";
  167.     private String orderId = "";
  168.  
  169.     private double grandTotal = 0;
  170.  
  171.     private CircularLoaderDialog circularLoaderDialog;
  172.  
  173.     @Override
  174.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  175.                              Bundle savedInstanceState) {
  176.         View view = inflater.inflate(R.layout.fragment_confirm_order, container, false);
  177.         unbinder = ButterKnife.bind(this, view);
  178.         Logger.addLogAdapter(new AndroidLogAdapter());
  179.  
  180.         logger = AppEventsLogger.newLogger(getContext());
  181.         mFirebaseAnalytics = FirebaseAnalytics.getInstance(getContext());
  182.  
  183.         customFontSemiBold = Typeface.createFromAsset(getContext().getAssets(), getString(R.string.fontSemiBoldText));
  184.         customFontRegular = Typeface.createFromAsset(getContext().getAssets(), getString(R.string.fontRegularText));
  185.  
  186.         Map<String, String> cartInfoAttribute = new HashMap<>();
  187.         cartInfoAttribute.put("store", "1");
  188.         cartInfoAttribute.put("mobile", "1");
  189.         Call<CartItemResponse> callback = RestClient.get().getCartInfo(getUserId(), cartInfoAttribute);
  190.         NetworkServiceHandler.processCallBack(callback, getContext());
  191.  
  192.         return view;
  193.     }
  194.  
  195.     @Override
  196.     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  197.         super.onViewCreated(view, savedInstanceState);
  198.         EventBus.getDefault().post(new CheckoutFragmentChangeEvent(this.getTag()));
  199.         confirmOrderButton.setOnClickListener(this);
  200.         bkashOkButton.setOnClickListener(this);
  201.         paymentWebView.setVisibility(View.GONE);
  202.         rootLayout.setVisibility(View.VISIBLE);
  203.         bkashPaymentView.setVisibility(View.GONE);
  204.         isShowPaymentPage = false;
  205.     }
  206.  
  207.     @Override
  208.     public void onDestroyView() {
  209.         super.onDestroyView();
  210.         unbinder.unbind();
  211.     }
  212.  
  213.     @Override
  214.     public void onClick(View v) {
  215.         super.onClick(v);
  216.         int resourceId = v.getId();
  217.         if (resourceId == R.id.confirmOrderButton) {
  218.             callPlaceOrderApi();
  219.         } else if (resourceId == R.id.bkashOkButton) {
  220.             redirectToHomeActivity();
  221.         }
  222.     }
  223.  
  224.  
  225.  
  226.     public void onEvent(CartItemResponse cartItemResponse) {
  227.  
  228.         if (cartItemResponse.getItems() != null && cartItemResponse.getItems().size() > 0) {
  229.             generateConfirmOrderPage(cartItemResponse);
  230.         }
  231.     }
  232.  
  233.     public void setProgressDialog() {
  234.         circularLoaderDialog = new CircularLoaderDialog(getContext());
  235.         circularLoaderDialog.show();
  236. //        pDialog = new ProgressDialog(getActivity());
  237. //        pDialog.setMessage("Loading...");
  238. //        pDialog.setCancelable(false);
  239. //        pDialog.setCanceledOnTouchOutside(false);
  240. //        pDialog.show();
  241.     }
  242.  
  243.     private void dismissDialog() {
  244.         try {
  245.             circularLoaderDialog.dismiss();
  246.         } catch (Exception e) {
  247.  
  248.         }
  249.     }
  250.  
  251.     private void loadUrlInWebView(final String redirectUrl) {
  252.         paymentWebView.getSettings().setJavaScriptEnabled(true);
  253.         paymentWebView.getSettings().setLoadWithOverviewMode(true);
  254.         paymentWebView.getSettings().setUseWideViewPort(true);
  255.         paymentWebView.setWebViewClient(new WebViewClient() {
  256.  
  257.             @Override
  258.             public void onPageStarted(WebView view, String url, Bitmap favicon) {
  259.                 super.onPageStarted(view, url, favicon);
  260.                 if (url.equals(redirectUrl)) {
  261.                     setProgressDialog();
  262.                 }
  263.             }
  264.  
  265.             @Override
  266.             public boolean shouldOverrideUrlLoading(WebView view, String url) {
  267.                 view.loadUrl(url);
  268.                 return true;
  269.             }
  270.  
  271.             @Override
  272.             public void onPageFinished(WebView view, final String url) {
  273.                 if (circularLoaderDialog!= null && url.equals(redirectUrl)) {
  274.                     dismissDialog();
  275.                 }
  276.  
  277.                 if (url.equals(CheckoutStepTag.ONLINE_PAYMENT_SUCCESS)) {
  278.                     showToast("Payment successful");
  279.                     showSuccessPopUp(orderIdForCityBankPayment);
  280.                 } else if (url.equals(CheckoutStepTag.ONLINE_PAYMENT_CANCEL)) {
  281.                     showToast("Payment not completed");
  282.                     redirectToHomeActivity();
  283.                 } else if (url.equals(CheckoutStepTag.ONLINE_PAYMENT_DECLINE)) {
  284.                     showToast("Payment not completed");
  285.                     redirectToHomeActivity();
  286.                 }
  287.             }
  288.  
  289.             @Override
  290.             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  291.                 if (circularLoaderDialog != null) {
  292.                     dismissDialog();
  293.                 }
  294.                 showToast("Payment not completed");
  295.                 redirectToHomeActivity();
  296.             }
  297.         });
  298.  
  299.         paymentWebView.loadUrl(redirectUrl);
  300.     }
  301.  
  302.     private void generateConfirmOrderPage(CartItemResponse cartItemResponse) {
  303.         CheckoutStepTag.cartItemProducts = cartItemResponse.getItems();
  304.         CheckoutStepTag.totalPrice = cartItemResponse.getGrandTotal();
  305.         CheckoutStepTag.discountPrice = getString(R.string.final_price, String.valueOf((int) cartItemResponse.getDiscount()));
  306.         CheckoutStepTag.subTotalPrice = getString(R.string.final_price, String.valueOf(cartItemResponse.getSubtotal()));
  307.  
  308.         Address shippingAddress = CheckoutStepTag.shippingAddress;
  309.         String shipAddressViewText = getString(R.string.label_phone_number) + ": " + shippingAddress.getTelePhone() + ", " +
  310.                 getString(R.string.street) + ": " + shippingAddress.getStreet() + ", " +
  311.                 getString(R.string.city) + ": " + shippingAddress.getCity() + ", " +
  312.                 getString(R.string.label_country) + ": " + shippingAddress.getCountryId();
  313.         shippingNameView.setText(getString(R.string.full_name, shippingAddress.getFirstName(), shippingAddress.getLastName()));
  314.         shippingAddressView.setText(shipAddressViewText);
  315.  
  316.         Address billingAddress = CheckoutStepTag.billingAddress;
  317.         String billingAddressViewText = getString(R.string.label_phone_number) + ": " + billingAddress.getTelePhone() + ", " +
  318.                 getString(R.string.street) + ": " + billingAddress.getStreet() + ", " +
  319.                 getString(R.string.city) + ": " + billingAddress.getCity() + ", " +
  320.                 getString(R.string.label_country) + ": " + billingAddress.getCountryId();
  321.         billingNameView.setText(getString(R.string.full_name, billingAddress.getFirstName(), billingAddress.getLastName()));
  322.         billingAddressView.setText(billingAddressViewText);
  323.  
  324.         ShippingMethodData shippingMethodData = CheckoutStepTag.shippingMethod;
  325.         shippingMethodView.setText(shippingMethodData.getTitle());
  326.  
  327.         PaymentMethodList paymentMethod = CheckoutStepTag.paymentMethod;
  328.         paymentMethodView.setText(paymentMethod != null ? paymentMethod.getLabel() : "Internal Rewards");
  329.  
  330.         grandTotal = CheckoutStepTag.totalPrice + CheckoutStepTag.shippingMethod.getAmount();
  331.         if (paymentMethod != null && paymentMethod.getValue().equals(CheckoutStepTag.EMI_PAYMENT_KEY)) {
  332.             if (CheckoutStepTag.bankEMIInfo != null && CheckoutStepTag.bankEMIInfo.getInterest() > 0.0) {
  333.                 grandTotal += CheckoutStepTag.bankEMIInfo.getInterest();
  334.                 interestTextView.setText(StringValue.bdtFormatWithZeroDecimalPoint(CheckoutStepTag.bankEMIInfo.getInterest()));
  335.                 interestLayout.setVisibility(View.VISIBLE);
  336.             } else {
  337.                 interestLayout.setVisibility(View.GONE);
  338.             }
  339.         } else {
  340.             interestLayout.setVisibility(View.GONE);
  341.         }
  342.  
  343.         subTotalTextView.setText(CheckoutStepTag.subTotalPrice);
  344.  
  345.         if (CheckoutStepTag.discountPrice.equals("ΰ§³ 0")) {
  346.             discountLayout.setVisibility(View.GONE);
  347.         } else {
  348.             if (cartItemResponse.getCoupon()!=null)
  349.                 discountLabel.setText(getString(R.string.discount_coupon, cartItemResponse.getCoupon()));
  350.             discountTextView.setText(CheckoutStepTag.discountPrice);
  351.             discountLayout.setVisibility(View.VISIBLE);
  352.         }
  353.  
  354.         shippingMethodLabelTextView.setText(getString(R.string.shipping_and_handling_placeholder, CheckoutStepTag.shippingMethod.getTitle(), CheckoutStepTag.shippingMethod.getMethod()));
  355.         shippingMethodValueTextView.setText(getString(R.string.final_price, StringValue.bdtFormatWithZeroDecimalPoint(CheckoutStepTag.shippingMethod.getAmount())));
  356.  
  357.         if (CheckoutStepTag.isReward) {
  358.             clubPointLayout.setVisibility(View.VISIBLE);
  359.             clubPointTextView.setText(getString(R.string.price_with_minus_sign, StringValue.bdtFormatWithZeroDecimalPoint(CheckoutStepTag.rewardAmount)));
  360.             grandTotal -= CheckoutStepTag.rewardAmount;
  361.         } else {
  362.             clubPointLayout.setVisibility(View.GONE);
  363.         }
  364.         grandTotalTextView.setText(getString(R.string.final_price, StringValue.bdtFormatWithZeroDecimalPoint(grandTotal)));
  365.         generateProductView();
  366.     }
  367.  
  368.     private void generateProductView() {
  369.         LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
  370.         productListRecyclerView.setLayoutManager(layoutManager);
  371.         productListRecyclerView.setHasFixedSize(true);
  372.         productListRecyclerView.setNestedScrollingEnabled(false);
  373.         ConfirmOrderProductAdapter adapter = new ConfirmOrderProductAdapter(getActivity(), CheckoutStepTag.cartItemProducts);
  374.         productListRecyclerView.setAdapter(adapter);
  375.         adapter.SetOnItemClickListener(new OnItemClickListener() {
  376.             @Override
  377.             public void onItemClick(View view, int position) {
  378.             }
  379.         });
  380.     }
  381.  
  382.     private void callPlaceOrderApi() {
  383.         String cardValue = "";
  384.         String bankName = "";
  385.         String emiValue = "";
  386.         String emiPay = "";
  387.  
  388.         if (CheckoutStepTag.paymentMethod != null && CheckoutStepTag.paymentMethod.getValue().equals(CheckoutStepTag.CARD_ON_DELIVERY_KEY)) {
  389.             cardValue = CheckoutStepTag.cardDeliveryPaymentOption.getValue();
  390.         } else if (CheckoutStepTag.paymentMethod != null && CheckoutStepTag.paymentMethod.getValue().equals(CheckoutStepTag.ONLINE_PAYMENT_KEY)) {
  391.             cardValue = CheckoutStepTag.onlinePaymentOption.getValue();
  392.         } else if (CheckoutStepTag.paymentMethod != null && CheckoutStepTag.paymentMethod.getValue().equals(CheckoutStepTag.EMI_PAYMENT_KEY)) {
  393.             bankName = CheckoutStepTag.emiBankName;
  394.             emiValue = String.valueOf(CheckoutStepTag.bankEMIInfo.getValue());
  395.             emiPay = CheckoutStepTag.isEmiOnlinePay ? "Online" : "Card On Delivery";
  396.         }
  397.  
  398.         Map<String, String> orderInfo = new HashMap<>();
  399.  
  400.         orderInfo.put("biladdr", CheckoutStepTag.billingAddress.getEntityId() + "");
  401.         orderInfo.put("shipaddr", CheckoutStepTag.shippingAddress.getEntityId() + "");
  402.         orderInfo.put("shipmethod", CheckoutStepTag.shippingMethod.getCode());
  403.         orderInfo.put("paymentmethod", CheckoutStepTag.paymentMethod != null ? CheckoutStepTag.paymentMethod.getValue() : CheckoutStepTag.rewardValue);
  404.         orderInfo.put("sameship", CheckoutStepTag.sameShip ? "yes" : "no");
  405.         orderInfo.put("saveship", "yes");
  406.         orderInfo.put("savebill", "yes");
  407.         orderInfo.put("newship", CheckoutStepTag.newShip ? "yes" : "no");
  408.         orderInfo.put("newbill", CheckoutStepTag.newBill ? "yes" : "no");
  409.         orderInfo.put("cardvalue", cardValue);
  410.         orderInfo.put("bankvalue", bankName);
  411.         orderInfo.put("emivalue", emiValue);
  412.         orderInfo.put("credit", CheckoutStepTag.isReward ? "yes" : "no");
  413.         orderInfo.put("emipaymentmode", emiPay);
  414.         orderInfo.put("mobile", "1");
  415.  
  416.         Call<OrderResponse> callback = RestClient.get().placeOrder(getUserId(), orderInfo);
  417.         NetworkServiceHandler.processCallBack(callback, getActivity());
  418.     }
  419.  
  420.     private void showSuccessPopUp(String orderId) {
  421.         final Dialog dialog = new Dialog(getContext());
  422.         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  423.         dialog.setCancelable(false);
  424.         dialog.setContentView(R.layout.confirm_order_dialog);
  425.         AppCompatTextView orderSuccessMessageTextView = dialog.findViewById(R.id.orderSuccessMessageTextView);
  426.         AppCompatTextView orderIdTextView = dialog.findViewById(R.id.orderIdTextView);
  427.         AppCompatButton okButton = dialog.findViewById(R.id.okButton);
  428.         okButton.setTypeface(customFontRegular);
  429.  
  430.         String title;
  431.         String message;
  432.  
  433.         if (isNumeric(orderId)) {
  434.             title = getString(R.string.order_placed_successfully);
  435.             message = getString(R.string.order_confirmation_placeholder, orderId);
  436.         }
  437.         else {
  438.             title = getString(R.string.payment_failed_title);
  439.             message = getString(R.string.online_order_failed_placeholder);
  440.         }
  441.  
  442.         orderSuccessMessageTextView.setText(title);
  443.         orderIdTextView.setText(message);
  444.  
  445.         orderSuccessMessageTextView.setTypeface(customFontSemiBold);
  446.         orderIdTextView.setTypeface(customFontRegular);
  447.         okButton.setOnClickListener(new View.OnClickListener() {
  448.             @Override
  449.             public void onClick(View v) {
  450.                 dialog.dismiss();
  451.                 redirectToHomeActivity();
  452.             }
  453.         });
  454.         dialog.show();
  455.     }
  456.  
  457.     private boolean isNumeric(String number) {
  458.         boolean flag = false;
  459.         try {
  460.             Integer.parseInt(number);
  461.             flag = true;
  462.         } catch (Exception e) {
  463.  
  464.         }
  465.         return flag;
  466.     }
  467.  
  468.     private void redirectToHomeActivity() {
  469.         getActivity().finish();
  470.         gotoHomeActivity(HomePageActivity.class);
  471.     }
  472.  
  473.     private void callRedirectUrlApi(String orderId) {
  474.         Map<String, String> orderInfo = new HashMap<>();
  475.         orderInfo.put("order_id", orderId);
  476.         Call<RedirectUrlResponse> callback = RestClient.get().getRedirectUrl(orderInfo);
  477.         NetworkServiceHandler.processCallBack(callback, getActivity());
  478.     }
  479.  
  480.     private void loadBKashWebView(final String orderId, double grandTotal) {
  481.  
  482.         isPaymentByBKash = true;
  483.  
  484.         PaymentRequest paymentRequest = new PaymentRequest();
  485.         paymentRequest.setAmount(String.valueOf(grandTotal));
  486.         paymentRequest.setIntent("sale");
  487.         paymentRequest.setOrderId(orderId);
  488.  
  489.         Gson gson = new Gson();
  490.         request = gson.toJson(paymentRequest);
  491.  
  492.         rootLayout.setVisibility(View.GONE);
  493.         paymentWebView.setVisibility(View.VISIBLE);
  494.  
  495.         WebSettings webSettings = paymentWebView.getSettings();
  496.         webSettings.setJavaScriptEnabled(true);
  497.         paymentWebView.setClickable(true);
  498.         paymentWebView.getSettings().setDomStorageEnabled(true);
  499.         paymentWebView.getSettings().setAppCacheEnabled(false);
  500.         paymentWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  501.         paymentWebView.clearCache(true);
  502.         paymentWebView.getSettings().setAllowFileAccessFromFileURLs(true);
  503.         paymentWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
  504.  
  505.         paymentWebView.addJavascriptInterface(new JavaScriptInterface(getContext()), "AndroidNative");
  506.  
  507.         paymentWebView.loadUrl(getString(R.string.CHECKOUT_PATH));
  508.  
  509.         paymentWebView.setWebChromeClient(new WebChromeClient());
  510.         paymentWebView.setWebViewClient(new CheckoutWebViewClient());
  511.     }
  512.  
  513.     class CheckoutWebViewClient extends WebViewClient {
  514.  
  515.         public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  516.             handler.cancel();
  517.             showSuccessPopUp(getString(R.string.online_order_failed_placeholder));
  518.         }
  519.  
  520.         @Override
  521.         public boolean shouldOverrideUrlLoading(WebView view, String url) {
  522.             if (url.equals("https://www.bkash.com/terms-and-conditions")) {
  523.                 Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  524.                 startActivity(myIntent);
  525.                 return true;
  526.             }
  527.             return super.shouldOverrideUrlLoading(view, url);
  528.         }
  529.  
  530.         @Override
  531.         public void onPageStarted(WebView view, String url, Bitmap favicon) {
  532.             setProgressDialog();
  533.         }
  534.  
  535.         @Override
  536.         public void onPageFinished(WebView view, String url) {
  537.             if (url.equalsIgnoreCase(getString(R.string.CHECKOUT_PATH))) {
  538.                 paymentWebView.loadUrl("javascript:setConfigs('" + BKASH_PAYMENT_CREATE + "','" + BKASH_PAYMENT_EXECUTE + "','" + NetworkServiceHandler.AuthToken + "','" + PreferenceService.GetPreferenceValue(TOKEN_KEY) + "')");
  539.                 String paymentRequest = "{paymentRequest:" + request + "}";
  540.                 paymentWebView.loadUrl("javascript:callReconfigure(" + paymentRequest + " )");
  541.                 paymentWebView.loadUrl("javascript:clickPayButton()");
  542.             } else if (url.equalsIgnoreCase("file:///android_asset/www/checkoutSuccess.html")) {
  543.                 showToast("Payment successful");
  544.                 showSuccessPopUp(orderId);
  545.             } else if (url.equalsIgnoreCase("file:///android_asset/www/checkout_failed.html")) {
  546.                 showToast("bKash Payment failed");
  547.                 showSuccessPopUp(getString(R.string.online_order_failed_placeholder));
  548.             }
  549.  
  550.             if (circularLoaderDialog != null) {
  551.                 dismissDialog();
  552.             }
  553.         }
  554.  
  555.     }
  556.  
  557.     public void onEvent(OrderResponse orderResponse) {
  558.         if (orderResponse.getResponse_code() == ResponseCode.OPERATION_SUCCESSFULL) {
  559.             EventBus.getDefault().post(new ConfirmOrderEvent());
  560.             if (CheckoutStepTag.paymentMethod != null && CheckoutStepTag.paymentMethod.getValue().equals(CheckoutStepTag.ONLINE_PAYMENT_KEY)) {
  561.                 orderIdForCityBankPayment = orderResponse.getData().getOrderId();
  562.                 callRedirectUrlApi(orderIdForCityBankPayment);
  563.             } else if (CheckoutStepTag.paymentMethod != null && CheckoutStepTag.paymentMethod.getValue().equals(CheckoutStepTag.EMI_PAYMENT_KEY)) {
  564.                 if (CheckoutStepTag.isEmiOnlinePay) {
  565.                     callRedirectUrlApi(orderResponse.getData().getOrderId());
  566.                 } else {
  567.                     showSuccessPopUp(orderResponse.getData().getOrderId());
  568.                 }
  569.             } else if (CheckoutStepTag.paymentMethod != null && CheckoutStepTag.paymentMethod.getValue().equals(CheckoutStepTag.BKASH_PAYMENT_KEY)) {
  570.                 isShowPaymentPage = true;
  571.                 orderId = orderResponse.getData().getOrderId();
  572.                 loadBKashWebView(orderResponse.getData().getOrderId(), grandTotal);
  573.             } else {
  574.                 showSuccessPopUp(orderResponse.getData().getOrderId());
  575.             }
  576.             eventLogForAnalytics(orderResponse.getData().getOrderId());
  577.         } else {
  578.             showToast(orderResponse.getErrors()[0]);
  579.         }
  580.     }
  581.  
  582.     //city bank url
  583.     public void onEvent(RedirectUrlResponse redirectUrlResponse) {
  584.         if (redirectUrlResponse.getResponse_code() == ResponseCode.OPERATION_SUCCESSFULL) {
  585.             String url = redirectUrlResponse.getData().getRedirectUrl();
  586.             rootLayout.setVisibility(View.GONE);
  587.             paymentWebView.setVisibility(View.VISIBLE);
  588.             isShowPaymentPage = true;
  589.             loadUrlInWebView(url);
  590.         } else {
  591.             showSuccessPopUp(redirectUrlResponse.getErrors()[0]);
  592.         }
  593.     }
  594.  
  595.     private void payBySsl(String orderId, double grandTotal) {
  596.         /*Mandatory Field*/
  597.         MandatoryFieldModel mandatoryFieldModel = new MandatoryFieldModel("testbox", "", String.valueOf(grandTotal), orderId, CurrencyType.BDT, SdkType.TESTBOX, SdkCategory.BANK_LIST);
  598.  
  599.         PayUsingSSLCommerz.getInstance().setData(getContext(), mandatoryFieldModel, new OnPaymentResultListener() {
  600.             @Override
  601.             public void transactionSuccess(TransactionInfo transactionInfo) {
  602.                 showSuccessPopUp(transactionInfo.getTranId());
  603.             }
  604.  
  605.             @Override
  606.             public void transactionFail(TransactionInfo transactionInfo) {
  607.                 showToast("Transaction failed");
  608.             }
  609.  
  610.             @Override
  611.             public void error(int errorCode) {
  612.  
  613.                 switch (errorCode) {
  614.                     // Your provides information is not valid.
  615.                     case ErrorKeys.USER_INPUT_ERROR:
  616.                         showToast("User Input Error");
  617.                         break;
  618.                         // Internet is not connected.
  619.                     case ErrorKeys.INTERNET_CONNECTION_ERROR:
  620.                         showToast("Internet Connection Error");
  621.                         break;
  622.                         // Server is not giving valid data.
  623.                     case ErrorKeys.DATA_PARSING_ERROR:
  624.                         showToast("Data Parsing Error");
  625.                         break;
  626.                         // User press back button or canceled the transaction.
  627.                     case ErrorKeys.CANCEL_TRANSACTION_ERROR:
  628.                         showToast("User Cancel The Transaction");
  629.                         break;
  630.                         // Server is not responding.
  631.                     case ErrorKeys.SERVER_ERROR:
  632.                         showToast("Server Error");
  633.                         break;
  634.                         // For some reason network is not responding
  635.                     case ErrorKeys.NETWORK_ERROR:
  636.                         showToast("Network Error");
  637.                         break;
  638.                 }
  639.             }
  640.  
  641.         });
  642.     }
  643.  
  644.  
  645.  
  646.     private void eventLogForAnalytics(String orderId) {
  647.         Bundle params = new Bundle();
  648.         try {
  649.             params.putLong(LogEventConstant.USER_ID, Long.parseLong(getUserId()));
  650.             params.putLong(LogEventConstant.ORDER_ID, Long.parseLong(orderId));
  651.             params.putString(LogEventConstant.CHECKOUT_STAGE, LogEventConstant.CONFIRM_ORDER);
  652.         } catch (Exception e) {
  653.             Log.d("Exception message", e.toString());
  654.         }
  655.  
  656.         logger.logEvent(LogEventConstant.CONFIRM_ORDER, params); //facebook
  657.         mFirebaseAnalytics.logEvent(LogEventConstant.CONFIRM_ORDER, params); //firebase
  658.     }
  659. }
Advertisement
Add Comment
Please, Sign In to add comment