Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.shipmentparcel.backend.Utils.PayU;
- import com.shipmentparcel.backend.Entity.ShipmentEntity;
- import com.shipmentparcel.backend.Http.Request.AddShipmentRequest;
- import com.shipmentparcel.backend.Http.Response.AddPaymentOrderResponse;
- import okhttp3.*;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.IOException;
- import java.util.concurrent.TimeUnit;
- public class PayuApi {
- private static PayuApi payuApi;
- private OkHttpClient httpClient;
- private static final String URL = "https://secure.snd.payu.com";
- private static final String CLIENT_ID = "370489";
- private static final String SECRET_KEY = "20a2786b2cadb9a2e281914f7842cb40";
- public static void main(String[] args) {
- PayuApi.getInstance().createNewOrder(null);
- }
- public static PayuApi getInstance(){
- if (payuApi == null) payuApi = new PayuApi();
- return payuApi;
- }
- public PayuApi() {
- httpClient = new OkHttpClient.Builder()
- .connectTimeout(20, TimeUnit.SECONDS)
- .writeTimeout(20, TimeUnit.SECONDS)
- .readTimeout(20, TimeUnit.SECONDS)
- .followRedirects(false)
- .build();
- }
- public String getJwtToken() {
- String url = URL+"/pl/standard/user/oauth/authorize";
- String bodyStr = "grant_type=client_credentials&client_id="+CLIENT_ID+"&client_secret="+SECRET_KEY;
- RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), bodyStr);
- Request request = new Request.Builder().url(url)
- .addHeader("Content-Type","application/x-www-form-urlencoded")
- .post(body)
- .build();
- Response response;
- String responseBody;
- String token = null;
- try {
- response = httpClient.newCall(request).execute();
- responseBody = response.body().string();
- token = new JSONObject(responseBody).getString("access_token");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return token;
- }
- public AddPaymentOrderResponse createNewOrder(ShipmentEntity shipment) {
- String url = URL+"/api/v2_1/orders/";
- String token = getJwtToken();
- JSONObject order = new JSONObject();
- JSONObject buyer = new JSONObject();
- buyer.put("firstName",shipment.getSender().getFirstName());
- buyer.put("lastName",shipment.getSender().getLastName());
- buyer.put("email",shipment.getSender().getEmail());
- buyer.put("phone",shipment.getSender().getPhoneNumber());
- order.put("buyer",buyer);
- JSONArray products = new JSONArray();
- JSONObject product = new JSONObject();
- product.put("name","Shipment " + shipment.getShipmentNumber());
- product.put("unitPrice",String.valueOf(((Double)(shipment.getCost()*100)).intValue()));
- product.put("quantity","1");
- products.put(product);
- order.put("products", products);
- order.put("customerIp", "127.0.0.1")
- //.put("notifyUrl", "http://localhost:8080/payment/notify")
- .put("continueUrl", "http://localhost:4200/send/confirmation")
- .put("merchantPosId", CLIENT_ID)
- .put("description", "Payment for shipment "+shipment.getShipmentNumber())
- .put("currencyCode", "PLN")
- .put("totalAmount", String.valueOf(((Double)(shipment.getCost()*100)).intValue()));
- RequestBody body = RequestBody.create(MediaType.parse("application/json"), order.toString());
- System.out.println(body.toString());
- Request request = new Request.Builder().url(url)
- .addHeader("Content-Type","application/json")
- .addHeader("Authorization","Bearer "+token)
- .post(body)
- .build();
- Response response;
- String responseBody;
- String redirectUrl = null, status = null, orderId=null;
- try {
- response = httpClient.newCall(request).execute();
- responseBody = response.body().string();
- System.out.println(responseBody);
- redirectUrl = new JSONObject(responseBody).getString("redirectUri");
- status = new JSONObject(responseBody).getJSONObject("status").getString("statusCode");
- orderId = new JSONObject(responseBody).getString("orderId");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return new AddPaymentOrderResponse(redirectUrl, status, orderId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement