Class: Paymentwall::Pingback

Inherits:
Base
  • Object
show all
Defined in:
lib/Paymentwall/Pingback.rb

Constant Summary collapse

PINGBACK_TYPE_REGULAR =
0
PINGBACK_TYPE_GOODWILL =
1
PINGBACK_TYPE_NEGATIVE =
2

Constants inherited from Base

Base::API_CART, Base::API_GOODS, Base::API_VC, Base::CONTROLLER_PAYMENT_CART, Base::CONTROLLER_PAYMENT_DIGITAL_GOODS, Base::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY, Base::DEFAULT_SIGNATURE_VERSION, Base::SIGNATURE_VERSION_1, Base::SIGNATURE_VERSION_2, Base::SIGNATURE_VERSION_3, Base::VERSION

Instance Method Summary collapse

Methods inherited from Base

getApiType, getAppKey, #getErrorSummary, #getErrors, getSecretKey, setApiType, setAppKey, setSecretKey

Constructor Details

#initialize(parameters = {}, ipAddress = '') ⇒ Pingback

Returns a new instance of Pingback.



8
9
10
11
# File 'lib/Paymentwall/Pingback.rb', line 8

def initialize(parameters = {}, ipAddress = '')
	@parameters = parameters
	@ipAddress = ipAddress
end

Instance Method Details

#getParameter(param) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/Paymentwall/Pingback.rb', line 98

def getParameter(param)
	if @parameters.include?(param)
		return @parameters[param]
	else 
		return nil
	end
end

#getPingbackUniqueIdObject



171
172
173
# File 'lib/Paymentwall/Pingback.rb', line 171

def getPingbackUniqueId()
	self.getReferenceId().to_s + '_' + self.getType().to_s
end

#getProductObject



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/Paymentwall/Pingback.rb', line 142

def getProduct()
	Paymentwall::Product.new(
		self.getProductId(),
		0,
		nil,
		nil,
		self.getProductPeriodLength() > 0 ? Paymentwall::Product::TYPE_SUBSCRIPTION : Paymentwall::Product::TYPE_FIXED,
		self.getProductPeriodLength(),
		self.getProductPeriodType()
	)
end

#getProductIdObject



130
131
132
# File 'lib/Paymentwall/Pingback.rb', line 130

def getProductId()
	self.getParameter('goodsid').to_s
end

#getProductPeriodLengthObject



134
135
136
# File 'lib/Paymentwall/Pingback.rb', line 134

def getProductPeriodLength()
	self.getParameter('slength').to_i
end

#getProductPeriodTypeObject



138
139
140
# File 'lib/Paymentwall/Pingback.rb', line 138

def getProductPeriodType()
	self.getParameter('speriod').to_s
end

#getProductsObject



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/Paymentwall/Pingback.rb', line 154

def getProducts()
	result = []
	productIds = self.getParameter('goodsid')

	if productIds.kind_of?(Array) && productIds.length > 0
		productIds.each do |id|
			result.push(Paymentwall::Product.new(id))
		end
	end

	return result
end

#getReferenceIdObject



167
168
169
# File 'lib/Paymentwall/Pingback.rb', line 167

def getReferenceId()
	self.getParameter('ref').to_s
end

#getTypeObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/Paymentwall/Pingback.rb', line 106

def getType()
	pingbackTypes = [
		self.class::PINGBACK_TYPE_REGULAR,
		self.class::PINGBACK_TYPE_GOODWILL,
		self.class::PINGBACK_TYPE_NEGATIVE
	]

	if @parameters.include?('type')
		if pingbackTypes.include?(@parameters['type'].to_i)
			return @parameters['type'].to_i
		end
	end

	return nil
end

#getUserIdObject



122
123
124
# File 'lib/Paymentwall/Pingback.rb', line 122

def getUserId
	self.getParameter('uid').to_s
end

#getVirtualCurrencyAmountObject



126
127
128
# File 'lib/Paymentwall/Pingback.rb', line 126

def getVirtualCurrencyAmount()
	self.getParameter('currency').to_i
end

#isCancelableObject



179
180
181
# File 'lib/Paymentwall/Pingback.rb', line 179

def isCancelable()
	self.getType() == self.class::PINGBACK_TYPE_NEGATIVE
end

#isDeliverableObject



175
176
177
# File 'lib/Paymentwall/Pingback.rb', line 175

def isDeliverable()
	self.getType() == self.class::PINGBACK_TYPE_REGULAR || self.getType() == self.class::PINGBACK_TYPE_GOODWILL
end

#isIpAddressValidObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/Paymentwall/Pingback.rb', line 64

def isIpAddressValid()
	ipsWhitelist = [
		'174.36.92.186',
		'174.36.96.66',
		'174.36.92.187',
		'174.36.92.192',
		'174.37.14.28'
	]

	ipsWhitelist.include? @ipAddress
end

#isParametersValidObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/Paymentwall/Pingback.rb', line 76

def isParametersValid()
	errorsNumber = 0
	requiredParams = []

	if self.class::getApiType() == self.class::API_VC
		requiredParams = ['uid', 'currency', 'type', 'ref', 'sig']
	elsif self.class::getApiType() == self.class::API_GOODS
		requiredParams = ['uid', 'goodsid', 'type', 'ref', 'sig']
	else
		requiredParams = ['uid', 'goodsid', 'type', 'ref', 'sig']
	end

	requiredParams.each do |field|
		if !@parameters.include?(field) # || $parameters[field] === ''
			self.appendToErrors("Parameter #{field} is missing")
			errorsNumber += 1
		end
	end

	errorsNumber == 0
end

#isSignatureValidObject



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
# File 'lib/Paymentwall/Pingback.rb', line 33

def isSignatureValid()
	signatureParamsToSign = {}

	if self.class::getApiType() == self.class::API_VC
		signatureParams = Array['uid', 'currency', 'type', 'ref']
	elsif self.class::getApiType() == self.class::API_GOODS
		signatureParams = Array['uid', 'goodsid', 'slength', 'speriod', 'type', 'ref']
	else
		signatureParams = Array['uid', 'goodsid', 'type', 'ref']
		@parameters['sign_version'] = self.class::SIGNATURE_VERSION_2

	end

	if !@parameters.include?('sign_version') || @parameters['sign_version'].to_i == self.class::SIGNATURE_VERSION_1
		signatureParams.each do |field|
			signatureParamsToSign[field] = @parameters.include?(field) ? @parameters[field] : nil
		end
		
		@parameters['sign_version'] = self.class::SIGNATURE_VERSION_1

	else
		signatureParamsToSign = @parameters
	end

	signatureCalculated = self.calculateSignature(signatureParamsToSign, self.class::getSecretKey(), @parameters['sign_version'])
	
	signature = @parameters.include?('sig') ? @parameters['sig'] : nil

	signature == signatureCalculated
end

#validate(skipIpWhitelistCheck = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/Paymentwall/Pingback.rb', line 13

def validate(skipIpWhitelistCheck = false)
	validated = false

	if self.isParametersValid()
		if self.isIpAddressValid() || skipIpWhitelistCheck
			if self.isSignatureValid()
				validated = true
			else 
				self.appendToErrors('Wrong signature')
			end
		else
			self.appendToErrors('IP address is not whitelisted')
		end
	else
		self.appendToErrors('Missing parameters')
	end

	validated
end