Paypal添加到购物车按钮 - 如何使用Java生成?

时间:2011-07-05 14:34:03

标签: java encryption button paypal

我正在使用Paypal NVP API和BMCreateButton API来生成带有Java代码的加密按钮。

我已经找到了最简单的按钮形式。例如,对于成本为8.00的T恤,生成按钮的代码是(请记住,这只是按钮变量的一部分) -

//...    
    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");
//...

有人可以帮帮我吗?谢谢:)

1 个答案:

答案 0 :(得分: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