Which function will help me to fetch tokens from our server to android app

Posted in General by Virendra Singh Wed Aug 16 2017 08:36:48 GMT+0000 (UTC)·Viewed 1,258 times

package com.travel.trodlydotcom; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.widget.AppCompatEditText; import android.support.v7.widget.AppCompatSpinner; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Toast; import com.instamojo.android.Instamojo; import com.instamojo.android.activities.PaymentDetailsActivity; import com.instamojo.android.callbacks.OrderRequestCallBack; import com.instamojo.android.helpers.Constants; import com.instamojo.android.models.Errors; import com.instamojo.android.models.Order; import com.instamojo.android.network.Request; import com.travel.trodlydotcom.net.TrodlyAsyncHttpRequest; import com.travel.trodlydotcom.net.TrodlyHelperModel; import com.travel.trodlydotcom.net.TrodlyHttpRequestHelper; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.RequestBody; import okhttp3.Response; public class KickPayment1 extends TrodlyBaseActivity { private static final HashMap<String, String> env_options = new HashMap<>(); private Context context; static { env_options.put("Test", "https://test.instamojo.com/"); env_options.put("Production", "https://api.instamojo.com/"); } private ProgressDialog dialog; private AppCompatEditText nameBox, emailBox, phoneBox, amountBox, descriptionBox; private String currentEnv = null; private String accessToken = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_kick_payment1); Button button = (Button) findViewById(R.id.pay); nameBox = (AppCompatEditText) findViewById(R.id.name); nameBox.setSelection(nameBox.getText().toString().trim().length()); emailBox = (AppCompatEditText) findViewById(R.id.email); emailBox.setSelection(emailBox.getText().toString().trim().length()); phoneBox = (AppCompatEditText) findViewById(R.id.phone); phoneBox.setSelection(phoneBox.getText().toString().trim().length()); amountBox = (AppCompatEditText) findViewById(R.id.amount); amountBox.setSelection(amountBox.getText().toString().trim().length()); descriptionBox = (AppCompatEditText) findViewById(R.id.description); descriptionBox.setSelection(descriptionBox.getText().toString().trim().length()); AppCompatSpinner envSpinner = (AppCompatSpinner) findViewById(R.id.env_spinner); final ArrayList<String> envs = new ArrayList<>(env_options.keySet()); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, envs); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); context=this; envSpinner.setAdapter(adapter); Instamojo.initialize(this); envSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { currentEnv = envs.get(position); Instamojo.setBaseUrl(env_options.get(currentEnv)); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); dialog = new ProgressDialog(this); dialog.setIndeterminate(true); dialog.setMessage("please wait..."); dialog.setCancelable(false); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fetchTokenAndTransactionID(); } }); //let's set the log level to debug Instamojo.setLogLevel(Log.DEBUG); } // this is for the market place // you should have created the order from your backend and pass back the order id to app for the payment private void fetchOrder(String accessToken, String orderID){ // Good time to show dialog Request request = new Request(accessToken, orderID, new OrderRequestCallBack() { @Override public void onFinish(final Order order, final Exception error) { runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); if (error != null) { if (error instanceof Errors.ConnectionError) { showToast("No internet connection"); } else if (error instanceof Errors.ServerError) { showToast("Server Error. Try again"); } else if (error instanceof Errors.AuthenticationError) { showToast("Access token is invalid or expired. Please Update the token!!"); } else { showToast(error.toString()); } return; } startPreCreatedUI(order); } }); } }); request.execute(); } private void createOrder(String accessToken, String transactionID) { String name = nameBox.getText().toString(); final String email = emailBox.getText().toString(); String phone = phoneBox.getText().toString(); String amount = amountBox.getText().toString(); String description = descriptionBox.getText().toString(); //Create the Order Order order = new Order(accessToken, transactionID, name, email, phone, amount, description); //set webhook order.setWebhook("http://your.server.com/webhook/"); //Validate the Order if (!order.isValid()) { //oops order validation failed. Pinpoint the issue(s). if (!order.isValidName()) { nameBox.setError("Buyer name is invalid"); } if (!order.isValidEmail()) { emailBox.setError("Buyer email is invalid"); } if (!order.isValidPhone()) { phoneBox.setError("Buyer phone is invalid"); } if (!order.isValidAmount()) { amountBox.setError("Amount is invalid or has more than two decimal places"); } if (!order.isValidDescription()) { descriptionBox.setError("Description is invalid"); } if (!order.isValidTransactionID()) { showToast("Transaction is Invalid"); } if (!order.isValidRedirectURL()) { showToast("Redirection URL is invalid"); } if (!order.isValidWebhook()) { showToast("Webhook URL is invalid"); } return; } //Validation is successful. Proceed dialog.show(); Request request = new Request(order, new OrderRequestCallBack() { @Override public void onFinish(final Order order, final Exception error) { runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); if (error != null) { if (error instanceof Errors.ConnectionError) { showToast("No internet connection"); } else if (error instanceof Errors.ServerError) { showToast("Server Error. Try again"); } else if (error instanceof Errors.AuthenticationError) { showToast("Access token is invalid or expired. Please Update the token!!"); } else if (error instanceof Errors.ValidationError) { // Cast object to validation to pinpoint the issue Errors.ValidationError validationError = (Errors.ValidationError) error; if (!validationError.isValidTransactionID()) { showToast("Transaction ID is not Unique"); return; } if (!validationError.isValidRedirectURL()) { showToast("Redirect url is invalid"); return; } if (!validationError.isValidWebhook()) { showToast("Webhook url is invalid"); return; } if (!validationError.isValidPhone()) { phoneBox.setError("Buyer's Phone Number is invalid/empty"); return; } if (!validationError.isValidEmail()) { emailBox.setError("Buyer's Email is invalid/empty"); return; } if (!validationError.isValidAmount()) { amountBox.setError("Amount is either less than Rs.9 or has more than two decimal places"); return; } if (!validationError.isValidName()) { nameBox.setError("Buyer's Name is required"); return; } } else { showToast(error.getMessage()); } return; } startPreCreatedUI(order); } }); } }); request.execute(); } private void startPreCreatedUI(Order order) { //Using Pre created UI Intent intent = new Intent(getBaseContext(), PaymentDetailsActivity.class); intent.putExtra(Constants.ORDER, order); startActivityForResult(intent, Constants.REQUEST_CODE); } private void startCustomUI(Order order) { //Custom UI Implementation Intent intent = new Intent(getBaseContext(), CustomUIActivity.class); intent.putExtra(Constants.ORDER, order); startActivityForResult(intent, Constants.REQUEST_CODE); } private void showToast(final String message) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show(); } }); } /** * * Fetch Access token and unique transactionID from developers server */ private void fetchTokenAndTransactionID() { if (!dialog.isShowing()) { dialog.show(); } OkHttpClient client = new OkHttpClient(); HttpUrl url = getHttpURLBuilder() .addPathSegment("create") .build(); RequestBody body = new FormBody.Builder() .add("env", currentEnv.toLowerCase()) .build(); okhttp3.Request request = new okhttp3.Request.Builder() .url(url) .post(body) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { runOnUiThread(new Runnable() { @Override public void run() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } showToast("Failed to fetch the Order Tokens"); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String responseString; String errorMessage = null; String transactionID = null; responseString = response.body().string(); response.body().close(); try { JSONObject responseObject = new JSONObject(responseString); if (responseObject.has("error")) { errorMessage = responseObject.getString("error"); } else { accessToken = responseObject.getString("access_token"); transactionID = responseObject.getString("transaction_id"); } } catch (JSONException e) { errorMessage = "Failed to fetch Order tokens"; } final String finalErrorMessage = errorMessage; final String finalTransactionID = transactionID; runOnUiThread(new Runnable() { @Override public void run() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } if (finalErrorMessage != null) { showToast(finalErrorMessage); return; } createOrder(accessToken, finalTransactionID); } }); } }); } /** * Will check for the transaction status of a particular Transaction * * @param transactionID Unique identifier of a transaction ID */ private void checkPaymentStatus(final String transactionID, final String orderID) { if (accessToken == null || (transactionID == null && orderID == null)) { return; } if (dialog != null && !dialog.isShowing()) { dialog.show(); } showToast("checking transaction status"); OkHttpClient client = new OkHttpClient(); HttpUrl.Builder builder = getHttpURLBuilder(); builder.addPathSegment("status"); if (transactionID != null){ builder.addQueryParameter("transaction_id", transactionID); } else { builder.addQueryParameter("id", orderID); } builder.addQueryParameter("env", currentEnv.toLowerCase()); HttpUrl url = builder.build(); okhttp3.Request request = new okhttp3.Request.Builder() .url(url) .addHeader("Authorization", "Bearer " + accessToken) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { runOnUiThread(new Runnable() { @Override public void run() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } showToast("Failed to fetch the Transaction status"); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String responseString = response.body().string(); response.body().close(); String status = null; String paymentID = null; String amount = null; String errorMessage = null; try { JSONObject responseObject = new JSONObject(responseString); JSONObject payment = responseObject.getJSONArray("payments").getJSONObject(0); status = payment.getString("status"); paymentID = payment.getString("id"); amount = responseObject.getString("amount"); } catch (JSONException e) { errorMessage = "Failed to fetch the Transaction status"; } final String finalStatus = status; final String finalErrorMessage = errorMessage; final String finalPaymentID = paymentID; final String finalAmount = amount; runOnUiThread(new Runnable() { @Override public void run() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } if (finalStatus == null) { showToast(finalErrorMessage); return; } if (!finalStatus.equalsIgnoreCase("successful")) { showToast("Transaction still pending"); return; } showToast("Transaction Successful for id - " + finalPaymentID); // refundTheAmount(transactionID, finalAmount); } }); } }); } /** * Will initiate a refund for a given transaction with given amount * * @param transactionID Unique identifier for the transaction * @param amount amount to be refunded */ private void refundTheAmount(String transactionID, String amount) { if (accessToken == null || transactionID == null || amount == null) { return; } if (dialog != null && !dialog.isShowing()) { dialog.show(); } showToast("Initiating a refund for - " + amount); OkHttpClient client = new OkHttpClient(); HttpUrl url = getHttpURLBuilder() .addPathSegment("refund") .addPathSegment("") .build(); RequestBody body = new FormBody.Builder() .add("env", currentEnv.toLowerCase()) .add("transaction_id", transactionID) .add("amount", amount) .add("type", "PTH") .add("body", "Refund the Amount") .build(); okhttp3.Request request = new okhttp3.Request.Builder() .url(url) .addHeader("Authorization", "Bearer " + accessToken) .post(body) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { runOnUiThread(new Runnable() { @Override public void run() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } showToast("Failed to Initiate a refund"); } }); } @Override public void onResponse(Call call, final Response response) throws IOException { runOnUiThread(new Runnable() { @Override public void run() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } String message; if (response.isSuccessful()) { message = "Refund intiated successfully"; } else { message = "Failed to Initiate a refund"; } showToast(message); } }); } }); } private HttpUrl.Builder getHttpURLBuilder() { return new HttpUrl.Builder() .scheme("https") .host("sample-sdk-server.instamojo.com"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == Constants.REQUEST_CODE && data != null) { String orderID = data.getStringExtra(Constants.ORDER_ID); String transactionID = data.getStringExtra(Constants.TRANSACTION_ID); String paymentID = data.getStringExtra(Constants.PAYMENT_ID); Toast.makeText(this, "Payment success, toast from mobile", Toast.LENGTH_SHORT).show(); TrodlyHelperModel helperModel = new TrodlyHelperModel(); TrodlyHttpRequestHelper helper = new TrodlyHttpRequestHelper (context); Bundle params = new Bundle(); params.putString("totalCost", "121"); params.putString("noOfPerson", "12"); params.putString("date", "15-08-2017"); params.putString("firstName", "qqqq"); params.putString("lastName", "qqq"); params.putString("email", "qqq@gmail.com"); params.putString("phNo", "9944332211"); params.putString("txnId", "1188336655"); params.putString("activityId", "207"); params.putString("packageId", "266"); params.putString("address", "new address of testing"); params.putString("city", "Kanpur"); params.putString("state", "Jammu"); params.putString("country", "India"); params.putString("zipcode", "302222"); params.putString("adultPax", "1"); params.putString("childPax", "3"); params.putString("paymentID", "1166223355"); String url = "https://www.trodly.com/api/v0/activityBookingSuccess_redesign.php"; Toast.makeText(getApplicationContext(),"before post",Toast.LENGTH_LONG).show(); requestType(url, 21, params, TrodlyAsyncHttpRequest.Type.POST); // Check transactionID, orderID, and orderID for null before using them to check the Payment status. if (transactionID != null || paymentID != null) { checkPaymentStatus(transactionID, orderID); } else { showToast("Oops!! Payment was cancelled"); } } } protected void requestType(String url, int reqwat,Bundle params,TrodlyAsyncHttpRequest.Type requestType) { TrodlyAsyncHttpRequest mAppRequest = new TrodlyAsyncHttpRequest(this, url, params, reqwat, requestType); mAppRequest.setRequestListener((TrodlyAsyncHttpRequest.TrodlyRequestListener) this); mAppRequest.execute(); } @Override public void handleActivity(JSONObject res, int requestCode) { } @Override public void error(String msg) { } }
  
Markdown is allowed