Skip to content

Instantly share code, notes, and snippets.

@rizkytegar
Created October 24, 2023 08:38
Show Gist options
  • Save rizkytegar/a8ad589453d6864fa9c250209087eeb8 to your computer and use it in GitHub Desktop.
Save rizkytegar/a8ad589453d6864fa9c250209087eeb8 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MidtransStatusFetcher {
public static void main(String[] args) {
try {
// URL transaksi yang ingin Anda cek statusnya
String transactionUrl = "https://api.sandbox.midtrans.com/v2/[ORDERID]/status";
URL url = new URL(transactionUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// Tambahkan header otorisasi sesuai kebutuhan
connection.setRequestProperty("Authorization", "Basic BASE64ENCODE");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Hasil respons dari Midtrans API
String responseText = response.toString();
System.out.println("Response: " + responseText);
} else {
System.out.println("Gagal mengambil status transaksi. Kode respons: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@rizkytegar
Copy link
Author

contoh response

Response: {
  "approval_code":"rahasia",
  "transaction_time":"2023-08-11 15:00:16",
  "gross_amount":"94000.00",
  "currency":"IDR",
  "order_id":"rahasia",
  "payment_type":"danamon_online",
  "signature_key":"rahasia",
  "status_code":"200",
  "transaction_id":"rahasia",
  "transaction_status":"settlement",
  "fraud_status":"accept",
  "expiry_time":"2023-08-11 17:00:16",
  "settlement_time":"2023-08-11 15:00:18",
  "status_message":"Success, transaction is found",
  "merchant_id":"okkk"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment