Paypal Java SDK支付问题

Paypal Java SDK支付问题,java,spring,api,paypal,payment-gateway,Java,Spring,Api,Paypal,Payment Gateway,我正在尝试将Papal与我的SpringWeb服务集成。我正在引用和使用这个 这是我的客户端代码 <script src="https://www.paypalobjects.com/api/checkout.js"> </script> <h1>Paypal Integration - Advanced Server Side Integration</h1> <div id="paypal-button-container"><

我正在尝试将Papal与我的SpringWeb服务集成。我正在引用和使用这个

这是我的客户端代码

<script src="https://www.paypalobjects.com/api/checkout.js">
</script>
<h1>Paypal Integration - Advanced Server Side Integration</h1>
<div id="paypal-button-container"></div>
<script>
// Render the PayPal button

paypal.Button.render({

    // Set your environment

    env: 'sandbox', // sandbox | production

    // Wait for the PayPal button to be clicked

    payment: function() {

        // Make a call to the merchant server to set up the payment

        return paypal.request.post('http://localhost:8080/api/createpayment').then(function(res) {
            console.log(res);
            return res.payToken;
        });
    },

    // Wait for the payment to be authorized by the customer

    onAuthorize: function(data, actions) {

        // Make a call to the merchant server to execute the payment

        return paypal.request.post('http://localhost:8080/api/createpayment', {
            payToken: data.paymentID,
            payerId: data.payerID
        }).then(function(res) {
            console.log(res);
            document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
        });
    }

}, '#paypal-button-container');
</script>

Paypal集成-高级服务器端集成
//渲染贝宝按钮
paypal.Button.render({
//设置您的环境
env:'沙盒',//沙盒|生产
//等待贝宝按钮被点击
支付:功能(){
//致电商户服务器设置付款
返回paypal.request.post('http://localhost:8080/api/createpayment)。然后(函数(res){
控制台日志(res);
返回res.payToken;
});
},
//等待客户授权付款
onAuthorize:函数(数据、操作){
//致电商户服务器以执行付款
返回paypal.request.post('http://localhost:8080/api/createpayment', {
payToken:data.paymentID,
payerId:data.payerId
}).然后(功能(res){
控制台日志(res);
document.querySelector(“#paypal按钮容器”).innerText=“支付完成!”;
});
}
}“#贝宝按钮容器”);
这是我的网络服务

@RequestMapping(value = "/createpayment", method = RequestMethod.POST)
    public @ResponseBody 
    Payment createPayment(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = new HashMap<String, String>();

        Payment createdPayment = null;
        try {

            final String clientID = "<clientId>";
            final String clientSecret = "clientSecret";
            // ### Api Context
            // Pass in a `ApiContext` object to authenticate
            // the call and to send a unique request id
            // (that ensures idempotency). The SDK generates
            // a request id if you do not pass one explicitly.
            APIContext apiContext = new APIContext(clientID, clientSecret, "sandbox");
            if (request.getParameter("PayerID") != null) {
                logger.info("Payment Execution");
                Payment payment = new Payment();
                if (request.getParameter("guid") != null) {
                    payment.setId(map.get(request.getParameter("guid")));
                }

                PaymentExecution paymentExecution = new PaymentExecution();
                paymentExecution.setPayerId(request.getParameter("PayerID"));


                createdPayment = payment.execute(apiContext, paymentExecution);
                logger.info("Executed Payment - Request :: \n " + Payment.getLastRequest());
                logger.info("Exceuted Payment - Response :; \n " + Payment.getLastResponse());
                //ResultPrinter.addResult(req, resp, "Executed The Payment", Payment.getLastRequest(), Payment.getLastResponse(), null);

                //ResultPrinter.addResult(req, resp, "Executed The Payment", Payment.getLastRequest(), null, e.getMessage());

            } else {
                logger.info("Create Payment");
                // ###Details
                // Let's you specify details of a payment amount.
                Details details = new Details();
                //details.setShipping("1");
                details.setSubtotal("0.1");
                //details.setTax("1");

                // ###Amount
                // Let's you specify a payment amount.
                Amount amount = new Amount();
                amount.setCurrency("USD");
                // Total must be equal to sum of shipping, tax and subtotal.
                amount.setTotal("0.1");
                amount.setDetails(details);

                // ###Transaction
                // A transaction defines the contract of a
                // payment - what is the payment for and who
                // is fulfilling it. Transaction is created with
                // a `Payee` and `Amount` types
                Transaction transaction = new Transaction();
                transaction.setAmount(amount);
                transaction.setDescription("This is the payment transaction description.");

                // ### Items
                Item item = new Item();
                item.setName("Item for Purchase").setQuantity("1").setCurrency("USD").setPrice("0.1");
                ItemList itemList = new ItemList();
                List<Item> items = new ArrayList<Item>();
                items.add(item);
                itemList.setItems(items);

                transaction.setItemList(itemList);


                // The Payment creation API requires a list of
                // Transaction; add the created `Transaction`
                // to a List
                List<Transaction> transactions = new ArrayList<Transaction>();
                transactions.add(transaction);

                // ###Payer
                // A resource representing a Payer that funds a payment
                // Payment Method
                // as 'paypal'
                Payer payer = new Payer();
                payer.setPaymentMethod("paypal");

                // ###Payment
                // A Payment Resource; create one using
                // the above types and intent as 'sale'
                Payment payment = new Payment();
                payment.setIntent("sale");
                payment.setPayer(payer);
                payment.setTransactions(transactions);

                // ###Redirect URLs
                RedirectUrls redirectUrls = new RedirectUrls();
                String guid = UUID.randomUUID().toString().replaceAll("-", "");
                redirectUrls.setCancelUrl(request.getScheme() + "://"
                        + request.getServerName() + ":" + request.getServerPort()
                        + request.getContextPath() + "/paymentwithpaypal?guid=" + guid);
                redirectUrls.setReturnUrl(request.getScheme() + "://"
                        + request.getServerName() + ":" + request.getServerPort()
                        + request.getContextPath() + "/paymentwithpaypal?guid=" + guid);
                payment.setRedirectUrls(redirectUrls);

                // Create a payment by posting to the APIService
                // using a valid AccessToken
                // The return object contains the status;
                try {
                    createdPayment = payment.create(apiContext);
                    logger.info("Created payment with id = "
                            + createdPayment.getId() + " and status = "
                            + createdPayment.getState());
                    // ###Payment Approval Url
                    Iterator<Links> links = createdPayment.getLinks().iterator();
                    while (links.hasNext()) {
                        Links link = links.next();
                        if (link.getRel().equalsIgnoreCase("approval_url")) {
                            request.setAttribute("redirectURL", link.getHref());
                        }
                    }
                    //ResultPrinter.addResult(req, resp, "Payment with PayPal", Payment.getLastRequest(), Payment.getLastResponse(), null);
                    map.put(guid, createdPayment.getId());
                } catch (PayPalRESTException e) {
                    e.printStackTrace();
                    //ResultPrinter.addResult(req, resp, "Payment with PayPal", Payment.getLastRequest(), null, e.getMessage());
                }
            }
        } catch (Exception e) {
            logger.error("Create Payment Exception ");
            e.printStackTrace();
        }
        return createdPayment;
    }
@RequestMapping(value=“/createpayment”,method=RequestMethod.POST)
公共@ResponseBody
Payment createPayment(HttpServletRequest请求,HttpServletResponse响应){
Map Map=newhashmap();
Payment createdPayment=null;
试一试{
最终字符串clientID=“”;
最后一个字符串clientSecret=“clientSecret”;
//####Api上下文
//传入'ApiContext'对象以进行身份验证
//调用并发送唯一的请求id
//(这确保了幂等性)
//如果未显式传递请求id,则为请求id。
APIContext APIContext=新的APIContext(clientID、clientSecret、“沙盒”);
if(request.getParameter(“PayerID”)!=null){
logger.info(“付款执行”);
付款=新付款();
if(request.getParameter(“guid”)!=null){
payment.setId(map.get(request.getParameter(“guid”));
}
PaymentExecution PaymentExecution=新的PaymentExecution();
paymentExecution.setPayerId(request.getParameter(“PayerID”);
createdPayment=payment.execute(apiContext,paymentexecute);
logger.info(“已执行的付款-请求::\n”+Payment.getLastRequest());
logger.info(“超额付款-响应:;\n”+Payment.getLastResponse());
//ResultPrint.addResult(req,resp,“已执行付款”,Payment.getLastRequest(),Payment.getLastResponse(),null);
//resultPrint.addResult(req,resp,“已执行付款”,Payment.getLastRequest(),null,e.getMessage());
}否则{
logger.info(“创建付款”);
//####详情
//让我们指定付款金额的详细信息。
详细信息=新的详细信息();
//详细信息。设置装运(“1”);
详细信息。设置分项(“0.1”);
//详细信息。设定税收(“1”);
//金额
//让我们指定一个付款金额。
金额=新金额();
金额。设定货币(“美元”);
//总计必须等于运费、税金和小计之和。
总金额(“0.1”);
金额。设置详细信息(详细信息);
//####交易
//事务定义了一个事务的契约
//付款-付款对象是什么?付款对象是谁
//正在实现它。事务是使用创建的
//“收款人”和“金额”类型
事务=新事务();
交易。设置金额(金额);
transaction.setDescription(“这是付款交易描述”);
//####项目
项目=新项目();
项目。设置名称(“采购项目”)。设置数量(“1”)。设置货币(“美元”)。设置价格(“0.1”);
ItemList ItemList=新的ItemList();
列表项=新建ArrayList();
项目。添加(项目);
itemList.setItems(项目);
transaction.setItemList(itemList);
//付款创建API需要一个
//事务;添加创建的“事务”`
//列入清单
列表事务=新建ArrayList();
交易。添加(交易);
//付款人
//一种资源,表示为付款提供资金的付款人
//付款方式
//作为“贝宝”
付款人付款人=新付款人();
付款人设置支付方式(“paypal”);
//支付
//支付资源;使用
//上述类型和意图为“销售”
付款=新付款();
支付意图(“出售”);
付款。设置付款人(付款人);
支付。设置交易(交易);
//####重定向URL
RedirectUrls RedirectUrls=新的重定向URL();
字符串guid=UUID.randomUUID().toString().replaceAll(“-”,”);
redirectUrls.setCancelUrl(request.getScheme()+“:/”
+request.getServerName()+“:”+request.getServerPort()
+request.getContextPath()+“/paymentwithpaypal?guid=“+guid”);
redirectUrls.setReturnUrl(request.getScheme()+“:/”
+request.getServerName()+“:”+request.getServerPort()
+request.getContextPath()+“/paymentwithpaypal?guid=”+
@CrossOrigin
@RequestMapping(value = "/createpayment", method = RequestMethod.POST)
    public @ResponseBody 
    Payment createPayment(HttpServletRequest request, HttpServletResponse response) {
.....
...