0

I was trying to implement PayPal into my java application, however, I was confused about multiple things.

First, how and where would you get the payer ID? For example, I have the following code so far:

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());
                }

The PayerID here is left blank, and causes the error ERROR com.paypal.base.HttpConnection - Response code: 400

Also, I am still confused with how the redirect urls and approval urls work. For example, this is the payment JSON file I got from executing the code:

{
  "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"
    }
  ]
}

How would I use the JSON file to redirect the buyer to the Paypal website and make sure they accept the payment so they can pay?

(I seeked help here because I found the doc to be deprecated and confusing)

Brandon Li
  • 55
  • 8
  • The payer_id is appended as a parameter to the redirect_url once they approve of the payment. https://developer.paypal.com/docs/integration/direct/payments/paypal-payments/ Checkout also: https://stackoverflow.com/questions/7121574/what-is-a-paypal-payer-id – BugsForBreakfast Jun 20 '19 at 19:42
  • @BugsForBreakfast I know payer_id would be appended to redirect_url, but how would you get a via a curl command or some type of command? (I'm trying to use this website to help me: [link](https://crunchify.com/paypal-java-sdk-tutorial-authorization-call/)) – Brandon Li Jun 20 '19 at 21:59
  • Did you get the solution of this? I don't really know how you get the response, is it a JSON or how is the data coming from PayPal answer when somebody purchases? – BugsForBreakfast Jun 21 '19 at 13:51
  • I just saw the JSON you have in your question, isn't that all the info you need mate? There is the id, and down there I can see some urls that have the respective methods like redirect, post, get – BugsForBreakfast Jun 21 '19 at 13:55
  • @BugsForBreakfast Does that I mean I just use the links that are generated in the JSON file? When I copy the links into the browser, I get: {"name":"AUTHENTICATION_FAILURE","message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]} (Sorry I am bad at this) – Brandon Li Jun 21 '19 at 20:36
  • That makes sense to me, Id do that bro, but also just to be sure you should get in contact with paypal support for this – BugsForBreakfast Jun 21 '19 at 20:39
  • @BugsForBreakfast Wait, do I need to use curl to get the access_token for this to work because I didn't do that. – Brandon Li Jun 21 '19 at 21:00

0 Answers0