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
PINGBACK_TYPE_RISK_UNDER_REVIEW =
200
PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED =
201
PINGBACK_TYPE_RISK_REVIEWED_DECLINED =
202
PINGBACK_TYPE_RISK_AUTHORIZATION_VOIDED =
203
PINGBACK_TYPE_SUBSCRIPTION_CANCELLED =
12
PINGBACK_TYPE_SUBSCRIPTION_EXPIRED =
13
PINGBACK_TYPE_SUBSCRIPTION_FAILED =
14

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.



18
19
20
21
# File 'lib/Paymentwall/Pingback.rb', line 18

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

Instance Method Details

#getParameter(param) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/Paymentwall/Pingback.rb', line 106

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

#getPingbackUniqueIdObject



186
187
188
# File 'lib/Paymentwall/Pingback.rb', line 186

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

#getProductObject



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

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



145
146
147
# File 'lib/Paymentwall/Pingback.rb', line 145

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

#getProductPeriodLengthObject



149
150
151
# File 'lib/Paymentwall/Pingback.rb', line 149

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

#getProductPeriodTypeObject



153
154
155
# File 'lib/Paymentwall/Pingback.rb', line 153

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

#getProductsObject



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/Paymentwall/Pingback.rb', line 169

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



182
183
184
# File 'lib/Paymentwall/Pingback.rb', line 182

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

#getTypeObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/Paymentwall/Pingback.rb', line 114

def getType()
  pingbackTypes = [
    self.class::PINGBACK_TYPE_REGULAR,
    self.class::PINGBACK_TYPE_GOODWILL,
    self.class::PINGBACK_TYPE_NEGATIVE,
    self.class::PINGBACK_TYPE_RISK_UNDER_REVIEW,
    self.class::PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED,
    self.class::PINGBACK_TYPE_RISK_REVIEWED_DECLINED,
    self.class::PINGBACK_TYPE_RISK_AUTHORIZATION_VOIDED,
    self.class::PINGBACK_TYPE_SUBSCRIPTION_CANCELLED,
    self.class::PINGBACK_TYPE_SUBSCRIPTION_EXPIRED,
    self.class::PINGBACK_TYPE_SUBSCRIPTION_FAILED
  ]

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

  return nil
end

#getUserIdObject



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

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

#getVirtualCurrencyAmountObject



141
142
143
# File 'lib/Paymentwall/Pingback.rb', line 141

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

#isCancelableObject



196
197
198
199
# File 'lib/Paymentwall/Pingback.rb', line 196

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

#isDeliverableObject



190
191
192
193
194
# File 'lib/Paymentwall/Pingback.rb', line 190

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

#isIpAddressValidObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/Paymentwall/Pingback.rb', line 72

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/Paymentwall/Pingback.rb', line 84

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



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

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']
  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

#isUnderReviewObject



201
202
203
# File 'lib/Paymentwall/Pingback.rb', line 201

def isUnderReview()
  self.getType() == self.class::PINGBACK_TYPE_RISK_UNDER_REVIEW
end

#validate(skipIpWhitelistCheck = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/Paymentwall/Pingback.rb', line 23

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