unionpay 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 991f601026a08c6b26af993b3191b116fe47fcba
4
- data.tar.gz: a2ea091456f7336ca08f13d62ff721d777ac0f30
3
+ metadata.gz: a4c823129d1f1dd841f122f5ea2ee1c013e14468
4
+ data.tar.gz: f3a0f27c3fdcff06e5f590e776ffea7c02705d26
5
5
  SHA512:
6
- metadata.gz: 544de5cd1070c90f11f32a32e8350fa771d3f94922d75f85b7fe393cc517c20bf0d14db220751521bf32f8d222ee39ec2d64aed51a000c94946d716ecc235ecf
7
- data.tar.gz: a7f66c2242ce38b48f295d602958ccdb0fcbf956e415f3e8dd99f01c8373c9b47f42508cca5acb65ae10472a096e8bf45bbd52e79b323be712c7f9fc4aa70d86
6
+ metadata.gz: 2023fee365fb05787f8d48c2aaea2016570c73f6acfe86dd53ba523bb8f93d05f29cae983a93b0d51932f3c0ee683645a7db1604fb81189c2a38e6a9b400a236
7
+ data.tar.gz: 1919939feccece40af863833464f58d0f7474bc2c485dd1a5c14246c6e736d19632050f0f6e726bdd5e43095b2d16efaa58735669bd5e4dda89858889f741c33
data/README.md CHANGED
@@ -5,11 +5,16 @@ unionpay
5
5
 
6
6
  ## Usage
7
7
 
8
+ ### Install
9
+
10
+ ```gem install unionpay```
11
+
8
12
  ### Config
9
13
 
10
14
  ```ruby
11
- UnionPay.mer_id = '105550149170027'
12
- UnionPay.mer_abbr = '商户名称'
15
+ UnionPay.environment = :development ## 测试环境, :pre_production #预上线环境, 默认 # 线上环境
16
+ UnionPay.mer_id = '105550149170027'
17
+ UnionPay.mer_abbr = '商户名称'
13
18
  UnionPay.security_key = '88888888'
14
19
  ```
15
20
 
@@ -32,5 +37,23 @@ param['backEndUrl'] = "http://www.example.com/sdk/utf8/back_notify.php" #
32
37
 
33
38
  # 其余可填空的参数可以不填写
34
39
 
35
- puts UnionPay::Service.front_pay(param).form{"<input type='submit' />"}
40
+ UnionPay::Service.front_pay(param).form(target: '_blank', id: 'form'){"<input type='submit' />"}
41
+ ```
42
+
43
+ ### Verify notify
44
+
45
+ ```ruby
46
+ # example in rails
47
+ # The notify url MUST be set when generate payment url
48
+ def unionpay_notify
49
+ # except :controller_name, :action_name, :host, etc.
50
+ notify_params = params.except(*request.path_parameters.keys)
51
+ args = UnionPay::Service.responce(notify_params)
52
+ if args['respCode'] == UnionPay::RESP_SUCCESS
53
+ # valid notify, code your business logic.
54
+ render :text => 'success'
55
+ else
56
+ render :text => 'error'
57
+ end
58
+ end
36
59
  ```
data/lib/unionpay/conf.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  #encoding:utf-8
2
2
  module UnionPay
3
- module Conf
3
+ class << self
4
+ attr_accessor :front_pay_url, :back_pay_url, :query_url
5
+ end
4
6
  VERIFY_HTTPS_CERT = false
5
7
 
6
8
  Timezone = "Asia/Shanghai" #时区
@@ -18,20 +20,6 @@ module UnionPay
18
20
  'merAbbr' => '商户名称'
19
21
  }
20
22
 
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
23
 
36
24
  FRONT_PAY = 1
37
25
  BACK_PAY = 2
@@ -155,5 +143,4 @@ module UnionPay
155
143
  Sign_ignore_params = [
156
144
  "bank",
157
145
  ]
158
- end
159
146
  end
@@ -2,33 +2,51 @@
2
2
  require 'open-uri'
3
3
  require 'digest'
4
4
  module UnionPay
5
+ RESP_SUCCESS = "00" #返回成功
6
+ QUERY_SUCCESS = "0" #查询成功
7
+ QUERY_FAIL = "1"
8
+ QUERY_WAIT = "2"
9
+ QUERY_INVALID = "3"
5
10
  module Service
6
- RESP_SUCCESS = "00" #返回成功
7
- QUERY_SUCCESS = "0" #查询成功
8
- QUERY_FAIL = "1"
9
- QUERY_WAIT = "2"
10
- QUERY_INVALID = "3"
11
11
 
12
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
-
13
+ args['orderTime'] ||= Time.now.strftime('%Y%m%d%H%M%S') #交易时间, YYYYmmhhddHHMMSS
14
+ args['orderCurrency'] ||= UnionPay::CURRENCY_CNY #交易币种,CURRENCY_CNY=>人民币
17
15
  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
16
+ if [UnionPay::CONSUME, UnionPay::PRE_AUTH].include? trans_type
17
+ @@api_url = UnionPay.front_pay_url
18
+ args.merge!(UnionPay::Pay_params_empty).merge!(UnionPay::Pay_params)
19
+ @@param_check = UnionPay::Pay_params_check
22
20
  else
23
21
  # 前台交易仅支持 消费 和 预授权
24
22
  raise("Bad trans_type for front_pay. Use back_pay instead")
25
23
  end
24
+ self.service(args,UnionPay::FRONT_PAY)
25
+ end
26
+
27
+ def responce(args)
28
+ cupReserved = (args['cupReserved'] ||= '')
29
+ cupReserved = Rack::Utils.parse_nested_query cupReserved.gsub(/^{/,'').gsub(/}$/,'')
30
+ if !args['signature'] || !args['signMethod']
31
+ raise('No signature Or signMethod set in notify data!')
32
+ end
33
+
34
+ args.delete 'signMethod'
35
+ if args.delete('signature') != self.sign(args)
36
+ raise('Bad signature returned!')
37
+ end
38
+ args.merge! cupReserved
39
+ args.delete 'cupReserved'
40
+ args
41
+ end
42
+
43
+ def self.service(args, service_type)
26
44
  if args['commodityUrl']
27
45
  args['commodityUrl'] = URI::encode(args['commodityUrl'])
28
46
  end
29
47
 
30
48
  has_reserved = false
31
- UnionPay::Conf::Mer_params_reserved.each do |k|
49
+ UnionPay::Mer_params_reserved.each do |k|
32
50
  if args.has_key? k
33
51
  value = args.delete k
34
52
  (arr_reserved ||= []) << "#{k}=#{value}"
@@ -42,27 +60,28 @@ module UnionPay
42
60
  args['merReserved'] ||= ''
43
61
  end
44
62
 
45
- param_check.each do |k|
63
+ @@param_check.each do |k|
46
64
  raise("KEY [#{k}] not set in params given") unless args.has_key? k
47
65
  end
48
66
 
49
67
  # signature
50
68
  args['signature'] = self.sign(args)
51
- args['signMethod'] = UnionPay::Conf::Sign_method
69
+ args['signMethod'] = UnionPay::Sign_method
52
70
  @@args = args
53
71
  self
54
72
  end
55
73
 
56
74
  def self.sign(args)
57
75
  sign_str = args.sort.map do |k,v|
58
- "#{k}=#{v}&" unless UnionPay::Conf::Sign_ignore_params.include? k
76
+ "#{k}=#{v}&" unless UnionPay::Sign_ignore_params.include? k
59
77
  end.join
60
78
  Digest::MD5.hexdigest(sign_str + Digest::MD5.hexdigest(UnionPay.security_key))
61
79
  end
62
80
 
63
- def self.form
81
+ def self.form options={}
82
+ attrs = options.map{|k,v| "#{k}='#{v}'"}.join(' ')
64
83
  html = [
65
- "<form id='pay_form' name='pay_form' action='#{@@api_url}' method='post'>"
84
+ "<form #{attrs} action='#{@@api_url}' method='post'>"
66
85
  ]
67
86
  @@args.each do |k,v|
68
87
  html << "<input type='hidden' name='#{k}' value='#{v}' />"
@@ -1,3 +1,3 @@
1
1
  module UnionPay
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/unionpay.rb CHANGED
@@ -4,14 +4,35 @@ require File.dirname(__FILE__) + '/unionpay/version'
4
4
 
5
5
  module UnionPay
6
6
  class << self
7
- attr_accessor :mer_id, :security_key, :mer_abbr
7
+ attr_accessor :mer_id, :security_key, :mer_abbr, :environment
8
8
 
9
9
  def mer_id= v
10
- UnionPay::Conf::Pay_params['merId'] = v
10
+ UnionPay::Pay_params['merId'] = v
11
11
  end
12
12
 
13
13
  def mer_abbr= v
14
- UnionPay::Conf::Pay_params['merAbbr'] = v
14
+ UnionPay::Pay_params['merAbbr'] = v
15
+ end
16
+
17
+ def environment= e
18
+ case e
19
+ ## 测试环境
20
+ when :development
21
+ UnionPay.front_pay_url = "http://58.246.226.99/UpopWeb/api/Pay.action"
22
+ UnionPay.back_pay_url = "http://58.246.226.99/UpopWeb/api/BSPay.action"
23
+ UnionPay.query_url = "http://58.246.226.99/UpopWeb/api/Query.action"
24
+ ## 预上线环境
25
+ when :pre_production
26
+ UnionPay.front_pay_url = "https://www.epay.lxdns.com/UpopWeb/api/Pay.action"
27
+ UnionPay.back_pay_url = "https://www.epay.lxdns.com/UpopWeb/api/BSPay.action"
28
+ UnionPay.query_url = "https://www.epay.lxdns.com/UpopWeb/api/Query.action"
29
+ ## 线上环境
30
+ else
31
+ UnionPay.front_pay_url = "https://unionpaysecure.com/api/Pay.action"
32
+ UnionPay.back_pay_url = "https://besvr.unionpaysecure.com/api/BSPay.action"
33
+ UnionPay.query_url = "https://query.unionpaysecure.com/api/Query.action"
34
+ end
15
35
  end
16
36
  end
37
+ self.environment= :production
17
38
  end
data/test.rb CHANGED
@@ -1,8 +1,13 @@
1
1
  #encoding:utf-8
2
2
  require './lib/unionpay'
3
3
 
4
+ UnionPay.environment = :development ## 测试环境, :pre_production #预上线环境, 默认 # 线上环境
5
+ UnionPay.mer_id = '105550149170027'
6
+ UnionPay.mer_abbr = '商户名称'
7
+ UnionPay.security_key = '88888888'
8
+
4
9
  param = {}
5
- param['transType'] = UnionPay::Conf::CONSUME; #交易类型,CONSUME or PRE_AUTH
10
+ param['transType'] = UnionPay::CONSUME; #交易类型,CONSUME or PRE_AUTH
6
11
  param['orderAmount'] = 11000; #交易金额
7
12
  param['orderNumber'] = Time.now.strftime('%Y%m%d%H%M%S') #订单号,必须唯一
8
13
  param['customerIp'] = '127.0.0.1'
@@ -18,9 +23,6 @@ param['backEndUrl'] = "http://www.example.com/sdk/utf8/back_notify.ph
18
23
 
19
24
  # 其余可填空的参数可以不填写
20
25
 
21
- UnionPay.mer_id = '105550149170027'
22
- UnionPay.mer_abbr = '商户名称'
23
- UnionPay.security_key = '88888888'
24
26
 
25
27
 
26
- puts UnionPay::Service.front_pay(param).form{"<input type='submit' />"}
28
+ puts UnionPay::Service.front_pay(param).form(target: '_blank', id: 'form'){"<input type='submit' />"}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unionpay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chen