Class: SpreeDelhivery::ShipmentCanceler
- Inherits:
-
Object
- Object
- SpreeDelhivery::ShipmentCanceler
- Defined in:
- app/services/spree_delhivery/shipment_canceler.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(shipment) ⇒ ShipmentCanceler
constructor
A new instance of ShipmentCanceler.
Constructor Details
#initialize(shipment) ⇒ ShipmentCanceler
Returns a new instance of ShipmentCanceler.
3 4 5 6 |
# File 'app/services/spree_delhivery/shipment_canceler.rb', line 3 def initialize(shipment) @shipment = shipment @client = SpreeDelhivery::Client.new end |
Instance Method Details
#call ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/services/spree_delhivery/shipment_canceler.rb', line 8 def call waybill = @shipment.delhivery_waybill return error("No Waybill found to cancel") unless waybill.present? # 1. Call API begin response = @client.cancel_shipment(waybill) rescue StandardError => e return error("API Connection Error: #{e.}") end # Delhivery success response is usually { "status" => true/false, ... } or "success" => true # Sometimes it returns specific codes. We check general success/failure keys. api_success = response['success'] || response['status'] == true || response['status'] == "Success" if api_success # 2. Clear Data & Revert State in Spree # We use update_columns to skip state machine transitions/validations @shipment.update_columns( tracking: nil, delhivery_waybill: nil, delhivery_ref_id: nil, delhivery_label_url: nil, delhivery_response_data: nil, state: 'ready', # Move back to Ready shipped_at: nil # Clear the shipped timestamp ) # 3. Update the Order's overall shipment state # This ensures the order status bar updates correctly @shipment.order.updater.update_shipment_state @shipment.order.save return success else return error(response['error'] || response['message'] || "Failed to cancel at Delhivery") end rescue StandardError => e return error(e.) end |