allpay_invoice 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40f22a1430eb3bca05c873d72ed7c32778f4b06f
4
+ data.tar.gz: 51a41f6bb11aa18336cf4435b421adf6bd07ff20
5
+ SHA512:
6
+ metadata.gz: 57489c9d1b12a23833d80b73b885557c6a1143dbecf59d55da91e680120c3885f15345176c321347a516cd2c7870dd0df1b7b19728991180f582751465c455df
7
+ data.tar.gz: c886d5c34c23ed5e19282613ed218f18b668075832e871e4b254b58a7c8a3429081def43753c4d62027ef192996bf0d7cc90a7ec5564e0bcb074450754575a8a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in allpay_invoice.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # AllpayInvoice
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/allpay_invoice`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'allpay_invoice'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install allpay_invoice
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/allpay_invoice/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'allpay_invoice/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "allpay_invoice"
8
+ spec.version = AllpayInvoice::VERSION
9
+ spec.authors = ["Wei-Yi Chiu"]
10
+ spec.email = ["bird1204@gmail.com"]
11
+
12
+ spec.summary = %q{allpay_invoice API}
13
+ spec.description = %q{allpay_invoice API}
14
+ spec.homepage = "https://github.com/bird1204"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.9"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "allpay_invoice"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/allpay.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'allpay_invoice/invoice'
2
+ require "allpay_invoice/version"
3
+ module Allpay
4
+ end
@@ -0,0 +1 @@
1
+ require 'allpay'
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def stringify_keys
3
+ result = self.class.new
4
+ each_key do |key|
5
+ result[key.to_s] = self[key]
6
+ end
7
+ result
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module Allpay
2
+ # Generic Allpay exception class.
3
+ class AllpayError < StandardError; end
4
+ class MissingOption < AllpayError; end
5
+ class InvalidMode < AllpayError; end
6
+ end
@@ -0,0 +1,168 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+ require 'digest'
5
+ require 'allpay_invoice/errors'
6
+ require 'allpay_invoice/core_ext/hash'
7
+
8
+ module Allpay
9
+ class Invoice
10
+ PRE_ENCODE_COLUMN = [:CustomerName, :CustomerAddr , :CustomerEmail, :ItemName, :ItemWord, :InvoiceRemark, :InvCreateDate, :NotifyMail, :Reason, :IIS_Customer_Name, :IIS_Customer_Addr]
11
+ BLACK_LIST_COLUMN = [:ItemName, :ItemWord, :InvoiceRemark, :Reason]
12
+ PRODUCTION_API_HOST = 'https://einvoice.allpay.com.tw'.freeze
13
+ TEST_API_HOST = 'http://einvoice-stage.allpay.com.tw'.freeze
14
+ TEST_OPTIONS = {
15
+ merchant_id: '2000132',
16
+ hash_key: 'ejCk326UnaZWKisg',
17
+ hash_iv: 'q9jcZX8Ib9LM8wYk'
18
+ }.freeze
19
+
20
+ attr_reader :options
21
+
22
+ def initialize(options = {})
23
+ @options = { mode: :production }.merge!(options)
24
+ case @options[:mode]
25
+ when :production
26
+ option_required! :merchant_id, :hash_key, :hash_iv
27
+ when :test
28
+ @options = TEST_OPTIONS.merge(options)
29
+ else
30
+ fail InvalidMode, 'option :mode is either :test or :production'
31
+ end
32
+ @options.freeze
33
+ end
34
+
35
+ def api_host
36
+ case @options[:mode]
37
+ when :production then PRODUCTION_API_HOST
38
+ when :test then TEST_API_HOST
39
+ end
40
+ end
41
+
42
+ def make_mac(params = {})
43
+ sort_hash = pre_encode(params.clone).sort_by { |x| x.to_s.downcase }
44
+ raw = sort_hash.map! { |k, v| "#{k}=#{v}" }.join('&')
45
+ str = "HashKey=#{@options[:hash_key]}&#{raw}&HashIV=#{@options[:hash_iv]}"
46
+ url_encoded = CGI.escape(str).downcase!
47
+ Digest::MD5.hexdigest(url_encoded).upcase!
48
+ end
49
+
50
+ def verify_mac(params = {})
51
+ stringified_keys = params.stringify_keys
52
+ check_mac_value = stringified_keys.delete('CheckMacValue')
53
+ make_mac(stringified_keys) == check_mac_value
54
+ end
55
+
56
+ def generate_params(overwrite_params = {})
57
+ result = overwrite_params
58
+ result[:TimeStamp] = Time.now.to_i
59
+ result[:MerchantID] = @options[:merchant_id]
60
+ result[:CheckMacValue] = make_mac(result)
61
+ result
62
+ end
63
+
64
+ def request(path, params = {})
65
+ api_url = URI.join(api_host + path)
66
+ Net::HTTP.post_form api_url, params
67
+ end
68
+
69
+ # 一般開立發票API
70
+ # url_encode => CustomerAddr / CustomerName / CustomerEmail / ItemName / ItemWord / InvCreateDate / InvoiceRemark
71
+ # 在產生 CheckMacValue 時,須將 ItemName、ItemWord 及 InvoiceRemark 等欄位排除
72
+ def issue(overwrite_params = {})
73
+ res = request '/Invoice/Issue', generate_params(overwrite_params)
74
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
75
+ end
76
+
77
+ # 延遲或觸發開立發票API
78
+ # url_encode => CustomerEmail / CustomerName / CustomerAddr / ItemName / ItemWord / InvoiceRemark
79
+ # 在產生 CheckMacValue 時, 須將 ItemName、ItemWord 及 InvoiceRemark 等欄位排除
80
+ def delay_issue(overwrite_params = {})
81
+ res = request '/Invoice/DelayIssue', overwrite_params
82
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
83
+ end
84
+
85
+ # 開立折讓API
86
+ # url_encode => NotifyMail / ItemName / ItemWord
87
+ # 在產生 CheckMacValue 時,須將 ItemName 及 ItemWord 等欄位排除
88
+ def allowance(overwrite_params = {})
89
+ res = request '/Invoice/Allowance', overwrite_params
90
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
91
+ end
92
+
93
+ # 發票作廢API
94
+ # url_encode => Reason
95
+ # 在產生 CheckMacValue 時,須將 Reason 欄位排除
96
+ def issue_invalid(overwrite_params = {})
97
+ res = request '/Invoice/IssueInvalid', overwrite_params
98
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
99
+ end
100
+
101
+ # 折讓作廢API
102
+ # url_encode => Reason
103
+ # 在產生 CheckMacValue 時,須將 ItemName 及 ItemWord 等欄位排除
104
+ def allowance_invalid(overwrite_params = {})
105
+ res = request '/AllowanceInvalid', overwrite_params
106
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
107
+ end
108
+
109
+ # 查詢發票API
110
+ # url_encode => IIS_Customer_Name / IIS_Customer_Addr / ItemName / ItemWord / InvoiceRemark
111
+ # 在產生 CheckMacValue 時,須將 ItemName、ItemWord 及 InvoiceRemark 等欄位排除
112
+ def query_issue(overwrite_params = {})
113
+ res = request '/Query/Issue', overwrite_params
114
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
115
+ end
116
+
117
+ # 查詢作廢發票API
118
+ # url_encode => Reason
119
+ # 在產生 CheckMacValue 時,須將 Reason 等欄位排除
120
+ def query_issue_invalid(overwrite_params = {})
121
+ res = request '/Query/IssueInvalid', overwrite_params
122
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
123
+ end
124
+
125
+ # 查詢折讓明細API
126
+ # url_encode => ItemName / ItemWord / IIS_Customer_Name
127
+ # 在產生 CheckMacValue 時,須將 ItemName、ItemWord 等欄位排除
128
+ def query_allowance(overwrite_params = {})
129
+ res = request '/Query/Allowance', overwrite_params
130
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
131
+ end
132
+
133
+ # 查詢折讓作廢明細API
134
+ # url_encode => Reason
135
+ # 在產生 CheckMacValue 時,須將 Reason 等欄位排除
136
+ def query_allowance_invalid(overwrite_params = {})
137
+ res = request '/Query/AllowanceInvalid', overwrite_params
138
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
139
+ end
140
+
141
+ # 發送通知API
142
+ def invoice_notify(overwrite_params = {})
143
+ res = request '/Notify/InvoiceNotify', overwrite_params
144
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
145
+ end
146
+
147
+ # 付款完成觸發或延遲開立發票API
148
+ def trigger_issue(overwrite_params = {})
149
+ res = request '/Invoice/TriggerIssue', overwrite_params
150
+ Hash[res.body.split('&').map! { |i| i.split('=') }]
151
+ end
152
+
153
+ private
154
+
155
+ def pre_encode(params)
156
+ PRE_ENCODE_COLUMN.each do |key|
157
+ params[key] = CGI.escape(params[key]) if params.key?(key)
158
+ end
159
+ params.delete_if { |key| BLACK_LIST_COLUMN.find_index(key) }
160
+ end
161
+
162
+ def option_required!(*option_names)
163
+ option_names.each do |option_name|
164
+ raise MissingOption, %Q{option "#{option_name}" is required.} if @options[option_name].nil?
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,3 @@
1
+ module AllpayInvoice
2
+ VERSION = "0.0.3"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: allpay_invoice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Wei-Yi Chiu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: allpay_invoice API
42
+ email:
43
+ - bird1204@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - .travis.yml
51
+ - Gemfile
52
+ - README.md
53
+ - Rakefile
54
+ - allpay_invoice.gemspec
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/allpay.rb
58
+ - lib/allpay_invoice.rb
59
+ - lib/allpay_invoice/core_ext/hash.rb
60
+ - lib/allpay_invoice/errors.rb
61
+ - lib/allpay_invoice/invoice.rb
62
+ - lib/allpay_invoice/version.rb
63
+ homepage: https://github.com/bird1204
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.6
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: allpay_invoice API
86
+ test_files: []
87
+ has_rdoc: