Class: Vendors::Dianping::ReservationWebhookWorker
- Inherits:
-
ApplicationWorker
- Object
- ApplicationWorker
- Vendors::Dianping::ReservationWebhookWorker
- Defined in:
- app/workers/vendors/dianping/reservation_webhook_worker.rb
Overview
Worker to process and push reservation status updates to Dianping.
This worker is a critical component in the Dianping integration flow. It receives reservation status updates from the system, serializes the data according to Dianping's API requirements, and sends a webhook to the Dianping order status push API (`dianping.order.syncOrderStatus`). The worker ensures that reservation status changes (such as confirmation or cancellation) are reliably communicated to Dianping.
Data Flow ==
-
The worker is triggered with a Hash payload (`payload_params`) containing reservation and status details.
-
It validates required fields and sets up logging context.
-
The payload is serialized and encoded as required by Dianping.
-
A signature is generated for security.
-
The webhook request is sent to Dianping's endpoint.
-
Success or failure is logged and reported for monitoring and alerting.
Expected payload_params Hash ==
vendor_reference_id: [String], # Unique vendor reference identifier (required)
vendor_reservation_id: [String], # Vendor's reservation ID (required)
reservation_id: [String], # Internal reservation ID (required)
ota_order_status: [String], # OTA order status code (required)
method: [String], # Action method, e.g., 'confirm' or 'cancel' (required)
refund_id: [String, nil] # Refund ID (optional, required for 'cancel')
Error Handling ==
-
Missing required keys: logs and reports error, exits gracefully.
-
Unsupported method: raises StandardError, which is caught and reported.
-
HTTP errors or unexpected exceptions: logged and reported for observability.
Edge Cases ==
-
If 'cancel' method is used, 'refund_id' must be present and convertible to integer.
-
All numeric fields are converted to integers for serialization.
-
Handles and logs any unexpected exceptions to avoid silent failures.
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')
- ROUTE =
'syncOrderStatus'
Instance Attribute Summary collapse
-
#order_payload ⇒ Object
readonly
Returns the value of attribute order_payload.
-
#reservation_id ⇒ Object
readonly
Returns the value of attribute reservation_id.
-
#serialized_data ⇒ Object
readonly
Returns the value of attribute serialized_data.
-
#vendor_reference_id ⇒ Object
readonly
Returns the value of attribute vendor_reference_id.
-
#vendor_reservation_id ⇒ Object
readonly
Returns the value of attribute vendor_reservation_id.
Instance Method Summary collapse
-
#perform(payload_params) ⇒ Boolean
Main entry point for the worker.
Instance Attribute Details
#order_payload ⇒ Object (readonly)
Returns the value of attribute order_payload.
41 42 43 |
# File 'app/workers/vendors/dianping/reservation_webhook_worker.rb', line 41 def order_payload @order_payload end |
#reservation_id ⇒ Object (readonly)
Returns the value of attribute reservation_id.
41 42 43 |
# File 'app/workers/vendors/dianping/reservation_webhook_worker.rb', line 41 def reservation_id @reservation_id end |
#serialized_data ⇒ Object (readonly)
Returns the value of attribute serialized_data.
41 42 43 |
# File 'app/workers/vendors/dianping/reservation_webhook_worker.rb', line 41 def serialized_data @serialized_data end |
#vendor_reference_id ⇒ Object (readonly)
Returns the value of attribute vendor_reference_id.
41 42 43 |
# File 'app/workers/vendors/dianping/reservation_webhook_worker.rb', line 41 def vendor_reference_id @vendor_reference_id end |
#vendor_reservation_id ⇒ Object (readonly)
Returns the value of attribute vendor_reservation_id.
41 42 43 |
# File 'app/workers/vendors/dianping/reservation_webhook_worker.rb', line 41 def vendor_reservation_id @vendor_reservation_id end |
Instance Method Details
#perform(payload_params) ⇒ Boolean
Main entry point for the worker.
Handles:
-
Validates presence of required keys.
-
Sets up logging context for traceability.
-
Builds and sends the Dianping webhook payload.
-
Logs and reports errors for missing keys, HTTP failures, or unexpected exceptions.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/workers/vendors/dianping/reservation_webhook_worker.rb', line 58 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_reference_id vendor_reservation_id reservation_id ota_order_status method] missing = required_keys - order_payload.keys if missing.any? return log_and_report_error("Missing keys: #{missing}") end @vendor_reservation_id = order_payload[:vendor_reservation_id] @vendor_reference_id = order_payload[:vendor_reference_id] @reservation_id = order_payload[:reservation_id] BUSINESS_LOGGER.set_business_context({ vendor_reference_id: vendor_reference_id, reservation_id: reservation_id }) BUSINESS_LOGGER.info("#{self.class}##{__method__}: Initiated", params: payload_params) payload = build_payload response = send_request(payload) if response&.status == 200 true else error_info = { response_code: response&.status, response_body: response&.body } VendorLogger.log_event(:webhook_out, ROUTE, payload: error_info, custom_message: :error) 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., backtrace: e.backtrace) end |