beecart 0.0.2 → 0.0.3

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.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/beecart/{models/shopping_cart.rb → cart.rb} +52 -25
  3. data/lib/beecart/configuration.rb +58 -0
  4. data/lib/beecart/current_cart.rb +23 -0
  5. data/lib/beecart/error.rb +6 -0
  6. data/lib/beecart/gateway/base_gateway.rb +23 -0
  7. data/lib/beecart/gateway/webpay_gateway.rb +60 -0
  8. data/lib/beecart/validator/validator.rb +7 -0
  9. data/lib/beecart/version.rb +1 -1
  10. data/lib/beecart.rb +9 -38
  11. data/test/dummy/app/controllers/application_controller.rb +2 -0
  12. data/test/dummy/app/controllers/carts_controller.rb +26 -0
  13. data/test/dummy/app/views/carts/index.html.erb +8 -2
  14. data/test/dummy/config/initializers/beecart.rb +12 -5
  15. data/test/dummy/config/initializers/webpay.rb +1 -0
  16. data/test/dummy/config/routes.rb +3 -2
  17. data/test/dummy/db/development.sqlite3 +0 -0
  18. data/test/dummy/log/development.log +7736 -0
  19. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  20. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  21. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  22. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  23. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  24. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  25. data/test/dummy/tmp/pids/server.pid +1 -0
  26. metadata +55 -10
  27. data/app/assets/javascripts/cartbee/application.js +0 -13
  28. data/app/assets/stylesheets/cartbee/application.css +0 -13
  29. data/app/controllers/beecart/application_controller.rb +0 -4
  30. data/app/helpers/beecart/application_helper.rb +0 -4
  31. data/app/views/layouts/beecart/application.html.erb +0 -14
  32. data/lib/beecart/controllers/core_ext.rb +0 -13
  33. data/lib/beecart/engine.rb +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ccbd0ea9e12e035c4b114b5835be8983b384a2f
4
- data.tar.gz: 14d64a85490ab46665a4962e2d9caf09db246019
3
+ metadata.gz: 11c85caf6fbbe4ab13f977d7c6bf3ee623388727
4
+ data.tar.gz: 76fcd0babb26f578d61d1d522509e8a098ce0749
5
5
  SHA512:
6
- metadata.gz: 28fff7170437a3712f2c443cd91f555430a92945b1a60ac5ce8783b551c009f2f54dbe0cbbb04137653bb676719555fde5a2edb053b4b529786031e25492633a
7
- data.tar.gz: 25f67576819a2c8123944cc462ac583461f814e99525357d42eba34e594d540ac65f80df9e37c1a27c29a7bee295284456112e064a90dc53c007eae729218ac5
6
+ metadata.gz: 576c19c3da8c9aa4233c5a7b84863e3f9dd740976973812f38a4ef4f48206d11e039476412582949985064b5766a9e62640866bc02064a4f8b53f43ba567d816
7
+ data.tar.gz: efa2b81f6b3aacb894c67589bc105d27dab30bcfab2f3b896c3191fa72fd931e505382b96c2f740ce5ec620560979d1d0a62769aec382105331b834fbb8171a9
@@ -23,27 +23,12 @@ module Beecart
23
23
  # @!attribute [r] key
24
24
  # @return [String] カートを識別するためのランダムな文字列
25
25
 
26
- class ShoppingCart
27
-
28
- ShippingColumns = {
29
- shipping_address: [
30
- :name, :zip, :province, :city, :address1, :address2, :tel
31
- ],
32
- billing_address: [
33
- :name, :zip, :province, :city, :address1,:address2, :tel
34
- ],
35
- shipping_instruction: [
36
- :delivery_date, :delivery_time_slot, :special_note
37
- ],
38
- credit_card: [
39
- :number, :cvc, :exp_year, :exp_month, :name
40
- ]
41
- }
26
+ class Cart
42
27
 
43
28
  attr_reader :key
44
29
 
45
30
  def initialize(cart_id=nil)
46
- @redis = Redis.new(Beecart.redis_conf)
31
+ @redis = Redis.new(Beecart.config.redis)
47
32
  @key = cart_id.nil? ? SecureRandom.hex : cart_id
48
33
  @data = data
49
34
  end
@@ -60,6 +45,7 @@ module Beecart
60
45
  end
61
46
 
62
47
  # カート内のitemsを返却
48
+ #
63
49
  # @return [Hash]
64
50
  def items
65
51
  data[:items]
@@ -67,11 +53,21 @@ module Beecart
67
53
 
68
54
  # カート内の商品の合計金額を計算する
69
55
  #
70
- # @return [Integer]
71
- def total_cost
56
+ # @return [Integer] 税抜き合計金額
57
+ def total_price
72
58
  data[:items].inject(0) do |res, (key, item)|
73
59
  res += item[:price].to_i * item[:quantity].to_i
74
- res
60
+ res.to_i
61
+ end
62
+ end
63
+
64
+ # カート内の商品の税込み合計金額を計算する
65
+ #
66
+ # @return [Integer] 税込み合計金額
67
+ def total_price_with_tax
68
+ data[:items].inject(0) do |res, (key, item)|
69
+ res += item[:price].to_i * item[:quantity].to_i * Beecart.config.tax_rate
70
+ res.ceil.to_i
75
71
  end
76
72
  end
77
73
 
@@ -83,11 +79,11 @@ module Beecart
83
79
  def add_item(item_info={ quantity: 0})
84
80
 
85
81
  unless item_info.has_key?(:price)
86
- raise "Price needs to be passed when adding a item to cart."
82
+ raise Error,"Price needs to be passed when adding a item to cart."
87
83
  end
88
84
 
89
85
  unless item_info.has_key?(:quantity)
90
- raise "Quantity needs to be passed when adding a item to cart."
86
+ raise Error, "Quantity needs to be passed when adding a item to cart."
91
87
  end
92
88
 
93
89
  @data[:items][rand_key] = item_info
@@ -100,7 +96,7 @@ module Beecart
100
96
  # @param [String] key 追加するデータの識別子
101
97
  # @param [Hash] data 追加するデータ
102
98
  # @return [Boolean]
103
- def append_transaction_data(key, data)
99
+ def append_info(key, data)
104
100
  if send(key.to_s + '_validate', data)
105
101
  @data[key.to_sym] = data
106
102
  dump_data
@@ -109,7 +105,9 @@ module Beecart
109
105
  end
110
106
  end
111
107
 
112
- # 指定されたkeyにあるデータを削除する
108
+ # 指定されたkeyのアイテムを削除する
109
+ #
110
+ # @param [String] key 削除するアイテムのレコード
113
111
  def remove_item(key)
114
112
  @data[:items].delete(key.to_sym)
115
113
 
@@ -117,6 +115,7 @@ module Beecart
117
115
  end
118
116
 
119
117
  # 指定されたkeyにあるデータのquantityを変更する
118
+ #
120
119
  # @param [String] key
121
120
  # @param [Integer] quantity
122
121
  # @return [Boolean]
@@ -131,8 +130,36 @@ module Beecart
131
130
  @redis.del(@key)
132
131
  end
133
132
 
133
+ # 仮計上(与信)をとる
134
+ #
135
+ # @param [Hash] payment_info 支払情報
136
+ # @return [WebPay::Charge]
137
+ def authorize payment_info={}
138
+ gateway.authorize total_price_with_tax, payment_info
139
+ end
140
+
141
+ # 本計上を取る
142
+ #
143
+ # @param [Hash] payment_info 支払情報
144
+ # @return [WebPay::Charge]
145
+ def charge payment_info={}
146
+ gateway.charge total_price_with_tax, payment_info
147
+ end
148
+
134
149
  private
135
150
 
151
+ def gateway name=nil
152
+ klass = name.nil? ?
153
+ gateway_class_name(Beecart.config.default_gateway).constantize :
154
+ gateway_class_name(name).constantize
155
+
156
+ klass.new
157
+ end
158
+
159
+ def gateway_class_name name
160
+ ["Beecart::Gateway::",name.to_s.camelize,"Gateway"].join('')
161
+ end
162
+
136
163
  # Redis内から取ってきたデータをデシリアイズして返却。
137
164
  # またdataがnilの場合はdataのひな形を返却
138
165
  #
@@ -158,7 +185,7 @@ module Beecart
158
185
  def dump_data
159
186
  @data[:updated_at] = Time.now.to_s
160
187
  @redis.set(@key, @data.to_msgpack)
161
- @redis.expire(@key, Beecart.expire_time)
188
+ @redis.expire(@key, Beecart.config.expire_time)
162
189
  end
163
190
 
164
191
  # ランダムな文字列を生成
@@ -0,0 +1,58 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "logger"
4
+ require "singleton"
5
+
6
+ module Beecart
7
+ class Configuration
8
+ include Singleton
9
+
10
+ def self.default_logger
11
+ logger = Logger.new(STDOUT)
12
+ logger.progname = "beecart"
13
+ logger
14
+ end
15
+
16
+ @@defaults = {
17
+ logger: default_logger,
18
+ expire_time: 30 * 60,
19
+ redis: {
20
+ host: 'localhost',
21
+ port: 5555
22
+ },
23
+ tax_rate: 0.05,
24
+ default_gateway: :webpay
25
+ }
26
+
27
+ def self.defaults
28
+ @@defaults
29
+ end
30
+
31
+ def initialize
32
+ @@defaults.each_pair{|k,v| self.send("#{k}=",v)}
33
+ end
34
+
35
+ def redis=(options={})
36
+ if @redis && @redis.is_a?(Hash)
37
+ @redis = @redis.merge(options)
38
+ else
39
+ @redis = options
40
+ end
41
+ end
42
+
43
+ attr_reader :redis
44
+ attr_accessor :logger, :expire_time, :tax_rate, :default_gateway
45
+ end
46
+
47
+ def self.config
48
+ Configuration.instance
49
+ end
50
+
51
+ def self.configure
52
+ yield config
53
+ end
54
+
55
+ def self.logger
56
+ config.logger
57
+ end
58
+ end
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Beecart
4
+
5
+ # ApplicationControllerや任意のControllerの中でinclude
6
+ module CurrentCart
7
+
8
+ # セッションで保持しているキーをもとに自分が保持しているカートを返却
9
+ #
10
+ # @return [ShoppingCart]
11
+ def current_cart
12
+ if session[:cart_id]
13
+ cart = Beecart::Cart.new(session[:cart_id])
14
+ else
15
+ session[:cart_id]
16
+ cart = Beecart::Cart.new()
17
+ session[:cart_id] = cart.key
18
+ end
19
+
20
+ cart
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Beecart
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Beecart
4
+ module Gateway
5
+ class BaseGateway
6
+
7
+ def initialize *args; end
8
+
9
+ # クレジットカードで本決済をかける
10
+ #
11
+ # @param [Integer] price 決済手段にて支払う額
12
+ # @return [Boolean]
13
+ def charge price; end
14
+
15
+ # クレジットカードに対して与信をかける
16
+ #
17
+ # @param [Integer] price 与信をかける額
18
+ # @return [Boolean]
19
+ def authorize price; end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Beecart
4
+ module Gateway
5
+ class WebpayGateway < Beecart::Gateway::BaseGateway
6
+ # @param [Hash] payment_info 支払い方法に関してのハッシュ
7
+ def initialize *args
8
+ super
9
+
10
+ # raise Error, "You need to set Webpay API Key" if Webpay.api_key.nil?
11
+ end
12
+
13
+ def charge price, payment_info
14
+
15
+ charge_info = {
16
+ amount: price,
17
+ currency: "jpy",
18
+ capture: true
19
+ }.merge(payment_parser(payment_info))
20
+
21
+ Rails.logger.debug "charge_info => #{ charge_info }"
22
+
23
+ WebPay::Charge.create(charge_info)
24
+ end
25
+
26
+ def authorize price, payment_info
27
+ charge_info = {
28
+ amount: price,
29
+ currency: "jpy",
30
+ capture: false
31
+ }.merge(payment_parser(payment_info))
32
+
33
+ WebPay::Charge.create(charge_info)
34
+ end
35
+
36
+ private
37
+
38
+ def payment_parser info
39
+
40
+ Beecart.logger.debug "info => #{ info }"
41
+
42
+ if info.has_key? :customer
43
+ return {
44
+ customer: info[:customer]
45
+ }
46
+ else
47
+ return {
48
+ card: {
49
+ number: info[:card][:number],
50
+ exp_month: info[:card][:exp_month],
51
+ exp_year: info[:card][:exp_year],
52
+ cvc: info[:card][:cvc],
53
+ name: info[:card][:name],
54
+ }
55
+ }
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ module Beecart
2
+ module Validator
3
+ class Validator
4
+ end
5
+ end
6
+ end
7
+
@@ -1,3 +1,3 @@
1
1
  module Beecart
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/beecart.rb CHANGED
@@ -1,41 +1,12 @@
1
- require "beecart/engine"
2
- require "beecart/models/shopping_cart"
3
- require "beecart/controllers/core_ext"
1
+ require "redis"
2
+ require "msgpack"
4
3
 
5
- module Beecart
6
- @expire_time = 20 * 60
7
- @redis_conf = {
8
- host: 'localhost',
9
- port: 5555
10
- }
4
+ require "beecart/configuration"
5
+ require "beecart/current_cart"
6
+ require "beecart/cart"
7
+ require "beecart/error"
11
8
 
9
+ require "beecart/gateway/base_gateway"
10
+ require "beecart/gateway/webpay_gateway"
12
11
 
13
-
14
- class << self
15
- #
16
- # @!attribute [rw] expire_time
17
- # 何秒でカートが消滅するか
18
- # @!attribute [rw] redis_conf
19
- # 接続するRedisへの情報
20
- #
21
- def expire_time=(val)
22
- @expire_time = val
23
- end
24
-
25
- def expire_time
26
- @expire_time
27
- end
28
-
29
- def redis_conf=(options)
30
- @redis_conf = options.inject(@redis_conf) do |res, (key, value)|
31
- @redis_conf[key] = value
32
- res
33
- end
34
- end
35
-
36
- def redis_conf
37
- @redis_conf
38
- end
39
- end
40
-
41
- end
12
+ module Beecart; end
@@ -2,4 +2,6 @@ class ApplicationController < ActionController::Base
2
2
  # Prevent CSRF attacks by raising an exception.
3
3
  # For APIs, you may want to use :null_session instead.
4
4
  protect_from_forgery with: :exception
5
+
6
+ include Beecart::CurrentCart
5
7
  end
@@ -35,6 +35,32 @@ class CartsController < ApplicationController
35
35
  end
36
36
  end
37
37
 
38
+ def charge
39
+ current_cart.charge({
40
+ card: {
41
+ :number => 4012888888881881,
42
+ :exp_month => 11,
43
+ :exp_year => 2014,
44
+ :cvc => 123,
45
+ :name => "KEI KUBO"
46
+ }
47
+ })
48
+ end
49
+
50
+ def examine
51
+ current_cart.examine({
52
+ card: {
53
+ :number => 4012888888881881,
54
+ :exp_month => 11,
55
+ :exp_year => 2014,
56
+ :cvc => 123,
57
+ :name => "KEI KUBO"
58
+ }
59
+ })
60
+
61
+ render :text => 'OK'
62
+ end
63
+
38
64
  private
39
65
 
40
66
  def get_current_cart
@@ -21,8 +21,8 @@
21
21
  <dt>Cart Key:</dt>
22
22
  <dd><%= @current_cart.key %></dd>
23
23
 
24
- <dt>Total Cost:</dt>
25
- <dd><%= @current_cart.total_cost %></dd>
24
+ <dt>Total Price:</dt>
25
+ <dd><%= @current_cart.total_price %></dd>
26
26
 
27
27
  <dt>Items:</dt>
28
28
  <dd class="item_list">
@@ -49,6 +49,12 @@
49
49
 
50
50
  <hr />
51
51
 
52
+ <h2>Buy Cart</h2>
53
+
54
+ <%= form_tag checkout_cart_path, method: 'post' do %>
55
+ <%= button_tag "Checkout Cart", class: "button" %>
56
+ <% end %>
57
+
52
58
  <h2>Delete Cart</h2>
53
59
 
54
60
  <%= form_tag destroy_cart_path, method: 'delete' do %>
@@ -1,6 +1,13 @@
1
- Beecart.redis_conf = {
2
- host: 'localhost',
3
- port: 5555
4
- }
1
+ # -*- coding: utf-8 -*-
5
2
 
6
- Beecart.expire_time = 30
3
+ Beecart.configure do |config|
4
+ config.expire_time = 30
5
+ config.redis = {
6
+ host: 'localhost',
7
+ port: 5555
8
+ }
9
+
10
+ config.tax_rate = 0.05
11
+
12
+ config.default_gateway = :webpay
13
+ end
@@ -0,0 +1 @@
1
+ WebPay.api_key = "test_secret_2TLg1y5kU5dye9v2BO9pd8yy"
@@ -1,9 +1,10 @@
1
1
  Rails.application.routes.draw do
2
- mount Beecart::Engine => "/beecart"
3
-
4
2
  root :to => 'carts#index'
5
3
 
4
+ post '/cart/checkout', as: 'checkout_cart', to: 'carts#charge'
5
+ post '/cart/examine', as: 'examine_cart', to: 'carts#charge'
6
6
  post '/cart/add_item', as: 'add_item', to: 'carts#add_item'
7
7
  delete '/cart/remove_item', as: 'remove_item', to: 'carts#remove_item'
8
+
8
9
  delete '/cart', as: 'destroy_cart', to: 'carts#destroy'
9
10
  end
File without changes