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

时间:2015-08-01 15:05:50

标签: java paypal paypal-adaptive-payments paypal-rest-sdk

我想在我用于快速结账的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", "AFcWxV21C7fd0v3bYYYRCpSSRl31A6JqBNQNOE3.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);
    }

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

enter image description here

这个问题有解决办法吗?

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

PS

我测试了这段代码:

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", "AFcWxV21C7fd0v3bYYYRCpSSRl31A6JqBNQNOE3.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 个答案:

答案 0 :(得分:1)

不是Java开发人员,但快速回答是在列表 $('.thumb-image').each(function (i, obj) { if ($(this).width() > $(this).eq(i + 1).width()) { $('.thumb-image').slice(i + 1).each(function () { $(this).css({ float: "left", width: "50%", padding: "0", margin: "0" }); }); } });

中添加另一个PaymentDetailsItemType item

从您自己的代码中获取:

List<PaymentDetailsItemType> lineItems

H个..