如何在Paypal for Java SDK中获得付款人ID?

时间:2019-06-20 18:59:35

标签: java paypal sdk

我试图在我的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网站,并确保他们接受付款以便可以付款?

(我在这里寻求帮助是因为我发现文档已过时且令人困惑)

0 个答案:

没有答案