2010-02-22 38 views
3

我想開發一個應用程序,通過authorize.net進行支付處理在android中實現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我顯示我的分裂結果,我只得到未知的錯誤異常。沒有顯示其他描述。我的方法是錯誤的還是有其他方法來執行付款處理?

+0

如果你不提供更多關於錯誤的信息,我們不能幫你。它僅僅是「未知的錯誤」,還是有錯誤代碼或任何其他信息?另外,你爲什麼要從'Button' onClick方法中調用'setContentView'? –

回答

0

推薦您使用HttpClient的,而不是如本例所示 How do I make an http request using cookies on Android?

從我可以在你的代碼中看到:

  1. 本節將給出額外&末
    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. 您只讀取了服務器返回的內容的一行。即使內容可能被放置在一個線,所述HTTP標頭將跨越幾個行
String responseData = rawResponse.readLine(); 
  • 您使用默認的用戶代理請求報頭可能不是由服務器
  • 如果你想嘗試修理你當前的代碼被接受,我建議你調試它或在某些日誌語句(你可以查看例如aLogCat應用程序)

    1

    感謝您的代碼:)我有新的想法形式的代碼來實現android中的authorize.net我不知道你面對的錯誤,但是當我試圖使用post url進行集成我遇到了一個錯誤,我添加了「 x_market_type「在我的代碼中,我得到了結果

     
    post_values.put ("x_market_type", "2"); 
    
    我也集成了authorize.net提供的示例代碼,但對於我的自定義要求,您的代碼可以幫助我很多。

    如果您需要任何幫助,請發表評論。 :)

    +0

    嗨Bhavin,我需要集成authrozed.net在我的應用程序..如何集成使用登錄ID和交易密鑰?我試圖發送的細節,但我得到E0003錯誤。 – Saravanan

    +0

    我沒有得到任何迴應結果,我剛剛得到了「onPostExecute: |&nbsp」 –

    0

    //Authorize.net android實現使用齊射。這可能會幫助你和rashmi感謝代碼和bhavin的錯誤幫助

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