Guest User

Untitled

a guest
Jan 24th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. public class PayPalTest {
  2. private static final Logger LOG = LoggerFactory.getLogger(PayPalTest.class);
  3.  
  4. private static final String CLIENT_ID = "";
  5. private static final String SECRET = "";
  6. private static final String MODE = "sandbox";
  7.  
  8. @Test
  9. public void createPayoutBatchTest() throws PayPalRESTException {
  10. createPayoutBatch(createPayout());
  11. }
  12.  
  13. @Test
  14. public void getPayoutItemStatusTest() throws PayPalRESTException {
  15.  
  16. final PayoutBatch batch = createPayoutBatch(createPayout());
  17.  
  18. String payoutBatchId = batch.getBatchHeader().getPayoutBatchId();
  19.  
  20. // ### Api Context
  21. // Pass in a `ApiContext` object to authenticate
  22. // the call and to send a unique request id
  23. // (that ensures idempotency). The SDK generates
  24. // a request id if you do not pass one explicitly.
  25. APIContext apiContext = new APIContext(CLIENT_ID, SECRET, MODE);
  26.  
  27. // ###Get Payout Batch Status
  28. PayoutBatch response = Payout.get(apiContext, payoutBatchId);
  29.  
  30. LOG.info("Payout Batch With ID: " + response.getBatchHeader().getPayoutBatchId());
  31.  
  32. assertEquals(response.getItems().size(), 3);
  33. }
  34.  
  35. private Payout createPayout() {
  36. // ###Payout
  37. // A resource representing a payout
  38. // This is how our body should look like:
  39. /*
  40. * { "sender_batch_header": { "sender_batch_id": "random_uniq_id",
  41. * "email_subject": "You have a payment" }, "items": [ {
  42. * "recipient_type": "EMAIL", "amount": { "value": 0.99, "currency":
  43. * "USD" }, "receiver": "shirt-supplier-one@mail.com", "note":
  44. * "Thank you.", "sender_item_id": "item_1" }, { "recipient_type":
  45. * "EMAIL", "amount": { "value": 0.90, "currency": "USD" }, "receiver":
  46. * "shirt-supplier-two@mail.com", "note": "Thank you.",
  47. * "sender_item_id": "item_2" }, { "recipient_type": "EMAIL", "amount":
  48. * { "value": 2.00, "currency": "USD" }, "receiver":
  49. * "shirt-supplier-three@mail.com", "note": "Thank you.",
  50. * "sender_item_id": "item_3" } ] }
  51. */
  52. Payout payout = new Payout();
  53.  
  54. PayoutSenderBatchHeader senderBatchHeader = new PayoutSenderBatchHeader();
  55.  
  56. // ### NOTE:
  57. // You can prevent duplicate batches from being processed. If you
  58. // specify a `sender_batch_id` that was used in the last 30 days, the
  59. // batch will not be processed. For items, you can specify a
  60. // `sender_item_id`. If the value for the `sender_item_id` is a
  61. // duplicate of a payout item that was processed in the last 30 days,
  62. // the item will not be processed.
  63. // #### Batch Header Instance
  64. Random random = new Random();
  65. senderBatchHeader.setSenderBatchId(
  66. Double.toString(random.nextDouble())).setEmailSubject(
  67. "You have a Payment!");
  68.  
  69. Currency amount1 = new Currency();
  70. amount1.setValue("1.00").setCurrency("USD");
  71.  
  72. Currency amount2 = new Currency();
  73. amount2.setValue("2.00").setCurrency("USD");
  74.  
  75. Currency amount3 = new Currency();
  76. amount3.setValue("4.00").setCurrency("USD");
  77.  
  78. PayoutItem senderItem1 = new PayoutItem();
  79. senderItem1.setRecipientType("Email")
  80. .setNote("Thanks for your patronage")
  81. .setReceiver("shirt-supplier-one@gmail.com")
  82. .setSenderItemId("201404324234").setAmount(amount1);
  83.  
  84. PayoutItem senderItem2 = new PayoutItem();
  85. senderItem2.setRecipientType("Email")
  86. .setNote("Thanks for your patronage")
  87. .setReceiver("shirt-supplier-two@gmail.com")
  88. .setSenderItemId("201404324235").setAmount(amount2);
  89.  
  90. PayoutItem senderItem3 = new PayoutItem();
  91. senderItem3.setRecipientType("Email")
  92. .setNote("Thanks for your patronage")
  93. .setReceiver("shirt-supplier-three@gmail.com")
  94. .setSenderItemId("201404324236").setAmount(amount3);
  95.  
  96. List<PayoutItem> items = new ArrayList<>();
  97. items.add(senderItem1);
  98. items.add(senderItem2);
  99. items.add(senderItem3);
  100.  
  101. payout.setSenderBatchHeader(senderBatchHeader).setItems(items);
  102. return payout;
  103. }
  104.  
  105. private PayoutBatch createPayoutBatch(final Payout payout) throws PayPalRESTException {
  106.  
  107. // ### Api Context
  108. // Pass in a `ApiContext` object to authenticate
  109. // the call and to send a unique request id
  110. // (that ensures idempotency). The SDK generates
  111. // a request id if you do not pass one explicitly.
  112. final APIContext apiContext = new APIContext(CLIENT_ID, SECRET, MODE);
  113.  
  114. final PayoutBatch batch = payout.create(apiContext, new HashMap<>());
  115. LOG.info("Payout Batch ID: " + batch.getBatchHeader().getPayoutBatchId());
  116.  
  117. return batch;
  118. }
  119. }
Add Comment
Please, Sign In to add comment