Create Payment API

Developer11 days ago

I wrote the payment gateway API like this in Java

package org.traccar.api.resource;

import javax.ws.rs.*;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.core.Response;   
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.json.JSONObject; 
import org.traccar.api.resource.PaymentRequest; 

@Path("payment")
@Produces({"application/json"})
@Consumes({"application/json"})
public class PaymentResource {

    @POST   
    @Path("create")
    @Consumes("application/json") 
    public Response createPayment(PaymentRequest paymentRequest) {
        try {
            URL url = new URL("https://sandbox.zarinpal.com/pg/v4/payment/request.json");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setDoOutput(true);

            String jsonInputString = "{"
                    + "\"merchant_id\": \"" + paymentRequest.getMerchantId() + "\","
                    + "\"amount\": " + paymentRequest.getAmount() + ","
                    + "\"callback_url\": \"" + paymentRequest.getCallbackUrl() + "\","
                    + "\"description\": \"" + paymentRequest.getDescription() + "\","
                    + "\"metadata\": {\"mobile\": \"" + paymentRequest.getMobile() + "\", \"email\": \"" + paymentRequest.getEmail() + "\"}"
                    + "}";

            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

              int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                String response = new String(conn.getInputStream().readAllBytes(), StandardCharsets.UTF_8);

                JSONObject jsonResponse = new JSONObject(response);
                String authority = jsonResponse.getJSONObject("data").getString("authority");
                String paymentUrl = "https://sandbox.zarinpal.com/pg/StartPay/" + authority;

                return Response.ok("{\"paymentUrl\": \"" + paymentUrl + "\"}").build();
            } else {
                return Response.status(Response.Status.BAD_REQUEST).entity("خطا در ارسال درخواست به زرین‌پال").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("خطای داخلی در سرور").build();
        }
    }
}

But when I try to convert the Java file into a class file, I receive these errors:

warning: [options] location of system modules is not set in conjunction with -source 11
  not setting the location of system modules may lead to class files that cannot run on JDK 11
    --release 11 is recommended instead of -source 11 -target 11 because it sets the location of system modules automatically
PaymentResource.java:4: error: package javax.ws.rs does not exist
import javax.ws.rs.POST;
                  ^
PaymentResource.java:5: error: package javax.ws.rs does not exist
import javax.ws.rs.Path;
                  ^
PaymentResource.java:6: error: package javax.ws.rs does not exist
import javax.ws.rs.HeaderParam;
                  ^
PaymentResource.java:7: error: package javax.ws.rs.core does not exist
import javax.ws.rs.core.Response;
                       ^
PaymentResource.java:12: error: package org.json does not exist
import org.json.JSONObject;
               ^
PaymentResource.java:13: error: cannot find symbol
import org.traccar.api.resource.PaymentRequest;
                               ^
  symbol:   class PaymentRequest
  location: package org.traccar.api.resource
PaymentResource.java:15: error: cannot find symbol
@Path("payment")
 ^
  symbol: class Path
PaymentResource.java:16: error: cannot find symbol
@Produces({"application/json"})
 ^
  symbol: class Produces
PaymentResource.java:17: error: cannot find symbol
@Consumes({"application/json"})
 ^
  symbol: class Consumes
PaymentResource.java:23: error: cannot find symbol
    public Response createPayment(PaymentRequest paymentRequest) {
                                  ^
  symbol:   class PaymentRequest
  location: class PaymentResource
PaymentResource.java:23: error: cannot find symbol
    public Response createPayment(PaymentRequest paymentRequest) {
           ^
  symbol:   class Response
  location: class PaymentResource
PaymentResource.java:3: error: package javax.ws.rs does not exist
import javax.ws.rs.*;
               ^
PaymentResource.java:20: error: cannot find symbol
    @POST
     ^
  symbol:   class POST
  location: class PaymentResource
PaymentResource.java:21: error: cannot find symbol
    @Path("create")
     ^
  symbol:   class Path
  location: class PaymentResource
PaymentResource.java:22: error: cannot find symbol
    @Consumes("application/json")
     ^
  symbol:   class Consumes
  location: class PaymentResource
PaymentResource.java:50: error: cannot find symbol
                JSONObject jsonResponse = new JSONObject(response);
                ^
  symbol:   class JSONObject
  location: class PaymentResource
PaymentResource.java:50: error: cannot find symbol
                JSONObject jsonResponse = new JSONObject(response);
                                              ^
  symbol:   class JSONObject
  location: class PaymentResource
PaymentResource.java:54: error: cannot find symbol
                return Response.ok("{\"paymentUrl\": \"" + paymentUrl + "\"}").build();
                       ^
  symbol:   variable Response
  location: class PaymentResource
PaymentResource.java:56: error: package Response does not exist
                return Response.status(Response.Status.BAD_REQUEST).entity("??? ?? ????? ??????? ?? ????????").build();
                                               ^
PaymentResource.java:56: error: cannot find symbol
                return Response.status(Response.Status.BAD_REQUEST).entity("??? ?? ????? ??????? ?? ????????").build();
                       ^
  symbol:   variable Response
  location: class PaymentResource
PaymentResource.java:60: error: package Response does not exist
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("???? ????? ?? ????").build();
                                           ^
PaymentResource.java:60: error: cannot find symbol
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("???? ????? ?? ????").build();
                   ^
  symbol:   variable Response
  location: class PaymentResource
Note: PaymentResource.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
22 errors
1 warning