my_alipay 0.1.0

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: 3d8efc3d2b43f7fe60b7fb59bd50570adf91be2b
4
+ data.tar.gz: 91edc8cd47da9e27e61725812838f61d938280d1
5
+ SHA512:
6
+ metadata.gz: 2574b7baab73955e0888b912c47967c893cbaed59b62ce6e097fbb668d79456f852db912d7d0644cba0be917a2d9578e804900f41ed29b518cf3afaa76bb6cba
7
+ data.tar.gz: b99fcc1b490c47a6b2ba903ff49f314abfb11cd1336b908f60a1e5308372f2ab5fe1f1025c57ae868046acdf2947f90d6fc9d9587a26ac8a0b1facd53dc6f05f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in my_alipay.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # MyAlipay
2
+
3
+ A gem for alipay, support wap api of 2017
4
+
5
+ 支持阿里手机支付2017最新版本的接口
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'my_alipay'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install my_alipay
22
+
23
+ ## Configuration
24
+
25
+
26
+ 在 ```config/initializers/my_alipay.rb```配置如下信息:
27
+
28
+ ```ruby
29
+ MyAlipay.app_id = 'APP_ID' #APPID即创建应用后生成
30
+ MyAlipay.app_private_key = 'APP_PRIVATE_KEY' #开发者应用私钥,由开发者自己生成
31
+ MyAlipay.alipay_public_key = 'ALIPAY_PUBLIC_KEY' #支付宝公钥,由支付宝生成
32
+ MyAlipay.gateway_url = 'URL' #支付宝网关(固定),根据个人情况可填写沙箱网关或者正式网关
33
+ MyAlipay.gateway_url = 'RSA2' #默认为RSA2 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2
34
+
35
+ ```
36
+ ## 手机网站支付接口(alipay_trade_wap)
37
+
38
+ ```MyAlipay::Wap::Service.create_alipay_trade_wap_pay_url params```
39
+
40
+ ### Example
41
+
42
+ ```ruby
43
+
44
+ params ={
45
+ return_url: 'return_url',
46
+ notify_url: 'notify_url',
47
+ biz_content: {"subject": "商品名称", "out_trade_no": "L2017052515281049",
48
+ "total_amount": "10.00",
49
+ "product_code": "QUICK_WAP_PAY"}.to_json
50
+ }
51
+
52
+ url = MyAlipay::Wap::Service.create_alipay_trade_wap_pay_url params
53
+
54
+ #跳转到支付页面
55
+ redirect_to url #或者在html页面跳转 window.location.href = url
56
+
57
+ ```
58
+
59
+ ## 支付宝异步通知验签
60
+
61
+ ### Example
62
+
63
+ ```MyAlipay::Wap::Notify.wap_notify params```
64
+
65
+ ```ruby
66
+ def notify
67
+ #验签
68
+ is_verify = MyAlipay::Wap::Notify.wap_notify params
69
+ if is_verify
70
+ #验签成功处理
71
+ else
72
+ #验签失败处理
73
+ end
74
+ end
75
+
76
+
77
+
78
+ ```
79
+
80
+
81
+ ## Contributing
82
+
83
+ Bug report or pull request are welcome.
84
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "my_alipay"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ module MyAlipay
2
+ module Utils
3
+
4
+ def self.params_to_string(params)
5
+ params.sort.map { |item| item.join('=') }.join('&')
6
+ end
7
+
8
+ def self.params_to_decoded_string(params)
9
+ params.sort.map { |key, value| %Q(#{key}=#{CGI.unescape(value.to_s)}) }.join('&')
10
+ end
11
+ # 退款批次号,支付宝通过此批次号来防止重复退款操作,所以此号生成后最好直接保存至数据库,不要在显示页面的时候生成
12
+ # 共 24 位(8 位当前日期 + 9 位纳秒 + 1 位随机数)
13
+ def self.generate_batch_no
14
+ t = Time.now
15
+ batch_no = t.strftime('%Y%m%d%H%M%S') + t.nsec.to_s
16
+ batch_no.ljust(24, rand(10).to_s)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module MyAlipay
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ module MyAlipay
2
+ module Wap
3
+ module Notify
4
+
5
+ def self.wap_notify params
6
+ params.stringify_keys
7
+ params.delete('sign_type')
8
+ params.delete('controller')
9
+ params.delete('action')
10
+ sign = params.delete('sign')
11
+ sign = Base64.decode64(sign)
12
+ sign_str = MyAlipay::Utils.params_to_string params
13
+ digest = OpenSSL::Digest::SHA256.new
14
+ pkey = OpenSSL::PKey::RSA.new(Base64.decode64(MyAlipay.alipay_public_key))
15
+ pkey.verify(digest, sign, sign_str)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module MyAlipay
2
+ module Wap
3
+ module Service
4
+ GATEWAY_URL = 'https://openapi.alipaydev.com/gateway.do'
5
+ #ALIPAYCONFIG = YAML.load_file("config/myalipay.yml")[Rails.env].with_indifferent_access.freeze
6
+
7
+ #手机支付url
8
+ def self.create_alipay_trade_wap_pay_url(params, options = {})
9
+ sign_params = MyAlipay::Wap::Sign.generate_wap_sign params
10
+ MyAlipay.gateway_url + '?' + sign_params[:sign_str] + "&sign=#{ERB::Util.url_encode(sign_params[:sign])}"
11
+ end
12
+
13
+
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module MyAlipay
2
+ module Wap
3
+ module Sign
4
+
5
+ #rsa2 SHA256
6
+ def self.generate_wap_sign(params)
7
+ params = {
8
+ app_id: MyAlipay.app_id,
9
+ method: 'alipay.trade.wap.pay',
10
+ charset: 'UTF-8',
11
+ #format: 'JSON',
12
+ sign_type: 'RSA2',
13
+ timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S'),
14
+ version: '1.0',
15
+ biz_content: ''
16
+ }.merge(params)
17
+
18
+ hash_str = params.stringify_keys #{a:1,b:2} 转换成 {'a'= '1','b'='2'}
19
+ sign_str = MyAlipay::Utils.params_to_string hash_str
20
+ #sign_str = "app_id=2016080200146859&biz_content={\"subject\":\"测试数据\",\"out_trade_no\":\"123456\",\"total_amount\":\"100\",\"product_code\":\"QUICK_WAP_PAY\"}&charset=UTF-8&method=alipay.trade.wap.pay&sign_type=RSA2&timestamp=2014-07-24 03:07:50&version=1.0"
21
+ #如果秘钥key开头没有-----BEGIN PRIVATE KEY-----和-----END PRIVATE KEY-----需要base64解码,如果有则不需要进行解码
22
+ rsa = OpenSSL::PKey::RSA.new(Base64.decode64(MyAlipay.app_private_key))
23
+ digest = OpenSSL::Digest::SHA256.new
24
+ sign = Base64.strict_encode64(rsa.sign(digest, sign_str))
25
+ {sign_str: sign_str,sign: sign}
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ module MyAlipay
2
+ module Wap
3
+ module Test
4
+
5
+
6
+ def self.test_sign
7
+ params ='app_id=2016080200146859&biz_content={"subject":"测试数据","out_trade_no":"123456","total_amount":"100","product_code":"QUICK_WAP_PAY"}&charset=UTF-8&method=alipay.trade.wap.pay&sign_type=RSA2&timestamp=2014-07-24 03:07:50&version=1.0'
8
+ #params = 'app_id=2016080200146859&biz_content={"button":[{"actionParam":"ZFB_HFCZ","actionType":"out","name":"话费充值"},{"name":"查询","subButton":[{"actionParam":"ZFB_YECX","actionType":"out","name":"余额查询"},{"actionParam":"ZFB_LLCX","actionType":"out","name":"流量查询"},{"actionParam":"ZFB_HFCX","actionType":"out","name":"话费查询"}]},{"actionParam":"http://m.alipay.com","actionType":"link","name":"最新优惠"}]}&charset=UTF-8&method=alipay.mobile.public.menu.add&sign_type=RSA2&timestamp=2014-07-24 03:07:50&version=1.0'
9
+ rsa = OpenSSL::PKey::RSA.new(Base64.decode64(MyAlipay.app_private_key))
10
+ digest = OpenSSL::Digest::SHA256.new
11
+ Base64.strict_encode64(rsa.sign(digest, params))
12
+
13
+ end
14
+
15
+ def self.test_sign2
16
+ params ='{"subject":"测试数据","out_trade_no":"123456","total_amount":"100","product_code":"QUICK_WAP_PAY"}'
17
+ MyAlipay::Wap::Sign.generate_wap_sign params
18
+ end
19
+
20
+
21
+ TEST_RSA_PRIVATE_KEY = <<EOF
22
+ -----BEGIN PRIVATE KEY-----
23
+ MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKM1WEl/09gMU3U8
24
+ V3hJcS/GaMPR2f7FDiVC7qjwdsZfPh179FFVMkMvjyrDGfzWVqvlw1IiNueENFeb
25
+ 19lyvO7BuOrFc6/Y6/sxkBr9YkRx++nddxKDEo8HngUtW8ej+vG4wrbGmmzjsulz
26
+ u+mmS+Leh9tdbrVL5pPRNYWRQxOfAgMBAAECgYB7xOSjOJFK8m4IJi6fRfLULD8e
27
+ 4XHUR1Qm5c9fxpwMbAYLDgmF9HodgV+tKi/3EgTAb4nkK5Y/lH6tQb47ZUvo/lKz
28
+ RlIVZ6Rm76V07g/+5exIZzTyvdD9T2fLeYQwKV/2JYUv0KSYWPvWykdaV4aNkCuw
29
+ mxTUjvhDxK/Ns31CIQJBANI1Y3gGBqbBIN9wxjx3ShBtt/U8YnipUJ92eTI7OU9p
30
+ ZsCIFPoeYG/X40miwDb5ouPnvJTtzuY4PkPokEefN9MCQQDGwurqa8RNK2APA62U
31
+ CdZbJuWimkdHEc53IKvD/l2tWVFqhVAy8bs+3LGzBNfuxUuAxOoQm9n0IVRaH5jn
32
+ l8GFAkEAijuTmsUTsKsGDAmkQvULHnyYYUuBUem92+9TycWKbX9Zk7ipWsWJE2N7
33
+ 0tuU3VISXR7yM1mjGl/YCl4wKvk4AwJAE1DkBY4dkKZTeoIP/2AJXehkzq2Rmb2I
34
+ RBl/t9djgTI58FEuXxUQ7mYCOvSQi5rO4J/CY4TR5KDMksmZUYB1BQJAIEfVDxz4
35
+ 5yoHL7L+6EoC5TWxUxFMN7z7FhObyKeaLKj3inEsbjfcPCA09zPUce0FSKBc/dVh
36
+ DEorJMaPK5vXiA==
37
+ -----END PRIVATE KEY-----
38
+ EOF
39
+ end
40
+ end
41
+
42
+ end
43
+
data/lib/my_alipay.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'my_alipay/version'
2
+ require 'my_alipay/utils'
3
+ require 'my_alipay/wap/sign'
4
+ require 'my_alipay/wap/service'
5
+ require 'my_alipay/wap/notify'
6
+ require 'my_alipay/wap/test'
7
+
8
+ module MyAlipay
9
+ @debug_mode = true
10
+ @sign_type = 'RSA2'
11
+
12
+ class << self
13
+ attr_accessor :app_id,:app_private_key, :alipay_public_key, :sign_type, :debug_mode,:gateway_url
14
+
15
+ def debug_mode?
16
+ !!@debug_mode
17
+ end
18
+ end
19
+ end
data/my_alipay.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'my_alipay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "my_alipay"
8
+ spec.version = MyAlipay::VERSION
9
+ spec.authors = ["yancheng"]
10
+ spec.email = ["zqalyc@163.com"]
11
+
12
+ spec.summary = %q{A gem for alipay, support wap api of 2017}
13
+ spec.description = %q{This is a new gem for alipay, support new alipay api of 2017}
14
+ spec.homepage = "https://github.com/yanchengv/my_alipay"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing 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 " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.14"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_alipay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yancheng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-01 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: This is a new gem for alipay, support new alipay api of 2017
42
+ email:
43
+ - zqalyc@163.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
54
+ - lib/my_alipay.rb
55
+ - lib/my_alipay/utils.rb
56
+ - lib/my_alipay/version.rb
57
+ - lib/my_alipay/wap/notify.rb
58
+ - lib/my_alipay/wap/service.rb
59
+ - lib/my_alipay/wap/sign.rb
60
+ - lib/my_alipay/wap/test.rb
61
+ - my_alipay.gemspec
62
+ homepage: https://github.com/yanchengv/my_alipay
63
+ licenses: []
64
+ metadata:
65
+ allowed_push_host: https://rubygems.org
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.6.10
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A gem for alipay, support wap api of 2017
86
+ test_files: []