implement .env to QrisAcctivity

This commit is contained in:
riz081 2025-08-06 13:47:26 +07:00
parent 69fd69ac3a
commit 78f9e95c3f

View File

@ -1,5 +1,6 @@
package com.example.bdkipoc; package com.example.bdkipoc;
import com.example.bdkipoc.qris.view.QrisResultActivity; import com.example.bdkipoc.qris.view.QrisResultActivity;
import com.example.bdkipoc.BuildConfig;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -69,18 +70,8 @@ public class QrisActivity extends AppCompatActivity {
private static final long REFERENCE_COOLDOWN_MS = 60000; // 1 minute cooldown private static final long REFERENCE_COOLDOWN_MS = 60000; // 1 minute cooldown
private static final long TRANSACTION_COOLDOWN_MS = 5000; // 5 second cooldown private static final long TRANSACTION_COOLDOWN_MS = 5000; // 5 second cooldown
private static final String BACKEND_BASE = "https://be-edc.msvc.app"; private static final String MIDTRANS_AUTH = BuildConfig.MIDTRANS_SANDBOX_AUTH;
private static final String MIDTRANS_SANDBOX_AUTH = "Basic U0ItTWlkLXNlcnZlci1PM2t1bXkwVDl4M1VvYnVvVTc3NW5QbXc=";
private static final String MIDTRANS_PRODUCTION_AUTH = "TWlkLXNlcnZlci1sMlZPalotdVlVanpvNnU4VzAtYmF1a2o="; // Base64 of "Mid-server-l2VOjZ-uYUjzo6u8W0-baukj:"
// Currently active server key (switch by commenting/uncommenting)
// private static final String MIDTRANS_AUTH = MIDTRANS_PRODUCTION_AUTH;
private static final String MIDTRANS_AUTH = MIDTRANS_SANDBOX_AUTH; // Default to sandbox
private static final String WEBHOOK_URL = "https://be-edc.msvc.app/webhooks/midtrans";
// Midtrans charge URL
private static final String MIDTRANS_CHARGE_URL = "https://api.sandbox.midtrans.com/v2/charge";
// private static final String MIDTRANS_CHARGE_URL = "https://api.midtrans.com/v2/charge";
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -311,10 +302,10 @@ public class QrisActivity extends AppCompatActivity {
editTextAmount.setText(formatAmount(amountStr)); editTextAmount.setText(formatAmount(amountStr));
descriptionText.setText("Tekan Konfirmasi untuk melanjutkan"); descriptionText.setText("Tekan Konfirmasi untuk melanjutkan");
// Enable button if amount is valid // Enable button if amount is valid (any positive amount)
try { try {
int amt = Integer.parseInt(amountStr); int amt = Integer.parseInt(amountStr);
initiatePaymentButton.setEnabled(amt >= 1000); initiatePaymentButton.setEnabled(amt > 0); // Changed from >= 1000 to > 0
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
initiatePaymentButton.setEnabled(false); initiatePaymentButton.setEnabled(false);
} }
@ -455,17 +446,17 @@ public class QrisActivity extends AppCompatActivity {
// Parse amount - expecting integer in lowest denomination (Indonesian Rupiah) // Parse amount - expecting integer in lowest denomination (Indonesian Rupiah)
amount = Integer.parseInt(amountText); amount = Integer.parseInt(amountText);
// Validate minimum amount // // Validate minimum amount
if (amount < 1000) { // if (amount < 1000) {
errorMessage = "Minimum amount is IDR 1,000"; // errorMessage = "Minimum amount is IDR 1,000";
return false; // return false;
} // }
// Validate maximum amount for testing // // Validate maximum amount for testing
if (amount > 10000000) { // if (amount > 10000000) {
errorMessage = "Maximum amount is IDR 10,000,000"; // errorMessage = "Maximum amount is IDR 10,000,000";
return false; // return false;
} // }
Log.d("MidtransCharge", "Parsed amount: " + amount); Log.d("MidtransCharge", "Parsed amount: " + amount);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -488,7 +479,7 @@ public class QrisActivity extends AppCompatActivity {
Log.d("MidtransCharge", "Backend transaction payload: " + payload.toString()); Log.d("MidtransCharge", "Backend transaction payload: " + payload.toString());
// Make the API call // Make the API call
URL url = new URI(BACKEND_BASE + "/transactions").toURL(); URL url = new URI(BuildConfig.BACKEND_BASE_URL + "/transactions").toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Type", "application/json");
@ -638,22 +629,22 @@ public class QrisActivity extends AppCompatActivity {
// Log the request details // Log the request details
Log.d("MidtransCharge", "=== MIDTRANS QRIS REQUEST ==="); Log.d("MidtransCharge", "=== MIDTRANS QRIS REQUEST ===");
Log.d("MidtransCharge", "URL: " + MIDTRANS_CHARGE_URL); Log.d("MidtransCharge", "URL: " + BuildConfig.MIDTRANS_CHARGE_URL);
Log.d("MidtransCharge", "Authorization: " + MIDTRANS_AUTH); Log.d("MidtransCharge", "Authorization: " + MIDTRANS_AUTH);
Log.d("MidtransCharge", "X-Override-Notification: " + WEBHOOK_URL); Log.d("MidtransCharge", "X-Override-Notification: " + BuildConfig.WEBHOOK_URL);
Log.d("MidtransCharge", "Reference ID: " + referenceId); Log.d("MidtransCharge", "Reference ID: " + referenceId);
Log.d("MidtransCharge", "Order ID: " + transactionUuid); Log.d("MidtransCharge", "Order ID: " + transactionUuid);
Log.d("MidtransCharge", "Amount: " + amount); Log.d("MidtransCharge", "Amount: " + amount);
Log.d("MidtransCharge", "================================"); Log.d("MidtransCharge", "================================");
// Make the API call to Midtrans // Make the API call to Midtrans
URL url = new URI(MIDTRANS_CHARGE_URL).toURL(); URL url = new URI(BuildConfig.MIDTRANS_CHARGE_URL).toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", MIDTRANS_AUTH); conn.setRequestProperty("Authorization", MIDTRANS_AUTH);
conn.setRequestProperty("X-Override-Notification", WEBHOOK_URL); conn.setRequestProperty("X-Override-Notification", BuildConfig.WEBHOOK_URL);
conn.setRequestProperty("User-Agent", "BDKIPOCApp/1.0"); conn.setRequestProperty("User-Agent", "BDKIPOCApp/1.0");
conn.setDoOutput(true); conn.setDoOutput(true);
conn.setConnectTimeout(30000); // 30 seconds conn.setConnectTimeout(30000); // 30 seconds