Rails5:如何在Heroku上测试API的webhook请求

时间:2019-06-28 05:34:23

标签: ruby-on-rails ruby-on-rails-5

我正在尝试使用后勤api的webhook根据其Webhook请求更改我的Order状态。 API Create Order的请求工作正常,但是当我Cancel Order仅从服务器到API服务器的成功请求时,Order的状态仍然没有改变。我不确定webhook是否正常工作

Webhook请求标头Content-Typeapplication/json

身体像

{
"OrderCode": "DC5D4NFUH",
"CurrentStatus": "Picking" //other status: delivered, cancel
}


//order.rb model 
 enum status: {
    in_progress: 0,
    shipping: 1,
    complete: 2,
    cancel: 3
}


//routes.rb
  post 'orders/api_webhook', to: 'orders#api_webhook' 


//orders_controller.rb
 class OrdersController < ApplicationController
  skip_before_action :verify_authenticity_token
  def api_webhook
   event = JSON.parse(request.body.read)
   if event['OrderCode']
    @payment = Payment.find_by(order_code: event['OrderCode'])
    case event['CurrentStatus']
     when "Picking"
       @payment.status = 1
     when "Delivered"
       @payment.status = 2
     when "Cancel"
       @payment.status = 3
    end
   @payment.save
  end
 end
end

0 个答案:

没有答案