在Java中使用Paypal [英] Use paypal with java

查看:95
本文介绍了在Java中使用Paypal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中使用贝宝作为付款流程.

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

在paypal上,他们展示了许多使用REST API,经典api以及使用SOAP API的方法.

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

我正在尝试通过以下方式使用SOAP客户端.

I am trying to use SOAP client by following way.

1.我正在尝试使用贝宝提供的wsdl文件创建一个SOAP Web服务客户端.但是,尽管创建客户端文件时,将客户端创建为无法重新获得该网址"时给我一个错误.

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.为proxyClass创建的对象,因为代理类包含所有必需的方法,例如getAuthDetails,setExpressCheckout,setEndPoint等.

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

3.因此,这些方法包含诸如DetailLevelCodeType [] detailLevel,MessageElement [] _ any之类的参数.那么它的价值是什么?

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

4.根据我的理解,如果我要对Payapal进行任何操作,它应该询问我第一个身份验证参数,例如用户名,秘密,令牌,帐户密码. 对于使用rest api的Paypal,我知道通过提供用户凭据来获取访问令牌.

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.

在下一个主题中,他们将说明,您的SOAP客户端必须设置 用户名,密码元素,用于传递API用户名/密码 SOAP请求标头中的组合.

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.

此过程包括编组.

//到目前为止使用的软件包

//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();
        }




    }

但是我发现缺少名为ObjectFactory的文件.

But I observed a file is missing named ObjectFactory .

我也包括了Merchant api sdk的jar文件.在我的网络服务中 客户端此文件不存在.

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

因此有人可以在这种情况下指导我为什么文件没有得到 从wsdl文件创建客户端时创建的文件?

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

我也曾尝试使用wsdl到Java插件来创建Web服务客户端,但这给了我

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

wsdlexception错误代码other_error,也说 FileNotFoundException

wsdlexception faultcode other_error ,it's also saying FileNotFoundException

,找不到https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl/eBLBaseComponents.xsd

推荐答案

免责声明:尽管大约2年前我设法使用了PayPal Web服务API,但这样做有点麻烦,因此,我建议您使用REST API,因为PayPal似乎无法使Web服务API保持一致.

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.

但是,我将尝试为您提供有关如何使用PayPal中的Web服务API的提示: 看来您所做的一切正确,问题出在PayPalSvc.wsdl文件中,因为它引用了eBLBaseComponents.xsd文件,该文件应位于PayPalSvc.wsdl文件旁边.如果您在浏览器中导航到网址

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.

CoreComponentTypes.xsdEnhancedDataTypes.xsd文件也相同,这些文件也被PayPalSvc.wsdl文件引用.

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

现在,我迅速尝试使用Apache CXFs wsdl2java实用程序生成客户端类,它给了我同样的错误.我不知道为什么这些实用程序期望这些xsd文件位于https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl/位置.

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. PayPalSvc.wsdlCoreComponentTypes.xsdeBLBaseComponents.xsdEnhancedDataTypes.xsd文件下载到您的计算机
  2. 将它们全部放入计算机上的单个文件夹中
  3. 再次运行该实用程序,指向本地PayPalSvc.wsdl文件
  4. 如果显示file not found错误,请尝试在网络上找到该文件,然后下载并重试
  5. 如果有任何问题,请添加评论,我(或其他人)将尝试提供帮助
  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...

这篇关于在Java中使用Paypal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆