paymentsjs-rails 0.3.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58093a5d42fc9719d526070986a566278d06d341
4
- data.tar.gz: 250d8086e14b7587ba637560381ded53de03ba8a
3
+ metadata.gz: 20b2a489e2799321e472e847081d23691d3de31b
4
+ data.tar.gz: 4303f77fb38b6f98a6de33fc501d492240fe4371
5
5
  SHA512:
6
- metadata.gz: 2b824101f01a2cbadf1adefce2dd4a6ce36240ff89b1d667ae803395e1b89be7516ef91bb2245782e3822ceadfa872f5d29c26cbf55f90aa2b4561e059ad96bf
7
- data.tar.gz: 7d50c58c307bc5f222e00377d9a243f52d8979b27ed67ff347d5f46061bf8bf1db7d1e3f552665efb082bb9430e627fe3f84667e352a756ac4bf87c2bf41efad
6
+ metadata.gz: 92b479fcdbec707eb479b86e1a9146199e82dc1f8fcb26228a71e5b05cc3423a36fcbec6e35baaabcd79e5d5244794a419cbeba6dda0df6955117449e0f6f888
7
+ data.tar.gz: e6b592b12dc855670655d5557da2d5402e6be45bc0a8d36dd677e62831d579805138efd5c614f741b007a2081989b4ab7af0893eeb3318ad15f15eb4fb9f4672
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Joshua Bartlett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,13 +1,11 @@
1
1
  # Sage PaymentsJS-Rails Gem
2
2
 
3
- ####NOTE: This gem is not yet finished and has not been extensively tested, be very careful using this gem until v.1.0 is released.
4
-
5
- The PaymentsJS-Rails gem simplifies the integration of Sage's PaymentsJS SDK by adding the PaymentsJs model and making configuring environmental variables easy.
3
+ The PaymentsJS-Rails gem simplifies the integration of Sage's PaymentsJS SDK by adding the PaymentsJs class and making configuring environmental variables easy.
6
4
 
7
5
  ##Installation
8
- Add it to your Gemfile:
6
+ Add to to your Gemfile:
9
7
  ```bash
10
- gem 'paymentsjs-rails'
8
+ gem 'paymentsjs-rails', ">=1.0.0"
11
9
  ```
12
10
 
13
11
  Use Bundler to install:
@@ -21,50 +19,98 @@ config/initializers/paymentsjs-rails.rb
21
19
  ```
22
20
  Then, in your `app/assets/javascripts/application.js` file, add:
23
21
  ```javascript
24
- //= require pay //this adds the pay.min.js file provided via Sage CDN
22
+ //= require payments //this adds the pay.min.js file provided via Sage CDN
25
23
  ```
26
-
27
- Currently this gem is only intended for those using the PayJS(['PayJS/UI']) module. With time it will be extended to other modules.
28
24
 
29
25
  ##Quick Start
30
26
 
31
- Follow the [PaymentsJS GitHub Quick Start guide](https://github.com/SagePayments/PaymentsJS "PaymentsJS"), minus the
27
+ In your `config/initializers/paymentsjs-rails.rb` file, add this:
28
+ ```ruby
29
+ PaymentsJs.configuration do |config|
30
+ config.mid = "YOUR MERCHANT ID"
31
+ config.mkey = "YOUR MERCHANT KEY"
32
+ config.api_key = "YOUR API KEY"
33
+ config.api_secret = "YOUR SECRET KEY"
34
+ end
35
+ ```
36
+ This will set the stationary variables, however you can also set any variable at this point if you think they won't change dynamically (postback_url is one to consider).
37
+ Restart your rails application at this point.
38
+
39
+ In one of your views, add a button:
32
40
  ```html
33
- <script type="text/javascript" src="https://www.sagepayments.net/pay/1.0.0/js/pay.min.js"></script>
41
+ <button id="paymentButton">Pay Now</button>
42
+ ```
43
+
44
+ And then in a script tag add:
45
+ ```javascript
46
+ PayJS(['PayJS/UI'],
47
+ function($UI) {
48
+ $UI.Initialize({
49
+ apiKey: "myDeveloperId",
50
+ merchantId: "999999999997",
51
+ authKey: "ABCD==",
52
+ requestType: "payment",
53
+ requestId: "Invoice12345",
54
+ amount: "1.00",
55
+ elementId: "paymentButton",
56
+ nonce: "ThisIsTotallyUnique",
57
+ debug: true,
58
+ preAuth: false,
59
+ environment: "cert",
60
+ billing: {
61
+ name: "Will Wade",
62
+ address: "123 Address St",
63
+ city: "Denver",
64
+ state: "CO",
65
+ postalCode: "12345"
66
+ }
67
+ });
68
+ $UI.setCallback(function(result) {
69
+ console.log(result.getResponse());
70
+ var wasApproved = result.getTransactionSuccess();
71
+ console.log(wasApproved ? "ka-ching!" : "bummer");
72
+ });
73
+ });
34
74
  ```
35
- part.
75
+ Take a look at the [official guide from Sage](https://github.com/SagePayments/PaymentsJS) for more details.
36
76
 
37
- PaymentsJS requires several variables to be added to the `$UI.Initialize()` function in order to work. The Quick Start comes with several variables preloaded. We'll replace these with embedded ruby to call the same preloaded variables:
77
+ This plugin works by calling the PaymentsJs.new class to generate all the data needed. In your controller, add this:
78
+
79
+ ```ruby
80
+ @payment = PaymentJs.new(amount: "1.00", request_id: "Invoice12345")
81
+ ```
82
+ You can dynamically add any variable, other than the ones set in the initializer. See further down for a list of all variables supported. The only two required for a new object are `amount` and `request_id`.
83
+
84
+ In your view, add this embedded Ruby:
38
85
 
39
86
  ```javascript
40
- PayJS(['PayJS/UI'], // the name of the module we want to use
41
- function($UI) { // assigning the module to a variable
42
- $UI.Initialize({ // configuring the UI
43
- apiKey: "<%= PaymentsJs.api_key %>", // your developer ID
44
- merchantId: "<%= PaymentsJs.mid %>", // your 12-digit account identifier
45
- authKey: "<%= PaymentsJs.encrypt %>", // covered in the next section!
46
- requestType: "<%= PaymentsJs.request_type %>", // use can use "vault" to tokenize a card without charging it
47
- requestId: "<%= PaymentsJs.req_id %>", // an order number, customer or account identifier, etc.
48
- amount: "<%= PaymentsJs.amount %>", // the amount to charge the card. in test mode, different amounts produce different results.
49
- elementId: "paymentButton", // the page element that will trigger the UI
50
- nonce: "<%= PaymentsJs.salt %>", // a unique identifier, used as salt
51
- debug: true, // enables verbose console logging
52
- preAuth: <%= PaymentsJs.pre_auth %>, // run a Sale, rather than a PreAuth
53
- environment: "<%= PaymentsJs.environment %>", // hit the certification environment
54
- addFakeData: true,
55
- billing: {
56
- name: "Shaka Smart",
57
- address: "",
58
- City: "Denver",
59
- state: "CO",
60
- postalCode: "80205"
61
- }
62
- });
63
- $UI.setCallback(function(result) { // custom code that will execute when the UI receives a response
64
- console.log(result.getResponse()); // log the result to the console
65
- var wasApproved = result.getTransactionSuccess();
66
- alert(wasApproved ? "ka-ching!" : "bummer");
67
- });
87
+ PayJS(['PayJS/UI'],
88
+ function($UI) {
89
+ $UI.Initialize({
90
+ apiKey: "<%= @payment.api_key %>",
91
+ merchantId: "<%= @payment.mid %>",
92
+ authKey: "<%= @payment.get_auth_key %>", // Calls the get_auth_key method which will generate the auth key
93
+ requestType: "payment",
94
+ requestId: "<%= @payment.request_id %>",
95
+ amount: "<%= @payment.amount %>",
96
+ elementId: "paymentButton",
97
+ nonce: "<%= @payment.get_salt %>", // Calls the get_salt method which will inject the salt used for the auth key
98
+ debug: true,
99
+ preAuth: false,
100
+ environment: "cert",
101
+ billing: {
102
+ name: "Will Wade",
103
+ address: "123 Address St",
104
+ city: "Denver",
105
+ state: "CO",
106
+ postalCode: "12345"
107
+ }
108
+ });
109
+ $UI.setCallback(function(result) {
110
+ console.log(result.getResponse());
111
+ var wasApproved = result.getTransactionSuccess();
112
+ console.log(wasApproved ? "ka-ching!" : "bummer");
113
+ });
68
114
  });
69
115
  ```
70
116
 
@@ -72,26 +118,15 @@ Reload the page and the payment system should work.
72
118
 
73
119
  ##Configuring
74
120
 
75
- In your `config/initializers/paymentsjs-rails.rb` file, add this:
76
- ```ruby
77
- PaymentsJs.configuration do |config|
78
- config.mid = "YOUR MERCHANT ID"
79
- config.mkey = "YOUR MERCHANT KEY"
80
- config.api_key = "YOUR API KEY"
81
- config.api_secret = "YOUR SECRET KEY"
82
- config.postback_url = "YOUR POSTBACK URL"
83
- end
84
- ```
85
- This will override the default variables.
121
+ Here are the required dynamic variables:
86
122
 
87
- ##Integration
123
+ Variable | Default Value
124
+ --- | ---
125
+ `amount:` | nil
126
+ `request_id:` | nil
127
+ `request_type:` | "payment"
128
+ `pre_auth:` | false
129
+ `postback_url:` | "https://www.example.com"
130
+ `environment:` | "cert"
88
131
 
89
- Integrating is easy and very variable. There are several values that will need to be dynamically set, and in a semi-order. Before you can call `PaymentsJs.encrypt` the following variables need to be set:
90
- ```ruby
91
- PaymentsJs.amount = "ORDER PRICE" #note, this needs to be a string, not a float/integer
92
- PaymentsJs.req_id = "ORDER NUMBER" #if blank, "invoice(xx)" with xx being a random integer between 10 and 42 will be generated
93
- PaymentsJs.request_type = "ORDER REQUEST TYPE"
94
- PaymentsJs.pre_auth = boolean
95
- PaymentsJs.environment = "ORDER ENVIRONMENT"
96
- ```
97
- The other variables are generated by encryption.
132
+ You may also want to add billing data to the object to dynamically load everything in the form, but it's your call.
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'PaymentsjsRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,27 @@
1
+ module Configuration
2
+
3
+ def configuration
4
+ yield self
5
+ end
6
+
7
+ def define_setting(name, default = nil)
8
+ class_variable_set("@@#{name}", default)
9
+
10
+ define_class_method "#{name}=" do |value|
11
+ class_variable_set("@@#{name}", value)
12
+ end
13
+
14
+ define_class_method name do
15
+ class_variable_get("@@#{name}")
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def define_class_method(name, &block)
22
+ (class << self; self; end).instance_eval do
23
+ define_method name, &block
24
+ end
25
+ end
26
+
27
+ end
@@ -1,51 +1,61 @@
1
+ module PaymentsjsRails
2
+ require 'paymentsjs-rails/engine'
3
+ end
4
+
1
5
  class PaymentsJs
2
- #require 'helpers/configuration'
6
+ require 'helpers/configuration'
3
7
  require 'openssl'
4
8
  require 'base64'
5
9
  require 'json'
6
- require 'paymentsjs-rails/rails/engine'
7
10
 
8
- attr_reader :address, :city, :state, :zip, :amount, :req_id, :name, :request_type, :pre_auth, :environment, :api_key, :salt, :mid, :postback_url, :auth_key
11
+ extend Configuration
9
12
 
10
- def initialize(order)
11
-
12
- p order
13
-
14
- @order = order
15
- @address = @order[:address]
16
- @city = @order[:city]
17
- @state = @order[:state]
18
- @zip = @order[:zip]
19
- @amount = @order[:total]
20
- @req_id = @order[:req_id]
21
- @name = @order[:name]
22
- @request_type = @order[:request_type]
23
- @pre_auth = @order[:pre_auth]
24
- @environment = @order[:environment]
25
- @mid = @order[:mid]
26
- @mkey = @order[:mkey]
27
- @api_key = @order[:api_key]
28
- @api_secret = @order[:api_secret]
29
- @postback_url = @order[:postback_url]
30
-
31
- cipher = OpenSSL::Cipher::AES.new(256, :CBC)
32
- cipher.encrypt
33
-
34
- iv = OpenSSL::Random.pseudo_bytes(16)
13
+ # Generate a salt
14
+ def generate_salt(iv)
35
15
  salt = iv.unpack('H*').first
36
16
  salt = salt.bytes.to_a
37
17
  salt = salt.pack('U*')
38
- @salt = Base64.strict_encode64(salt)
39
-
40
- p @salt
41
- p @api_secret
18
+ Base64.strict_encode64(salt)
19
+ end
20
+
21
+ # Set defaults for testing, these will be overwritten in the config
22
+ define_setting :request_type, "payment"
23
+ define_setting :pre_auth, false
24
+ define_setting :postback_url, "https://www.example.com"
25
+ define_setting :environment, "cert"
26
+
27
+ attr_accessor :address, :city, :state, :zip, :amount, :request_id, :name, :request_type, :pre_auth, :environment, :api_key, :salt, :mid, :postback_url, :auth_key
28
+
29
+ def initialize(args = {})
30
+
31
+ @api_key = PaymentsJs.api_key
32
+ @mid = PaymentsJs.mid
33
+ @mkey = PaymentsJs.mkey
34
+ @api_secret = PaymentsJs.api_secret
35
+
36
+ @request_type = args[:request_type].present? ? args[:request_type] : PaymentsJs.request_type
37
+ @request_id = args[:request_id]
38
+ @postback_url = args[:postback_url].present? ? args[:postback_url] : PaymentsJs.postback_url
39
+ @amount = args[:amount]
40
+ @pre_auth = args[:pre_auth].present? ? args[:pre_auth] : PaymentsJs.pre_auth
41
+ @environment = args[:environment].present? ? args[:environment] : PaymentsJs.environment
42
+
43
+ # Generate the salt and iv at initialization so they can be consistently called
44
+ @iv = OpenSSL::Random.pseudo_bytes(16)
45
+ @salt = generate_salt(@iv)
46
+
47
+ end
48
+
49
+ def get_auth_key
50
+ cipher = OpenSSL::Cipher::AES.new(256, :CBC)
51
+ cipher.encrypt
42
52
 
43
53
  req = {
44
54
  "apiKey" => @api_key,
45
55
  "merchantId" => @mid,
46
56
  "merchantKey" => @mkey,
47
57
  "requestType" => @request_type,
48
- "requestId" => @req_id,
58
+ "requestId" => @request_id,
49
59
  "postbackUrl" => @postback_url,
50
60
  "amount" => @amount,
51
61
  "nonce" => @salt,
@@ -56,10 +66,13 @@ class PaymentsJs
56
66
  data = JSON.generate(req)
57
67
  key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(@api_secret, @salt, 1500, 32)
58
68
  cipher.key = key
59
- cipher.iv = iv
60
- authKey = cipher.update(data) + cipher.final()
61
- @auth_key = Base64.strict_encode64(authKey)
62
-
69
+ cipher.iv = @iv
70
+ auth_key = cipher.update(data) + cipher.final()
71
+
72
+ Base64.strict_encode64(auth_key)
63
73
  end
64
-
65
- end
74
+
75
+ def get_salt
76
+ @salt
77
+ end
78
+ end
@@ -0,0 +1,6 @@
1
+ module PaymentsjsRails
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module PaymentsjsRails
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :paymentsjs_rails do
3
+ # # Task goes here
4
+ # end
metadata CHANGED
@@ -1,26 +1,66 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paymentsjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Bartlett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-20 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
13
47
  description: A gem to simplify the encryption of data for the Sage PaymentsJS SDK
14
48
  for usage in Rails apps.
15
- email: joshua.r.bartlett@gmail.com
49
+ email:
50
+ - joshua.r.bartlett@gmail.com
16
51
  executables: []
17
52
  extensions: []
18
53
  extra_rdoc_files: []
19
54
  files:
55
+ - MIT-LICENSE
20
56
  - README.md
21
- - lib/assets/javascripts/pay.js
57
+ - Rakefile
58
+ - lib/helpers/configuration.rb
22
59
  - lib/paymentsjs-rails.rb
23
- - lib/paymentsjs-rails/rails/engine.rb
60
+ - lib/paymentsjs-rails/engine.rb
61
+ - lib/paymentsjs-rails/version.rb
62
+ - lib/tasks/paymentsjs_rails_tasks.rake
63
+ - vendor/assets/javascripts/payments.js
24
64
  homepage: http://rubygems.org/gems/paymentjs-rails
25
65
  licenses:
26
66
  - MIT
@@ -1,7 +0,0 @@
1
- class PaymentsJs
2
- module Rails
3
- class Engine < ::Rails::Engine
4
-
5
- end
6
- end
7
- end