yeepay 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99ff51b58c0b04ef39766d8b5fa17cd7cbb38e7f
4
+ data.tar.gz: 670e56c778fa0ec615ea8419ed8df16591b9aa4f
5
+ SHA512:
6
+ metadata.gz: 17c9ae559425fd8e3b419f3cd89eefc3483dd7dadf098b56afbf27372ac101b868ae7ba4ef4667735402dc7bdd64027ed53a5eb2fa8ff55255cb6d99bb4fb5bb
7
+ data.tar.gz: 38fd112c956e2af0441eef04dcae57af0c0b7b254afaae459151be704e41187fca5d60c187bd496e58a3316976ffc2b84d1210b511a7bbcc7adb927d7709b0b1
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+ /spec/
12
+
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yeepay.gemspec
4
+ gemspec
5
+
6
+ gem 'ruby-hmac'
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+ # Yeepay
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/yeepay`. 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 'ruby-hmac'
13
+ gem 'yeepay', github: 'mumaoxi/yeepay'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install yeepay
23
+
24
+ ## Configuration
25
+
26
+ ```ruby
27
+ Yeepay.p1_mer_id = 'YOUR_MER_ID' #商户编号
28
+ Yeepay.merchant_key = 'YOUR_MERCHANT_KEY' #商户密钥
29
+
30
+ #Yeepay.debug_mode = true # Enable parameter check. Default is true.
31
+ ```
32
+
33
+
34
+ ## Service
35
+
36
+ ### 充值卡支付接口
37
+
38
+ #### Name
39
+
40
+ ```ruby
41
+ create_trade
42
+ ```
43
+
44
+ #### Definition
45
+
46
+ ```ruby
47
+ Yeepay::Service::Card.create_trade({OPTIONS})
48
+ ```
49
+
50
+ #### Example
51
+ ```ruby
52
+ options = {
53
+ out_trade_no: DateTime.now.strftime('%Y%m%d%H%M%S')+Random.rand(1000).to_s,
54
+ total_fee: 150,
55
+ verify_fee: true,
56
+ notify_url: 'http://example.com/pays/yeepay_notify',
57
+ cards: [
58
+ {amt: 50,
59
+ no: '123456',
60
+ pwd: '654321'
61
+ },
62
+ {amt: 100,
63
+ no: '123',
64
+ pwd: '654'
65
+ }
66
+ ],
67
+ frp_id: 'SZX'
68
+
69
+ }
70
+ Yeepay::Service::Card.create_trade(options)
71
+ # => '{:r0_Cmd=>"ChargeCardDirect", :r1_Code=>"1", :r6_Order=>"20150415150818837", :rq_ReturnMsg=>"提交成功!", :hmac=>"ba06a390eb8f487d84282da5fb90b862"}'
72
+ ```
73
+
74
+ #### Options
75
+
76
+ | Key | Requirement | Description |
77
+ | --- | ----------- | ----------- |
78
+ | out_trade_no | required | Order id in your application. |
79
+ | total_fee | required | Order item's price. |
80
+ | verify_fee | required | whether check price between cards and order. |
81
+ | notify_url | required | Yeepay asyn notify url. |
82
+ | frp_id | required | Pay channel id |
83
+
84
+ This is not a complete list of arguments, please read official document: http://www.yeepay.com .
85
+
86
+
87
+ ### 充值卡支付通知接口
88
+
89
+ #### Name
90
+
91
+ ```ruby
92
+ verify?
93
+ ```
94
+
95
+ #### Definition
96
+ ```ruby
97
+ Yeepay::Service::Card.verify?({OPTIONS})
98
+ ```
99
+
100
+ ### Example
101
+ ```ruby
102
+ options = {
103
+ r0_Cmd: 'ChargeCardDirect',
104
+ r1_Code: '2',
105
+ p1_MerId: '10012367775',
106
+ p2_Order: '20150415123733472',
107
+ p3_Amt: '0.0',
108
+ p4_FrpId: 'SZX',
109
+ p5_CardNo: '123,123456',
110
+ p6_confirmAmount: '0.0,0.0',
111
+ p7_realAmount: '0.0,0.0',
112
+ p8_cardStatus: '7,7',
113
+ p9_MP: '',
114
+ pb_BalanceAmt: '',
115
+ pc_BalanceAct: '',
116
+ r2_TrxId: '',
117
+ hmac: '654b58ece9b4377eaad4c1ebb013a668'
118
+ }
119
+ Yeepay::Service::Card.verify?(options)
120
+ # => true
121
+ ```
122
+
123
+ ## Development
124
+
125
+ 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.
126
+
127
+ 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).
128
+
129
+ ## Contributing
130
+
131
+ 1. Fork it ( https://github.com/[my-github-username]/yeepay/fork )
132
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
133
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
134
+ 4. Push to the branch (`git push origin my-new-feature`)
135
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yeepay"
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/yeepay.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+ require 'hmac-md5'
4
+ require 'yeepay/version'
5
+ require 'yeepay/service'
6
+ require 'yeepay/service/card'
7
+
8
+ module Yeepay
9
+ class << self
10
+ attr_accessor :p1_mer_id
11
+ attr_accessor :merchant_key
12
+ attr_writer :debug_mode
13
+
14
+ def debug_mode?
15
+ @debug_mode.nil? ? true : !!@debug_mode
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,98 @@
1
+ module Yeepay
2
+ module Service
3
+ GATEWAY_URL = 'https://www.yeepay.com/app-merchant-proxy/command.action'
4
+
5
+ #参数签名
6
+ def self.hmac_md5_sign(options={})
7
+ warn("options #{options}")
8
+ HMAC::MD5.hexdigest(Yeepay.merchant_key, options.values.join(''))
9
+ end
10
+
11
+ def self.string_key2_sym(options={})
12
+ options.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
13
+ end
14
+
15
+ module Card
16
+
17
+ #提交支付返回的错误代码
18
+ SUBMIT_ERROR_CODES = {
19
+ -100 => '卡面额与订单金额不符',
20
+ -1 => '签名较验失败或未知错误',
21
+ 1 => '提交成功!',
22
+ 2 => '卡密成功处理过或者提交卡号过于频繁',
23
+ 5 => '卡数量过多,目前最多支持10张卡',
24
+ 7 => '支付卡密无效!',
25
+ 11 => '订单号重复',
26
+ 66 => '支付金额有误',
27
+ 95 => '支付方式未开通',
28
+ 112 => '业务状态不可用,未开通此类卡业务',
29
+ 8001 => '卡面额组填写错误',
30
+ 8002 => '卡号密码为空或者数量不相等',
31
+ 2005 => '卡密已经成功处理过,不能重复使用'
32
+ }
33
+
34
+ #校验卡的支付金额
35
+ def self.card_fee_ok?(options={})
36
+ if options[:verify_fee]
37
+ card_total_fee = options[:cards].collect { |card| card[:amt] }.inject(:+)
38
+ warn("\ncard_total_fee:#{card_total_fee.to_f},order_total_fee:#{options[:total_fee].to_f}")
39
+ unless options[:total_fee].to_f == card_total_fee.to_f
40
+ return false
41
+ end
42
+ end
43
+ true
44
+ end
45
+
46
+ #组合发起支付请求的必要参数
47
+ def self.gen_params(options={})
48
+ {
49
+ :p0_Cmd => 'ChargeCardDirect',
50
+ :p1_MerId => Yeepay.p1_mer_id,
51
+ :p2_Order => options[:out_trade_no],
52
+ :p3_Amt => options[:total_fee],
53
+ :p4_verifyAmt => options[:verify_fee],
54
+ :p8_Url => options[:notify_url],
55
+ :pa7_cardAmt => options[:cards].collect { |card| card[:amt] }.join(','),
56
+ :pa8_cardNo => options[:cards].collect { |card| card[:no] }.join(','),
57
+ :pa9_cardPwd => options[:cards].collect { |card| card[:pwd] }.join(','),
58
+ :pd_FrpId => options[:frp_id],
59
+ :pr_NeedResponse => '1',
60
+ }
61
+ end
62
+
63
+ #获取返回的需要验证的参数
64
+ def self.res_verifying_params(options={})
65
+ memo = {}
66
+ [:r0_Cmd,
67
+ :r1_Code,
68
+ :p1_MerId,
69
+ :p2_Order,
70
+ :p3_Amt,
71
+ :p4_FrpId,
72
+ :p5_CardNo,
73
+ :p6_confirmAmount,
74
+ :p7_realAmount,
75
+ :p8_cardStatus,
76
+ :p9_MP,
77
+ :pb_BalanceAmt,
78
+ :pc_BalanceAct
79
+ ].map { |key| memo[key] = options[key] }
80
+ memo
81
+ end
82
+
83
+ #处理支付请求结果
84
+ def self.receive_response(options, res)
85
+ res_data = {}
86
+ res.to_s.split(/\n/).map { |item|
87
+ key_value = item.to_s.split(/=/)
88
+ res_data[key_value[0].to_s.to_sym] = key_value[1]
89
+ }
90
+ unless card_fee_ok?(options)
91
+ res_data[:r1_Code] = -100
92
+ end
93
+ res_data[:rq_ReturnMsg] = SUBMIT_ERROR_CODES[res_data[:r1_Code].to_s.to_i]
94
+ res_data
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,39 @@
1
+ module Yeepay
2
+ module Service
3
+ module Card
4
+ #发起支付交易
5
+ def self.create_trade(options = {})
6
+ options = Yeepay::Service.string_key2_sym(options)
7
+ #组合支付请求参数
8
+ trade_data = gen_params(options)
9
+ trade_data[:hmac] = Yeepay::Service.hmac_md5_sign(trade_data)
10
+ warn("\ntrade_data #{trade_data}")
11
+
12
+ #发起支付请求
13
+ begin
14
+ res = open("#{GATEWAY_URL}?#{trade_data.collect { |key, value| "#{key}=#{value}" }.join('&')}").read
15
+ warn("\nres #{res}")
16
+ rescue => err
17
+ warn("\nSocketError #{err}\n\n")
18
+ return {}
19
+ end
20
+
21
+ #处理支付结果
22
+ res_data = receive_response(options, res)
23
+ warn("\nres_data #{res_data}")
24
+ res_data
25
+ end
26
+
27
+ #校验支付签名
28
+ def self.verify?(options={})
29
+ options = Yeepay::Service.string_key2_sym(options)
30
+ notify_params = res_verifying_params(options)
31
+ warn("\nnotfy_params #{notify_params}")
32
+ notify_hmac = Yeepay::Service.hmac_md5_sign(notify_params)
33
+
34
+ warn("\nnotify_sign #{options[:hmac]}, verify_sign: #{notify_hmac}")
35
+ notify_hmac == options[:hmac]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Yeepay
2
+ VERSION = "0.1.9"
3
+ end
data/yeepay.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yeepay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yeepay"
8
+ spec.version = Yeepay::VERSION
9
+ spec.authors = ["mumaoxi"]
10
+ spec.email = ["15201280641@qq.com"]
11
+
12
+ spec.summary = %q{易宝支付}
13
+ spec.description = %q{易宝支付}
14
+ spec.homepage = "https://github.com/mumaoxi/yeepay"
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'] = "https://rubygems.org"
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 "rspec", "~> 3.2"
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yeepay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.9
5
+ platform: ruby
6
+ authors:
7
+ - mumaoxi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: 易宝支付
56
+ email:
57
+ - 15201280641@qq.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/yeepay.rb
70
+ - lib/yeepay/service.rb
71
+ - lib/yeepay/service/card.rb
72
+ - lib/yeepay/version.rb
73
+ - yeepay.gemspec
74
+ homepage: https://github.com/mumaoxi/yeepay
75
+ licenses: []
76
+ metadata:
77
+ allowed_push_host: https://rubygems.org
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: 易宝支付
98
+ test_files: []