Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Enhance Checkout Process with Error Handling and Form Data #155

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ private String getProductInfoUrl(int index) {
public void checkout() {
Log.i("checkout", "checkout >>>");
selectedStoreItems = this.adapter.getSelectedStoreItems();

if (selectedStoreItems == null || selectedStoreItems.isEmpty()) {
Log.w("checkout", "Attempted checkout with empty cart");
Sentry.captureMessage("Attempted checkout with empty cart", SentryLevel.WARNING);
return;
}

ITransaction checkoutTransaction = Sentry.startTransaction("checkout [android]", "http.client");
checkoutTransaction.setOperation("http");
Sentry.configureScope(scope -> scope.setTransaction(checkoutTransaction));
Expand Down Expand Up @@ -350,20 +357,36 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();

processDeliveryItem(checkoutTransaction);

checkoutTransaction.finish(SpanStatus.INTERNAL_ERROR);
}
});
}
}
try {
if (!response.isSuccessful()) {
String errorBody = response.body() != null ? response.body().string() : "Unknown error";
Log.e("checkout", "Checkout failed with status: " + response.code() + ", error: " + errorBody);

Sentry.configureScope(scope -> {
scope.setExtra("response_code", response.code());
scope.setExtra("error_body", errorBody);
scope.setTag("error.type", "checkout_failed");
});

getActivity().runOnUiThread(() -> {

@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
progressDialog.dismiss();
Sentry.captureException(e);
});
} else {
String successBody = response.body() != null ? response.body().string() : "";
Log.d("checkout", "Checkout successful: " + successBody);

getActivity().runOnUiThread(() -> {
checkoutTransaction.finish(SpanStatus.OK);
// Reset cart
mCartItemCount = 0;
((EmpowerPlantActivity) getActivity()).textCartItemCount.setText("0");
selectedStoreItems.clear();
adapter.notifyDataSetChanged();
});
}
} finally {
response.close();

processDeliveryItem(checkoutTransaction);
checkoutTransaction.finish(SpanStatus.INTERNAL_ERROR);
Expand Down Expand Up @@ -401,30 +424,49 @@ private JSONObject buildJSONPostData(HashMap<String, StoreItem> selectedStoreIte
} catch (JSONException e) {
ISpan span = Sentry.getSpan();
if (span != null) {
JSONObject form = new JSONObject();
span.setThrowable(e);
span.finish(SpanStatus.INTERNAL_ERROR);
span.finish();
}
Sentry.captureException(e);
}
return postBody;
}

private void processDeliveryItem(ITransaction checkoutTransaction) {
Log.i("processDeliveryItem", "processDeliveryItem >>>");
ISpan processDeliverySpan = checkoutTransaction.startChild("task", "process delivery");

try {
throw new MainFragment.BackendAPIException("Failed to init delivery item workflow");
} catch (Exception e) {
Log.e("processDeliveryItem", e.getMessage());
processDeliverySpan.setThrowable(e);

// Add required form fields with proper structure
form.put("email", "[email protected]");
form.put("shipping", new JSONObject()
.put("address", "123 Street")
.put("city", "San Francisco")
.put("state", "CA")
.put("zip", "94105")
.put("country", "US"));
form.put("billing", new JSONObject()
.put("address", "123 Street")
.put("city", "San Francisco")
.put("state", "CA")
.put("zip", "94105")
.put("country", "US"));
form.put("payment", new JSONObject()
.put("method", "credit_card")
.put("card_token", "tok_visa"));

postBody.put("form", form);
processDeliverySpan.setStatus(SpanStatus.INTERNAL_ERROR);
Sentry.captureException(e);
}

if (processDeliverySpan.getStatus() != SpanStatus.INTERNAL_ERROR) {
processDeliverySpan.setStatus(SpanStatus.OK);
span.setTag("error.type", "json_construction");
span.setTag("error.details", e.getMessage());
}
processDeliverySpan.finish();
Log.i("processDeliveryItem", "<<< processDeliveryItem");
Expand Down
Loading