Search in sources :

Example 1 with Token

use of net.iGap.model.qrCodePayment.Token in project iGap-Android by KianIranian-STDG.

the class QRCodePaymentFragment method setObservers.

private void setObservers() {
    mViewModel.getConfirmButtonClick().observe(getViewLifecycleOwner(), new Observer<Boolean>() {

        @Override
        public void onChanged(Boolean aBoolean) {
            if (aBoolean) {
                if (!mMerchantPcqr && mBinding.desireAmount == null || !mMerchantPcqr && mBinding.desireAmount.getText().toString().replaceAll("\\D", "").length() == 0) {
                    Toast.makeText(getActivity(), R.string.you_have_not_specified_the_amount, Toast.LENGTH_SHORT).show();
                } else if (!mMerchantPcqr && mBinding.desireAmount.getText().toString().replaceAll("\\D", "").length() != 0 && Integer.parseInt(mBinding.desireAmount.getText().toString().replaceAll("\\D", "")) < 1000) {
                    Toast.makeText(getActivity(), R.string.the_amount_can_not_be_less_than_1000_rials, Toast.LENGTH_SHORT).show();
                } else if (!mMerchantPcqr && mBinding.desireAmount.getText().toString().replaceAll("\\D", "").length() != 0 && Integer.parseInt(mBinding.desireAmount.getText().toString().replaceAll("\\D", "")) > 500000000) {
                    Toast.makeText(getActivity(), R.string.the_amount_can_not_be_more_than_500_million_rials, Toast.LENGTH_SHORT).show();
                } else {
                    mBinding.progressBar.setVisibility(View.VISIBLE);
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("qr_code", mMerchantCode);
                    if (mMerchantPcqr) {
                        /**
                         *1000 is a default amount for pcqr true state. true pcqr means that The amount is filled automatically and do not need to get amount from customer
                         */
                        jsonObject.addProperty("amount", 1000);
                    } else {
                        jsonObject.addProperty("amount", Integer.parseInt(mBinding.desireAmount.getText().toString().replaceAll("\\D", "")));
                    }
                    Call<Token> call = new RetrofitFactory().getPecQrRetrofit().getPaymentToken(jsonObject);
                    call.enqueue(new Callback<Token>() {

                        @Override
                        public void onResponse(Call<Token> call, Response<Token> response) {
                            if (response.isSuccessful()) {
                                Token token = response.body();
                                new HelperFragment(getActivity().getSupportFragmentManager(), PaymentFragment.getInstance(getActivity().getResources().getString(R.string.payment), token.getToken(), new PaymentCallBack() {

                                    @Override
                                    public void onPaymentFinished(PaymentResult result) {
                                    }
                                })).setReplace(false).setAddToBackStack(true).load();
                                mBinding.progressBar.setVisibility(View.GONE);
                            } else {
                                try {
                                    String[] splittedErrorBody = response.errorBody().string().split("\"");
                                    Toast.makeText(getActivity(), splittedErrorBody[splittedErrorBody.length - 2], Toast.LENGTH_LONG).show();
                                    mBinding.progressBar.setVisibility(View.GONE);
                                    getActivity().getSupportFragmentManager().popBackStackImmediate();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<Token> call, Throwable t) {
                        }
                    });
                }
            }
        }
    });
}
Also used : Call(retrofit2.Call) PaymentResult(net.iGap.model.payment.PaymentResult) JsonObject(com.google.gson.JsonObject) Token(net.iGap.model.qrCodePayment.Token) IOException(java.io.IOException) Response(retrofit2.Response) Callback(retrofit2.Callback) PaymentCallBack(net.iGap.observers.interfaces.PaymentCallBack) RetrofitFactory(net.iGap.api.apiService.RetrofitFactory) HelperFragment(net.iGap.helper.HelperFragment)

Aggregations

JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 RetrofitFactory (net.iGap.api.apiService.RetrofitFactory)1 HelperFragment (net.iGap.helper.HelperFragment)1 PaymentResult (net.iGap.model.payment.PaymentResult)1 Token (net.iGap.model.qrCodePayment.Token)1 PaymentCallBack (net.iGap.observers.interfaces.PaymentCallBack)1 Call (retrofit2.Call)1 Callback (retrofit2.Callback)1 Response (retrofit2.Response)1