Class: ErpInvoicing::SmsController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/erp_invoicing/sms_controller.rb

Instance Method Summary collapse

Instance Method Details

#receive_responseObject

Receive SMS callback from clickatell Example: If you provide this URL www.yourdomain.com/erp_invoicing/sms/receive_response then we will do a POST or GET as follows: www.yourdomain.com/sms/receive_response?api_id=12345&from=279991235642&to=27123456789& timestamp=2008-08-0609:43:50&text=Hereisthe%20messagetext&charset=ISO-8859-1&udh=&moMsgId=b2aee337abd962489b123fda9c3480fa



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/erp_invoicing/sms_controller.rb', line 9

def receive_response
  message_text = params[:text]
  moMsgId = params[:moMsgId]
  to_number = params[:to]
  from_number = params[:from]

  if message_text.blank? or to_number.blank? or from_number.blank?
    Rails.logger.error 'ErpInvoicing::SmsController#receive_response called with insufficient data'
    render_false and return
    render :json => {:success => false} and return
  end

  # find the comm event sent within past 10mins where that incoming message is a reponse to
  cmm_evt = CommunicationEvent.find_by_sql("SELECT * FROM communication_events
                                JOIN phone_numbers from_phone ON from_contact_mechanism_id=from_phone.id
                                JOIN phone_numbers to_phone ON to_contact_mechanism_id=to_phone.id
                                WHERE from_contact_mechanism_type = 'PhoneNumber' 
                                AND from_contact_mechanism_type='PhoneNumber'
                                AND from_phone.phone_number = '#{to_number.to_s}'
                                AND (to_phone.phone_number = '#{from_number.to_s}' OR to_phone.phone_number = '#{from_number[1..from_number.length]}')
                                AND communication_events.created_at > '#{SMS_TIME_WINDOW.minutes.ago.to_s}'
                                ORDER BY communication_events.created_at DESC").first

  unless cmm_evt.nil?
    if message_text.downcase.include?('yespay') or message_text.downcase.include?('yes pay')
       = BillingAccount.find(cmm_evt.case_id)
      payment_due = .payment_due

      # TODO: scrape for amount and compare with payment_due on account?

      if .has_outstanding_balance?
        success = submit_payment(cmm_evt, ) if .has_outstanding_balance?
      else
        Rails.logger.error 'ErpInvoicing::SmsController#receive_response called: skipping payment NO OUTSTANDING BALANCE TO PAY'
      end
      to_party = cmm_evt.from_party

      # log communication event
      new_cmm_evt = CommunicationEvent.new
      new_cmm_evt.short_description = 'SMS Response'
      new_cmm_evt.comm_evt_purpose_types << CommEvtPurposeType.find_by_internal_identifier('sms_response')
      new_cmm_evt.to_role = RoleType.find_by_internal_identifier('application')
      new_cmm_evt.to_party = to_party
      new_cmm_evt.to_contact_mechanism = to_party.default_phone_number
      new_cmm_evt.from_contact_mechanism = cmm_evt.to_party.billing_phone_number
      new_cmm_evt.from_role = RoleType.find_by_internal_identifier('customer')
      new_cmm_evt.from_party = cmm_evt.to_party
      new_cmm_evt.case_id = cmm_evt.case_id
      new_cmm_evt.notes = "From Number: #{from_number}, To Number: #{to_number}, Message: #{message_text}, Payment: #{success}"
      new_cmm_evt.external_identifier = moMsgId
      new_cmm_evt.save

      if success
        # send successful payment notification
        clikatell = ErpTechSvcs::SmsWrapper::Clickatell.new
        clikatell.send_message(from_number, SMS_SUCCESS_NOTIFICATION.gsub('payment_due', payment_due.to_s), :mo => 1, :from => to_number)
        render_true and return
      else
        render_false and return
      end
    end
  else
    Rails.logger.error "ErpInvoicing::SmsController#receive_  ation event #{moMsgId}"
    render :json => {:success => false} and return
  end
end