微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Paypal Java SDK-尝试创建加密按钮-不起作用,找不到文档

如何解决Paypal Java SDK-尝试创建加密按钮-不起作用,找不到文档

|| 我最近收到了达蒙·威廉姆斯(damon Williams)的书“ Pro Paypal E-Commerce”。它是2007年的副本,因此可以预期,随着时间的推移,某些事情(例如代码)会发生变化。 我正在尝试使下面的代码正常工作。我下载了paypal_base.jar文件,还下载了paypal_wpstoolkit.jar,并将它们放入jakarta-tomcat下的lib文件夹中(我的所有其他jar都放在其中)。我在编译代码时遇到问题。 此代码示例来自本书,也来自http://en.csharp-online.net/Encrypted_Website_Payments%E2%80%94Using_the_PayPal_Java_SDK 我做了些微修改
import com.paypal.sdk.profiles.EWPProfile;
import com.paypal.sdk.profiles.ProfileFactory;

import com.paypal.wpstoolkit.services.EWPServices;

import com.paypal.sdk.exceptions.PayPalException;


    public class PaypalTest {


      // path to your PKCS12 file
      public static final String PKCS12 = \"./Certs/my_pkcs12.p12\";

      // path to PayPal\'s public certificate
      public static final String PAYPAL_CERT = \"./Certs/paypal_cert_pem.txt\";

      // use https://www.sandBox.paypal.com if testing
      //public static final String URL = \"https://www.paypal.com\";
      public static final String URL = \"https://sandBox.paypal.com\";

      public static void main (String args[]) {

        // Check to see if the user provided a password
        if (args.length != 1) {
          System.out.println(\"You must provide a password.\");
          System.exit(0);
        }

        // password used to encrypt your PKCS12 files
        // obtained from the command line
        String USER_PASSWORD = args[0];


        // First we will create the EWPProfile object
        try {
          com.paypal.sdk.profiles.EWPProfile ewpProfile = ProfileFactory.createEWPProfile();

          ewpProfile.setCertificateFile(PKCS12);
          ewpProfile.setPayPalCertificateFile(PAYPAL_CERT);
          ewpProfile.setPrivateKeyPassword(USER_PASSWORD);
          ewpProfile.setUrl(URL);

          String buttonParameters = \"cmd=_xclick\\nbusiness=buyer@hotmail.com\\nitem_name=vase\\nitemprice=25.00\";

          // Next we will create the EWPServices object
          // and tell it which EWPProfile object to use
          EWPServices ewpServices = new EWPServices();
          ewpServices.setEWPProfile(ewpProfile);

          // Finally we are ready to call the method to perform the button encryption
          String encryptedButton = ewpServices.encryptButton(buttonParameters.getBytes());

          System.out.println(encryptedButton);
        } catch (PayPalException ppe) {
          System.out.println(\"An exception occurred when creating the button.\");
          ppe.printstacktrace();
        }       

    }

}//class    
我在编译过程中遇到的错误如下-
java:51: cannot find symbol
symbol: method setEWPProfile(com.paypal.sdk.profiles.EWPProfile)
location: class com.paypal.wpstoolkit.services.EWPServices
ewpServices.setEWPProfile(ewpProfile);


java:55: encryptButton(byte[],java.lang.String,java.lang.String.,java.lang.String) in com.paypal.wpstoolkit.services.EWPServices cannot be applied to (byte[])
ewpServices.encryptButton(buttonParameters.getBytes());
paypal_base jar
中只有
NVPCallerServices.class
,而没有
EWPServices
。 EWPServices在ѭ5中。 如何解决我的错误?我在查找有关贝宝类的文档时遇到了麻烦。     

解决方法

        可以在以下位置找到更新的Java SDK + API文档: https://cms.paypal.com/cms_content/US/zh_CN/files/developer/PP_Java_NVP_SDK.zip 解压缩该.zip并打开docs / index.html 在那可以找到所有API文档。似乎您正在尝试调用不再存在的方法。浏览一下新课程,看看有什么适合您的。     ,        看起来,使用较新API的Paypal时,您希望从其Web服务生成所有按钮代码,因为他们似乎已从SDK中删除了“ 6”类。但是后来我注意到它们仍然为您提供客户端实用程序,您可以在此处手动生成代码。稍作调整后,我就在那里得到了执行所需代码的代码(在本地加密上传购物车按钮)。 假设您使用Java 5+,只需确保在类路径中使用this和this。现在的代码已经不完美了,因为它包含了许多不赞成使用的方法,但是对于像加密按钮代码这样的琐碎任务,它就可以正常工作。
public String getButtonEncryptionValue(String _data,String _privateKeyPath,String _certPath,String _payPalCertPath,String _keyPass) throws IOException,CertificateException,KeyStoreException,UnrecoverableKeyException,InvalidAlgorithmParameterException,NoSuchAlgorithmException,NoSuchProviderException,CertStoreException,CMSException {
    _data = _data.replace(\',\',\'\\n\');
    CertificateFactory cf = CertificateFactory.getInstance(\"X509\",\"BC\");

    // Read the Private Key
    KeyStore ks = KeyStore.getInstance(\"PKCS12\",\"BC\");
    ks.load(new FileInputStream(_privateKeyPath),_keyPass.toCharArray());

    String keyAlias = null;
    Enumeration<String> aliases = ks.aliases();
    while (aliases.hasMoreElements()) {
        keyAlias = aliases.nextElement();
    }

    PrivateKey privateKey = (PrivateKey) ks.getKey(keyAlias,_keyPass.toCharArray());

    // Read the Certificate
    X509Certificate certificate = (X509Certificate) cf
            .generateCertificate(new FileInputStream(_certPath));

    // Read the PayPal Cert
    X509Certificate payPalCert = (X509Certificate) cf
            .generateCertificate(new FileInputStream(_payPalCertPath));

    // Create the Data
    byte[] data = _data.getBytes();

    // Sign the Data with my signing only key pair
    CMSSignedDataGenerator signedGenerator = new CMSSignedDataGenerator();

    signedGenerator.addSigner(privateKey,certificate,CMSSignedDataGenerator.DIGEST_SHA1);

    ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
    certList.add(certificate);
    CertStore certStore = CertStore.getInstance(\"Collection\",new CollectionCertStoreParameters(certList));
    signedGenerator.addCertificatesAndCRLs(certStore);

    CMSProcessableByteArray cmsByteArray = new CMSProcessableByteArray(data);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    cmsByteArray.write(baos);
    System.out.println(\"CMSProcessableByteArray contains [\"
            + baos.toString() + \"]\");

    CMSSignedData signedData = signedGenerator.generate(cmsByteArray,true,\"BC\");

    byte[] signed = signedData.getEncoded();

    CMSEnvelopedDataGenerator envGenerator = new CMSEnvelopedDataGenerator();
    envGenerator.addKeyTransRecipient(payPalCert);
    CMSEnvelopedData envData = envGenerator.generate(
            new CMSProcessableByteArray(signed),CMSEnvelopedDataGenerator.DES_EDE3_CBC,\"BC\");

    byte[] pkcs7Bytes = envData.getEncoded();

    return new String(DERtoPEM(pkcs7Bytes,\"PKCS7\"));

}

public static byte[] DERtoPEM(byte[] bytes,String headfoot) {
    ByteArrayOutputStream pemStream = new ByteArrayOutputStream();
    PrintWriter writer = new PrintWriter(pemStream);

    byte[] stringBytes = Base64.encode(bytes);

    System.out.println(\"Converting \" + stringBytes.length + \" bytes\");

    String encoded = new String(stringBytes);

    if (headfoot != null) {
        writer.print(\"-----BEGIN \" + headfoot + \"-----\\n\");
    }

    // write 64 chars per line till done
    int i = 0;
    while ((i + 1) * 64 < encoded.length()) {
        writer.print(encoded.substring(i * 64,(i + 1) * 64));
        writer.print(\"\\n\");
        i++;
    }
    if (encoded.length() % 64 != 0) {
        writer.print(encoded.substring(i * 64)); // write remainder
        writer.print(\"\\n\");
    }
    if (headfoot != null) {
        writer.print(\"-----END \" + headfoot + \"-----\\n\");
    }
    writer.flush();
    return pemStream.toByteArray();
}
    ,        一种更简单的方法不是加密,而是使用未加密的按钮,然后使用哈希技巧来检测篡改。我在这里用PHP进行了解释,但是您可以翻译成Java。 如何制作带有自定义字段的PayPal加密的立即购买按钮?     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。