esun 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1d7d912521e19bab033cb7d5d640ed0ae89d008
4
+ data.tar.gz: 5347eb745032281e3d7a473fd6961c65d942544d
5
+ SHA512:
6
+ metadata.gz: e5d259e7a83bef81f1aa756c461f26ab4465c90d4543402c4eee5e348fd05f061d6e6adcb5a1ece038c361d3e2e298585624affc1382fa7a09304e2f70486505
7
+ data.tar.gz: 8d92656ac6f7652112ac97d62a925302417be19382c917cc5caf1196f492219d17ef0ef7c7795ef67e0b0a4b36ff93a5f502286bd25de136807ca88566a9637c
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/esun.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/esun/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "esun"
6
+ s.version = Esun::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "taiwan esun bank vitural account service."
9
+ s.description = "virtual account generator, and payment callback parser"
10
+
11
+ s.required_ruby_version = ">= 1.8.7"
12
+ s.required_rubygems_version = ">= 1.3.6"
13
+
14
+ s.authors = ["Eddie"]
15
+ s.email = ["eddie@visionbundles.com"]
16
+ s.homepage = "https://github.com/afunction/esun"
17
+
18
+ s.files = %w{Gemfile esun.gemspec} + Dir['**/*.{rb,yml}']
19
+ s.test_files = s.files.grep(%r{^(test|spec|locales)/})
20
+ s.require_paths = %w{lib}
21
+
22
+ s.license = 'MIT'
23
+
24
+ s.add_dependency "rails"
25
+ s.add_development_dependency "rspec"
26
+ end
data/lib/esun.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/enumerable'
3
+ require 'action_controller'
4
+
5
+ require 'esun/atm'
6
+ require 'esun/callback_helper'
7
+ require 'esun/result'
data/lib/esun/atm.rb ADDED
@@ -0,0 +1,61 @@
1
+ module Esun
2
+ class ATM
3
+ # Setup the compnay_code when init your rails app
4
+ # config/initializers/esun.rb
5
+ # Esun::ATM.company_code = Settings.esun.company_code
6
+ cattr_accessor :company_code
7
+ @@company_code = ''
8
+
9
+ WEIGHTING_TABLE = '654321987654321'
10
+
11
+ class << self
12
+ # 局號
13
+ def bank_code
14
+ '808'
15
+ end
16
+
17
+ # 取得虛擬帳號
18
+ def build_vaccount(oid, amount, expire_date)
19
+ unless expire_date.is_a?(Date)
20
+ raise "expire_date must be is Date"
21
+ end
22
+ # 訂單編號 000001
23
+ oid = get_oid_format(oid)
24
+ # 限制日期 10/10
25
+ expire_date = expire_date.strftime('%m%d')
26
+ # 虛擬帳號
27
+ vaccount = "#{company_code}#{expire_date}#{oid}"
28
+ # 檢查碼
29
+ check_numbers = check_numbers_generator(amount, vaccount)
30
+
31
+ vaccount = "#{vaccount}#{check_numbers}"
32
+ unless vaccount.length == 16
33
+ raise "vaccount generate error (not = 16 length) output: '#{vaccount}'"
34
+ end
35
+ return vaccount
36
+ end
37
+
38
+ private
39
+ # OID 6 碼 (000001)
40
+ def get_oid_format(oid)
41
+ oid = oid.to_i.to_s.rjust(6, '0')
42
+ unless oid.length == 6
43
+ raise "OID over 6 chars output: #{oid.to_s}"
44
+ end
45
+ return oid
46
+ end
47
+
48
+ # 檢查碼檢查
49
+ def check_numbers_generator(amount, vaccount)
50
+ return ((get_weight_code(amount) + get_weight_code(vaccount)) % 10).to_s
51
+ end
52
+
53
+ # 取得 numbers weight
54
+ def get_weight_code(numbers)
55
+ numbers = numbers.to_s
56
+ weight = WEIGHTING_TABLE.reverse[0, numbers.length].reverse
57
+ return numbers.split('').map.with_index { |number, index| (number.to_i * weight[index].to_i) }.sum.to_i
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,65 @@
1
+ module Esun
2
+ module CallbackHelper
3
+ extend ActiveSupport::Concern
4
+ # 玉山銀行 Callback IP
5
+ WHIT_LIST_IPS = %w(203.67.45.142 61.220.45.142 127.0.0.1 192.241.217.93)
6
+
7
+ module ClassMethods
8
+ protected
9
+ # 增加 white list IP
10
+ # Example:
11
+ # add_allow_ip '127.0.0.1'
12
+ # add_allow_ip '127.0.0.1', '123.212.346.45'
13
+ def add_allow_ip(ips)
14
+ if ips.is_a? Array
15
+ WHIT_LIST_IPS.contcat(ips)
16
+ else
17
+ WHIT_LIST_IPS.push(ips.to_s)
18
+ end
19
+ end
20
+
21
+ # 1. 跳過 Rails 4 預設 authenticity_token 驗證
22
+ # 2. 設定 IP Whit List before_action
23
+ def set_esun_callback_action(action)
24
+ action = action.to_sym
25
+ skip_before_filter :verify_authenticity_token, only: action
26
+ before_action :esun_callback_ip_check, only: action
27
+ end
28
+ end
29
+
30
+ protected
31
+ # 檢查 IP Before Action
32
+ def esun_callback_ip_check
33
+ unless WHIT_LIST_IPS.include?(remote_ip)
34
+ raise ActionController::RoutingError.new('IP not allow')
35
+ end
36
+ end
37
+
38
+ def render_esun_ok
39
+ render text: 'OK'
40
+ end
41
+
42
+ def remote_ip
43
+ (request.headers['HTTP_X_REAL_IP'] || request.remote_ip) if request.present?
44
+ end
45
+
46
+ # Parse 玉山 Callback
47
+ def payment_params
48
+ @payment_params ||= parse_payment_params(params[:Data])
49
+ end
50
+
51
+ def parse_payment_params(data)
52
+ payment_parameters = data.gsub('0D 0A', '').split(',')
53
+ oid = payment_parameters[3][9, 6]
54
+ return ::Esun::Result.new(
55
+ handle_date: Date.parse(payment_parameters[0]),
56
+ channel: payment_parameters[1],
57
+ serial_number: payment_parameters[2],
58
+ vaccount: payment_parameters[3],
59
+ oid: oid.to_i,
60
+ amount: payment_parameters[4].to_i,
61
+ pay_time: Time.parse(payment_parameters[5]),
62
+ data: data)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ module Esun
2
+ class Result
3
+ attr_accessor :handle_date, :channel,
4
+ :serial_number, :vaccount,
5
+ :oid, :amount,
6
+ :pay_time, :data
7
+
8
+ def initialize(attrs)
9
+ attrs.each do |column, value|
10
+ self.send("#{column}=", value)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Esun
2
+ VERSION = "0.0.1"
3
+ end
data/spec/atm_spec.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ Esun::ATM.company_code = '92424'
4
+
5
+ describe Esun::ATM do
6
+ subject { ::Esun::ATM }
7
+
8
+ it "company code should exists" do
9
+ subject.company_code.should == '92424'
10
+ end
11
+
12
+ its(:bank_code) { should == '808' }
13
+
14
+ context "when create a virtual account" do
15
+ subject { ::Esun::ATM.build_vaccount(1, 1000, Date.parse('2014/03/18') ) }
16
+
17
+ its(:length) { should == 16}
18
+
19
+ it "should match compnay_code == 92424" do
20
+ subject[0..4].should == '92424'
21
+ end
22
+
23
+ it "order_id should auto fill 0 => 000001" do
24
+ subject[9..14].should == '000001'
25
+ subject[9..14].to_i.should == 1
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ class PaymentController < ActionController::Base
4
+ include ::Esun::CallbackHelper
5
+ add_allow_ip '192.168.3.10'
6
+ set_esun_callback_action :esun
7
+
8
+ def esun; end
9
+ end
10
+
11
+
12
+ describe PaymentController do
13
+ it "192.168.3.10 should in white list CONST" do
14
+ ::Esun::CallbackHelper::WHIT_LIST_IPS.should include('192.168.3.10')
15
+ end
16
+
17
+ it "#payment_params" do
18
+ payment_params = subject.send(:parse_payment_params, "20140311,銀行,1,965730312000988,10000,20140311133132")
19
+ payment_params.should be_a(::Esun::Result)
20
+ payment_params.oid.should == 988
21
+ payment_params.vaccount.length.should == 15
22
+ payment_params.amount.should == 10000
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'esun' # and any other gems you need
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eddie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-13 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: virtual account generator, and payment callback parser
42
+ email:
43
+ - eddie@visionbundles.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - esun.gemspec
50
+ - lib/esun.rb
51
+ - lib/esun/atm.rb
52
+ - lib/esun/callback_helper.rb
53
+ - lib/esun/result.rb
54
+ - lib/esun/version.rb
55
+ - spec/atm_spec.rb
56
+ - spec/callback_helper_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/afunction/esun
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.8.7
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.6
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: taiwan esun bank vitural account service.
82
+ test_files:
83
+ - spec/atm_spec.rb
84
+ - spec/callback_helper_spec.rb
85
+ - spec/spec_helper.rb