2011-07-05 66 views
6

我使用PayPal NVP API以及BMCreateButton API來生成帶有Java代碼的加密按鈕。Paypal添加到購物車按鈕 - 如何使用Java生成?

我已經找到了最簡單的按鈕形式。因此,作爲一個例子,對於一個T恤,售價8.00,生成按鈕的代碼(記住,這只是按鈕的變量部分的片段) -

//...  
    NVPEncoder encoder = new NVPEncoder(); 
    encoder.add("METHOD", "BMCreateButton"); 
    encoder.add("BUTTONCODE","ENCRYPTED"); 
    encoder.add("BUTTONTYPE","CART"); 
    encoder.add("L_BUTTONVAR1","amount=8.00"); 
    encoder.add("L_BUTTONVAR2","item_number=6985855"); 
    encoder.add("L_BUTTONVAR3","item_name=T-Shirt"); 
//... 

這是很簡單 - 但實際上,產品還有其他選擇。 T恤可以有顏色和尺寸選項,這些選項在頁面上顯示爲html<select>菜單。另外,每種顏色/尺寸選項都會有不同的價格。

這就是我陷入困境的地方。在Paypal上的HTML Variable ReferenceBMCreateButton API頁面之間,我很困惑!

的HTML應與選擇菜單選項輸出的代碼會是這樣的 -

<input type="hidden" name="on0" value="Color &amp; Size">Color &amp; Size 
<input type="hidden" name="option_select0" value="Pink Small" /> 
<input type="hidden" name="option_amount0" value="6.00" /> 
<input type="hidden" name="option_select1" value="Pink Medium" /> 
<input type="hidden" name="option_amount1" value="7.00" /> 
<input type="hidden" name="option_select2" value="Pink Large" /> 
<input type="hidden" name="option_amount2" value="8.00" /> 

<select name="os0"> 
    <option value="Pink Small">Pink - Small $6.00 - (13)</option> 
    <option value="Pink Medium">Pink - Medium $7.00</option> 
    <option value="Pink Large">Pink - Large $8.00</option> 
</select> 

如何代碼?

我能拿出最好的 - 但沒有工作,當然 - 是這樣的 -

//... 
    NVPEncoder encoder = new NVPEncoder(); 
    encoder.add("METHOD", "BMCreateButton"); 
    encoder.add("BUTTONCODE","ENCRYPTED"); 
    encoder.add("BUTTONTYPE","CART"); 
    encoder.add("L_BUTTONVAR1","item_number=6985855"); 
    encoder.add("L_BUTTONVAR2","item_name=Dress"); 
    encoder.add("L_BUTTONVAR3","on0=Color & Size"); 
    encoder.add("L_BUTTONVAR4","option_select0=Pink Small"); 
    encoder.add("L_BUTTONVAR5","option_amount0=6.00"); 
    encoder.add("L_BUTTONVAR6","option_select1=Pink Medium"); 
    encoder.add("L_BUTTONVAR7","option_amount1=7.00"); 
    encoder.add("L_BUTTONVAR8","option_select2=Pink Large"); 
    encoder.add("L_BUTTONVAR9","option_select2=8.00"); 

    encoder.add("OPTION0NAME","Color & Size"); 
    encoder.add("L_OPTION0SELECT0","Pink Small"); 
    encoder.add("L_OPTION0PRICE0","6.00"); 
    encoder.add("L_OPTION0SELECT1","Pink Medium"); 
    encoder.add("L_OPTION0PRICE1","7.00"); 
    encoder.add("L_OPTION0SELECT2","Pink Large"); 
    encoder.add("L_OPTION0PRICE2","8.00"); 
//... 

是否有人可以幫助我嗎?謝謝你:)

回答

7

在通過他們的Merchant Support網站與PayPal的幾個通信後,我終於得到了我需要的答案。順便說一下,如果您在使用Paypal的API時遇到問題,並且在他們的網站上不知疲倦地尋找之後,仍然沒有找到您需要的答案(歸咎於他們組織糟糕,缺乏良好,徹底的文檔) - 我敦促您通過他們的Merchant Support網站聯繫他們的技術和/或開發者支持。它幾乎是獲得答案的唯一方法!

如果您是一位像我這樣的Java開發人員,那麼這個代碼也可以派上用場。

public static String createEncryptedButton(PrintWriter out) throws Exception {   
    String returnResult = "";   
    NVPEncoder encoder = new NVPEncoder(); 

    encoder.add("METHOD","BMCreateButton"); 

    encoder.add("BUTTONCODE","ENCRYPTED"); 
    encoder.add("BUTTONTYPE","CART"); 
    encoder.add("BUTTONSUBTYPE","PRODUCTS");   
    encoder.add("L_BUTTONVAR0","business="+businessEmail); //use your sandbox or paypal email 
    encoder.add("L_BUTTONVAR1","item_name=Dress"); 
    encoder.add("L_BUTTONVAR2","item_number=100100"); 
    encoder.add("OPTION0NAME","Color and Size"); 
    encoder.add("L_OPTION0SELECT0","Pink Small"); 
    encoder.add("L_OPTION0PRICE0","6.00"); 
    encoder.add("L_OPTION0SELECT1","Pink Medium"); 
    encoder.add("L_OPTION0PRICE1","7.00"); 
    encoder.add("L_OPTION0SELECT2","Pink Large"); 
    encoder.add("L_OPTION0PRICE2","8.00"); 

    String strNVPString = encoder.encode(); 
    String ppresponse = call(strNVPString,out); 
    NVPDecoder results = new NVPDecoder(); 
    results.decode(ppresponse);     

    String buttonCode = results.get("WEBSITECODE"); 

    out.print("the code is :"+buttonCode);    

    return returnResult; 
} 

    public static String call(String payload, PrintWriter out) throws Exception { 

//Remember to setup your API credentials, whether you're using Sandbox 
//for testing or Paypal when you go live 
String USER = "yourUsername"; //API Username 
String PWD = "yourPassword"; //API Password 
String SIGNATURE = "yourSignature"; //API Signature 
String VERSION = "74.0"; //Version numbers differ from Paypal and Sandbox site. Do View > Source and look in source code for current version number under each site. 

StringBuffer request = new StringBuffer(); 
request.append("USER="+USER+"&PWD="+PWD+"&SIGNATURE="+SIGNATURE+"&VERSION="+VERSION); 
request.append("&"); 


//this is for Sandbox testing 
//when you go live with paypal, switch it to 
//https://api-3t.paypal.com/nvp 
URL url = new URL("https://api-3t.sandbox.paypal.com/nvp"); 

     HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 
     connection.setRequestProperty("Content-Type", "text/namevalue"); 
     DataOutputStream outst = new DataOutputStream(connection.getOutputStream());   
     outst.write(request.toString().getBytes()); 
     outst.close(); 

     // Read the gateway response 
     BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     StringBuffer sb = new StringBuffer(); 
     String line; 
     while ((line = in.readLine()) != null) { 
      sb.append(line); 
     } 
     in.close(); 
     return sb.toString(); 
    } // call 
+0

我正在嘗試在.Net中使用BMCreateButton API,就像在java中一樣。我設置了我的沙盒商業帳戶,我有API憑證,我寫了代碼,它工作正常,我得到了BMCreateButton回答,但是當我將HTML(由BMCreateButton生成)放在網頁上時:當我點擊「立即購買「按鈕我被帶到了貝寶網站,它實際上顯示了所有的價值(項目名稱,項目價格...)但它說:」解密安全訂單時出現問題,請與您的商家聯繫。「任何關於錯誤在哪裏的想法?我沒有設置任何SSL證書,這是否重要? – Max

+0

這是我使用的代碼http://stackoverflow.com/questions/9939960/how-to-use-the-bmcreatebutton-nvp-paypal-api-to-create-encrypted-paynow-button我在嘗試創建一個簡單的加密PayNow按鈕。你有什麼想法我做錯了嗎? – Max

相關問題