Implementing authorize.net in android

I want to develop an application that processes processing through authorize.net

But every time I get an unknown error.

First, I fill in all the text fields and use these values ​​in the button click event.
My code is:

txtAmount=Double.parseDouble(Amount.getText().toString()); txtFName = FName.getText().toString(); txtLName=LName.getText().toString(); txtAddress=Address.getText().toString(); txtCity=City.getText().toString(); txtState=State.getText().toString(); txtzipcode=zipcode.getText().toString(); txtEmail=Email.getText().toString(); txtCreditCard=CreditCard.getText().toString(); txtCVV2=CVV2.getText().toString(); drpMonth=selectedmonth; drpYear=selectedyear; date= drpMonth.concat(drpYear); try { URL post_url = new URL("https://test.authorize.net/gateway/transact.dll"); Hashtable post_values = new Hashtable(); // the API Login ID and Transaction Key must be replaced with valid values post_values.put ("x_login", "8SX5gkJb46g"); post_values.put ("x_tran_key", "8Wx295Gr4hd9Y5kd"); post_values.put ("x_version", "3.1"); post_values.put ("x_delim_data", "TRUE"); post_values.put ("x_delim_char", "|"); post_values.put ("x_relay_response", "FALSE"); post_values.put ("x_type", "AUTH_CAPTURE"); post_values.put ("x_method", "CC"); post_values.put ("x_card_num", txtCreditCard); post_values.put ("x_exp_date", date); post_values.put ("x_amount", txtAmount); post_values.put ("x_description", "Sample Transaction"); post_values.put ("x_first_name",txtFName); post_values.put ("x_last_name",txtLName); post_values.put ("x_address", txtAddress); post_values.put ("x_city", txtCity); post_values.put ("x_state",txtState); post_values.put ("x_zip", txtzipcode); post_values.put ("x_email", txtEmail); // Additional fields can be added here as outlined in the AIM integration // guide at: http://developer.authorize.net // This section takes the input fields and converts them to the proper format // for an http post. For example: "x_login=username&x_tran_key=a1B2c3D4" StringBuffer post_string = new StringBuffer(); Enumeration keys = post_values.keys(); while( keys.hasMoreElements() ) { String key = URLEncoder.encode(keys.nextElement().toString(),"UTF-8"); String value = URLEncoder.encode(post_values.get(key).toString(),"UTF-8"); post_string.append(key + "=" + value + "&"); } // Open a URLConnection to the specified post url URLConnection connection = post_url.openConnection(); connection.setDoOutput(true); connection.setUseCaches(false); // this line is not necessarily required but fixes a bug with some servers connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); // submit the post_string and close the connection DataOutputStream requestObject = new DataOutputStream(connection.getOutputStream()); requestObject.write(post_string.toString().getBytes()); requestObject.flush(); requestObject.close(); // process and read the gateway response BufferedReader rawResponse = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; String responseData = rawResponse.readLine(); rawResponse.close(); // no more data // split the response into an array String [] responses = responseData.split("|"); // The results are output to the screen in the form of an html numbered list. for(Iterator iter=Arrays.asList(responses).iterator(); iter.hasNext();) { result="\n"+ iter.next() +"&nbsp"; tv.setText(result); //out.println("<LI>" + iter.next() + "&nbsp;</LI>"); } setContentView(tv); } catch(Exception e) { tv.setText(e.getMessage()); setContentView(tv); } 

Can anybody help me?

via setContentView I show my split result and get only an unknown error exception. No other description is displayed. Is my method incorrect or is there any other method for processing payments?

+3
source share
3 answers

Thanks for your code :) I got a new idea by creating my own code for implementing authorize.net in android. I have no idea that you encountered an error, but when I tried to use the post url for integration, I encountered one error, and I added "x_market_type" in my code, and I got the result

 post_values.put ("x_market_type", "2"); 
I also integrated the sample code that authorize.net provides, but for my custom requirement your code helps me a lot.

If you need help than comment. :)

+1
source

We recommend using HTTPClient instead, as shown in this example. How do I make an HTTP request using cookies on Android?

From what I see in your code:

  • This section provides additional and final
     while( keys.hasMoreElements() ) { String key = URLEncoder.encode(keys.nextElement().toString(),"UTF-8"); String value = URLEncoder.encode(post_values.get(key).toString(),"UTF-8"); post_string.append(key + "=" + value + "&"); 

    }

  • You read only one line of what was returned by the server. Although content can be placed on one line, HTTP headers will span multiple lines
 String responseData = rawResponse.readLine(); 
  1. You are using the default user agent request header, which cannot be accepted by the server

If you want to try to fix your current code, I recommend that you debug it or paste it into some log statements (which you can view, for example, using the aLogCat application)

0
source

// Implementation of the browser authorize.net using volleyball. this can help you and rashmi thanks for the code and bhavin for helping with the error

 final String txtAmount=totalprice.getText().toString(); final String txtFName = first_name; final String txtLName=last_name; final String txtAddress=address_1; final String txtCity=city; final String txtState=state; final String txtzipcode=postcode; final String txtEmail=email; final String txtCreditCard=CardNumber; String txtCVV2=Cvv; String drpMonth=Month; String drpYear=Year; final String date= drpMonth.concat(drpYear); String url = Uri.parse("https://test.authorize.net/gateway/transact.dll").buildUpon() .build().toString(); StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e("====", response); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } try { JSONObject jsonObject = new JSONObject(response); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("====", error.toString()); if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> post_values = new HashMap<String, String>(); post_values.put ("x_login", "8RtP63yV"); post_values.put ("x_tran_key", "9A4a59AThQh4c6wG"); post_values.put ("x_version", "3.1"); post_values.put ("x_delim_data", "TRUE"); post_values.put ("x_delim_char", "|"); post_values.put ("x_relay_response", "FALSE"); post_values.put ("x_market_type", "2"); post_values.put ("x_type", "AUTH_CAPTURE"); post_values.put ("x_method", "CC"); post_values.put ("x_card_num", txtCreditCard); post_values.put ("x_exp_date", date); post_values.put ("x_amount", txtAmount); post_values.put ("x_description", "Sample Transaction"); post_values.put ("x_first_name",txtFName); post_values.put ("x_last_name",txtLName); post_values.put ("x_address", txtAddress); post_values.put ("x_city", txtCity); post_values.put ("x_state",txtState); post_values.put ("x_zip", txtzipcode); post_values.put ("x_email", txtEmail); return post_values; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); 
0
source

Source: https://habr.com/ru/post/1489721/


All Articles