@Test public void testCreditCardUnknownFileConfiguration() { try { CreditCard.initConfig(new File("unknown.properties")); } catch (PayPalRESTException e) { Assert.assertEquals(e.getCause().getClass().getSimpleName(), "FileNotFoundException"); } }
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; }
private String getAccessToken() { String accessToken = null; Map<String, String> sdkConfig = new HashMap<String, String>(); sdkConfig.put("mode", "sandbox"); try { accessToken = new OAuthTokenCredential( "AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd", "EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX", sdkConfig) .getAccessToken(); } catch (PayPalRESTException e) { // TODO Auto-generated catch block e.printStackTrace(); } return accessToken; }
/** * Utility method that creates a {@link PayPalRESTException} object from {@link * HttpErrorException}. if {@link HttpErrorException} contains 400 response code, error response * is converted to {@link Error} object. * * @param httpErrorException {@link HttpErrorException} thrown from API call * @return PayPalRESTException */ protected static PayPalRESTException createFromHttpErrorException( HttpErrorException httpErrorException) { PayPalRESTException ppre = new PayPalRESTException(httpErrorException.getMessage(), httpErrorException); ppre.setResponsecode(httpErrorException.getResponsecode()); if (httpErrorException.getResponsecode() >= 400 && httpErrorException.getErrorResponse() != null && isJSONValid(httpErrorException.getErrorResponse())) { try { Error details = JSONFormatter.fromJSON(httpErrorException.getErrorResponse(), Error.class); ppre.setDetails(details); } catch (Exception e) { log.error( "Exception thrown while parsing error response: " + httpErrorException.getErrorResponse(), e); } } return ppre; }
public PayoutBatch createBatchPayout(HttpServletRequest req, HttpServletResponse resp) { // ###Payout // A resource representing a payout // This is how our body should look like: /* * { "sender_batch_header": { "sender_batch_id": "random_uniq_id", * "email_subject": "You have a payment" }, "items": [ { * "recipient_type": "EMAIL", "amount": { "value": 0.99, "currency": * "USD" }, "receiver": "*****@*****.**", "note": * "Thank you.", "sender_item_id": "item_1" }, { "recipient_type": * "EMAIL", "amount": { "value": 0.90, "currency": "USD" }, "receiver": * "*****@*****.**", "note": "Thank you.", * "sender_item_id": "item_2" }, { "recipient_type": "EMAIL", "amount": * { "value": 2.00, "currency": "USD" }, "receiver": * "*****@*****.**", "note": "Thank you.", * "sender_item_id": "item_3" } ] } */ Payout payout = new Payout(); PayoutSenderBatchHeader senderBatchHeader = new PayoutSenderBatchHeader(); // ### NOTE: // You can prevent duplicate batches from being processed. If you // specify a `sender_batch_id` that was used in the last 30 days, the // batch will not be processed. For items, you can specify a // `sender_item_id`. If the value for the `sender_item_id` is a // duplicate of a payout item that was processed in the last 30 days, // the item will not be processed. // #### Batch Header Instance Random random = new Random(); senderBatchHeader .setSenderBatchId(new Double(random.nextDouble()).toString()) .setEmailSubject("You have a Payment!"); // ### Currency Currency amount1 = new Currency(); amount1.setValue("1.00").setCurrency("USD"); // ### Currency Currency amount2 = new Currency(); amount2.setValue("2.00").setCurrency("USD"); // ### Currency Currency amount3 = new Currency(); amount3.setValue("4.00").setCurrency("USD"); // #### Sender Item 1 // Please note that if you are using single payout with sync mode, you // can only pass one Item in the request PayoutItem senderItem1 = new PayoutItem(); senderItem1 .setRecipientType("Email") .setNote("Thanks for your patronage") .setReceiver("*****@*****.**") .setSenderItemId("201404324234") .setAmount(amount1); // #### Sender Item 1 // Please note that if you are using single payout with sync mode, you // can only pass one Item in the request PayoutItem senderItem2 = new PayoutItem(); senderItem2 .setRecipientType("Email") .setNote("Thanks for your patronage") .setReceiver("*****@*****.**") .setSenderItemId("201404324235") .setAmount(amount2); // #### Sender Item 1 // Please note that if you are using single payout with sync mode, you // can only pass one Item in the request PayoutItem senderItem3 = new PayoutItem(); senderItem3 .setRecipientType("Email") .setNote("Thanks for your patronage") .setReceiver("*****@*****.**") .setSenderItemId("201404324236") .setAmount(amount3); List<PayoutItem> items = new ArrayList<PayoutItem>(); items.add(senderItem1); items.add(senderItem2); items.add(senderItem3); payout.setSenderBatchHeader(senderBatchHeader).setItems(items); PayoutBatch batch = null; try { // ### Api Context // Pass in a `ApiContext` object to authenticate // the call and to send a unique request id // (that ensures idempotency). The SDK generates // a request id if you do not pass one explicitly. APIContext apiContext = new APIContext(clientID, clientSecret, mode); // ###Create Batch Payout batch = payout.create(apiContext, new HashMap<String, String>()); LOGGER.info("Payout Batch With ID: " + batch.getBatchHeader().getPayoutBatchId()); ResultPrinter.addResult( req, resp, "Payout Batch Create", Payout.getLastRequest(), Payout.getLastResponse(), null); } catch (PayPalRESTException e) { ResultPrinter.addResult( req, resp, "Payout Batch Create", Payout.getLastRequest(), null, e.getMessage()); } return batch; }