Writing a PayPal SOAP client with Java 6
2011-02-18I have always been mystified on the inner workings of SOAP. That was until I learned about the "wsimport" utility that comes with Java 6. It makes the entire process very easy. Below is an example of writing a SOAP client for PayPal’s Sandbox. This code will execute the SetExpressCheckout API call.
Just enter the following on your command line to generate the com.javazquez package
wsimport -keep -XadditionalHeaders -Xnocompile -p com.javazquez http://www.sandbox.paypal.com/wsdl/PayPalSvc.wsdl
open your favorite java editor(I used eclipse) and add the package(“com.javazquezâ€..created in the above command) to your new project
next, write some code to test out the APIs
package com.javazquez;
import javax.xml.ws.Holder;
public class TestEC {
public static void main(String[] args) {
SetExpressCheckoutReq req = new SetExpressCheckoutReq();
SetExpressCheckoutRequestType reqType = new SetExpressCheckoutRequestType();
SetExpressCheckoutRequestDetailsType details = new SetExpressCheckoutRequestDetailsType();
AddressType addr = new AddressType();
addr.cityName = "omaha";
addr.street1 = "123 main";
addr.country = CountryCodeType.US;
addr.name = "joe tester";
details.address = addr;
details.orderTotal = new BasicAmountType();
details.orderTotal.currencyID = CurrencyCodeType.USD;
details.orderTotal.value = "1.00";
details.cancelURL = "http://javazquez.com/cancel";
details.returnURL = "http://javazquez.com/return";
reqType.setVersion("2.10");
reqType.setExpressCheckoutRequestDetails = details;
req.setSetExpressCheckoutRequest(reqType);
UserIdPasswordType user = new UserIdPasswordType();
user.username = "XXX";
user.password = "XXXX";
user.signature = "XXXX";
PayPalAPIInterfaceService pp = new PayPalAPIInterfaceService();
PayPalAPIAAInterface pinterface = pp.getPayPalAPIAA();
Holder security = new Holder(new CustomSecurityHeaderType();
security.value.setCredentials(user);
try{
SetExpressCheckoutResponseType resp = pinterface.setExpressCheckout(req, security);
System.out.println(resp.token);
System.out.println(resp.correlationID);
for(ErrorType msg: resp.errors){
System.out.println(msg.longMessage);
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}