5
6
7
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'app/controllers/spree/admin/delhivery_returns_controller.rb', line 5
def create_pickup
@return_auth = Spree::ReturnAuthorization.find(params[:id])
if @return_auth.stock_location.delhivery_warehouse_name.blank?
flash[:error] = "Delhivery Warehouse Name is missing in Stock Location settings."
redirect_back(fallback_location: admin_order_return_authorizations_path(@return_auth.order))
return
end
= {
brand: params[:brand],
category: params[:category]
}
begin
client = SpreeDelhivery::Client.new
response = client.create_return_request(@return_auth, )
resp = response.is_a?(HTTParty::Response) ? response.parsed_response : response
Rails.logger.info "Delhivery Return Response: #{resp.inspect}"
is_success = false
if resp['packages'].present? && resp['packages'].is_a?(Array)
first_pkg = resp['packages'].first
if first_pkg['status'] == 'Success' || first_pkg['waybill'].to_s.length > 5
is_success = true
end
end
if is_success
waybill = resp['packages'].first['waybill']
ref_id = resp['packages'].first['ref_id']
@return_auth.update_columns(
delhivery_waybill: waybill,
delhivery_ref_id: ref_id
)
flash[:success] = "Reverse Pickup Scheduled! Waybill: #{waybill}"
else
error_text = "Unknown Error"
if resp['detail'].present?
error_text = "Auth/Permission Error: #{resp['detail']}"
elsif resp['rmk'].present?
error_text = resp['rmk']
elsif resp['packages'].present? && resp['packages'].first
pkg = resp['packages'].first
error_text = pkg['remarks'] || pkg['status'] || "Package Error"
elsif resp['error'].present?
error_text = resp['error'].to_s
end
flash[:error] = "Delhivery Error: #{error_text}"
end
rescue StandardError => e
flash[:error] = "Connection Exception: #{e.message}"
end
redirect_back(fallback_location: admin_order_return_authorizations_path(@return_auth.order))
end
|