在android中实现authorize.net

发布于 2024-08-22 01:55:35 字数 3879 浏览 3 评论 0原文

我想开发一个通过authorize.net进行付款处理的应用程序

,但每次我都会收到未知错误。

首先,我填充所有文本框并在按钮单击事件上使用这些值。
我的代码是:

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);
}

有人可以帮助我吗?

通过 setContentView 我显示了分割结果,并且只得到未知错误异常。没有显示其他描述。是我的方法错误还是有其他方法可以实现付款处理?

I want to develop an application which does payment processing through authorize.net

But everytime I get unknown error.

First I fill all the textboxes and use these values on a 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() +" ";
        tv.setText(result);
        //out.println("<LI>" + iter.next() + " </LI>");
    }

    setContentView(tv);
} catch(Exception e) {
    tv.setText(e.getMessage());
    setContentView(tv);
}

Can anyone help me?

through setContentView I am showing my splited result and I get only unknown error exception. No other description is shown. Is my method wrong or there is any other method to implement payment processing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清醇 2024-08-29 01:55:35

感谢您的代码:)我从您的代码中得到了在android中实现authorize.net的新想法,我不知道您遇到了什么错误,但是当我尝试使用post url进行集成时,我遇到了一个错误,并且我在其中添加了“x_market_type”我的代码和我得到的结果
<代码>

 post_values.put ("x_market_type", "2");

I have also integrated Sample code which authorize.net provide, but for my custom requirement your code helps me lot.

如果您需要任何帮助,请发表评论。 :)

Thanks for your code :) I got new idea form your code to implement authorize.net in android I have no idea what you faced error but when I tried to use post url for integration I faced one error and I have added "x_market_type" in my code and I got the result

 post_values.put ("x_market_type", "2");


I have also integrated Sample code which authorize.net provide, but for my custom requirement your code helps me lot.

If you need any help than post comment. :)

强者自强 2024-08-29 01:55:35

建议您使用 HTTPClient,如本示例所示
如何使用 cookie 发出 http 请求Android?

从我在你的代码中看到的:

  1. 本节将给出一个额外的 &在最后

    <块引用>

    while(keys.hasMoreElements()) {
    String key = URLEncoder.encode(keys.nextElement().toString(),"UTF-8");
    字符串值 = URLEncoder.encode(post_values.get(key).toString(),"UTF-8");
    post_string.append(key + "=" + value + "&");
    

    }

  2. 只读取了服务器返回的一行内容。尽管内容可能放在一行中,但 HTTP 标头将跨越几行
字符串responseData = rawResponse.readLine();
  1. 您使用服务器可能不接受的默认用户代理请求标头

如果您想尝试修复当前代码,我建议您对其进行调试或添加一些日志语句(您可以使用 aLogCat 应用程序等进行查看)

Recommend you use the HTTPClient instead as shown in this example
How do I make an http request using cookies on Android?

From what I can see in your code:

  1. This section will given an extra & at the end
    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 + "&");
    

    }

  2. You read only a single line of what has been returned by the server. Even though the content might be placed in one line, the HTTP headers will span several lines
String responseData = rawResponse.readLine();
  1. You use the default user-agent request header which may not be accepted by the server

If you want to try fixing your current code, I recommend you debug it or throw in some log statements (which you can view with for example the aLogCat application)

染年凉城似染瑾 2024-08-29 01:55:35

//Authorize.net android 实现使用 volley post。这可能对你有帮助,拉什米感谢你的代码,感谢巴文的错误帮助

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);

//Authorize.net android implementation using volley post. this might help you and rashmi thanks for the code and bhavin for the error help

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文