Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package com.shipmentparcel.backend.Utils.PayU;
  2.  
  3. import com.shipmentparcel.backend.Entity.ShipmentEntity;
  4. import com.shipmentparcel.backend.Http.Request.AddShipmentRequest;
  5. import com.shipmentparcel.backend.Http.Response.AddPaymentOrderResponse;
  6. import okhttp3.*;
  7. import org.json.JSONArray;
  8. import org.json.JSONObject;
  9.  
  10. import java.io.IOException;
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. public class PayuApi {
  14. private static PayuApi payuApi;
  15. private OkHttpClient httpClient;
  16. private static final String URL = "https://secure.snd.payu.com";
  17. private static final String CLIENT_ID = "370489";
  18. private static final String SECRET_KEY = "20a2786b2cadb9a2e281914f7842cb40";
  19.  
  20. public static void main(String[] args) {
  21. PayuApi.getInstance().createNewOrder(null);
  22. }
  23.  
  24. public static PayuApi getInstance(){
  25. if (payuApi == null) payuApi = new PayuApi();
  26. return payuApi;
  27. }
  28.  
  29. public PayuApi() {
  30. httpClient = new OkHttpClient.Builder()
  31. .connectTimeout(20, TimeUnit.SECONDS)
  32. .writeTimeout(20, TimeUnit.SECONDS)
  33. .readTimeout(20, TimeUnit.SECONDS)
  34. .followRedirects(false)
  35. .build();
  36. }
  37.  
  38. public String getJwtToken() {
  39. String url = URL+"/pl/standard/user/oauth/authorize";
  40. String bodyStr = "grant_type=client_credentials&client_id="+CLIENT_ID+"&client_secret="+SECRET_KEY;
  41. RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), bodyStr);
  42. Request request = new Request.Builder().url(url)
  43. .addHeader("Content-Type","application/x-www-form-urlencoded")
  44. .post(body)
  45. .build();
  46. Response response;
  47. String responseBody;
  48. String token = null;
  49. try {
  50. response = httpClient.newCall(request).execute();
  51. responseBody = response.body().string();
  52. token = new JSONObject(responseBody).getString("access_token");
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56.  
  57. return token;
  58. }
  59.  
  60. public AddPaymentOrderResponse createNewOrder(ShipmentEntity shipment) {
  61. String url = URL+"/api/v2_1/orders/";
  62.  
  63. String token = getJwtToken();
  64.  
  65. JSONObject order = new JSONObject();
  66. JSONObject buyer = new JSONObject();
  67. buyer.put("firstName",shipment.getSender().getFirstName());
  68. buyer.put("lastName",shipment.getSender().getLastName());
  69. buyer.put("email",shipment.getSender().getEmail());
  70. buyer.put("phone",shipment.getSender().getPhoneNumber());
  71. order.put("buyer",buyer);
  72.  
  73. JSONArray products = new JSONArray();
  74. JSONObject product = new JSONObject();
  75. product.put("name","Shipment " + shipment.getShipmentNumber());
  76. product.put("unitPrice",String.valueOf(((Double)(shipment.getCost()*100)).intValue()));
  77. product.put("quantity","1");
  78. products.put(product);
  79. order.put("products", products);
  80.  
  81. order.put("customerIp", "127.0.0.1")
  82. //.put("notifyUrl", "http://localhost:8080/payment/notify")
  83. .put("continueUrl", "http://localhost:4200/send/confirmation")
  84. .put("merchantPosId", CLIENT_ID)
  85. .put("description", "Payment for shipment "+shipment.getShipmentNumber())
  86. .put("currencyCode", "PLN")
  87. .put("totalAmount", String.valueOf(((Double)(shipment.getCost()*100)).intValue()));
  88.  
  89. RequestBody body = RequestBody.create(MediaType.parse("application/json"), order.toString());
  90. System.out.println(body.toString());
  91. Request request = new Request.Builder().url(url)
  92. .addHeader("Content-Type","application/json")
  93. .addHeader("Authorization","Bearer "+token)
  94. .post(body)
  95. .build();
  96. Response response;
  97. String responseBody;
  98. String redirectUrl = null, status = null, orderId=null;
  99. try {
  100. response = httpClient.newCall(request).execute();
  101. responseBody = response.body().string();
  102. System.out.println(responseBody);
  103. redirectUrl = new JSONObject(responseBody).getString("redirectUri");
  104. status = new JSONObject(responseBody).getJSONObject("status").getString("statusCode");
  105. orderId = new JSONObject(responseBody).getString("orderId");
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109.  
  110. return new AddPaymentOrderResponse(redirectUrl, status, orderId);
  111. }
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement