Skip to content

Commit

Permalink
PaymentSession underway
Browse files Browse the repository at this point in the history
  • Loading branch information
papricek committed Sep 23, 2012
1 parent 6988ba3 commit fe2b59f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
25 changes: 10 additions & 15 deletions lib/gopay/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
module GoPay

BASE_PATH = File.expand_path("../../../", __FILE__)
CREATED = "CREATED"
PAYMENT_METHOD_CHOSEN = "PAYMENT_METHOD_CHOSEN"
PAID = "PAID"
AUTHORIZED = "AUTHORIZED"
CANCELED = "CANCELED"
TIMEOUTED = "TIMEOUTED"
REFUNDED = "REFUNDED"
FAILED = "FAILED"
CALL_COMPLETED = "CALL_COMPLETED"
CALL_FAILED = "CALL_FAILED"
UNKNOWN = "UNKNOWN"
STATUSES = {:created => "CREATED", :payment_method_chosen => "PAYMENT_METHOD_CHOSEN",
:paid => "PAID", :authorized => "AUTHORIZED",
:canceled => "CANCELED", :timeouted => "TIMEOUTED",
:refunded => "REFUNDED", :failed => "FAILED",
:call_completed => "CALL_COMPLETED", :call_failed => "CALL_FAILED",
:unknown => "UNKNOWN"}

def self.configure
yield configuration
Expand All @@ -37,10 +32,10 @@ def self.configure_from_rails
path = ::Rails.root.join("config", "gopay.yml")
configure_from_yaml(path) if File.exists?(path)
env = if defined?(::Rails) && ::Rails.respond_to?(:env)
::Rails.env.to_sym
elsif defined?(::RAILS_ENV)
::RAILS_ENV.to_sym
end
::Rails.env.to_sym
elsif defined?(::RAILS_ENV)
::RAILS_ENV.to_sym
end
configuration.environment ||= (env == :development) ? :test : env
warn "GoPay wasnt properly configured." if GoPay.configuration.goid.blank?
configuration
Expand Down
57 changes: 44 additions & 13 deletions lib/gopay/models/base_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,29 @@ def initialize(attributes = {})
:first_name, :last_name, :city, :street, :postal_code, :country_code,
:email, :phone_number, :p1, :p2, :p3, :p4, :lang,
:session_state
attr_accessor :payment_session_id, :last_response

attr_accessor :payment_session_id, :response

def create
client = Savon::Client.new GoPay.configuration.urls["wsdl"]
response = client.request "createPayment" do |soap|
soap.body = {"paymentCommand" => self.to_soap}
soap_response = client.request "createPayment" do |soap|
soap.body = {"paymentCommand" => payment_command_hash}
end
response = response.to_hash[:create_payment_response][:create_payment_return]
self.response = soap_response.to_hash[:create_payment_response][:create_payment_return]
self.payment_session_id = response[:payment_session_id]
self.last_response = response
valid_response?(response, GoPay::CREATED)
valid_response?(response, GoPay::STATUSES[:created])
end

def load(desired_status = nil)
client = Savon::Client.new GoPay.configuration.urls["wsdl"]
soap_response = client.request "paymentStatus" do |soap|
soap.body = {"paymentSessionInfo" => payment_session_hash}
end
self.response = soap_response.to_hash[:payment_status_response][:payment_status_return]
valid_payment_session?(response, desired_status)
end

def to_soap
def payment_command_hash
{"targetGoId" => target_goid.to_i,
"productName" => product_name,
"totalPrice" => total_price_in_cents,
Expand All @@ -45,7 +54,7 @@ def to_soap
"preAuthorization" => false,
"defaultPaymentChannel" => default_payment_channel,
"recurrentPayment" => false,
"encryptedSignature" => GoPay::Crypt.encrypt(self.concat_payment_command),
"encryptedSignature" => GoPay::Crypt.encrypt(concat_payment_command),
"customerData" => {
"firstName" => first_name,
"lastName" => last_name,
Expand All @@ -59,24 +68,46 @@ def to_soap
"lang" => lang}
end

def to_check_soap
{"eshopGoId" => GoPay.configuration.goid.to_i,
def payment_session_hash
{"targetGoId" => target_goid.to_i,
"paymentSessionId" => payment_session_id,
"encryptedSignature" => GoPay::Crypt.encrypt(self.concat_for_check)}
"encryptedSignature" => GoPay::Crypt.encrypt(concat_payment_session)}
end

def valid_response?(response, status)
raise "CALL NOT COMPLETED " if response[:result] != GoPay::STATUSES[:call_completed]
goid_valid = (response[:target_go_id].to_s == target_goid)

response_valid = {:result => GoPay::CALL_COMPLETED,
:session_state => status,
response_valid = {:session_state => status,
:product_name => product_name,
:total_price => total_price_in_cents.to_s
}.all? { |key, value| response[key].to_s == value.to_s }

response_valid && goid_valid
end

def valid_payment_session?(response, status = nil)
raise "CALL NOT COMPLETED " if response[:result] != GoPay::STATUSES[:call_completed]
status_valid = if status
response[:session_state] == status
else
GoPay::STATUSES.values.include?(response[:session_state])
end
response_valid = {:order_number => order_number,
:product_name => product_name,
:target_go_id => target_goid,
:total_price => total_price_in_cents,
:currency => currency
}.all? { |key, value|
response[key].to_s == value.to_s }

ap response_valid
signature_valid = GoPay::Crypt.sha1(concat_payment_session) == GoPay::Crypt.decrypt(response[:encrypted_signature])
puts signature_valid.inspect

status_valid && response_valid && signature_valid
end

def valid_identity?(params)
params['targetGoId'] == target_goid.to_s &&
params['orderNumber'] == order_number.to_s &&
Expand Down
11 changes: 5 additions & 6 deletions test/models_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ class ModelsTest < Test::Unit::TestCase
should "create and verify this payment on paygate" do
assert @base_payment.create
assert @base_payment.payment_session_id.to_i > 0
assert @base_payment.last_response.is_a?(Hash)
assert @base_payment.response.is_a?(Hash)
end

#should "check status of that payment" do
# @payment.create
# assert @payment.is_in_state?(GoPay::WAITING)
#end

should "check status of that payment" do
@base_payment.create
assert @base_payment.load(GoPay::STATUSES[:created])
end

should "validate base payment identity" do
params = {'targetGoId' => GoPay.configuration.goid.to_s,
Expand Down

0 comments on commit fe2b59f

Please sign in to comment.