Use paypal with java

2019-07-20 05:32发布

I want to use paypal as a payment process in my application.

At paypal they have shown many ways as using REST API, classic api, and also using SOAP API.

I am trying to use SOAP client by following way.

1.I am trying to create a SOAP web service client using paypal's provided wsdl file. But it's giving me error while creating client as ' unable to retieve that url',though it creates client files.

2.Created object for proxyClass, as proxy class contains all required methods like getAuthDetails,setExpressCheckout,setEndPoint etc.

3.So these method contains parameters like DetailLevelCodeType[]detailLevel,MessageElement[]_any. so what will be the value for it ?

4.As per my understanding if I am performing any operation on payapal, it should ask me first authentication parameters like username,secret, token, password of my account. When it comes to paypal using rest api, I am aware about getting access token by providing user credentials.

So in soap web service client how to get that first? I didin't get any method available here for that. 

But I have got one way to perform authorization from this link. security credentials .

So I followed these steps, now I have my username,password.

In next topic they are explaining , your SOAP client must set the Username, Password elements to pass an API username/password combination in the SOAP request header.

So I am trying in this way to add username,password in request header reference.

This process includes marshalling.

//packages used so far

import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPHeader;




    public static void main(String[] args) {
        // TODO Auto-generated method stub


        PayPalAPIAAInterfaceProxy proxy=new PayPalAPIAAInterfaceProxy();

    //  proxy.setEndpoint("");

        // following class is generated by wsdl2java utility Service class
        final PayPalAPIInterfaceService payPalService = new PayPalAPIInterfaceServiceLocator();
        PayPalAPIAAInterface expressCheckoutPort;
        try {
            expressCheckoutPort = payPalService.getPayPalAPIAA();
            final Binding binding = ((BindingProvider) expressCheckoutPort).getBinding();
            List<Handler> handlersList = new ArrayList<Handler>();

            // now, adding instance of Handler to handlersList which should do our job:
            // creating header instance
            final CustomSecurityHeaderType headerObj = new CustomSecurityHeaderType();
            final UserIdPasswordType credentials = new UserIdPasswordType();
            credentials.setUsername("username");
            credentials.setPassword("password");
            credentials.setSignature("signature");
            headerObj.setCredentials(credentials);


            // bookmark #1 - please read explanation after code
            final ObjectFactory objectFactory = new ObjectFactory();
            // creating JAXBElement from headerObj
            final JAXBElement<CustomSecurityHeaderType> requesterCredentials = objectFactory.createRequesterCredentials(headerObj);

            handlersList.add(new SOAPHandler<SOAPMessageContext>() {
                @Override
                public boolean handleMessage(final SOAPMessageContext context) {        
                    try {
                        // checking whether handled message is outbound one as per Martin Strauss answer
                        final Boolean outbound = (Boolean) context.get("javax.xml.ws.handler.message.outbound");
                        if (outbound != null && outbound) {
                            // obtaining marshaller which should marshal instance to xml
                            final Marshaller marshaller = JAXBContext.newInstance(CustomSecurityHeaderType.class).createMarshaller();
                            // adding header because otherwise it's null
                            final SOAPHeader soapHeader = context.getMessage().getSOAPPart().getEnvelope().addHeader();
                            // marshalling instance (appending) to SOAP header's xml node
                            marshaller.marshal(requesterCredentials, soapHeader);
                        }
                    } catch (final Exception e) {
                        throw new RuntimeException(e);
                    }
                    return true;
                }

                // ... default implementations of other methods go here

            });

            // as per Jean-Bernard Pellerin's comment setting handlerChain list here, after all handlers were added to list
            binding.setHandlerChain(handlersList);


        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

But I observed a file is missing named ObjectFactory .

I have included Merchant api sdk's jar file also. And in my web service client this file is not present.

So can anybody guide me in this context that why file is not getting created when creating client from wsdl file ?

I have also tried to create web service client using wsdl to java plugin, but it is giving me error as

wsdlexception faultcode other_error ,it's also saying FileNotFoundException

, unable to find https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl/eBLBaseComponents.xsd

3条回答
我只想做你的唯一
2楼-- · 2019-07-20 05:37

My suggestion is best way is through REST API

Try this Link: https://devtools-paypal.com/guide/pay_paypal

Hope this Helps you

查看更多
Deceive 欺骗
3楼-- · 2019-07-20 05:40

Disclaimer: though I managed to use PayPal web services API around 2 years ago, it was kind of cumbersome to do, so I'll advise you to use REST API, since it looks like PayPal fails to keep web service API consistent.

However, I'll try to give you hints on how to use web service API from PayPal: It seems you are doing everything right, the problem is in PayPalSvc.wsdl file, because it references eBLBaseComponents.xsd file which should be located next to PayPalSvc.wsdl file. If you navigate to url in browser

https://www.paypalobjects.com/wsdl/eBLBaseComponents.xsd

you'll see that that file exists at that location.

Same goes for CoreComponentTypes.xsd and EnhancedDataTypes.xsd files which are also referenced by PayPalSvc.wsdl file.

Now, I quickly tried to use Apache CXFs wsdl2java utility to generate client classes and it gave me the same error you have. I don't know why those utilities are expecting those xsd files to be located in https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl/ location.

So, what you can do to bypass this problem is to:

  1. Download PayPalSvc.wsdl, CoreComponentTypes.xsd, eBLBaseComponents.xsd and EnhancedDataTypes.xsd files to your computer
  2. Put them all into single folder on your computer
  3. Run the utility again pointing to the local PayPalSvc.wsdl file
  4. If file not found error is shown, try to find that file on the web, download it and try again
  5. In case of any problems - add comments, I (or somebody else) will try to help

Hope this helps...

查看更多
The star\"
4楼-- · 2019-07-20 06:04

PayPal provides libs for Java which makes the integration a lot easier for you.

http://repo1.maven.org/maven2/com/paypal/sdk/

I used merchantsdk and paypal-core for my express-checkout integration.

查看更多
登录 后发表回答