You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 line
4.8KB

  1. <%
  2. ' Auto-generated Controller: OrderApi
  3. ' Generated on 7/28/2026 2:56:29 PM
  4. ' Generator: generateController.vbs v1.0
  5. '
  6. ' Remember to:
  7. ' 1. Add to app/controllers/autoload_controllers.asp
  8. ' 2. Register in core/lib.ControllerRegistry.asp
  9. ' 3. Add routes in public/Default.asp
  10. Class OrderApiController_Class
  11. Private m_useLayout
  12. Private m_title
  13. Private Sub Class_Initialize()
  14. m_useLayout = False ' this controller returns JSON only, never the shared HTML layout
  15. m_title = "OrderApi"
  16. End Sub
  17. Public Property Get useLayout
  18. useLayout = m_useLayout
  19. End Property
  20. Public Property Let useLayout(v)
  21. m_useLayout = v
  22. End Property
  23. Public Property Get Title
  24. Title = m_title
  25. End Property
  26. Public Property Let Title(v)
  27. m_title = v
  28. End Property
  29. '-------------------------------------------------------------------------------------------------------------------
  30. ' POST: accept the completed SurveyJS order-details form (as JSON) for the order matching ?token=
  31. '-------------------------------------------------------------------------------------------------------------------
  32. Public Sub SubmitOrderDetails()
  33. Response.ContentType = "application/json"
  34. If Request.ServerVariables("REQUEST_METHOD") <> "POST" Then
  35. WriteJsonError "405 Method Not Allowed", "This endpoint only accepts POST."
  36. Exit Sub
  37. End If
  38. Dim token, order
  39. token = Trim(Request.QueryString("token"))
  40. Set order = Nothing
  41. If Len(token) > 0 Then Set order = OrdersRepository().FindByToken(token)
  42. If order Is Nothing Then
  43. WriteJsonError "404 Not Found", "Order not found."
  44. Exit Sub
  45. End If
  46. If order("TokenExpiresAt") < Now() Then
  47. WriteJsonError "410 Gone", "This order link has expired. Please request a new one."
  48. Exit Sub
  49. End If
  50. If Not IsNull(order("TokenUsedAt")) Then
  51. WriteJsonError "410 Gone", "This order has already been submitted."
  52. Exit Sub
  53. End If
  54. Dim csrfToken : csrfToken = Request.ServerVariables("HTTP_X_CSRF_TOKEN")
  55. If Not HTMLSecurity().IsValidAntiCSRFToken("Order.Continue", csrfToken) Then
  56. WriteJsonError "403 Forbidden", "Your form session expired. Please refresh the page and try again."
  57. Exit Sub
  58. End If
  59. Dim turnstileToken : turnstileToken = Request.ServerVariables("HTTP_X_TURNSTILE_TOKEN")
  60. If Not VerifyTurnstileToken(turnstileToken, Request.ServerVariables("REMOTE_ADDR")) Then
  61. WriteJsonError "403 Forbidden", "Verification challenge failed. Please refresh the page and try again."
  62. Exit Sub
  63. End If
  64. Dim answers
  65. On Error Resume Next
  66. json().loadJSON GetRawJsonFromRequest()
  67. If Err.Number <> 0 Then
  68. Err.Clear
  69. On Error GoTo 0
  70. WriteJsonError "400 Bad Request", "Could not read the submitted form data."
  71. Exit Sub
  72. End If
  73. On Error GoTo 0
  74. Set answers = json().data
  75. Dim validationError : validationError = ValidateOrderDetailsAnswers(answers)
  76. If Len(validationError) > 0 Then
  77. WriteJsonError "400 Bad Request", validationError
  78. Exit Sub
  79. End If
  80. Dim model : Set model = New POBO_OrderDetails
  81. On Error Resume Next
  82. MapOrderDetailsAnswers model, answers
  83. model.SubmittedAt = Now()
  84. If Err.Number <> 0 Then
  85. Err.Clear
  86. On Error GoTo 0
  87. WriteJsonError "400 Bad Request", "The submitted data could not be processed. Please check your entries and try again."
  88. Exit Sub
  89. End If
  90. On Error GoTo 0
  91. On Error Resume Next
  92. OrderDetailsRepository().SaveForOrder order("OrderID"), model
  93. If Err.Number <> 0 Then
  94. Err.Clear
  95. On Error GoTo 0
  96. WriteJsonError "500 Internal Server Error", "Could not save your order details. Please try again."
  97. Exit Sub
  98. End If
  99. On Error GoTo 0
  100. OrdersRepository().MarkTokenUsed order("OrderID")
  101. ' Best-effort: the order is already safely persisted at this point, so an SMTP hiccup
  102. ' should not turn a successful submission into a failed one.
  103. OrderMailer().SendOrderDetailsEmail order, model
  104. Flash().Success = "Your order has been submitted. We'll be in touch with your quote soon."
  105. Response.Write "{""success"":true}"
  106. End Sub
  107. End Class
  108. ' Singleton instance
  109. Dim OrderApiController_Class__Singleton
  110. Function OrderApiController()
  111. If IsEmpty(OrderApiController_Class__Singleton) Then
  112. Set OrderApiController_Class__Singleton = New OrderApiController_Class
  113. End If
  114. Set OrderApiController = OrderApiController_Class__Singleton
  115. End Function
  116. %>

Powered by TurnKey Linux.