class Paymentwall::Pingback

Constants

PINGBACK_TYPE_GOODWILL
PINGBACK_TYPE_NEGATIVE
PINGBACK_TYPE_REGULAR
PINGBACK_TYPE_RISK_AUTHORIZATION_VOIDED
PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED
PINGBACK_TYPE_RISK_REVIEWED_DECLINED
PINGBACK_TYPE_RISK_UNDER_REVIEW
PINGBACK_TYPE_SUBSCRIPTION_CANCELLED
PINGBACK_TYPE_SUBSCRIPTION_EXPIRED
PINGBACK_TYPE_SUBSCRIPTION_FAILED

Public Class Methods

new(parameters = {}, ipAddress = '') click to toggle source
# File lib/Paymentwall/Pingback.rb, line 18
def initialize(parameters = {}, ipAddress = '')
        @parameters = parameters
        @ipAddress = ipAddress
end

Public Instance Methods

getParameter(param) click to toggle source
# File lib/Paymentwall/Pingback.rb, line 106
def getParameter(param)
        if @parameters.include?(param)
                return @parameters[param]
        else 
                return nil
        end
end
getPingbackUniqueId() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 186
def getPingbackUniqueId()
        self.getReferenceId().to_s + '_' + self.getType().to_s
end
getProduct() click to toggle source
# 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
getProductId() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 145
def getProductId()
        self.getParameter('goodsid').to_s
end
getProductPeriodLength() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 149
def getProductPeriodLength()
        self.getParameter('slength').to_i
end
getProductPeriodType() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 153
def getProductPeriodType()
        self.getParameter('speriod').to_s
end
getProducts() click to toggle source
# 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
getReferenceId() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 182
def getReferenceId()
        self.getParameter('ref').to_s
end
getType() click to toggle source
# 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
getUserId() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 137
def getUserId
        self.getParameter('uid').to_s
end
getVirtualCurrencyAmount() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 141
def getVirtualCurrencyAmount()
        self.getParameter('currency').to_i
end
isCancelable() click to toggle source
# 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
isDeliverable() click to toggle source
# 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
isIpAddressValid() click to toggle source
# 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
isParametersValid() click to toggle source
# 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
isSignatureValid() click to toggle source
# 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
isUnderReview() click to toggle source
# File lib/Paymentwall/Pingback.rb, line 201
def isUnderReview()
        self.getType() == self.class::PINGBACK_TYPE_RISK_UNDER_REVIEW
end
validate(skipIpWhitelistCheck = false) click to toggle source
# 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

Protected Instance Methods

calculateSignature(params, secret, version) click to toggle source
# File lib/Paymentwall/Pingback.rb, line 207
def calculateSignature(params, secret, version)
        
        params = params.clone
        params.delete('sig')

        sortKeys = (version.to_i == self.class::SIGNATURE_VERSION_2 or version.to_i == self.class::SIGNATURE_VERSION_3)
        keys = sortKeys ? params.keys.sort : params.keys

        baseString = ''

        keys.each do |name| 
                p = params[name]

                # converting array to hash
                if p.kind_of?(Array)
                        p = Hash[p.map.with_index { |key, value| [value, key] }]
                end

                if p.kind_of?(Hash)
                        subKeys = sortKeys ? p.keys.sort : p.keys;
                        subKeys.each do |key|
                                value = p[key] 
                                baseString += "#{name}[#{key}]=#{value}"
                        end
                else
                        baseString += "#{name}=#{p}"
                end
        end

        baseString += secret

        require 'digest'
        if version.to_i == self.class::SIGNATURE_VERSION_3
                return Digest::SHA256.hexdigest(baseString)
        else 
                return Digest::MD5.hexdigest(baseString)
        end
end