unionpay 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: 991f601026a08c6b26af993b3191b116fe47fcba
4
+ data.tar.gz: a2ea091456f7336ca08f13d62ff721d777ac0f30
5
+ SHA512:
6
+ metadata.gz: 544de5cd1070c90f11f32a32e8350fa771d3f94922d75f85b7fe393cc517c20bf0d14db220751521bf32f8d222ee39ec2d64aed51a000c94946d716ecc235ecf
7
+ data.tar.gz: a7f66c2242ce38b48f295d602958ccdb0fcbf956e415f3e8dd99f01c8373c9b47f42508cca5acb65ae10472a096e8bf45bbd52e79b323be712c7f9fc4aa70d86
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ unionpay
2
+ ========
3
+
4
+ 银联支付api
5
+
6
+ ## Usage
7
+
8
+ ### Config
9
+
10
+ ```ruby
11
+ UnionPay.mer_id = '105550149170027'
12
+ UnionPay.mer_abbr = '商户名称'
13
+ UnionPay.security_key = '88888888'
14
+ ```
15
+
16
+ ### Generate payment form
17
+ ```ruby
18
+ param = {}
19
+ param['transType'] = UnionPay::Conf::CONSUME #交易类型,CONSUME or PRE_AUTH
20
+ param['orderAmount'] = 11000 #交易金额
21
+ param['orderNumber'] = Time.now.strftime('%Y%m%d%H%M%S') #订单号,必须唯一
22
+ param['customerIp'] = '127.0.0.1'
23
+ param['frontEndUrl'] = "http://www.example.com/sdk/utf8/front_notify.php" #前台回调URL
24
+ param['backEndUrl'] = "http://www.example.com/sdk/utf8/back_notify.php" #后台回调URL
25
+
26
+ # 可填空字段
27
+ # param['commodityUrl'] = "http://www.example.com/product?name=商品" #商品URL
28
+ # param['commodityName'] = '商品名称' # 商品名称
29
+ # param['commodityUnitPrice'] = 11000 #商品单价
30
+ # param['commodityQuantity'] = 1 #商品数量
31
+ #
32
+
33
+ # 其余可填空的参数可以不填写
34
+
35
+ puts UnionPay::Service.front_pay(param).form{"<input type='submit' />"}
36
+ ```
@@ -0,0 +1,159 @@
1
+ #encoding:utf-8
2
+ module UnionPay
3
+ module Conf
4
+ VERIFY_HTTPS_CERT = false
5
+
6
+ Timezone = "Asia/Shanghai" #时区
7
+ Sign_method = "md5" #摘要算法,目前仅支持md5 (2011-08-22)
8
+
9
+ #Security_key = UnionPay.security_key #商户密钥
10
+
11
+ # 支付请求预定义字段
12
+ Pay_params = {
13
+ 'version' => '1.0.0',
14
+ 'charset' => 'UTF-8', #UTF-8, GBK等
15
+ 'merId' => '88888888', #商户填写
16
+ 'acqCode' => '', #收单机构填写
17
+ 'merCode' => '', #收单机构填写
18
+ 'merAbbr' => '商户名称'
19
+ }
20
+
21
+ # 测试环境
22
+ Front_pay_url = "http://58.246.226.99/UpopWeb/api/Pay.action"
23
+ Back_pay_url = "http://58.246.226.99/UpopWeb/api/BSPay.action"
24
+ Query_url = "http://58.246.226.99/UpopWeb/api/Query.action"
25
+
26
+ ## 预上线环境
27
+ #$front_pay_url = "https://www.epay.lxdns.com/UpopWeb/api/Pay.action"
28
+ #$back_pay_url = "https://www.epay.lxdns.com/UpopWeb/api/BSPay.action"
29
+ #$query_url = "https://www.epay.lxdns.com/UpopWeb/api/Query.action"
30
+ #
31
+ ## 线上环境
32
+ #$front_pay_url = "https://unionpaysecure.com/api/Pay.action"
33
+ #$back_pay_url = "https://besvr.unionpaysecure.com/api/BSPay.action"
34
+ #$query_url = "https://query.unionpaysecure.com/api/Query.action"
35
+
36
+ FRONT_PAY = 1
37
+ BACK_PAY = 2
38
+ RESPONSE = 3
39
+ QUERY = 4
40
+
41
+ CONSUME = "01"
42
+ CONSUME_VOID = "31"
43
+ PRE_AUTH = "02"
44
+ PRE_AUTH_VOID = "32"
45
+ PRE_AUTH_COMPLETE = "03"
46
+ PRE_AUTH_VOID_COMPLETE = "33"
47
+ REFUND = "04"
48
+ REGISTRATION = "71"
49
+
50
+ CURRENCY_CNY = "156"
51
+
52
+ # 支付请求可为空字段(但必须填写)
53
+ Pay_params_empty = {
54
+ "origQid" => "",
55
+ "acqCode" => "",
56
+ "merCode" => "",
57
+ "commodityUrl" => "",
58
+ "commodityName" => "",
59
+ "commodityUnitPrice" => "",
60
+ "commodityQuantity" => "",
61
+ "commodityDiscount" => "",
62
+ "transferFee" => "",
63
+ "customerName" => "",
64
+ "defaultPayType" => "",
65
+ "defaultBankNumber" => "",
66
+ "transTimeout" => "",
67
+ "merReserved" => ""
68
+ }
69
+
70
+ # 支付请求必填字段检查
71
+ Pay_params_check = [
72
+ "version",
73
+ "charset",
74
+ "transType",
75
+ "origQid",
76
+ "merId",
77
+ "merAbbr",
78
+ "acqCode",
79
+ "merCode",
80
+ "commodityUrl",
81
+ "commodityName",
82
+ "commodityUnitPrice",
83
+ "commodityQuantity",
84
+ "commodityDiscount",
85
+ "transferFee",
86
+ "orderNumber",
87
+ "orderAmount",
88
+ "orderCurrency",
89
+ "orderTime",
90
+ "customerIp",
91
+ "customerName",
92
+ "defaultPayType",
93
+ "defaultBankNumber",
94
+ "transTimeout",
95
+ "frontEndUrl",
96
+ "backEndUrl",
97
+ "merReserved"
98
+ ]
99
+
100
+ # 查询请求必填字段检查
101
+ $query_params_check = [
102
+ "version",
103
+ "charset",
104
+ "transType",
105
+ "merId",
106
+ "orderNumber",
107
+ "orderTime",
108
+ "merReserved",
109
+ ]
110
+
111
+ # 商户保留域可能包含的字段
112
+ Mer_params_reserved = [
113
+ # NEW NAME OLD NAME
114
+ "cardNumber", "pan",
115
+ "cardPasswd", "password",
116
+ "credentialType", "idType",
117
+ "cardCvn2", "cvn",
118
+ "cardExpire", "expire",
119
+ "credentialNumber", "idNo",
120
+ "credentialName", "name",
121
+ "phoneNumber", "mobile",
122
+ "merAbstract",
123
+
124
+ # tdb only
125
+ "orderTimeoutDate",
126
+ "origOrderNumber",
127
+ "origOrderTime",
128
+ ]
129
+
130
+ Notify_param_check = [
131
+ "version",
132
+ "charset",
133
+ "transType",
134
+ "respCode",
135
+ "respMsg",
136
+ "respTime",
137
+ "merId",
138
+ "merAbbr",
139
+ "orderNumber",
140
+ "traceNumber",
141
+ "traceTime",
142
+ "qid",
143
+ "orderAmount",
144
+ "orderCurrency",
145
+ "settleAmount",
146
+ "settleCurrency",
147
+ "settleDate",
148
+ "exchangeRate",
149
+ "exchangeDate",
150
+ "cupReserved",
151
+ "signMethod",
152
+ "signature",
153
+ ]
154
+
155
+ Sign_ignore_params = [
156
+ "bank",
157
+ ]
158
+ end
159
+ end
@@ -0,0 +1,77 @@
1
+ #encoding:utf-8
2
+ require 'open-uri'
3
+ require 'digest'
4
+ module UnionPay
5
+ module Service
6
+ RESP_SUCCESS = "00" #返回成功
7
+ QUERY_SUCCESS = "0" #查询成功
8
+ QUERY_FAIL = "1"
9
+ QUERY_WAIT = "2"
10
+ QUERY_INVALID = "3"
11
+
12
+ def self.front_pay(args)
13
+
14
+ args['orderTime'] ||= Time.now.strftime('%Y%m%d%H%M%S') #交易时间, YYYYmmhhddHHMMSS
15
+ args['orderCurrency'] ||= UnionPay::Conf::CURRENCY_CNY #交易币种,CURRENCY_CNY=>人民币
16
+
17
+ trans_type = args['transType']
18
+ if [UnionPay::Conf::CONSUME, UnionPay::Conf::PRE_AUTH].include? trans_type
19
+ @@api_url = UnionPay::Conf::Front_pay_url
20
+ args.merge!(UnionPay::Conf::Pay_params_empty).merge!(UnionPay::Conf::Pay_params)
21
+ param_check = UnionPay::Conf::Pay_params_check
22
+ else
23
+ # 前台交易仅支持 消费 和 预授权
24
+ raise("Bad trans_type for front_pay. Use back_pay instead")
25
+ end
26
+ if args['commodityUrl']
27
+ args['commodityUrl'] = URI::encode(args['commodityUrl'])
28
+ end
29
+
30
+ has_reserved = false
31
+ UnionPay::Conf::Mer_params_reserved.each do |k|
32
+ if args.has_key? k
33
+ value = args.delete k
34
+ (arr_reserved ||= []) << "#{k}=#{value}"
35
+ has_reserved = true
36
+ end
37
+ end
38
+
39
+ if has_reserved
40
+ args['merReserved'] = arr_reserved.join('&')
41
+ else
42
+ args['merReserved'] ||= ''
43
+ end
44
+
45
+ param_check.each do |k|
46
+ raise("KEY [#{k}] not set in params given") unless args.has_key? k
47
+ end
48
+
49
+ # signature
50
+ args['signature'] = self.sign(args)
51
+ args['signMethod'] = UnionPay::Conf::Sign_method
52
+ @@args = args
53
+ self
54
+ end
55
+
56
+ def self.sign(args)
57
+ sign_str = args.sort.map do |k,v|
58
+ "#{k}=#{v}&" unless UnionPay::Conf::Sign_ignore_params.include? k
59
+ end.join
60
+ Digest::MD5.hexdigest(sign_str + Digest::MD5.hexdigest(UnionPay.security_key))
61
+ end
62
+
63
+ def self.form
64
+ html = [
65
+ "<form id='pay_form' name='pay_form' action='#{@@api_url}' method='post'>"
66
+ ]
67
+ @@args.each do |k,v|
68
+ html << "<input type='hidden' name='#{k}' value='#{v}' />"
69
+ end
70
+ if block_given?
71
+ html << yield
72
+ html << "</form>"
73
+ end
74
+ html.join
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module UnionPay
2
+ VERSION = "0.0.1"
3
+ end
data/lib/unionpay.rb ADDED
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/unionpay/conf'
2
+ require File.dirname(__FILE__) + '/unionpay/service'
3
+ require File.dirname(__FILE__) + '/unionpay/version'
4
+
5
+ module UnionPay
6
+ class << self
7
+ attr_accessor :mer_id, :security_key, :mer_abbr
8
+
9
+ def mer_id= v
10
+ UnionPay::Conf::Pay_params['merId'] = v
11
+ end
12
+
13
+ def mer_abbr= v
14
+ UnionPay::Conf::Pay_params['merAbbr'] = v
15
+ end
16
+ end
17
+ end
data/test.rb ADDED
@@ -0,0 +1,26 @@
1
+ #encoding:utf-8
2
+ require './lib/unionpay'
3
+
4
+ param = {}
5
+ param['transType'] = UnionPay::Conf::CONSUME; #交易类型,CONSUME or PRE_AUTH
6
+ param['orderAmount'] = 11000; #交易金额
7
+ param['orderNumber'] = Time.now.strftime('%Y%m%d%H%M%S') #订单号,必须唯一
8
+ param['customerIp'] = '127.0.0.1'
9
+ param['frontEndUrl'] = "http://www.example.com/sdk/utf8/front_notify.php" #前台回调URL
10
+ param['backEndUrl'] = "http://www.example.com/sdk/utf8/back_notify.php" #后台回调URL
11
+
12
+ # 可填空字段
13
+ # param['commodityUrl'] = "http://www.example.com/product?name=商品" #商品URL
14
+ # param['commodityName'] = '商品名称' # 商品名称
15
+ # param['commodityUnitPrice'] = 11000; //商品单价
16
+ # param['commodityQuantity'] = 1; //商品数量
17
+ #
18
+
19
+ # 其余可填空的参数可以不填写
20
+
21
+ UnionPay.mer_id = '105550149170027'
22
+ UnionPay.mer_abbr = '商户名称'
23
+ UnionPay.security_key = '88888888'
24
+
25
+
26
+ puts UnionPay::Service.front_pay(param).form{"<input type='submit' />"}
data/unionpay.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unionpay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unionpay"
8
+ spec.version = UnionPay::VERSION
9
+ spec.authors = ["Chen"]
10
+ spec.email = ["kikyous@163.com"]
11
+ spec.description = %q{An unofficial unionpay gem}
12
+ spec.summary = %q{An unofficial unionpay gem}
13
+ spec.homepage = "https://github.com/kikyous/unionpay"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unionpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-19 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: An unofficial unionpay gem
42
+ email:
43
+ - kikyous@163.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/unionpay.rb
50
+ - lib/unionpay/conf.rb
51
+ - lib/unionpay/service.rb
52
+ - lib/unionpay/version.rb
53
+ - test.rb
54
+ - unionpay.gemspec
55
+ homepage: https://github.com/kikyous/unionpay
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.0.3
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: An unofficial unionpay gem
79
+ test_files: []