コード例 #1
0
  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
  private Authorization getAuthorization(APIContext apiContext) throws PayPalRESTException {

    // ###Details
    // Let's you specify details of a payment amount.
    Details details = new Details();
    details.setShipping("0.03");
    details.setSubtotal("107.41");
    details.setTax("0.03");

    // ###Amount
    // Let's you specify a payment amount.
    Amount amount = new Amount();
    amount.setCurrency("USD");
    amount.setTotal("107.47");
    amount.setDetails(details);

    // ###Transaction
    // A transaction defines the contract of a
    // payment - what is the payment for and who
    // is fulfilling it. Transaction is created with
    // a `Payee` and `Amount` types
    Transaction transaction = new Transaction();
    transaction.setAmount(amount);
    transaction.setDescription("This is the payment transaction description.");

    // 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);

    // ###Address
    // Base Address object used as shipping or billing
    // address in a payment. [Optional]
    Address billingAddress = new Address();
    billingAddress.setCity("Johnstown");
    billingAddress.setCountryCode("US");
    billingAddress.setLine1("52 N Main ST");
    billingAddress.setPostalCode("43210");
    billingAddress.setState("OH");

    // ###CreditCard
    // A resource representing a credit card that can be
    // used to fund a payment.
    CreditCard creditCard = new CreditCard();
    creditCard.setBillingAddress(billingAddress);
    creditCard.setCvv2("874");
    creditCard.setExpireMonth(11);
    creditCard.setExpireYear(2018);
    creditCard.setFirstName("Joe");
    creditCard.setLastName("Shopper");
    creditCard.setNumber("4417119669820331");
    creditCard.setType("visa");

    // ###FundingInstrument
    // A resource representing a Payeer's funding instrument.
    // Use a Payer ID (A unique identifier of the payer generated
    // and provided by the facilitator. This is required when
    // creating or using a tokenized funding instrument)
    // and the `CreditCardDetails`
    FundingInstrument fundingInstrument = new FundingInstrument();
    fundingInstrument.setCreditCard(creditCard);

    // The Payment creation API requires a list of
    // FundingInstrument; add the created `FundingInstrument`
    // to a List
    List<FundingInstrument> fundingInstruments = new ArrayList<FundingInstrument>();
    fundingInstruments.add(fundingInstrument);

    // ###Payer
    // A resource representing a Payer that funds a payment
    // Use the List of `FundingInstrument` and the Payment Method
    // as 'credit_card'
    Payer payer = new Payer();
    payer.setFundingInstruments(fundingInstruments);
    payer.setPaymentMethod("credit_card");

    // ###Payment
    // A Payment Resource; create one using
    // the above types and intent as 'authorize'
    Payment payment = new Payment();
    payment.setIntent("authorize");
    payment.setPayer(payer);
    payment.setTransactions(transactions);

    Payment responsePayment = payment.create(apiContext);
    return responsePayment.getTransactions().get(0).getRelatedResources().get(0).getAuthorization();
  }