allpay_client 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: 9e71e6f61f2ea5a62e48c694640f883125966ff1
4
+ data.tar.gz: 5851603bedddfec6ea4f39643095d046e9b8b9f6
5
+ SHA512:
6
+ metadata.gz: e67fb023a81e3de2b388236f573635b865fe8c5e9d94fe02449a3d2a2c9b6cf0fb293a02c48aeedc459c38dfcc1d0ab911332da1881be2878ef20673752bcdca
7
+ data.tar.gz: 6b7476079c50b4154c74a5b89bf317ef78d4d8352a1842b3722beadd3b3be804ad041310353386364a3428b2575354d227674802489eb0f0f6e98183acd1ab65
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ - 2.1.5
5
+ - 2.0.0
6
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jian Weihang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Allpay 歐付寶
2
+
3
+ 這是歐付寶 API 的 Ruby 包裝,更多資訊參考他們的[官方文件](https://www.allpay.com.tw/Content/files/%E5%85%A8%E6%96%B9%E4%BD%8D%E9%87%91%E6%B5%81%E4%BB%8B%E6%8E%A5%E6%8A%80%E8%A1%93%E6%96%87%E4%BB%B6.pdf)。
4
+
5
+ ## 安裝
6
+
7
+ ```bash
8
+ gem install allpay_client
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ```ruby
14
+ client = Allpay::Client.new({
15
+ merchant_id: '2000132',
16
+ hash_key: '5294y06JbISpM5x9',
17
+ hash_iv: 'v77hoKGq4kWxNNIS',
18
+ mode: :test
19
+ })
20
+ client.request '/Cashier/QueryTradeInfo',
21
+ MerchantTradeNo: '0457ce27',
22
+ TimeStamp: Time.now.to_i
23
+ ```
24
+
25
+ 歐付寶共有 5 個 API:
26
+
27
+ - /Cashier/AioCheckOut
28
+ - /Cashier/QueryTradeInfo
29
+ - /Cashier/QueryPeriodCreditCardTradeInfo
30
+ - /CreditDetail/DoAction
31
+ - /Cashier/AioChargeback
32
+
33
+ 每個 API 有哪些參數建議直接參考歐付寶文件,不過要注意幾點:
34
+
35
+ - 原本 API 都需要 `MerchantID` 與 `CheckMacValue`,不過 `Client#request` 已經都處理好了,使用時可忽略這兩個參數,正如上述範例一樣。
36
+ - `/Cashier/AioCheckOut` 回傳的內容是 HTML,這個請求應該是交給瀏覽器發送的,所以不應該寫出 `client.request '/Cashier/AioCheckOut'`這樣的內容。
37
+
38
+ ## Allpay::Client
39
+
40
+ 實體方法 | 回傳 | 說明
41
+ --- | --- | ---
42
+ `request(path, **params)` | `Net::HTTPResponse` | 發送 API 請求
43
+ `make_mac(**params)` | `String` | 用於產生 `CheckMacValue`,單純做加密,`params` 需要完整包含到 `MerchantID`
44
+ `query_trade_info(merchant_trade_number, platform = nil)` | `Hash` | `/Cashier/QueryTradeInfo` 的捷徑方法,將 `TimeStamp` 設定為當前時間
45
+ `query_period_credit_card_trade_info(merchant_trade_number)` | `Hash` | `/Cashier/QueryPeriodCreditCardTradeInfo` 的捷徑方法,將 `TimeStamp` 設定為當前時間
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/allpay.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'allpay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "allpay_client"
8
+ spec.version = Allpay::VERSION
9
+ spec.authors = ["Jian Weihang"]
10
+ spec.email = ["tonytonyjan@gmail.com"]
11
+ spec.summary = %q{API client for allpay}
12
+ spec.description = %q{API client for allpay}
13
+ # spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,21 @@
1
+ $: << File.expand_path('../../lib', __FILE__)
2
+ require 'sinatra'
3
+ require 'allpay'
4
+
5
+ get '/' do
6
+ client = Allpay::Client.new(merchant_id: '2000132', hash_key: '5294y06JbISpM5x9', hash_iv: 'v77hoKGq4kWxNNIS', mode: :test)
7
+ @params = {
8
+ MerchantID: client.merchant_id,
9
+ MerchantTradeNo: SecureRandom.hex(4),
10
+ MerchantTradeDate: Time.now.strftime('%Y/%m/%d %H:%M:%S'),
11
+ PaymentType: 'aio',
12
+ TotalAmount: 100,
13
+ TradeDesc: '腦袋有動工作室',
14
+ ItemName: '物品一#物品二',
15
+ ReturnURL: 'http://requestb.in/11zuej31',
16
+ ClientBackURL: 'http://requestb.in/11zuej31?inspect',
17
+ ChoosePayment: 'Credit'
18
+ }
19
+ @mac = client.make_mac(@params)
20
+ erb :index
21
+ end
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Document</title>
6
+ </head>
7
+ <body>
8
+ <form action="http://payment-stage.allpay.com.tw/Cashier/AioCheckOut" method="post">
9
+ <input type="text" name="MerchantID" value="<%= @params[:MerchantID] %>">
10
+ <input type="text" name="MerchantTradeNo" value="<%= @params[:MerchantTradeNo] %>">
11
+ <input type="text" name="MerchantTradeDate" value="<%= @params[:MerchantTradeDate] %>">
12
+ <input type="text" name="PaymentType" value="<%= @params[:PaymentType] %>">
13
+ <input type="text" name="TotalAmount" value="<%= @params[:TotalAmount] %>">
14
+ <input type="text" name="TradeDesc" value="<%= @params[:TradeDesc] %>">
15
+ <input type="text" name="ItemName" value="<%= @params[:ItemName] %>">
16
+ <input type="text" name="ReturnURL" value="<%= @params[:ReturnURL] %>">
17
+ <input type="text" name="ClientBackURL" value="<%= @params[:ClientBackURL] %>">
18
+ <input type="text" name="ChoosePayment" value="<%= @params[:ChoosePayment] %>">
19
+ <input type="text" name="CheckMacValue" value="<%= @mac %>">
20
+ <input type="submit">
21
+ </form>
22
+ </body>
23
+ </html>
@@ -0,0 +1,52 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Allpay
5
+ class Client
6
+ PRODUCTION_API_HOST = 'https://payment.allpay.com.tw'.freeze
7
+ TEST_API_HOST = 'http://payment-stage.allpay.com.tw'.freeze
8
+
9
+ attr_accessor :merchant_id, :hash_key, :hash_iv, :mode
10
+
11
+ def initialize(merchant_id:, hash_key:, hash_iv:, mode: :production)
12
+ @merchant_id, @hash_key, @hash_iv, @mode = merchant_id, hash_key, hash_iv, mode
13
+ end
14
+
15
+ def api_host
16
+ case mode
17
+ when :production then PRODUCTION_API_HOST
18
+ when :test then TEST_API_HOST
19
+ else raise '`mode` is either :test or :production (default)'
20
+ end
21
+ end
22
+
23
+ def make_mac **params
24
+ raw = params.sort.map!{|k,v| "#{k}=#{v}"}.join('&')
25
+ padded = "HashKey=#{@hash_key}&#{raw}&HashIV=#{@hash_iv}"
26
+ url_encoded = CGI.escape(padded).downcase!
27
+ Digest::MD5.hexdigest(url_encoded).upcase!
28
+ end
29
+
30
+ def request path, **params
31
+ params[:MerchantID] = @merchant_id
32
+ params[:CheckMacValue] = make_mac(params)
33
+ api_url = URI.join(api_host, path)
34
+ Net::HTTP.post_form api_url, params
35
+ end
36
+
37
+ def query_trade_info merchant_trade_number, platform = nil
38
+ res = request '/Cashier/QueryTradeInfo',
39
+ MerchantTradeNo: merchant_trade_number,
40
+ TimeStamp: Time.now.to_i,
41
+ PlatformID: platform
42
+ Hash[res.body.split('&').map!{|i| i.split('=')}]
43
+ end
44
+
45
+ def query_period_credit_card_trade_info merchant_trade_number
46
+ res = request '/Cashier/QueryPeriodCreditCardTradeInfo',
47
+ MerchantTradeNo: merchant_trade_number,
48
+ TimeStamp: Time.now.to_i
49
+ JSON.parse(res.body)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module Allpay
2
+ VERSION = "0.0.1"
3
+ end
data/lib/allpay.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "allpay/version"
2
+ require "allpay/client"
3
+
4
+ module Allpay
5
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'securerandom'
3
+ describe Allpay::Client do
4
+ before :all do
5
+ @client = Allpay::Client.new(merchant_id: '2000132', hash_key: '5294y06JbISpM5x9', hash_iv: 'v77hoKGq4kWxNNIS', mode: :test)
6
+ end
7
+
8
+ it '#api /Cashier/AioCheckOut' do
9
+ res = @client.request '/Cashier/AioCheckOut',
10
+ MerchantTradeNo: SecureRandom.hex(4),
11
+ MerchantTradeDate: Time.now.strftime('%Y/%m/%d %H:%M:%S'),
12
+ PaymentType: 'aio',
13
+ TotalAmount: 100,
14
+ TradeDesc: '腦袋有動工作室',
15
+ ItemName: '物品一#物品二',
16
+ ReturnURL: 'http://requestb.in/11zuej31',
17
+ ClientBackURL: 'http://requestb.in/11zuej31?inspect',
18
+ ChoosePayment: 'Credit'
19
+ expect(res.code).to eq '302'
20
+ end
21
+
22
+ it '#api /Cashier/QueryTradeInfo' do
23
+ res = @client.request '/Cashier/QueryTradeInfo',
24
+ MerchantTradeNo: '0457ce27',
25
+ TimeStamp: Time.now.to_i
26
+ expect(res.code).to eq '200'
27
+ end
28
+
29
+ it '#query_trade_info' do
30
+ result_hash = @client.query_trade_info '0457ce27'
31
+ expect(result_hash.keys).to match_array %w[HandlingCharge ItemName MerchantID MerchantTradeNo
32
+ PaymentDate PaymentType PaymentTypeChargeFee TradeAmt TradeDate TradeNo TradeStatus CheckMacValue]
33
+ end
34
+
35
+ it '#query_period_credit_card_trade_info' do
36
+ result_hash = @client.query_period_credit_card_trade_info '0457ce27'
37
+ expect(result_hash.keys).to match_array %w[MerchantID MerchantTradeNo TradeNo RtnCode PeriodType
38
+ Frequency ExecTimes PeriodAmount amount gwsr process_date auth_code card4no
39
+ card6no TotalSuccessTimes TotalSuccessAmount ExecLog]
40
+ end
41
+
42
+ it '#make_mac' do
43
+ client = Allpay::Client.new(merchant_id: '12345678', hash_key: 'xdfaefasdfasdfa32d', hash_iv: 'sdfxfafaeafwexfe')
44
+ mac = client.make_mac({
45
+ ItemName: 'sdfasdfa',
46
+ MerchantID: '12345678',
47
+ MerchantTradeDate: '2013/03/12 15:30:23',
48
+ MerchantTradeNo: 'allpay_1234',
49
+ PaymentType: 'allpay',
50
+ ReturnURL: 'http:sdfasdfa',
51
+ TotalAmount: '500',
52
+ TradeDesc: 'dafsdfaff'
53
+ })
54
+ expect(mac).to eq '40D9A6C00A4A78A300ED458237071BDA'
55
+ end
56
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'allpay'
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: allpay_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jian Weihang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-03 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: API client for allpay
56
+ email:
57
+ - tonytonyjan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - allpay.gemspec
70
+ - examples/server.rb
71
+ - examples/views/index.erb
72
+ - lib/allpay.rb
73
+ - lib/allpay/client.rb
74
+ - lib/allpay/version.rb
75
+ - spec/allpay_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage:
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: API client for allpay
101
+ test_files:
102
+ - spec/allpay_spec.rb
103
+ - spec/spec_helper.rb
104
+ has_rdoc: