在 Paypal Express 结帐中添加多个项目

Posted

技术标签:

【中文标题】在 Paypal Express 结帐中添加多个项目【英文标题】:Add multiple items in Paypal Express checkout 【发布时间】:2015-10-24 03:24:04 【问题描述】:

我想在这个 Java 代码中添加第二个项目,用于快速结帐:

public void initPayPal() 
    
        PaymentDetailsType paymentDetails = new PaymentDetailsType();
        paymentDetails.setPaymentAction(PaymentActionCodeType.SALE);
        PaymentDetailsItemType item = new PaymentDetailsItemType();
        BasicAmountType amt = new BasicAmountType();
        amt.setCurrencyID(CurrencyCodeType.fromValue("USD"));
        double itemAmount = 1.00;
        amt.setValue(String.valueOf(itemAmount));
        int itemQuantity = 1;
        item.setQuantity(itemQuantity);
        item.setName("item");
        item.setAmount(amt);

        List<PaymentDetailsItemType> lineItems = new ArrayList<PaymentDetailsItemType>();
        lineItems.add(item);
        paymentDetails.setPaymentDetailsItem(lineItems);
        BasicAmountType orderTotal = new BasicAmountType();
        orderTotal.setCurrencyID(CurrencyCodeType.fromValue("USD"));
        orderTotal.setValue(String.valueOf(itemAmount * itemQuantity));
        paymentDetails.setOrderTotal(orderTotal);
        List<PaymentDetailsType> paymentDetailsList = new ArrayList<PaymentDetailsType>();
        paymentDetailsList.add(paymentDetails);

        SetExpressCheckoutRequestDetailsType setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();
        setExpressCheckoutRequestDetails.setReturnURL("http://devtools-paypal.com/guide/expresscheckout/java?success=true");
        setExpressCheckoutRequestDetails.setCancelURL("http://devtools-paypal.com/guide/expresscheckout/java?cancel=true");

        setExpressCheckoutRequestDetails.setPaymentDetails(paymentDetailsList);

        SetExpressCheckoutRequestType setExpressCheckoutRequest = new SetExpressCheckoutRequestType(setExpressCheckoutRequestDetails);
        setExpressCheckoutRequest.setVersion("104.0");

        SetExpressCheckoutReq setExpressCheckoutReq = new SetExpressCheckoutReq();
        setExpressCheckoutReq.setSetExpressCheckoutRequest(setExpressCheckoutRequest);

        Map<String, String> sdkConfig = new HashMap<String, String>();
        sdkConfig.put("mode", "sandbox");
        sdkConfig.put("acct1.UserName", "peter.penzov_api1.gmail.com");
        sdkConfig.put("acct1.Password", "5LQB5QHQJVKP9QKN");
        sdkConfig.put("acct1.Signature", "AFcWxV21C7fd0v3bYYYRCps-s-rl31A6JqBNQNOE3.Jwxfs7nIV1jR7qcl");
        PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(sdkConfig);
        SetExpressCheckoutResponseType setExpressCheckoutResponse = service.setExpressCheckout(setExpressCheckoutReq);

        String token = setExpressCheckoutResponse.getToken();

        String link = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + token;

        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        externalContext.redirect(link);
    

如何将第二个项目添加到项目列表中:

这个问题有什么解决办法吗?

我找不到任何可以用来解决问题的好例子?

附言

我测试了这段代码:

public void initPayPal()
    
        PaymentDetailsType paymentDetails = new PaymentDetailsType();
        paymentDetails.setPaymentAction(PaymentActionCodeType.SALE);
        PaymentDetailsItemType item = new PaymentDetailsItemType();
        BasicAmountType amt = new BasicAmountType();
        amt.setCurrencyID(CurrencyCodeType.fromValue("USD"));
        amt.setValue(String.valueOf(totalPrice));
        int itemQuantity = 1;
        item.setQuantity(itemQuantity);
        item.setName("item 1");
        item.setAmount(amt);

        //this is now "item2"
        PaymentDetailsItemType item2 = new PaymentDetailsItemType();
        BasicAmountType amt2 = new BasicAmountType();
        amt2.setCurrencyID(CurrencyCodeType.fromValue("USD"));
        double itemAmount2 = 1.00;
        amt2.setValue(String.valueOf(itemAmount2));
        int itemQuantity2 = 1;
        item2.setQuantity(itemQuantity2);
        item2.setName("item 2");
        item2.setAmount(amt2);

        //add item2 to your lineItems List
        List<PaymentDetailsItemType> lineItems = new ArrayList<PaymentDetailsItemType>();
        lineItems.add(item);
        lineItems.add(item2);

        paymentDetails.setPaymentDetailsItem(lineItems);
        BasicAmountType orderTotal = new BasicAmountType();
        orderTotal.setCurrencyID(CurrencyCodeType.fromValue("USD"));
        orderTotal.setValue(String.valueOf(totalPrice * itemQuantity));
        paymentDetails.setOrderTotal(orderTotal);
        List<PaymentDetailsType> paymentDetailsList = new ArrayList<PaymentDetailsType>();
        paymentDetailsList.add(paymentDetails);

        SetExpressCheckoutRequestDetailsType setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();
        setExpressCheckoutRequestDetails.setReturnURL("http://52.25.168.28:8080/Web_site/summary_payment.xhtml");
        setExpressCheckoutRequestDetails.setCancelURL("http://devtools-paypal.com/guide/expresscheckout/java?cancel=true");

        setExpressCheckoutRequestDetails.setPaymentDetails(paymentDetailsList);

        SetExpressCheckoutRequestType setExpressCheckoutRequest = new SetExpressCheckoutRequestType(setExpressCheckoutRequestDetails);
        setExpressCheckoutRequest.setVersion("104.0");

        SetExpressCheckoutReq setExpressCheckoutReq = new SetExpressCheckoutReq();
        setExpressCheckoutReq.setSetExpressCheckoutRequest(setExpressCheckoutRequest);

        Map<String, String> sdkConfig = new HashMap<String, String>();
        sdkConfig.put("mode", "sandbox");
        sdkConfig.put("acct1.UserName", "peter.penzov_api1.gmail.com");
        sdkConfig.put("acct1.Password", "5LQB5QHQJVKP9QKN");
        sdkConfig.put("acct1.Signature", "AFcWxV21C7fd0v3bYYYRCps-s-rl31A6JqBNQNOE3.Jwxfs7nIV1jR7qcl");
        PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(sdkConfig);
        SetExpressCheckoutResponseType setExpressCheckoutResponse = service.setExpressCheckout(setExpressCheckoutReq);

        String token = setExpressCheckoutResponse.getToken();

        String link = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + token;

        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        externalContext.redirect(link);
    

得到错误This transaction is invalid. Please return to the recipient's website to complete your transaction using their regular checkout flow.

【问题讨论】:

【参考方案1】:

不是 Java 开发人员,但快速的答案是将另一个 PaymentDetailsItemType item 添加到您的列表 List&lt;PaymentDetailsItemType&gt; lineItems

所以取自上面你自己的代码:

//this is now "item2"
PaymentDetailsItemType item2 = new PaymentDetailsItemType(); 
BasicAmountType amt2 = new BasicAmountType();
amt2.setCurrencyID(CurrencyCodeType.fromValue("USD"));
double itemAmount2 = 1.00;
amt2.setValue(String.valueOf(itemAmount2));
int itemQuantity2 = 1;
item2.setQuantity(itemQuantity2);
item2.setName("item 2");
item2.setAmount(amt2);

//add item2 to your lineItems List
lineItems.add(item2);

第..

【讨论】:

我收到消息This transaction is invalid. Please return to the recipient's website to complete your transaction using their regular checkout flow. 您的附加项目会影响总数等,因此请查看您的代码 - 例如orderTotal.setValue(String.valueOf(totalPrice * itemQuantity)); 正确!我没有怀疑 PayPal API 中有对总金额的内部检查。不幸的是,没有办法看到这些简单错误的任何调试信息。

以上是关于在 Paypal Express 结帐中添加多个项目的主要内容,如果未能解决你的问题,请参考以下文章

Android:Paypal Express 结帐集成不起作用

使用 Activemerchant Gem 切换到多个商家帐户以进行 Paypal Express 结帐

实时服务器中的 Paypal Express 结帐问题

使用 AngularJS 的 Paypal Express 结帐

Express PayPal 结帐,无法创建 SSL/TLS 安全通道

Omnipay - 使用 Paypal Express 结帐 Laravel