paymentwall 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +140 -0
- data/lib/Paymentwall/Base.rb +66 -0
- data/lib/Paymentwall/Pingback.rb +224 -0
- data/lib/Paymentwall/Product.rb +62 -0
- data/lib/Paymentwall/Widget.rb +199 -0
- data/lib/paymentwall.rb +5 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 896e8bc0169a0bf8b239c20cd18234ec99778ce1
|
4
|
+
data.tar.gz: 0f9247445692318337eb0b488e0b94c3ff2488e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c60ef08f45155361801b8e632e0e0434a5b43a517997756d656c75b954715214db2a3747de58bb13802aa3b65374eb22e7c278fc9ec5a3c5a20c00aed5d9922f
|
7
|
+
data.tar.gz: fc4405a6374e4b226412e60d787f6b137754f18b4ca6ff45375ae0da6bf0569d757d2c39ed1e7d806e4844d786d2ccf6c12ed2ca56d023b2d0ec1f114dd4e1b6
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2010-2014 Paymentwall, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
#About Paymentwall
|
2
|
+
[Paymentwall](http://paymentwall.com/?source=gh) is the leading digital payments platform for globally monetizing digital goods and services. Paymentwall assists game publishers, dating sites, rewards sites, SaaS companies and many other verticals to monetize their digital content and services.
|
3
|
+
Merchants can plugin Paymentwall's API to accept payments from over 100 different methods including credit cards, debit cards, bank transfers, SMS/Mobile payments, prepaid cards, eWallets, landline payments and others.
|
4
|
+
|
5
|
+
To sign up for a Paymentwall Merchant Account, [click here](http://paymentwall.com/signup/merchant?source=gh).
|
6
|
+
|
7
|
+
#Paymentwall Ruby Library
|
8
|
+
This library allows developers to use [Paymentwall APIs](http://paymentwall.com/en/documentation/API-Documentation/722?source=gh) (Virtual Currency, Digital Goods featuring recurring billing, and Virtual Cart).
|
9
|
+
|
10
|
+
To use Paymentwall, all you need to do is to sign up for a Paymentwall Merchant Account so you can setup an Application designed for your site.
|
11
|
+
To open your merchant account and set up an application, you can [sign up here](http://paymentwall.com/signup/merchant?source=gh).
|
12
|
+
|
13
|
+
#Installation
|
14
|
+
To install the library in your environment, you can download the [ZIP archive](https://github.com/paymentwall/paymentwall-ruby/archive/master.zip), unzip it and place into your project.
|
15
|
+
|
16
|
+
Alternatively, you can run:
|
17
|
+
|
18
|
+
<code>git clone git://github.com/paymentwall/paymentwall-ruby.git</code>
|
19
|
+
|
20
|
+
Then use a code sample below.
|
21
|
+
|
22
|
+
#Code Samples
|
23
|
+
|
24
|
+
##Digital Goods API
|
25
|
+
|
26
|
+
####Initializing Paymentwall
|
27
|
+
<pre><code>require_relative '/path/to/paymentwall-ruby/lib/paymentwall.rb'
|
28
|
+
Paymentwall::Base::setApiType(Paymentwall::Base::API_GOODS)
|
29
|
+
Paymentwall::Base::setAppKey('YOUR_APPLICATION_KEY') # available in your Paymentwall merchant area
|
30
|
+
Paymentwall::Base::setSecretKey('YOUR_SECRET_KEY') # available in your Paymentwall merchant area
|
31
|
+
</code></pre>
|
32
|
+
|
33
|
+
####Widget Call
|
34
|
+
[Web API details](http://www.paymentwall.com/en/documentation/Digital-Goods-API/710#paymentwall_widget_call_flexible_widget_call)
|
35
|
+
|
36
|
+
The widget is a payment page hosted by Paymentwall that embeds the entire payment flow: selecting the payment method, completing the billing details, and providing customer support via the Help section. You can redirect the users to this page or embed it via iframe. Below is an example that renders an iframe with Paymentwall Widget.
|
37
|
+
|
38
|
+
<pre><code>widget = Paymentwall::Widget.new(
|
39
|
+
'user40012', # id of the end-user who's making the payment
|
40
|
+
'p1_1', # widget code, e.g. p1; can be picked inside of your merchant account
|
41
|
+
[ # product details for Flexible Widget Call. To let users select the product on Paymentwall's end, leave this array empty
|
42
|
+
Paymentwall::Product.new(
|
43
|
+
'product301', # id of the product in your system
|
44
|
+
9.99, # price
|
45
|
+
'USD', # currency code
|
46
|
+
'Gold Membership', # product name
|
47
|
+
Paymentwall::Product::TYPE_SUBSCRIPTION, # this is a time-based product
|
48
|
+
1, # duration is 1
|
49
|
+
Paymentwall::Product::PERIOD_TYPE_MONTH, # month
|
50
|
+
true # recurring
|
51
|
+
)
|
52
|
+
],
|
53
|
+
{'email' => 'user@hostname.com'} # additional parameters
|
54
|
+
)
|
55
|
+
puts widget.getHtmlCode()
|
56
|
+
</code></pre>
|
57
|
+
|
58
|
+
####Pingback Processing
|
59
|
+
|
60
|
+
The Pingback is a webhook notifying about a payment being made. Pingbacks are sent via HTTP/HTTPS to your servers. To process pingbacks use the following code:
|
61
|
+
<pre><code>pingback = Paymentwall::Pingback.new(request_get_params, request_ip_address)
|
62
|
+
if pingback.validate()
|
63
|
+
productId = pingback.getProduct().getId()
|
64
|
+
if pingback.isDeliverable()
|
65
|
+
# deliver the product
|
66
|
+
elsif pingback.isCancelable()
|
67
|
+
# withdraw the product
|
68
|
+
end
|
69
|
+
puts 'OK' # Paymentwall expects response to be OK, otherwise the pingback will be resent
|
70
|
+
else
|
71
|
+
puts pingback.getErrorSummary()
|
72
|
+
end</code></pre>
|
73
|
+
|
74
|
+
##Virtual Currency API
|
75
|
+
|
76
|
+
####Initializing Paymentwall
|
77
|
+
<pre><code>require_relative '/path/to/paymentwall-ruby/lib/paymentwall.rb'
|
78
|
+
Paymentwall::Base::setApiType(Paymentwall_Base::API_VC)
|
79
|
+
Paymentwall::Base::setAppKey('YOUR_APPLICATION_KEY') # available in your Paymentwall merchant area
|
80
|
+
Paymentwall::Base::setSecretKey('YOUR_SECRET_KEY') # available in your Paymentwall merchant area
|
81
|
+
</code></pre>
|
82
|
+
|
83
|
+
####Widget Call
|
84
|
+
<pre><code>widget = Paymentwall::Widget.new(
|
85
|
+
'user40012', # id of the end-user who's making the payment
|
86
|
+
'p1_1', # widget code, e.g. p1; can be picked inside of your merchant account
|
87
|
+
[], # array of products - leave blank for Virtual Currency API
|
88
|
+
{'email' => 'user@hostname.com'} # additional parameters
|
89
|
+
)
|
90
|
+
puts widget.getHtmlCode()
|
91
|
+
</code></pre>
|
92
|
+
|
93
|
+
####Pingback Processing
|
94
|
+
<pre><code>pingback = Paymentwall::Pingback.new(request_get_params, request_ip_address)
|
95
|
+
if pingback.validate()
|
96
|
+
virtualCurrency = pingback.getVirtualCurrencyAmount()
|
97
|
+
if pingback.isDeliverable()
|
98
|
+
# deliver the virtual currency
|
99
|
+
elsif pingback.isCancelable()
|
100
|
+
# withdraw the virual currency
|
101
|
+
end
|
102
|
+
puts 'OK' # Paymentwall expects response to be OK, otherwise the pingback will be resent
|
103
|
+
else
|
104
|
+
puts pingback.getErrorSummary()
|
105
|
+
end</code></pre>
|
106
|
+
|
107
|
+
##Cart API
|
108
|
+
|
109
|
+
####Initializing Paymentwall
|
110
|
+
<pre><code>require_relative '/path/to/paymentwall-ruby/lib/paymentwall.rb'
|
111
|
+
Paymentwall::Base::setApiType(Paymentwall_Base::API_CART)
|
112
|
+
Paymentwall::Base::setAppKey('YOUR_APPLICATION_KEY') # available in your Paymentwall merchant area
|
113
|
+
Paymentwall::Base::setSecretKey('YOUR_SECRET_KEY') # available in your Paymentwall merchant area
|
114
|
+
</code></pre>
|
115
|
+
|
116
|
+
####Widget Call
|
117
|
+
<pre><code>widget = Paymentwall::Widget.new(
|
118
|
+
'user40012', # id of the end-user who's making the payment
|
119
|
+
'p1_1', # widget code, e.g. p1; can be picked inside of your merchant account,
|
120
|
+
[
|
121
|
+
Paymentwall::Product.new('product301', 3.33, 'EUR'), # first product in cart
|
122
|
+
Paymentwall::Product.new('product607', 7.77, 'EUR') # second product in cart
|
123
|
+
],
|
124
|
+
{'email' => 'user@hostname.com'} # additional params
|
125
|
+
)
|
126
|
+
puts widget.getHtmlCode()</code></pre>
|
127
|
+
|
128
|
+
####Pingback Processing
|
129
|
+
<pre><code>pingback = Paymentwall::Pingback.new(request_get_params, request_ip_address)
|
130
|
+
if pingback.validate()
|
131
|
+
products = pingback.getProducts()
|
132
|
+
if pingback.isDeliverable()
|
133
|
+
# deliver the products
|
134
|
+
elsif pingback.isCancelable()
|
135
|
+
# withdraw the products
|
136
|
+
end
|
137
|
+
puts 'OK' # Paymentwall expects response to be OK, otherwise the pingback will be resent
|
138
|
+
else
|
139
|
+
puts pingback.getErrorSummary()
|
140
|
+
end</code></pre>
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Paymentwall
|
2
|
+
class Base
|
3
|
+
|
4
|
+
VERSION = '1.0.0'
|
5
|
+
|
6
|
+
API_VC = 1
|
7
|
+
API_GOODS = 2
|
8
|
+
API_CART = 3
|
9
|
+
|
10
|
+
CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps'
|
11
|
+
CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription'
|
12
|
+
CONTROLLER_PAYMENT_CART = 'cart'
|
13
|
+
|
14
|
+
DEFAULT_SIGNATURE_VERSION = 3
|
15
|
+
SIGNATURE_VERSION_1 = 1
|
16
|
+
SIGNATURE_VERSION_2 = 2
|
17
|
+
SIGNATURE_VERSION_3 = 3
|
18
|
+
|
19
|
+
@@apiType
|
20
|
+
@@appKey
|
21
|
+
@@secretKey
|
22
|
+
|
23
|
+
def self.setApiType(value)
|
24
|
+
@@apiType = value
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.getApiType
|
29
|
+
@@apiType
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.setAppKey(value)
|
33
|
+
@@appKey = value
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.getAppKey
|
38
|
+
@@appKey
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.setSecretKey(value)
|
42
|
+
@@secretKey = value
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.getSecretKey
|
47
|
+
@@secretKey
|
48
|
+
end
|
49
|
+
|
50
|
+
def getErrors
|
51
|
+
@errors
|
52
|
+
end
|
53
|
+
|
54
|
+
def getErrorSummary
|
55
|
+
@errors.join("\n")
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
|
60
|
+
def appendToErrors(err)
|
61
|
+
@errors ||=[]
|
62
|
+
@errors.push(err)
|
63
|
+
self
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,224 @@
|
|
1
|
+
module Paymentwall
|
2
|
+
class Pingback < Paymentwall::Base
|
3
|
+
|
4
|
+
PINGBACK_TYPE_REGULAR = 0
|
5
|
+
PINGBACK_TYPE_GOODWILL = 1
|
6
|
+
PINGBACK_TYPE_NEGATIVE = 2
|
7
|
+
|
8
|
+
def initialize(parameters = {}, ipAddress = '')
|
9
|
+
@parameters = parameters
|
10
|
+
@ipAddress = ipAddress
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate(skipIpWhitelistCheck = false)
|
14
|
+
validated = false
|
15
|
+
|
16
|
+
if self.isParametersValid()
|
17
|
+
if self.isIpAddressValid() || skipIpWhitelistCheck
|
18
|
+
if self.isSignatureValid()
|
19
|
+
validated = true
|
20
|
+
else
|
21
|
+
self.appendToErrors('Wrong signature')
|
22
|
+
end
|
23
|
+
else
|
24
|
+
self.appendToErrors('IP address is not whitelisted')
|
25
|
+
end
|
26
|
+
else
|
27
|
+
self.appendToErrors('Missing parameters')
|
28
|
+
end
|
29
|
+
|
30
|
+
validated
|
31
|
+
end
|
32
|
+
|
33
|
+
def isSignatureValid()
|
34
|
+
signatureParamsToSign = {}
|
35
|
+
|
36
|
+
if self.class::getApiType() == self.class::API_VC
|
37
|
+
signatureParams = Array['uid', 'currency', 'type', 'ref']
|
38
|
+
elsif self.class::getApiType() == self.class::API_GOODS
|
39
|
+
signatureParams = Array['uid', 'goodsid', 'slength', 'speriod', 'type', 'ref']
|
40
|
+
else
|
41
|
+
signatureParams = Array['uid', 'goodsid', 'type', 'ref']
|
42
|
+
@parameters['sign_version'] = self.class::SIGNATURE_VERSION_2
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
if !@parameters.include?('sign_version') || @parameters['sign_version'].to_i == self.class::SIGNATURE_VERSION_1
|
47
|
+
signatureParams.each do |field|
|
48
|
+
signatureParamsToSign[field] = @parameters.include?(field) ? @parameters[field] : nil
|
49
|
+
end
|
50
|
+
|
51
|
+
@parameters['sign_version'] = self.class::SIGNATURE_VERSION_1
|
52
|
+
|
53
|
+
else
|
54
|
+
signatureParamsToSign = @parameters
|
55
|
+
end
|
56
|
+
|
57
|
+
signatureCalculated = self.calculateSignature(signatureParamsToSign, self.class::getSecretKey(), @parameters['sign_version'])
|
58
|
+
|
59
|
+
signature = @parameters.include?('sig') ? @parameters['sig'] : nil
|
60
|
+
|
61
|
+
signature == signatureCalculated
|
62
|
+
end
|
63
|
+
|
64
|
+
def isIpAddressValid()
|
65
|
+
ipsWhitelist = [
|
66
|
+
'174.36.92.186',
|
67
|
+
'174.36.96.66',
|
68
|
+
'174.36.92.187',
|
69
|
+
'174.36.92.192',
|
70
|
+
'174.37.14.28'
|
71
|
+
]
|
72
|
+
|
73
|
+
ipsWhitelist.include? @ipAddress
|
74
|
+
end
|
75
|
+
|
76
|
+
def isParametersValid()
|
77
|
+
errorsNumber = 0
|
78
|
+
requiredParams = []
|
79
|
+
|
80
|
+
if self.class::getApiType() == self.class::API_VC
|
81
|
+
requiredParams = ['uid', 'currency', 'type', 'ref', 'sig']
|
82
|
+
elsif self.class::getApiType() == self.class::API_GOODS
|
83
|
+
requiredParams = ['uid', 'goodsid', 'type', 'ref', 'sig']
|
84
|
+
else
|
85
|
+
requiredParams = ['uid', 'goodsid', 'type', 'ref', 'sig']
|
86
|
+
end
|
87
|
+
|
88
|
+
requiredParams.each do |field|
|
89
|
+
if !@parameters.include?(field) # || $parameters[field] === ''
|
90
|
+
self.appendToErrors("Parameter #{field} is missing")
|
91
|
+
errorsNumber += 1
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
errorsNumber == 0
|
96
|
+
end
|
97
|
+
|
98
|
+
def getParameter(param)
|
99
|
+
if @parameters.include?(param)
|
100
|
+
return @parameters[param]
|
101
|
+
else
|
102
|
+
return nil
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def getType()
|
107
|
+
pingbackTypes = [
|
108
|
+
self.class::PINGBACK_TYPE_REGULAR,
|
109
|
+
self.class::PINGBACK_TYPE_GOODWILL,
|
110
|
+
self.class::PINGBACK_TYPE_NEGATIVE
|
111
|
+
]
|
112
|
+
|
113
|
+
if @parameters.include?('type')
|
114
|
+
if pingbackTypes.include?(@parameters['type'].to_i)
|
115
|
+
return @parameters['type'].to_i
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
return nil
|
120
|
+
end
|
121
|
+
|
122
|
+
def getUserId
|
123
|
+
self.getParameter('uid').to_s
|
124
|
+
end
|
125
|
+
|
126
|
+
def getVirtualCurrencyAmount()
|
127
|
+
self.getParameter('currency').to_i
|
128
|
+
end
|
129
|
+
|
130
|
+
def getProductId()
|
131
|
+
self.getParameter('goodsid').to_s
|
132
|
+
end
|
133
|
+
|
134
|
+
def getProductPeriodLength()
|
135
|
+
self.getParameter('slength').to_i
|
136
|
+
end
|
137
|
+
|
138
|
+
def getProductPeriodType()
|
139
|
+
self.getParameter('speriod').to_s
|
140
|
+
end
|
141
|
+
|
142
|
+
def getProduct()
|
143
|
+
Paymentwall::Product.new(
|
144
|
+
self.getProductId(),
|
145
|
+
0,
|
146
|
+
nil,
|
147
|
+
nil,
|
148
|
+
self.getProductPeriodLength() > 0 ? Paymentwall::Product::TYPE_SUBSCRIPTION : Paymentwall::Product::TYPE_FIXED,
|
149
|
+
self.getProductPeriodLength(),
|
150
|
+
self.getProductPeriodType()
|
151
|
+
)
|
152
|
+
end
|
153
|
+
|
154
|
+
def getProducts()
|
155
|
+
result = []
|
156
|
+
productIds = self.getParameter('goodsid')
|
157
|
+
|
158
|
+
if productIds.kind_of?(Array) && productIds.length > 0
|
159
|
+
productIds.each do |id|
|
160
|
+
result.push(Paymentwall::Product.new(id))
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
return result
|
165
|
+
end
|
166
|
+
|
167
|
+
def getReferenceId()
|
168
|
+
self.getParameter('ref').to_s
|
169
|
+
end
|
170
|
+
|
171
|
+
def getPingbackUniqueId()
|
172
|
+
self.getReferenceId().to_s + '_' + self.getType().to_s
|
173
|
+
end
|
174
|
+
|
175
|
+
def isDeliverable()
|
176
|
+
self.getType() == self.class::PINGBACK_TYPE_REGULAR || self.getType() == self.class::PINGBACK_TYPE_GOODWILL
|
177
|
+
end
|
178
|
+
|
179
|
+
def isCancelable()
|
180
|
+
self.getType() == self.class::PINGBACK_TYPE_NEGATIVE
|
181
|
+
end
|
182
|
+
|
183
|
+
protected
|
184
|
+
|
185
|
+
def calculateSignature(params, secret, version)
|
186
|
+
|
187
|
+
params = params.clone
|
188
|
+
params.delete('sig')
|
189
|
+
|
190
|
+
sortKeys = (version.to_i == self.class::SIGNATURE_VERSION_2 or version.to_i == self.class::SIGNATURE_VERSION_3)
|
191
|
+
keys = sortKeys ? params.keys.sort : params.keys
|
192
|
+
|
193
|
+
baseString = ''
|
194
|
+
|
195
|
+
keys.each do |name|
|
196
|
+
p = params[name]
|
197
|
+
|
198
|
+
# converting array to hash
|
199
|
+
if p.kind_of?(Array)
|
200
|
+
p = Hash[p.map.with_index { |key, value| [value, key] }]
|
201
|
+
end
|
202
|
+
|
203
|
+
if p.kind_of?(Hash)
|
204
|
+
subKeys = sortKeys ? p.keys.sort : p.keys;
|
205
|
+
subKeys.each do |key|
|
206
|
+
value = p[key]
|
207
|
+
baseString += "#{name}[#{key}]=#{value}"
|
208
|
+
end
|
209
|
+
else
|
210
|
+
baseString += "#{name}=#{p}"
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
baseString += secret
|
215
|
+
|
216
|
+
require 'digest'
|
217
|
+
if version.to_i == self.class::SIGNATURE_VERSION_3
|
218
|
+
return Digest::SHA256.hexdigest(baseString)
|
219
|
+
else
|
220
|
+
return Digest::MD5.hexdigest(baseString)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Paymentwall
|
2
|
+
class Product
|
3
|
+
|
4
|
+
TYPE_SUBSCRIPTION = 'subscription'
|
5
|
+
TYPE_FIXED = 'fixed'
|
6
|
+
|
7
|
+
PERIOD_TYPE_DAY = 'day'
|
8
|
+
PERIOD_TYPE_WEEK = 'week'
|
9
|
+
PERIOD_TYPE_MONTH = 'month'
|
10
|
+
PERIOD_TYPE_YEAR = 'year'
|
11
|
+
|
12
|
+
def initialize(productId, amount = 0.0, currencyCode = nil, name = nil, productType = self.class::TYPE_FIXED, periodLength = 0, periodType = nil, recurring = false, trialProduct = nil)
|
13
|
+
@productId = productId
|
14
|
+
@amount = amount.round(2)
|
15
|
+
@currencyCode = currencyCode
|
16
|
+
@name = name
|
17
|
+
@productType = productType
|
18
|
+
@periodLength = periodLength
|
19
|
+
@periodType = periodType
|
20
|
+
@recurring = recurring
|
21
|
+
if (productType == Paymentwall::Product::TYPE_SUBSCRIPTION && recurring && recurring != 0)
|
22
|
+
@trialProduct = trialProduct
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def getId()
|
27
|
+
@productId
|
28
|
+
end
|
29
|
+
|
30
|
+
def getAmount()
|
31
|
+
@amount
|
32
|
+
end
|
33
|
+
|
34
|
+
def getCurrencyCode
|
35
|
+
@currencyCode
|
36
|
+
end
|
37
|
+
|
38
|
+
def getName()
|
39
|
+
@name
|
40
|
+
end
|
41
|
+
|
42
|
+
def getType()
|
43
|
+
@productType
|
44
|
+
end
|
45
|
+
|
46
|
+
def getPeriodType()
|
47
|
+
@periodType
|
48
|
+
end
|
49
|
+
|
50
|
+
def getPeriodLength()
|
51
|
+
@periodLength
|
52
|
+
end
|
53
|
+
|
54
|
+
def isRecurring()
|
55
|
+
@recurring
|
56
|
+
end
|
57
|
+
|
58
|
+
def getTrialProduct()
|
59
|
+
@trialProduct
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
module Paymentwall
|
2
|
+
class Widget < Paymentwall::Base
|
3
|
+
|
4
|
+
BASE_URL = 'https://api.paymentwall.com/api'
|
5
|
+
|
6
|
+
def initialize(userId, widgetCode, products = array(), extraParams = {})
|
7
|
+
@userId = userId
|
8
|
+
@widgetCode = widgetCode
|
9
|
+
@extraParams = extraParams
|
10
|
+
@products = products
|
11
|
+
end
|
12
|
+
|
13
|
+
def getDefaultSignatureVersion()
|
14
|
+
return self.class::getApiType() != self.class::API_CART ? self.class::DEFAULT_SIGNATURE_VERSION : self.class::SIGNATURE_VERSION_2
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def getUrl()
|
19
|
+
params = {
|
20
|
+
'key' => self.class::getAppKey(),
|
21
|
+
'uid' => @userId,
|
22
|
+
'widget' => @widgetCode
|
23
|
+
}
|
24
|
+
|
25
|
+
productsNumber = @products.count()
|
26
|
+
|
27
|
+
if self.class::getApiType() == self.class::API_GOODS
|
28
|
+
|
29
|
+
if @products.kind_of?(Array)
|
30
|
+
|
31
|
+
if productsNumber == 1
|
32
|
+
product = @products[0]
|
33
|
+
if product.kind_of?(Paymentwall::Product)
|
34
|
+
|
35
|
+
postTrialProduct = nil
|
36
|
+
if product.getTrialProduct().kind_of?(Paymentwall::Product)
|
37
|
+
postTrialProduct = product
|
38
|
+
product = product.getTrialProduct()
|
39
|
+
end
|
40
|
+
|
41
|
+
params['amount'] = product.getAmount()
|
42
|
+
params['currencyCode'] = product.getCurrencyCode()
|
43
|
+
params['ag_name'] = product.getName()
|
44
|
+
params['ag_external_id'] = product.getId()
|
45
|
+
params['ag_type'] = product.getType()
|
46
|
+
|
47
|
+
if product.getType() == Paymentwall::Product::TYPE_SUBSCRIPTION
|
48
|
+
params['ag_period_length'] = product.getPeriodLength()
|
49
|
+
params['ag_period_type'] = product.getPeriodType()
|
50
|
+
if product.isRecurring()
|
51
|
+
|
52
|
+
params['ag_recurring'] = product.isRecurring() ? 1 : 0
|
53
|
+
|
54
|
+
if postTrialProduct
|
55
|
+
params['ag_trial'] = 1;
|
56
|
+
params['ag_post_trial_external_id'] = postTrialProduct.getId()
|
57
|
+
params['ag_post_trial_period_length'] = postTrialProduct.getPeriodLength()
|
58
|
+
params['ag_post_trial_period_type'] = postTrialProduct.getPeriodType()
|
59
|
+
params['ag_post_trial_name'] = postTrialProduct.getName()
|
60
|
+
params['post_trial_amount'] = postTrialProduct.getAmount()
|
61
|
+
params['post_trial_currencyCode'] = postTrialProduct.getCurrencyCode()
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
else
|
66
|
+
#TODO: self.appendToErrors('Not an instance of Paymentwall::Product')
|
67
|
+
end
|
68
|
+
else
|
69
|
+
#TODO: self.appendToErrors('Only 1 product is allowed in flexible widget call')
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
elsif self.class::getApiType() == self.class::API_CART
|
75
|
+
index = 0
|
76
|
+
@products.each do |product|
|
77
|
+
params['external_ids[' + index.to_s + ']'] = product.getId()
|
78
|
+
|
79
|
+
if product.getAmount() > 0
|
80
|
+
params['prices[' + index.to_s + ']'] = product.getAmount()
|
81
|
+
end
|
82
|
+
if product.getCurrencyCode() != '' && product.getCurrencyCode() != nil
|
83
|
+
params['currencies[' + index.to_s + ']'] = product.getCurrencyCode()
|
84
|
+
end
|
85
|
+
index += 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
params['sign_version'] = signatureVersion = self.getDefaultSignatureVersion()
|
90
|
+
|
91
|
+
if @extraParams.include?('sign_version')
|
92
|
+
signatureVersion = params['sign_version'] = @extraParams['sign_version']
|
93
|
+
end
|
94
|
+
|
95
|
+
params = params.merge(@extraParams)
|
96
|
+
|
97
|
+
params['sign'] = self.class.calculateSignature(params, self.class::getSecretKey(), signatureVersion)
|
98
|
+
|
99
|
+
return self.class::BASE_URL + '/' + self.buildController(@widgetCode) + '?' + self.http_build_query(params)
|
100
|
+
end
|
101
|
+
|
102
|
+
def getHtmlCode(attributes = {})
|
103
|
+
defaultAttributes = {
|
104
|
+
'frameborder' => '0',
|
105
|
+
'width' => '750',
|
106
|
+
'height' => '800'
|
107
|
+
}
|
108
|
+
|
109
|
+
attributes = defaultAttributes.merge(attributes)
|
110
|
+
|
111
|
+
attributesQuery = ''
|
112
|
+
attributes.each do |attr, value|
|
113
|
+
attributesQuery += ' ' + attr.to_s + '="' + value.to_s + '"'
|
114
|
+
end
|
115
|
+
|
116
|
+
return '<iframe src="' + self.getUrl() + '" ' + attributesQuery + '></iframe>'
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.calculateSignature(params, secret, version)
|
120
|
+
require 'digest'
|
121
|
+
baseString = ''
|
122
|
+
|
123
|
+
if version == self::SIGNATURE_VERSION_1
|
124
|
+
# TODO: throw exception if no uid parameter is present
|
125
|
+
|
126
|
+
baseString += params.include?('uid') ? params['uid'] : ''
|
127
|
+
baseString += secret
|
128
|
+
|
129
|
+
return Digest::MD5.hexdigest(baseString)
|
130
|
+
|
131
|
+
else
|
132
|
+
|
133
|
+
keys = params.keys.sort
|
134
|
+
|
135
|
+
keys.each do |name|
|
136
|
+
p = params[name]
|
137
|
+
|
138
|
+
# converting array to hash
|
139
|
+
if p.kind_of?(Array)
|
140
|
+
p = Hash[p.map.with_index { |key, value| [value, key] }]
|
141
|
+
end
|
142
|
+
|
143
|
+
if p.kind_of?(Hash)
|
144
|
+
subKeys = p.keys.sort
|
145
|
+
subKeys.each do |key|
|
146
|
+
value = p[key]
|
147
|
+
baseString += "#{name}[#{key}]=#{value}"
|
148
|
+
end
|
149
|
+
else
|
150
|
+
baseString += "#{name}=#{p}"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
baseString += secret
|
155
|
+
|
156
|
+
if version == self::SIGNATURE_VERSION_3
|
157
|
+
return Digest::SHA256.hexdigest(baseString)
|
158
|
+
else
|
159
|
+
return Digest::MD5.hexdigest(baseString)
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
protected
|
166
|
+
|
167
|
+
def buildController(widget, flexibleCall = false)
|
168
|
+
if self.class::getApiType() == self.class::API_VC
|
169
|
+
if !/^w|s|mw/.match(widget)
|
170
|
+
return self.class::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY
|
171
|
+
end
|
172
|
+
elsif self.class::getApiType() == self.class::API_GOODS
|
173
|
+
if !flexibleCall
|
174
|
+
if !/^w|s|mw/.match(widget)
|
175
|
+
return self.class::CONTROLLER_PAYMENT_DIGITAL_GOODS
|
176
|
+
end
|
177
|
+
else
|
178
|
+
return self.class::CONTROLLER_PAYMENT_DIGITAL_GOODS
|
179
|
+
end
|
180
|
+
else
|
181
|
+
return self.class::CONTROLLER_PAYMENT_CART
|
182
|
+
end
|
183
|
+
|
184
|
+
return ''
|
185
|
+
end
|
186
|
+
|
187
|
+
def http_build_query(params)
|
188
|
+
result = [];
|
189
|
+
params.each do |key, value|
|
190
|
+
result.push(key + '=' + self.url_encode(value))
|
191
|
+
end
|
192
|
+
return result.join('&')
|
193
|
+
end
|
194
|
+
|
195
|
+
def url_encode(value)
|
196
|
+
URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
data/lib/paymentwall.rb
ADDED
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paymentwall
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paymentwall
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: "This library allows developers to use Paymentwall APIs (Virtual Currency,
|
14
|
+
Digital Goods featuring recurring billing, and Virtual Cart). \n To use Paymentwall,
|
15
|
+
all you need to do is to sign up for a Paymentwall Merchant Account so you can setup
|
16
|
+
an Application designed for your site. To open your merchant account and set up
|
17
|
+
an application. See https://paymentwall.com for details."
|
18
|
+
email: devsupport@paymentwall.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- lib/Paymentwall/Base.rb
|
26
|
+
- lib/Paymentwall/Pingback.rb
|
27
|
+
- lib/Paymentwall/Product.rb
|
28
|
+
- lib/Paymentwall/Widget.rb
|
29
|
+
- lib/paymentwall.rb
|
30
|
+
homepage: https://github.com/paymentwall/paymentwall-ruby
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
metadata: {}
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 2.0.14
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Ruby library for use Paymentwall API!
|
54
|
+
test_files: []
|