private String makePayment(String accessToken) {
    Map<String, String> sdkConfig = new HashMap<String, String>();
    sdkConfig.put("mode", "sandbox");

    // String accessToken = "Bearer A015XqgSXpttQxsNdAzq6DiMBx8oWx8p3Jt2wEyK-OmExEA";
    APIContext apiContext = new APIContext(accessToken);
    apiContext.setConfigurationMap(sdkConfig);

    Amount amount = new Amount();
    amount.setCurrency("USD");
    amount.setTotal("25");

    Transaction transaction = new Transaction();
    transaction.setDescription("Creating a payment for $" + amount.getTotal());
    transaction.setAmount(amount);

    List<Transaction> transactions = new ArrayList<Transaction>();
    transactions.add(transaction);

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

    Payment payment = new Payment();
    payment.setIntent("sale");
    payment.setPayer(payer);
    payment.setTransactions(transactions);
    RedirectUrls redirectUrls = new RedirectUrls();
    redirectUrls.setCancelUrl("https://devtools-paypal.com/guide/pay_paypal?cancel=true");
    redirectUrls.setReturnUrl("https://devtools-paypal.com/guide/pay_paypal?success=true");
    payment.setRedirectUrls(redirectUrls);

    String redirectLink = null;
    try {
      Payment createdPayment = payment.create(apiContext);
      List<Links> links = createdPayment.getLinks();

      for (Links link : links) {
        if ("redirect".equals(link.getMethod().toLowerCase())) {
          redirectLink = link.getHref();
        }
      }

      System.out.println();
    } catch (PayPalRESTException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return redirectLink;
  }
示例#2
0
 /**
  * Creates (and processes) a new Capture Transaction added as a related resource.
  *
  * @param apiContext {@link APIContext} used for the API call.
  * @param capture Capture
  * @return Capture
  * @throws PayPalRESTException
  */
 public Capture capture(APIContext apiContext, Capture capture) throws PayPalRESTException {
   if (apiContext == null) {
     throw new IllegalArgumentException("APIContext cannot be null");
   }
   if (apiContext.getAccessToken() == null || apiContext.getAccessToken().trim().length() <= 0) {
     throw new IllegalArgumentException("AccessToken cannot be null or empty");
   }
   if (apiContext.getHTTPHeaders() == null) {
     apiContext.setHTTPHeaders(new HashMap<String, String>());
   }
   apiContext
       .getHTTPHeaders()
       .put(Constants.HTTP_CONTENT_TYPE_HEADER, Constants.HTTP_CONTENT_TYPE_JSON);
   apiContext.setSdkVersion(new SDKVersionImpl());
   if (this.getId() == null) {
     throw new IllegalArgumentException("Id cannot be null");
   }
   if (capture == null) {
     throw new IllegalArgumentException("capture cannot be null");
   }
   Object[] parameters = new Object[] {this.getId()};
   String pattern = "v1/payments/orders/{0}/capture";
   String resourcePath = RESTUtil.formatURIPath(pattern, parameters);
   String payLoad = capture.toJSON();
   return configureAndExecute(apiContext, HttpMethod.POST, resourcePath, payLoad, Capture.class);
 }