javaspringeasypost

Easypost webhook refund event


What webhook event should i listen to to know that the refund of a shipment label was accepted by the carrier?

Here's how I do it now, but unfortunately when I run the easypost webhook locally, it does not emit the event even though a refund is submitted. I never get 'return_update" in statusDetail. Here's my code:

if (statusDetail.equals("status_update") || statusDetail.equals("out_for_delivery") || statusDetail.equals("arrived_at_destination")) {
    String easypostShipmentId = (String) result.get("shipmentId");

    Shipment userShipment = shipmentService.retrieveShipmentFromEasypostId(easypostShipmentId);

    if (statusDetail.equals("arrived_at_destination")) {
        userShipment.setEasypostStatus(EasypostShipmentStatus.DELIVERED);
        userShipment.setStatus(ShipmentStatus.DELIVERED);
        userShipment.setDeliveryDate(Instant.now());
    } else {
        String shipmentStatus = (String) result.get("status");
        userShipment.setEasypostStatus(EasypostShipmentStatus.valueOf(shipmentStatus.toUpperCase()));
        Long estDeliveryDateMillis = (Long) result.get("estDeliveryDate");
        userShipment.setDeliveryDate(Instant.ofEpochMilli(estDeliveryDateMillis));
    }

    shipmentService.updateShipment(userShipment);
    activityLoggerService.insert(userShipment.getUser(), userShipment, activityLoggerService.getShipmentStatusChangeMessage(userShipment), ActivityMessageType.STATUS_UPDATE);
} else if (statusDetail.equals("return_update")) {
    String easypostShipmentId = (String) result.get("shipmentId");
    Shipment userShipment = shipmentService.retrieveShipmentFromEasypostId(easypostShipmentId);
    String shipmentStatus = (String) result.get("status");

    String chargeId = userShipment.getStripeChargeId();
    try {
        // Refund successful
        Refund refund = stripeService.refund(chargeId);
        userShipment.setStripeRefundId(refund.getId());
        userShipment.setStatus(ShipmentStatus.REFUND_PROCESSED);
        userShipment.setEasypostStatus(EasypostShipmentStatus.valueOf(shipmentStatus.toUpperCase()));

        shipmentService.updateShipment(userShipment);
        double refundAmount = stripeService.getRefundAmount(refund);
        activityLoggerService.insert(userShipment.getUser(), userShipment, activityLoggerService.getShipmentReturnProcessed(userShipment, refundAmount), ActivityMessageType.RETURN_PROCESSED);
    } catch (StripeException e) {
        System.err.println("Error issuing refund: " + e.getMessage());
    }
}

Solution

  • A refund.successful Event is created whenever a refund is successfully processed on the EasyPost backend. This can happen days or weeks after the refund is requested. USPS specifically takes up to 15-30 days to refund labels. Unfortunately I don't believe there is a way to mock this behavior in the test environment.

    https://www.easypost.com/docs/api#events

    Refund A "refund.successful" Event is created whenever a non-instantaneous Refund request is completed. USPS is the best example of this, as USPS postage takes 15+ days to be refunded after the initial refund creation.