Class: GoPay::BasePayment

Inherits:
Object
  • Object
show all
Defined in:
lib/gopay/models/base_payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BasePayment

Returns a new instance of BasePayment.



7
8
9
10
11
12
13
14
15
# File 'lib/gopay/models/base_payment.rb', line 7

def initialize(attributes = {})
  attributes.each do |key, value|
    instance_variable_set(:"@#{key}", value) if self.respond_to?(key)
  end
  @target_goid ||= GoPay.configuration.goid.to_s
  @secure_key ||= GoPay.configuration.secure_key.to_s
  @payment_channels ||= []
  @payment_channels = @payment_channels.join(',')
end

Instance Attribute Details

#cityObject (readonly)

Returns the value of attribute city.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def city
  @city
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def country_code
  @country_code
end

#currencyObject (readonly)

Returns the value of attribute currency.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def currency
  @currency
end

#default_payment_channelObject (readonly)

Returns the value of attribute default_payment_channel.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def default_payment_channel
  @default_payment_channel
end

#emailObject (readonly)

Returns the value of attribute email.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def email
  @email
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def first_name
  @first_name
end

#langObject (readonly)

Returns the value of attribute lang.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def lang
  @lang
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def last_name
  @last_name
end

#order_numberObject (readonly)

Returns the value of attribute order_number.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def order_number
  @order_number
end

#p1Object (readonly)

Returns the value of attribute p1.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def p1
  @p1
end

#p2Object (readonly)

Returns the value of attribute p2.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def p2
  @p2
end

#p3Object (readonly)

Returns the value of attribute p3.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def p3
  @p3
end

#p4Object (readonly)

Returns the value of attribute p4.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def p4
  @p4
end

#payment_channelsObject (readonly)

Returns the value of attribute payment_channels.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def payment_channels
  @payment_channels
end

#payment_session_idObject

Returns the value of attribute payment_session_id.



23
24
25
# File 'lib/gopay/models/base_payment.rb', line 23

def payment_session_id
  @payment_session_id
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def phone_number
  @phone_number
end

#postal_codeObject (readonly)

Returns the value of attribute postal_code.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def postal_code
  @postal_code
end

#product_nameObject (readonly)

Returns the value of attribute product_name.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def product_name
  @product_name
end

#responseObject

Returns the value of attribute response.



23
24
25
# File 'lib/gopay/models/base_payment.rb', line 23

def response
  @response
end

#secure_keyObject (readonly)

Returns the value of attribute secure_key.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def secure_key
  @secure_key
end

#session_stateObject (readonly)

Returns the value of attribute session_state.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def session_state
  @session_state
end

#streetObject (readonly)

Returns the value of attribute street.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def street
  @street
end

#target_goidObject (readonly)

Returns the value of attribute target_goid.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def target_goid
  @target_goid
end

#total_price_in_centsObject (readonly)

Returns the value of attribute total_price_in_cents.



17
18
19
# File 'lib/gopay/models/base_payment.rb', line 17

def total_price_in_cents
  @total_price_in_cents
end

Instance Method Details

#concat_payment_commandObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gopay/models/base_payment.rb', line 126

def concat_payment_command
  [target_goid,
   product_name.strip,
   total_price_in_cents,
   currency,
   order_number,
   GoPay.configuration.failed_url,
   GoPay.configuration.success_url,
   0, #preAuthorization
   0, #recurrentPayment
   nil, #recurrenceDateTo
   nil, #recurrenceCycle
   nil, #recurrencePeriod,
   payment_channels,
   secure_key].map { |attr| attr.to_s }.join("|")
end

#concat_payment_identity(params) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/gopay/models/base_payment.rb', line 118

def concat_payment_identity(params)
  [params['targetGoId'],
   params['paymentSessionId'],
   params['parentPaymentSessionId'],
   params['orderNumber'],
   secure_key].map { |attr| attr.to_s.strip }.join("|")
end

#concat_payment_sessionObject



143
144
145
146
147
# File 'lib/gopay/models/base_payment.rb', line 143

def concat_payment_session
  [target_goid,
   payment_session_id,
   secure_key].map { |attr| attr.to_s.strip }.join("|")
end

#concat_payment_status(response) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gopay/models/base_payment.rb', line 149

def concat_payment_status(response)
  [response[:target_go_id],
   response[:product_name],
   response[:total_price],
   response[:currency],
   response[:order_number],
   response[:recurrent_payment] ? 1 : 0,
   response[:parent_payment_session_id],
   response[:pre_authorization] ? 1 : 0,
   response[:result],
   response[:session_state],
   response[:session_sub_state],
   response[:payment_channel],
   secure_key].map { |attr| attr.is_a?(Hash) ? "" : attr.to_s }.join("|")
end

#createObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/gopay/models/base_payment.rb', line 25

def create
  client = Savon::Client.new :wsdl => GoPay.configuration.urls["wsdl"], :log => false
  soap_response = client.call :create_payment, :message => { :payment_command => payment_command_hash }

  self.response = soap_response.to_hash[:create_payment_response][:create_payment_return]
  valid_response?(response, GoPay::STATUSES[:created])
  valid = valid_response?(response, GoPay::STATUSES[:created])
  self.payment_session_id = response[:payment_session_id] if valid
  valid
end

#gopay_urlObject



165
166
167
168
169
170
171
172
# File 'lib/gopay/models/base_payment.rb', line 165

def gopay_url
  return unless payment_session_id
  parameters = {"sessionInfo.targetGoId" => target_goid,
                "sessionInfo.paymentSessionId" => payment_session_id,
                "sessionInfo.encryptedSignature" => GoPay::Crypt.encrypt(self.concat_payment_session)}
  query_string = parameters.map { |key, value| "#{key}=#{value}" }.join("&")
  GoPay.configuration.urls["full_integration"] + "?" + query_string
end

#is_in_status?(status) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/gopay/models/base_payment.rb', line 44

def is_in_status?(status)
  load(status)
end

#load(validated_status = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/gopay/models/base_payment.rb', line 36

def load(validated_status = nil)
  client = Savon::Client.new :wsdl => GoPay.configuration.urls["wsdl"], :log => false
  soap_response = client.call :payment_status, :message => { :payment_session_info => payment_session_hash }

  self.response = soap_response.to_hash[:payment_status_response][:payment_status_return]
  valid_payment_session?(response, validated_status)
end

#payment_command_hashObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gopay/models/base_payment.rb', line 48

def payment_command_hash
  {"targetGoId" => target_goid.to_i,
   "productName" => product_name,
   "totalPrice" => total_price_in_cents,
   "currency" => currency,
   "orderNumber" => order_number,
   "successURL" => GoPay.configuration.success_url,
   "failedURL" => GoPay.configuration.failed_url,
   "preAuthorization" => false,
   "defaultPaymentChannel" => default_payment_channel,
   "recurrentPayment" => false,
   "encryptedSignature" => GoPay::Crypt.encrypt(concat_payment_command),
   "customerData" => {
       "firstName" => first_name,
       "lastName" => last_name,
       "city" => city,
       "street" => street,
       "postalCode" => postal_code,
       "countryCode" => country_code,
       "email" => email,
       "phoneNumber" => phone_number},
   "paymentChannels" => payment_channels,
   "lang" => lang}
end

#payment_session_hashObject



73
74
75
76
77
# File 'lib/gopay/models/base_payment.rb', line 73

def payment_session_hash
  {"targetGoId" => target_goid.to_i,
   "paymentSessionId" => payment_session_id,
   "encryptedSignature" => GoPay::Crypt.encrypt(concat_payment_session)}
end

#valid_identity?(params, padding_off = false) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
# File 'lib/gopay/models/base_payment.rb', line 111

def valid_identity?(params, padding_off = false)
  raise 'invalid targetGoId' unless params['targetGoId'] == target_goid.to_s
  raise 'invalid orderNumber' unless params['orderNumber'] == order_number.to_s
  raise 'invalid encryptedSignature' unless GoPay::Crypt.sha1(concat_payment_identity(params)) == GoPay::Crypt.decrypt(params['encryptedSignature'], padding_off)
  true
end

#valid_payment_session?(response, status = nil) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gopay/models/base_payment.rb', line 91

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 }

  signature_valid = GoPay::Crypt.sha1(concat_payment_status(response)) == GoPay::Crypt.decrypt(response[:encrypted_signature])

  status_valid && response_valid && signature_valid
end

#valid_response?(response, status) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gopay/models/base_payment.rb', line 79

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 = {: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