首页 > 解决方案 > 如何在 Paypal for java SDK 中获取付款人 ID?

问题描述

我试图在我的 java 应用程序中实现 PayPal,但是,我对很多事情感到困惑。

首先,您将如何以及从何处获得付款人 ID?例如,到目前为止,我有以下代码:

Payer Payer = new Payer();
                Payer.setPaymentMethod("paypal");

                // Redirect URLs
                RedirectUrls redirectUrls = new RedirectUrls();
                redirectUrls.setCancelUrl("http://localhost:3000/cancel");
                redirectUrls.setReturnUrl("http://localhost:3000/return");

                // Set Payment Details Object
                Details details = new Details();
                details.setShipping(shipping);
                details.setSubtotal(subtotal);
                details.setTax(tax);

                // Set Payment amount
                Amount amount = new Amount();
                amount.setCurrency("USD");
                amount.setTotal(totalPrice);
                amount.setDetails(details);

                // Set Transaction information
                Transaction transaction = new Transaction();
                transaction.setAmount(amount);
                transaction.setDescription("Shoe Raffle Ticket by Coppers Odds");
                List<Transaction> crunchifyTransactions = new ArrayList<Transaction>();
                crunchifyTransactions.add(transaction);

                // Add Payment details
                Payment payment = new Payment();

                // Set Payment intent to authorize
                payment.setIntent("authorize");
                payment.setPayer(Payer);
                payment.setTransactions(crunchifyTransactions);
                payment.setRedirectUrls(redirectUrls);

                // Pass the clientID, secret and mode. The easiest, and most widely used option.
                APIContext apiContext = new APIContext(clientID, secret, "sandbox");

//              List<String> scopes = new ArrayList<String>() {/**
//                   * 
//                   */
//                  private static final long serialVersionUID = 1L;
//
//              {
//                  /**
//                  * 'openid'
//                  * 'profile'
//                  * 'address'
//                  * 'email'
//                  * 'phone'
//                  * 'https://uri.paypal.com/services/paypalattributes'
//                  * 'https://uri.paypal.com/services/expresscheckout'
//                  * 'https://uri.paypal.com/services/invoicing'
//                  */
//                  add("openid");
//                  add("profile");
//                  add("email");
//              }};
//              
//              String redirectUrl = Session.getRedirectURL("UserConsent", scopes, apiContext);
//              System.out.println(redirectUrl);


                Payment myPayment = null;

                String authorizationId = null;

                try {

                    myPayment = payment.create(apiContext);

                    System.out.println("createdPayment Object Details ==> " + myPayment.toString());

                    // Identifier of the payment resource created

                    payment.setId(myPayment.getId());

                    PaymentExecution PaymentExecution = new PaymentExecution();

                    // Set your PayerID. The ID of the Payer, passed in the `return_url` by PayPal.

                    PaymentExecution.setPayerId(" ");

                    // This call will fail as user has to access Payment on UI. Programmatically
                    // there is no way you can get Payer's consent.

                    Payment createdAuthPayment = payment.execute(apiContext, PaymentExecution);

                    // Transactional details including the amount and item details.

                    Authorization Authorization = createdAuthPayment.getTransactions().get(0).getRelatedResources()
                            .get(0).getAuthorization();

                    authorizationId = Authorization.getId();

                    Edit_JSON.edit(currentShoe, amountofTickets + Edit_JSON.getOriginal(currentShoe));

                    LocalDate localDate = LocalDate.now();
                    com.pulsebeat02.main.gui.windows.payment.Payment paymentFinal = new com.pulsebeat02.main.gui.windows.payment.Payment("Bought Raffle Tickets",
                            DateTimeFormatter.ofPattern("yyy/MM/dd").format(localDate),
                            "Bought " + amountofTickets + " ticket(s)", "Bought with Paypal", price, null, true, account);

                    ManagePayments.allPayments.add(paymentFinal);

                } catch (PayPalRESTException e1) {

                    // The "standard" error output stream. This stream is already open and ready to
                    // accept output data.
                    Logger.LOG.error("Payment Exception");
                    System.err.println(e1.getDetails());
                }

这里的 PayerID 留空,导致错误ERROR com.paypal.base.HttpConnection - Response code: 400

此外,我仍然对重定向 url 和批准 url 的工作方式感到困惑。例如,这是我通过执行代码得到的付款 JSON 文件:

{
  "id": "PAYID-LUF4ZQI88510624KV359214L",
  "intent": "authorize",
  "payer": {
    "payment_method": "paypal"
  },
  "transactions": [
    {
      "related_resources": [],
      "amount": {
        "currency": "USD",
        "total": "1.30",
        "details": {
          "subtotal": "0.00",
          "shipping": "0.00",
          "tax": "0.00"
        }
      },
      "description": "Shoe Raffle Ticket by Coppers Odds"
    }
  ],
  "state": "created",
  "create_time": "2019-06-20T18:13:21Z",
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LUF4ZQI88510624KV359214L",
      "rel": "self",
      "method": "GET"
    },
    {
      "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd\u003d_express-checkout\u0026token\u003dEC-31C40815LT657700L",
      "rel": "approval_url",
      "method": "REDIRECT"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LUF4ZQI88510624KV359214L/execute",
      "rel": "execute",
      "method": "POST"
    }
  ]
}

我将如何使用 JSON 文件将买家重定向到 Paypal 网站并确保他们接受付款以便付款?

(我在这里寻求帮助是因为我发现该文档已被弃用且令人困惑)

标签: javapaypalsdk

解决方案


推荐阅读