6

I'm using the Paypal NVP API, along with the BMCreateButton API, to generate encrypted buttons with my Java code.

I've got the simplest form of a button figured out. So as an example, for a T-Shirt that costs 8.00, the code to generate the button is (keep in mind, this is a snippet of the button variables part only) --

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

That's simple enough - but realistically, products have other options. A T-Shirt may have color and size options, which would appear as html <select> menus on the page. Plus, each color/size option would have a different price.

This is where I'm getting stuck. Between the HTML Variable Reference and BMCreateButton API pages on Paypal, I'm confused!

The Html code that should be outputted with select menu options would be like this -

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

How do I code that?

The best I could come up with - but didn't work, of course - was this -

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

Can someone please help me out? Thank You:)

katura
  • 2,177
  • 14
  • 39
  • 48

1 Answers1

7

After a few correspondences with Paypal via their Merchant Support site, I finally got the answer I needed. By the way, if you ever have a problem with Paypal's API's, and after looking tirelessly on their site, you still haven't found the answer you need (blame their poor organization and lack of good, thorough, documentation for that) - I urge you to contact their Technical and/or Developer Support through their Merchant Support site. Its pretty much the only way to get an answer!

If you're a Java developer like me, this code should come in handy for you too.

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
katura
  • 2,177
  • 14
  • 39
  • 48
  • I'm trying to use the BMCreateButton API in .Net, exactly the way you do in java. I setup my sandbox business account, I got API credentials, I wrote the code, it works, I get the BMCreateButton answer back, but when I put the HTML (generated by BMCreateButton) on a web page: When I click the "Buy now" button I'm taken to the paypal website, which actually SHOW all the value (item name, item price...) BUT it say "There was a problem with the decryption of your secure order. Please contact your merchant." Any idea about where is the error? I didn't setup any SSL certificate, does this matter? – Max Apr 02 '12 at 15:18
  • Here is the code I'm using http://stackoverflow.com/questions/9939960/how-to-use-the-bmcreatebutton-nvp-paypal-api-to-create-encrypted-paynow-button I'm trying to create a simple encrypted PayNow button. Do you have any idea about what I'm doing wrong? – Max Apr 03 '12 at 07:43