File size: 897 Bytes
874ae95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
module Users
  class PostmarkWebhookController < ApplicationController
    skip_before_action :verify_authenticity_token
    before_action :authenticate_request_from_postmark

    # POST /users/email_bounce
    def email_bounce
      if params[:Email].present? && params[:Type].in?(accepted_webhook_types)
        mark_email_bounced
      end
      head :ok
    end

    private

    def authenticate_request_from_postmark
      if authenticate_with_http_basic { |username, password|
           username == ENV["POSTMARK_HOOK_ID"] &&
             password == ENV["POSTMARK_HOOK_SECRET"]
         }
        true
      else
        head :unauthorized
      end
    end

    def accepted_webhook_types
      %w[HardBounce SpamComplaint]
    end

    def mark_email_bounced
      BounceReport.where(email: params[:Email]).first_or_create!(
        bounce_type: params[:Type]
      )
    end
  end
end