JekyllEWP 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyllEWP.rb +238 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 12964c4531c2345816914ddd5597571207bf80bc0b89ad8f30bcec4c5f1f39e3
4
+ data.tar.gz: 17635e0f34f63be74e297cd1e6a13a1a362dbfb31e4e1c58c25664f38be1593e
5
+ SHA512:
6
+ metadata.gz: 9465bcf7f3fcd95bc3b868c7ce90e7f823394200914d2994d7858f9bd0f17317318e04da473bd2c2a34bd183ca9d7396a670c17b91dd89118398aa46b9f151a9
7
+ data.tar.gz: e144ba1cc3f0e415800cf05e9870987c56d0da1b54905519d04c26d2b6366e7cba081a644c78dbd692a5758ed34a32044d69dd895884b838d140d4c4772c9fc8
@@ -0,0 +1,238 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+
5
+ def wrapInForm(encryptedValue, use_sandbox=false, separate_submit=false, button_image = "", identifier="")
6
+
7
+ if identifier.nil?
8
+ identifier = ""
9
+ end
10
+
11
+ if button_image.nil?
12
+ button_image = ""
13
+ end
14
+
15
+ if getBool(use_sandbox) == true
16
+ stage = "sandbox."
17
+ else
18
+ stage=""
19
+ end
20
+
21
+ unless getBool(separate_submit) == true
22
+ submit = '<input type="image" src="' + button_image + '" border="0" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!">'
23
+ id=''
24
+ else
25
+ submit = ""
26
+ id=' id="' + identifier + '"'
27
+ end
28
+
29
+
30
+ return_str = '<form' + id +' action="https://www.' + stage + 'paypal.com/cgi-bin/webscr" method="post">' + '<input type="hidden" name="cmd" value="_s-xclick">' + submit + '<input type="hidden" name="encrypted" value="' + encryptedValue + '"></form>';
31
+
32
+ return return_str
33
+
34
+ end
35
+
36
+
37
+
38
+ def getButtonEncryptionValue(data, privateKeyData, certData, payPalCertData, keyPass = nil)
39
+ #puts data
40
+ #get keys and certs
41
+
42
+ #https://stackoverflow.com/a/11136771
43
+ paypal_pub_cert = OpenSSL::X509::Certificate.new(payPalCertData.gsub('\n', "\n"))
44
+
45
+ my_pub_cert = OpenSSL::X509::Certificate.new(certData.gsub('\n', "\n"))
46
+
47
+ my_private_key = ''
48
+ if keyPass
49
+ #https://stackoverflow.com/a/862090S
50
+ #https://docs.ruby-lang.org/en/2.1.0/OpenSSL/PKey/RSA.html#method-c-new
51
+ my_private_key = OpenSSL::PKey::RSA.new(privateKeyData.gsub('\n', "\n"), keyPass)
52
+ else
53
+ my_private_key = OpenSSL::PKey::RSA.new(privateKeyData.gsub('\n', "\n"))
54
+ end
55
+
56
+
57
+
58
+ #modified from http://railscasts.com/episodes/143-paypal-security
59
+ #https://docs.ruby-lang.org/en/2.1.0/OpenSSL/PKCS7.html#method-c-sign
60
+ signed = OpenSSL::PKCS7::sign(OpenSSL::X509::Certificate.new(my_pub_cert), my_private_key, data, [], OpenSSL::PKCS7::BINARY)
61
+
62
+ OpenSSL::PKCS7::encrypt([OpenSSL::X509::Certificate.new(paypal_pub_cert)], signed.to_der, OpenSSL::Cipher.new("des-ede3-cbc"), OpenSSL::PKCS7::BINARY).to_s.gsub("\n", "")
63
+
64
+ # puts signed.class
65
+ return signed.to_pem()
66
+
67
+ end
68
+
69
+
70
+ def getButtonOptionsString(certID, cmd, paypal_business_email, item_name, item_price, item_number = "0000", currency_code = "USD", tax = nil, shipping = nil )
71
+ options = ""
72
+
73
+ options.concat("cert_id=" + certID + "\n")
74
+ options.concat("cmd=" + cmd + "\n")
75
+
76
+ # if cmd == "_cart"
77
+ # case cart_options
78
+ # when "add"
79
+ # when "display"
80
+ # options.concat(cart_options + "=1\n")
81
+ # when "upload"
82
+ # puts "unsupported value 'upload' used in paypal EWP plugin. the form probably isnt going to work"
83
+ # end
84
+ # end
85
+
86
+ options.concat("business=" + paypal_business_email + "\n")
87
+ options.concat("item_name=" + item_name + "\n")
88
+ #options.concat("item_number=" + item_number + "\n")
89
+ options.concat("amount=" + item_price + "\n")
90
+ options.concat("currency_code=" + currency_code + "\n")
91
+
92
+
93
+
94
+ unless tax.nil? || tax == "0"
95
+ options.concat("tax=" + tax + "\n")
96
+ end
97
+
98
+ unless shipping.nil? || shipping == "0"
99
+ options.concat("shipping=" + shipping + "\n")
100
+ end
101
+
102
+
103
+
104
+ =begin
105
+ Below is the full list of supported key/vaue pairs from the paypal docs (https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/encryptedwebpayments/#id08A3I0PD04Y) the ones beginning with a hash (#) are not implemented here.
106
+
107
+ some of these are also passthrough variables that arent used by paypal: https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/formbasics/#variations-on-basic-variables
108
+
109
+
110
+ cert_id=Z24MFU6DSHBXQ
111
+ cmd=_xclick
112
+ business=sales@company.com
113
+ item_name=Handheld Computer
114
+ #item_number=1234
115
+ #custom=sc-id-789
116
+ amount=500.00
117
+ currency_code=USD
118
+ tax=41.25
119
+ shipping=20.00
120
+ #address_override=1
121
+ #address1=123 Main St
122
+ #city=Austin
123
+ #state=TX
124
+ #zip=94085
125
+ #country=USA
126
+ #cancel_return=https://example.com/cancel
127
+ =end
128
+ return options
129
+
130
+ end
131
+
132
+ #determines the button command from the string input.
133
+ #possible commands listed at https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/formbasics/#specifying-button-type--cmd
134
+
135
+ def getButtonCmd(purpose)
136
+
137
+ case purpose
138
+ when "addtocart"
139
+ return "_cart\nadd=1" #this is a dirty hack to insert the correct parameter for the cart buttons. better solutions welcome
140
+ when "viewcart"
141
+ return "_cart\ndisplay=1" #this is a dirty hack to insert the correct parameter for the cart buttons. better solutions welcome
142
+ when "buynow"
143
+ return "_xclick"
144
+ when "donate"
145
+ return "_donations"
146
+ when "autobilling"
147
+ return "_xclick-auto-billing"
148
+ when "paymentplan"
149
+ return "_xclick-payment-plan"
150
+ else
151
+ return "_xclick"
152
+ end
153
+
154
+ end
155
+
156
+ def getBool(val)
157
+ val.to_s.downcase == 'true'
158
+ end
159
+
160
+
161
+
162
+
163
+ module Jekyll
164
+ class PayPalEWP < Liquid::Tag
165
+
166
+
167
+
168
+ def initialize(tag_name, variables, tokens)
169
+ super
170
+ @variables = variables.split(" ")
171
+
172
+ @buttonpurpose = @variables[0]
173
+
174
+ unless @variables[1].nil?
175
+ @separatesubmitbutton = getBool(@variables[1])
176
+ else
177
+ @separatesubmitbutton = false
178
+ end
179
+
180
+
181
+ unless @variables[2].nil?
182
+
183
+ if @separatesubmitbutton == true
184
+ #is an id
185
+ @formid = @variables[2]
186
+ else
187
+ #is an image
188
+ @buttonimage = @variables[2]
189
+ end
190
+
191
+ else
192
+ #no value provided
193
+ if @separatesubmitbutton == true
194
+ #is an id
195
+ @formid = 0
196
+ else
197
+ #is an image
198
+ @buttonimage = "https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" #some arbitrary thing
199
+ end
200
+
201
+ end
202
+
203
+
204
+ end
205
+
206
+ # Lookup allows access to the page/post variables through the tag context
207
+ #https://blog.sverrirs.com/2016/04/custom-jekyll-tags.html
208
+ def lookup(context, name)
209
+ lookup = context
210
+ name.split(".").each { |value| lookup = lookup[value] }
211
+ lookup
212
+ end
213
+
214
+ def render(context)
215
+
216
+ wrapInForm(
217
+ getButtonEncryptionValue(
218
+ getButtonOptionsString(
219
+ "#{lookup(context, 'site.paypal_cert_id')}",
220
+ getButtonCmd(@buttonpurpose),
221
+ "#{lookup(context, 'site.paypal_email_address')}",
222
+ "#{lookup(context, 'page.name')}", #product name
223
+ "#{lookup(context, 'page.price')}"), #product price
224
+ #"#{lookup(context, 'page.sku')}" #product identifier
225
+ ENV['EWP_PRIVKEY'],
226
+ ENV['EWP_PUBCERT'],
227
+ ENV['EWP_PAYPAL_PUBCERT'],
228
+ ENV['EWP_PRIVKEY_PASS']),
229
+ "#{lookup(context, 'site.paypal_sandbox_mode')}",
230
+ @separatesubmitbutton,
231
+ @buttonimage,
232
+ @formid)
233
+ end
234
+ end
235
+ end
236
+
237
+ Liquid::Template.register_tag('EWPform', Jekyll::PayPalEWP)
238
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JekyllEWP
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Edwards
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This Jekyll plugin creates paypal Encrypted Web Payments buttons and
14
+ encrypts them at build time so items in your store cannot have their prices or other
15
+ attributes changed in Inspect Element
16
+ email: adrian@adriancedwards.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/jekyllEWP.rb
22
+ homepage: https://github.com/MoralCode/Jekyll-EWP
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.1.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A Jekyll plugin to generate and encrypt PayPal buttons on the fly.
45
+ test_files: []