Class: Paymentwall::Widget

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

Constant Summary collapse

BASE_URL =
'https://api.paymentwall.com/api'

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(userId, widgetCode, products = array(), extraParams = {}) ⇒ Widget

Returns a new instance of Widget.



6
7
8
9
10
11
# File 'lib/Paymentwall/Widget.rb', line 6

def initialize(userId, widgetCode, products = array(), extraParams = {})
	@userId = userId
	@widgetCode = widgetCode
	@extraParams = extraParams
	@products = products
end

Class Method Details

.calculateSignature(params, secret, version) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/Paymentwall/Widget.rb', line 119

def self.calculateSignature(params, secret, version)
	require 'digest'
	baseString = ''

	if version == self::SIGNATURE_VERSION_1
		# TODO: throw exception if no uid parameter is present

		baseString += params.include?('uid') ? params['uid'] : ''
		baseString += secret

		return Digest::MD5.hexdigest(baseString)

	else

		keys = params.keys.sort

		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 = p.keys.sort
				subKeys.each do |key|
					value = p[key] 
					baseString += "#{name}[#{key}]=#{value}"
				end
			else
				baseString += "#{name}=#{p}"
			end
		end

		baseString += secret

		if version == self::SIGNATURE_VERSION_3
			return Digest::SHA256.hexdigest(baseString)
		else 
			return Digest::MD5.hexdigest(baseString)
		end

	end
end

Instance Method Details

#getDefaultSignatureVersionObject



13
14
15
# File 'lib/Paymentwall/Widget.rb', line 13

def getDefaultSignatureVersion()
	return self.class::getApiType() != self.class::API_CART ? self.class::DEFAULT_SIGNATURE_VERSION : self.class::SIGNATURE_VERSION_2
end

#getHtmlCode(attributes = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/Paymentwall/Widget.rb', line 102

def getHtmlCode(attributes = {})
	defaultAttributes = {
		'frameborder' => '0',
		'width' => '750',
		'height' => '800'
	}

	attributes = defaultAttributes.merge(attributes)

	attributesQuery = ''
	attributes.each do |attr, value|
		attributesQuery += ' ' + attr.to_s + '="' + value.to_s + '"'
	end

	return '<iframe src="' + self.getUrl() + '" ' + attributesQuery + '></iframe>'
end

#getUrlObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/Paymentwall/Widget.rb', line 18

def getUrl()
	params = {
		'key' => self.class::getAppKey(),
		'uid' => @userId,
		'widget' => @widgetCode
	}

	productsNumber = @products.count()

	if self.class::getApiType() == self.class::API_GOODS

		if @products.kind_of?(Array)

			if productsNumber == 1
				product = @products[0]
				if product.kind_of?(Paymentwall::Product)

					postTrialProduct = nil
					if product.getTrialProduct().kind_of?(Paymentwall::Product)
							postTrialProduct = product
							product = product.getTrialProduct()
					end

					params['amount'] = product.getAmount()
					params['currencyCode'] = product.getCurrencyCode()
					params['ag_name'] = product.getName()
					params['ag_external_id'] = product.getId()
					params['ag_type'] = product.getType()

					if product.getType() == Paymentwall::Product::TYPE_SUBSCRIPTION
						params['ag_period_length'] = product.getPeriodLength()
						params['ag_period_type'] = product.getPeriodType()
						if product.isRecurring()

							params['ag_recurring'] = product.isRecurring() ? 1 : 0

							if postTrialProduct
								params['ag_trial'] = 1;
								params['ag_post_trial_external_id'] = postTrialProduct.getId()
								params['ag_post_trial_period_length'] = postTrialProduct.getPeriodLength()
								params['ag_post_trial_period_type'] = postTrialProduct.getPeriodType()
								params['ag_post_trial_name'] = postTrialProduct.getName()
								params['post_trial_amount'] = postTrialProduct.getAmount()
								params['post_trial_currencyCode'] = postTrialProduct.getCurrencyCode()
							end
						end
					end
				else
					#TODO: self.appendToErrors('Not an instance of Paymentwall::Product')
				end
			else 
				#TODO: self.appendToErrors('Only 1 product is allowed in flexible widget call')
			end

		end

	elsif self.class::getApiType() == self.class::API_CART
		index = 0
		@products.each do |product|
			params['external_ids[' + index.to_s + ']'] = product.getId()

			if product.getAmount() > 0
				params['prices[' + index.to_s + ']'] = product.getAmount()
			end
			if product.getCurrencyCode() != '' && product.getCurrencyCode() != nil
				params['currencies[' + index.to_s + ']'] = product.getCurrencyCode()
			end
			index += 1
		end
	end

	params['sign_version'] = signatureVersion = self.getDefaultSignatureVersion()

	if @extraParams.include?('sign_version')
		signatureVersion = params['sign_version'] = @extraParams['sign_version']
	end

	params = params.merge(@extraParams)

	params['sign'] = self.class.calculateSignature(params, self.class::getSecretKey(), signatureVersion)

	return self.class::BASE_URL + '/' + self.buildController(@widgetCode) + '?' + self.http_build_query(params)
end