贝宝(Paypal)付款成功执行,但款项并未从买方帐户添加到卖方的帐户

时间:2018-09-21 13:33:10

标签: java spring paypal paypal-sandbox paypal-rest-sdk

因此,在这里,我借助paypal的api文档创建了一个非常简单的程序。代码很简单。程序可以正常运行,甚至可以通过返回URL执行付款时显示成功响应。

但是该金额未从卖方帐户添加到商家帐户。任何帮助,将不胜感激。谢谢!

@GetMapping()
public String paypalPay(HttpServletRequest req) {
    String redirectUrl = null;
    APIContext apiContext = new APIContext(clientID, clientSecret, mode);

    // Set payment details
    Details details = new Details();
    details.setShipping("1");
    details.setSubtotal("5");
    details.setTax("1");

    // Payment amount
    Amount amount = new Amount();
    amount.setCurrency("USD");
    // Total must be equal to sum of shipping, tax and subtotal.
    amount.setTotal("7");
    amount.setDetails(details);

    // Transaction information
    Transaction transaction = new Transaction();
    transaction.setAmount(amount);
    transaction.setDescription("This is the payment transaction description.");

    // ### Items
    Item item = new Item();
    item.setName("Ground Coffee 40 oz").setQuantity("1").setCurrency("USD").setPrice("5");
    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");

    // Add payment details
    Payment payment = new Payment();
    payment.setIntent("sale");
    payment.setPayer(payer);
    // payment.setRedirectUrls(redirectUrls);
    payment.setTransactions(transactions);

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

    // Create payment
    try {
        Payment createdPayment = payment.create(apiContext);

        // ###Payment Approval Url
        Iterator<Links> links = createdPayment.getLinks().iterator();
        while (links.hasNext()) {
            Links link = links.next();
            if (link.getRel().equalsIgnoreCase("approval_url")) {
                redirectUrl = link.getHref();
            }
        }

    } catch (PayPalRESTException e) {
        System.err.println(e.getDetails());
        return "redirect:/paypal/error";
    }
    if (redirectUrl != null) {
        return "redirect:" + redirectUrl;
    } else {
        return "redirect:/paypal/error";
    }
}

@GetMapping("/payment/success")
@ResponseBody
public String executePayment(HttpServletRequest req) {
    APIContext apiContext = new APIContext(clientID, clientSecret, mode);

    Payment payment = new Payment();
    payment.setId(req.getParameter("paymentId"));

    PaymentExecution paymentExecution = new PaymentExecution();
    paymentExecution.setPayerId(req.getParameter("PayerID"));
    try {
      Payment createdPayment = payment.execute(apiContext, paymentExecution);
      System.out.println(createdPayment);
      return "Success";
    } catch (PayPalRESTException e) {
      System.err.println(e.getDetails());
      return "Failed";
    }
} 

0 个答案:

没有答案