spree_komoju 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +16 -0
  5. data/Gemfile +8 -0
  6. data/LICENSE +26 -0
  7. data/README.md +39 -0
  8. data/Rakefile +21 -0
  9. data/app/assets/javascripts/spree/backend/spree_komoju.js +2 -0
  10. data/app/assets/javascripts/spree/frontend/spree_komoju.js +2 -0
  11. data/app/assets/stylesheets/spree/backend/spree_komoju.css +4 -0
  12. data/app/assets/stylesheets/spree/frontend/spree_komoju.css +4 -0
  13. data/app/controllers/spree/checkout_controller_decorator.rb +18 -0
  14. data/app/models/spree/bank_transfer.rb +26 -0
  15. data/app/models/spree/gateway/komoju_bank_transfer.rb +25 -0
  16. data/app/models/spree/gateway/komoju_credit_card.rb +40 -0
  17. data/app/models/spree/gateway/komoju_konbini.rb +23 -0
  18. data/app/models/spree/komoju_gateway.rb +51 -0
  19. data/app/models/spree/konbini.rb +32 -0
  20. data/app/overrides/add_instruction_to_order_show.rb +8 -0
  21. data/app/views/spree/admin/payments/source_views/_komoju_credit_card.html.erb +1 -0
  22. data/app/views/spree/admin/payments/source_views/_komoju_konbini.html.erb +25 -0
  23. data/app/views/spree/checkout/payment/_komoju_bank_transfer.html.erb +35 -0
  24. data/app/views/spree/checkout/payment/_komoju_credit_card.html.erb +1 -0
  25. data/app/views/spree/checkout/payment/_komoju_konbini.html.erb +11 -0
  26. data/app/views/spree/orders/_bank_transfer.html.erb +10 -0
  27. data/app/views/spree/orders/_konbini.html.erb +17 -0
  28. data/bin/rails +7 -0
  29. data/config/locales/en.yml +16 -0
  30. data/config/routes.rb +2 -0
  31. data/db/migrate/20150331051027_create_spree_konbinis.rb +18 -0
  32. data/db/migrate/20150401074721_create_spree_bank_transfers.rb +22 -0
  33. data/db/migrate/20151126050103_add_gateway_profile_ids_to_spree_konbinis.rb +6 -0
  34. data/lib/active_merchant/billing/gateways/komoju.rb +135 -0
  35. data/lib/generators/spree_komoju/install/images/circle-k.png +0 -0
  36. data/lib/generators/spree_komoju/install/images/daily-yamazaki.png +0 -0
  37. data/lib/generators/spree_komoju/install/images/family-mart.png +0 -0
  38. data/lib/generators/spree_komoju/install/images/lawson.png +0 -0
  39. data/lib/generators/spree_komoju/install/images/ministop.png +0 -0
  40. data/lib/generators/spree_komoju/install/images/seven-eleven.png +0 -0
  41. data/lib/generators/spree_komoju/install/images/sunkus.png +0 -0
  42. data/lib/generators/spree_komoju/install/install_generator.rb +36 -0
  43. data/lib/spree_komoju.rb +2 -0
  44. data/lib/spree_komoju/engine.rb +29 -0
  45. data/lib/spree_komoju/factories.rb +6 -0
  46. data/spec/controllers/spree/checkout_controller_spec.rb +13 -0
  47. data/spec/models/spree/bank_transfer_spec.rb +69 -0
  48. data/spec/models/spree/gateway/komoju_bank_transfer_spec.rb +59 -0
  49. data/spec/models/spree/gateway/komoju_credit_card_spec.rb +117 -0
  50. data/spec/models/spree/gateway/komoju_konbini_spec.rb +41 -0
  51. data/spec/models/spree/komoju_gateway_spec.rb +50 -0
  52. data/spec/models/spree/konbini_spec.rb +69 -0
  53. data/spec/spec_helper.rb +87 -0
  54. data/spree_komoju.gemspec +32 -0
  55. metadata +66 -5
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe Spree::Gateway::KomojuBankTransfer, type: :model do
4
+ subject { described_class.new }
5
+
6
+ describe "#authorize" do
7
+ let(:money) { 1000.0 }
8
+ let(:source) do
9
+ Spree::BankTransfer.create!(
10
+ email: "email",
11
+ given_name: "given_name",
12
+ family_name: "family_name",
13
+ given_name_kana: "given_name_kana",
14
+ family_name_kana: "family_name_kana"
15
+ )
16
+ end
17
+ let(:options) { { login: "api_key", shipping: 100.0, tax: 200.0, subtotal: 800.0, discount: 100.0,
18
+ currency: currency } }
19
+ let(:details) do
20
+ {
21
+ type: "bank_transfer",
22
+ phone: nil,
23
+ email: "email",
24
+ given_name: "given_name",
25
+ family_name: "family_name",
26
+ given_name_kana: "given_name_kana",
27
+ family_name_kana: "family_name_kana"
28
+ }
29
+ end
30
+ let(:params) { { "payment_deadline" => Time.now.iso8601.to_s, "payment_details" => { "order_id" => "order_id", "bank_transfer" => "bank_transfer" } } }
31
+ let(:response) { double(ActiveMerchant::Billing::Response, params: params) }
32
+
33
+ before do
34
+ allow_any_instance_of(Spree::Gateway::KomojuBankTransfer).to receive(:options) { options }
35
+ end
36
+
37
+ context "with currency is USD" do
38
+ let(:currency) { "USD" }
39
+
40
+ it "calls ActiveMerchant::Billing::KomojuGateway#purchase with original options" do
41
+ expect_any_instance_of(ActiveMerchant::Billing::KomojuGateway).to receive(:purchase).with(800.0, details, options) { response }
42
+
43
+ subject.authorize(money, source, options)
44
+ end
45
+ end
46
+
47
+ context "with currency is JPY" do
48
+ let(:currency) { "JPY" }
49
+
50
+ it "calls ActiveMerchant::Billing::KomojuGateway#purchase with options converted from cents to dollars" do
51
+ options_converted_to_dollars = { login: "api_key", shipping: 1.0, tax: 2.0, subtotal: 8.0, discount: 1.0,
52
+ currency: currency }
53
+ expect_any_instance_of(ActiveMerchant::Billing::KomojuGateway).to receive(:purchase).with(998.0, details, options_converted_to_dollars) { response }
54
+
55
+ subject.authorize(money, source, options)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,117 @@
1
+ require "spec_helper"
2
+
3
+ describe Spree::Gateway::KomojuCreditCard, type: :model do
4
+ subject { described_class.new }
5
+ let(:komoju_gateway) { double(ActiveMerchant::Billing::KomojuGateway, purchase: response) }
6
+ let(:response) { double(ActiveMerchant::Billing::Response) }
7
+ before do
8
+ allow(ActiveMerchant::Billing::KomojuGateway).to receive(:new) { komoju_gateway }
9
+ end
10
+
11
+ describe "#purchase" do
12
+ let(:money) { 1000.0 }
13
+ let(:source) do
14
+ double("credit card", gateway_payment_profile_id: payment_profile_id,
15
+ gateway_customer_profile_id: nil,
16
+ to_active_merchant: details)
17
+ end
18
+ let(:currency) { "JPY" }
19
+ let(:details) { double("payment details") }
20
+ let(:options) do
21
+ {
22
+ email: "foo@example.com",
23
+ login: "api_key",
24
+ shipping: 100.0,
25
+ tax: 200.0,
26
+ subtotal: 800.0,
27
+ discount: 100.0,
28
+ currency: currency,
29
+ billing_address: { phone: "09011112222" }
30
+ }
31
+ end
32
+
33
+ context "with no payment profile id" do
34
+ let(:payment_profile_id) { nil }
35
+
36
+ it "calls provider#purchase with original options" do
37
+ expect(komoju_gateway).to receive(:purchase).with(998.0, details, options) { response }
38
+ expect(subject.purchase(money, source, options)).to eq(response)
39
+ end
40
+ end
41
+
42
+ context "with a payment profile id" do
43
+ let(:payment_profile_id) { "tok_123" }
44
+
45
+ it "calls provider#purchase with profile id" do
46
+ expect(komoju_gateway).to receive(:purchase).with(998.0, "tok_123", options) { response }
47
+ expect(subject.purchase(money, source, options)).to eq(response)
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#create_profile" do
53
+ let(:payment) { double("payment", source: source) }
54
+ let(:details) { double("payment details") }
55
+
56
+ context "source has credit card and no gateway_payment_profile_id" do
57
+ let(:source) { double("credit card", gateway_payment_profile_id: nil,
58
+ number: "4111111111111111",
59
+ to_active_merchant: details) }
60
+ before do
61
+ allow(response).to receive(:params).and_return("id" => "tok_123")
62
+ expect(komoju_gateway).to receive(:store).with(details, hash_including()) { response }
63
+ end
64
+
65
+ context "response is success" do
66
+ before do
67
+ allow(response).to receive(:success?).and_return(true)
68
+ allow(source).to receive(:update_attributes!)
69
+ end
70
+
71
+ it "stores token" do
72
+ subject.create_profile(payment)
73
+ end
74
+
75
+ it "updates source with payment profile id" do
76
+ allow(komoju_gateway).to receive(:store).with(details, hash_including()) { response }
77
+ expect(source).to receive(:update_attributes!).with(gateway_payment_profile_id: "tok_123")
78
+ subject.create_profile(payment)
79
+ end
80
+ end
81
+
82
+ context "response is failure" do
83
+ before do
84
+ allow(response).to receive(:success?).and_return(false)
85
+ allow(response).to receive(:message).and_return("error message")
86
+ end
87
+
88
+ it "calls gateway_error on payment" do
89
+ expect(payment).to receive(:gateway_error).with("error message")
90
+ subject.create_profile(payment)
91
+ end
92
+ end
93
+ end
94
+
95
+ context "source has no number" do
96
+ let(:source) { double("credit card", gateway_payment_profile_id: nil,
97
+ number: nil,
98
+ to_active_merchant: details) }
99
+
100
+ it "does not try to store the payment details" do
101
+ expect(komoju_gateway).not_to receive(:store)
102
+ subject.create_profile(payment)
103
+ end
104
+ end
105
+
106
+ context "source already has a payment profile id" do
107
+ let(:source) { double("credit card", gateway_payment_profile_id: "tok_123",
108
+ number: nil,
109
+ to_active_merchant: details) }
110
+
111
+ it "does not try to store the payment details" do
112
+ expect(komoju_gateway).not_to receive(:store)
113
+ subject.create_profile(payment)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe Spree::Gateway::KomojuKonbini, type: :model do
4
+ subject { described_class.new }
5
+
6
+ describe "#authorize" do
7
+ let(:money) { 1000.0 }
8
+ let(:source) { Spree::Konbini.create!(convenience: "lawson") }
9
+ let(:options) { { email: "foo@example.com", login: "api_key", shipping: 100.0, tax: 200.0, subtotal: 800.0, discount: 100.0,
10
+ currency: currency, billing_address: { phone: "09011112222" } } }
11
+ let(:details) { { type: "konbini", store: "lawson", phone: "09011112222", email: "foo@example.com" } }
12
+ let(:params) { { "payment_deadline" => Time.now.iso8601.to_s, "payment_details" => { "receipt" => "receipt", "instructions_url" => "instructions_url", "confirmation_code" => "confirmation_code"} } }
13
+ let(:response) { double(ActiveMerchant::Billing::Response, params: params, success?: true) }
14
+
15
+ before do
16
+ allow_any_instance_of(Spree::Gateway::KomojuKonbini).to receive(:options) { options }
17
+ end
18
+
19
+ context "with currency is USD" do
20
+ let(:currency) { "USD" }
21
+
22
+ it "calls ActiveMerchant::Billing::KomojuGateway#purchase with original options" do
23
+ expect_any_instance_of(ActiveMerchant::Billing::KomojuGateway).to receive(:purchase).with(800.0, details, options) { response }
24
+
25
+ subject.authorize(money, source, options)
26
+ end
27
+ end
28
+
29
+ context "with currency is JPY" do
30
+ let(:currency) { "JPY" }
31
+
32
+ it "calls ActiveMerchant::Billing::KomojuGateway#purchase with options converted from cents to dollars" do
33
+ options_converted_to_dollars = { email: "foo@example.com", login: "api_key", shipping: 1.0, tax: 2.0, subtotal: 8.0, discount: 1.0,
34
+ currency: currency, billing_address: { phone: "09011112222" } }
35
+ expect_any_instance_of(ActiveMerchant::Billing::KomojuGateway).to receive(:purchase).with(998.0, details, options_converted_to_dollars) { response }
36
+
37
+ subject.authorize(money, source, options)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Spree::KomojuGateway, type: :model do
4
+ subject { described_class.new }
5
+
6
+ describe "#provider_class" do
7
+ it { expect(subject.provider_class).to eq ActiveMerchant::Billing::KomojuGateway }
8
+ end
9
+
10
+ describe "#options" do
11
+ it "returns options" do
12
+ api_key = double("api_key")
13
+ allow(subject).to receive(:preferred_api_key) { api_key }
14
+ allow(subject).to receive(:preferred_test) { true }
15
+
16
+ expect(subject.options).to eq ({ api_key: nil, test: true, server: "test", test_mode: true, login: api_key })
17
+ end
18
+ end
19
+
20
+ describe "#auto_capture?" do
21
+ it { expect(subject.auto_capture?).to be_falsy }
22
+ end
23
+
24
+ describe "#supports?" do
25
+ it "returns true" do
26
+ source = double("source")
27
+ expect(subject.supports?(source)).to be_truthy
28
+ end
29
+ end
30
+
31
+ describe "#change_options_to_dollar" do
32
+ it "changes cents to dollar" do
33
+ options = { shipping: 100, tax: 100, subtotal: 100, discount: 100 }
34
+
35
+ expect(subject.change_options_to_dollar(options)).to eq ({ shipping: 1, tax: 1, subtotal: 1, discount: 1 })
36
+ end
37
+ end
38
+
39
+ describe "#gateway_type" do
40
+ it { expect(subject.gateway_type).to eq "gateway" }
41
+ end
42
+
43
+ describe "#method_type" do
44
+ it { expect(subject.method_type).to eq "komoju_gateway" }
45
+ end
46
+
47
+ describe "#payment_source_class" do
48
+ it { expect(subject.payment_source_class).to eq Spree::Gateway }
49
+ end
50
+ end
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ describe Spree::Konbini, type: :model do
4
+ subject { described_class.new }
5
+
6
+ describe "#actions" do
7
+ it { expect(subject.actions).to eq ["capture", "void"] }
8
+ end
9
+
10
+ describe "#can_capture?" do
11
+ let(:payment) { double(Spree::Payment, state: state) }
12
+
13
+ before do
14
+ allow(subject).to receive(:payment) { payment }
15
+ end
16
+
17
+ context "when payment state is not checkout or pending" do
18
+ let(:state) { "void" }
19
+
20
+ it { expect(subject.can_capture?(payment)).to be_falsy }
21
+ end
22
+
23
+ context "when payment state is pending" do
24
+ let(:payment) { double(Spree::Payment, state: state, source: source) }
25
+ let(:state) { "pending" }
26
+ let(:source) { double(Spree::Konbini, expires_at: expires_at) }
27
+
28
+ before do
29
+ allow(subject).to receive(:payment) { payment }
30
+ end
31
+
32
+ context "when expires_at is tomorrow" do
33
+ let(:expires_at) { Date.tomorrow }
34
+
35
+ it { expect(subject.can_capture?(payment)).to be_truthy }
36
+ end
37
+
38
+ context "when expires_at is yesterday" do
39
+ let(:expires_at) { Date.yesterday}
40
+
41
+ it { expect(subject.can_capture?(payment)).to be_falsy }
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#can_void?" do
47
+ let(:payment) { double(Spree::Payment, state: state) }
48
+
49
+ before do
50
+ allow(subject).to receive(:payment) { payment }
51
+ end
52
+
53
+ context "when payment state is void" do
54
+ let(:state) { "void" }
55
+
56
+ it { expect(subject.can_void?(payment)).to be_falsy }
57
+ end
58
+
59
+ context "when payment state is pending" do
60
+ let(:state) { "pending" }
61
+
62
+ it { expect(subject.can_void?(payment)).to be_truthy }
63
+ end
64
+ end
65
+
66
+ describe "#instructions_partial_path" do
67
+ it { expect(subject.instructions_partial_path).to eq "spree/orders/konbini" }
68
+ end
69
+ end
@@ -0,0 +1,87 @@
1
+ # Run Coverage report
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter 'spec/dummy'
5
+ add_group 'Controllers', 'app/controllers'
6
+ add_group 'Helpers', 'app/helpers'
7
+ add_group 'Mailers', 'app/mailers'
8
+ add_group 'Models', 'app/models'
9
+ add_group 'Views', 'app/views'
10
+ add_group 'Libraries', 'lib'
11
+ end
12
+
13
+ # Configure Rails Environment
14
+ ENV['RAILS_ENV'] = 'test'
15
+
16
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
17
+
18
+ require 'rspec/rails'
19
+ require 'database_cleaner'
20
+ require 'ffaker'
21
+
22
+ # Requires supporting ruby files with custom matchers and macros, etc,
23
+ # in spec/support/ and its subdirectories.
24
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
25
+
26
+ # Requires factories and other useful helpers defined in spree_core.
27
+ require 'spree/testing_support/authorization_helpers'
28
+ require 'spree/testing_support/capybara_ext'
29
+ require 'spree/testing_support/controller_requests'
30
+ require 'spree/testing_support/factories'
31
+ require 'spree/testing_support/url_helpers'
32
+
33
+ # Requires factories defined in lib/spree_komoju/factories.rb
34
+ require 'spree_komoju/factories'
35
+
36
+ RSpec.configure do |config|
37
+ config.include FactoryGirl::Syntax::Methods
38
+
39
+ # Infer an example group's spec type from the file location.
40
+ config.infer_spec_type_from_file_location!
41
+
42
+ # == URL Helpers
43
+ #
44
+ # Allows access to Spree's routes in specs:
45
+ #
46
+ # visit spree.admin_path
47
+ # current_path.should eql(spree.products_path)
48
+ config.include Spree::TestingSupport::UrlHelpers
49
+
50
+ # == Mock Framework
51
+ #
52
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
53
+ #
54
+ # config.mock_with :mocha
55
+ # config.mock_with :flexmock
56
+ # config.mock_with :rr
57
+ config.mock_with :rspec
58
+ config.color = true
59
+
60
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
61
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
62
+
63
+ # Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner
64
+ # to cleanup after each test instead. Without transactional fixtures set to false the records created
65
+ # to setup a test will be unavailable to the browser, which runs under a separate server instance.
66
+ config.use_transactional_fixtures = false
67
+
68
+ # Ensure Suite is set to use transactions for speed.
69
+ config.before :suite do
70
+ DatabaseCleaner.strategy = :transaction
71
+ DatabaseCleaner.clean_with :truncation
72
+ end
73
+
74
+ # Before each spec check if it is a Javascript test and switch between using database transactions or not where necessary.
75
+ config.before :each do
76
+ DatabaseCleaner.strategy = RSpec.current_example.metadata[:js] ? :truncation : :transaction
77
+ DatabaseCleaner.start
78
+ end
79
+
80
+ # After each spec clean the database.
81
+ config.after :each do
82
+ DatabaseCleaner.clean
83
+ end
84
+
85
+ config.fail_fast = ENV['FAIL_FAST'] || false
86
+ config.order = "random"
87
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ Gem::Specification.new do |s|
3
+ s.platform = Gem::Platform::RUBY
4
+ s.name = 'spree_komoju'
5
+ s.version = '0.0.3'
6
+ s.summary = 'Spree Komoju Payment Gateway'
7
+ s.description = 'Spree Payment gateway for Komoju payment gateway'
8
+ s.authors = ['Masahiro Saito', 'Chris Salzberg']
9
+ s.required_ruby_version = '>= 2.0.0'
10
+
11
+ # s.author = 'You'
12
+ # s.email = 'you@example.com'
13
+ # s.homepage = 'http://www.spreecommerce.com'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.require_path = 'lib'
18
+ s.requirements << 'none'
19
+
20
+ s.add_dependency 'spree_core', '~> 3.0.0'
21
+
22
+ s.add_development_dependency 'capybara', '~> 2.4'
23
+ s.add_development_dependency 'coffee-rails'
24
+ s.add_development_dependency 'database_cleaner'
25
+ s.add_development_dependency 'factory_girl', '~> 4.5'
26
+ s.add_development_dependency 'ffaker'
27
+ s.add_development_dependency 'rspec-rails', '~> 3.1'
28
+ s.add_development_dependency 'sass-rails', '~> 5.0.0.beta1'
29
+ s.add_development_dependency 'selenium-webdriver'
30
+ s.add_development_dependency 'simplecov'
31
+ s.add_development_dependency 'sqlite3'
32
+ end