Class: BillingIntegration::Mpay

Inherits:
BillingIntegration
  • Object
show all
Defined in:
app/models/billing_integration/mpay.rb

Overview

Integrate our payment gateway with spree. This is needed to allow configuration through spreeā€™s web interface, etc.

Constant Summary collapse

TEST_REDIRECT_URL =
'https://test.mPAY24.com/app/bin/etpv5'
PRODUCTION_REDIRECT_URL =
'https://www.mpay24.com/app/bin/etpv5'
MPAY24_IP =
"213.164.25.245"
MPAY24_TEST_IP =
"213.164.23.169"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject



22
23
24
25
26
# File 'app/models/billing_integration/mpay.rb', line 22

def self.current
  # I'm not sure why I'm needing RAILS_ENV.to_s. It looks like a string
  # but cannot otherwise be compared to another string
  BillingIntegration::Mpay.where(:active => true).where(:environment => RAILS_ENV.to_s).first
end

Instance Method Details

#find_order(tid) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/billing_integration/mpay.rb', line 46

def find_order(tid)
  if prefers_secret_phrase?
    if tid.starts_with?(preferred_secret_phrase)
      tid = tid.gsub(/^#{preferred_secret_phrase}_/, "")
    else
      raise "unknown secret phrase: #{tid}".inspect
    end
  end

  Order.find(:first, :conditions => { :id => tid })
end

#gateway_urlObject



58
59
60
# File 'app/models/billing_integration/mpay.rb', line 58

def gateway_url
  prefers_test_mode? ? TEST_REDIRECT_URL : PRODUCTION_REDIRECT_URL
end

#generate_url(order) ⇒ Object

generate the iframe URL



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/billing_integration/mpay.rb', line 71

def generate_url(order)

  cmd = generate_mdxi(order)

  # send the HTTP request
  response = send_request(merchant_id, cmd)

  result = parse_result(response)

  # if everything did work out: return the link url. Otherwise
  # output an ugly exception (at least we will get notified)
  if result["STATUS"] == "OK" && result["RETURNCODE"] == "REDIRECT"
    order.created_at = Time.now
    order.save!
    return result["LOCATION"].chomp
  else
    raise response.body.inspect
  end
end

#merchant_idObject



66
67
68
# File 'app/models/billing_integration/mpay.rb', line 66

def merchant_id
  prefers_test_mode? ? preferred_test_merchant_id : preferred_production_merchant_id
end

#mpay24_ipObject



62
63
64
# File 'app/models/billing_integration/mpay.rb', line 62

def mpay24_ip
  prefers_test_mode? ? MPAY24_TEST_IP : MPAY24_IP
end

#provider_classObject



18
19
20
# File 'app/models/billing_integration/mpay.rb', line 18

def provider_class
  ActiveMerchant::Billing::MpayGateway
end

#verify_ip(request) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/billing_integration/mpay.rb', line 28

def verify_ip(request)
  if request.env['REMOTE_ADDR'] != mpay24_ip
    if request.env['REMOTE_ADDR'] == "127.0.0.1"
      #maybe we've gotten forwarded by the nginx reverse proxy
	if request.env.include?('HTTP_X_FORWARDED_FOR')
        ips = request.env['HTTP_X_FORWARDED_FOR'].split(',').map(&:strip)
        if ips[1] != mpay24_ip
          raise "invalid forwarded originator IP of x#{ips[1]}x vs #{mpay24_ip}".inspect
        end 
      else
        raise request.env.inspect
      end
    else
      raise "invalid originator IP of #{request.env['REMOTE_ADDR']} vs #{mpay24_ip}".inspect
    end
  end
end