Class: Vendors::Dianping::OrderWebhookWorker

Inherits:
ApplicationWorker
  • Object
show all
Defined in:
app/workers/vendors/dianping/order_webhook_worker.rb

Overview

Worker to process Dianping orders. Accepts raw Hash payload_params and serializes using OrderSerializer. Sends webhook to dianping.order.syncOrderStatus (order status push API)

Expected payload_params Hash:

vendor_order_id: [String],
ticket_transaction_id: [String],
ota_order_status: [String],
method: [String],
tickets: [Array<Hash>] (optional),
refund_id: [String, nil] (optional)

Constant Summary collapse

PUSH_API_URL =
ENV.fetch('DIANPING_ORDER_WEBHOOK_URL')
OTA_ID =
ENV.fetch('DIANPING_OTA_ID')
SECURITY_CODE =
ENV.fetch('DIANPING_SECRET_CODE')

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#order_payloadObject (readonly)

Returns the value of attribute order_payload.



20
21
22
# File 'app/workers/vendors/dianping/order_webhook_worker.rb', line 20

def order_payload
  @order_payload
end

#ticket_transaction_idObject (readonly)

Returns the value of attribute ticket_transaction_id.



20
21
22
# File 'app/workers/vendors/dianping/order_webhook_worker.rb', line 20

def ticket_transaction_id
  @ticket_transaction_id
end

#vendor_order_idObject (readonly)

Returns the value of attribute vendor_order_id.



20
21
22
# File 'app/workers/vendors/dianping/order_webhook_worker.rb', line 20

def vendor_order_id
  @vendor_order_id
end

Instance Method Details

#perform(payload_params) ⇒ Object



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
# File 'app/workers/vendors/dianping/order_webhook_worker.rb', line 26

def perform(payload_params)
  BUSINESS_LOGGER.set_business_context({ vendor_name: ApiVendorV1::Constants::DIANPING_VENDOR_NAME })
  @order_payload = payload_params.symbolize_keys
  required_keys = %i[vendor_order_id ticket_transaction_id ota_order_status method]
  missing = required_keys - order_payload.keys
  if missing.any?
    return log_and_report_error("Missing keys: #{missing}")
  end

  @ticket_transaction_id = order_payload[:ticket_transaction_id]
  @vendor_order_id = order_payload[:vendor_order_id]

  BUSINESS_LOGGER.set_business_context({ vendor_reference_id: vendor_order_id,
                                         ticket_transaction_id: ticket_transaction_id })
  BUSINESS_LOGGER.info("#{self.class}##{__method__}: Initiated", params: payload_params)

  ticket_transaction = TicketTransaction.find_by(id: ticket_transaction_id)

  unless ticket_transaction
    return log_and_report_error('Ticket Transaction not found')
  end

  payload = build_payload(ticket_transaction)
  response = send_request(payload)

  if response&.status == 200
    log_success(response)
  else
    log_and_report_error('Webhook Failed', status: response&.status, response: response.body)
  end
rescue StandardError => e
  log_and_report_error('Unexpected error occurred', error: e.message, backtrace: e.backtrace)
end