onepay 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6062604f036a7156a91e4185abcc0b2d8e32d103
4
+ data.tar.gz: 82d51225fa3313a8f71607e85cbb5de612737fb4
5
+ SHA512:
6
+ metadata.gz: d5e35366719cef1af33399408171f35db11a794db34e76bde7f72bfc7ebb3ce9caebce470698bcd332b34f321a7ac440661938557dbc883958e6b2b2dec624af
7
+ data.tar.gz: 59e29a116eb46a3c6a1de03d0436ba9d2131d90bc9bc82ad03ee9f76a9abdab4b48ddca22a157386511b4fe33b34b10159277dba3a7b98422b3fac830940ad4f
@@ -0,0 +1,40 @@
1
+ require 'version'
2
+ require 'yaml'
3
+ require 'digest/md5'
4
+ require 'cgi'
5
+
6
+ require File.dirname(__FILE__) + '/onepay/config'
7
+ require File.dirname(__FILE__) + '/onepay/request/alipay'
8
+ require File.dirname(__FILE__) + '/onepay/response/alipay'
9
+ require File.dirname(__FILE__) + '/onepay/request/swiftpass'
10
+ require File.dirname(__FILE__) + '/onepay/response/swiftpass'
11
+
12
+ module Onepay
13
+ ADAPTER_TYPES = %w[alipay swiftpass]
14
+
15
+ def self.build_request(adapter)
16
+ case adapter
17
+ when :alipay
18
+ return Onepay::AlipayRequest.new
19
+ when :swiftpass
20
+ return Onepay::SwiftpassRequest.new
21
+ end
22
+
23
+ raise "Not vaid onepay adapter, it should be in #{ADAPTER_TYPES}"
24
+ end
25
+
26
+ def self.build_response(adapter, params)
27
+ case adapter
28
+ when :alipay
29
+ return Onepay::AlipayRequest.new(params)
30
+ when :swiftpass
31
+ return Onepay::SwiftpassRequest.new(params)
32
+ end
33
+
34
+ raise "Not vaid onepay adapter, it should be in #{ADAPTER_TYPES}"
35
+ end
36
+ end
37
+
38
+ if defined?(Rails)
39
+ require File.dirname(__FILE__) + '/onepay/railtie'
40
+ end
@@ -0,0 +1,25 @@
1
+ module Onepay
2
+ class << self
3
+ attr_accessor :config
4
+ end
5
+
6
+ def self.load_config
7
+ begin
8
+ filename = File.expand_path(Rails.root.to_s + '/config/onepay.yml')
9
+ @config = YAML.safe_load(File.open(filename))[Rails.env]
10
+ rescue Exception => e
11
+ puts <<-EXAMPLE
12
+ ---
13
+ development:
14
+ alipay:
15
+ parter: 12111XXXX
16
+ key: XXXXXXX
17
+ seller_email: XXXXX@XXXXX.com
18
+ swiftpass:
19
+ appid: wxd678efh567hg6787
20
+ mch_id: 1230000109
21
+ EXAMPLE
22
+ raise "Please configure your Onepay settings in #{filename}."
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Onepay
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "加载rails的配置文件" do
4
+ Onepay.load_config
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+ require 'alipay'
3
+
4
+ module Onepay
5
+ class AlipayRequest
6
+ GATEWAY_URL = 'https://openapi.alipay.com/gateway.do?'.freeze
7
+
8
+ attr_accessor :title, :total_fee, :order_id
9
+
10
+ def url
11
+ alipay_client = Alipay::Client.new(
12
+ url: GATEWAY_URL,
13
+ app_id: Onepay.config['alipay']['app_id'],
14
+ app_private_key: Onepay.config['alipay']['private_key'],
15
+ alipay_public_key: Onepay.config['alipay']['public_key']
16
+ )
17
+
18
+ alipay_client.page_execute_url(
19
+ method: 'alipay.trade.wap.pay',
20
+ notify_url: Onepay.config['alipay']['notify_url'],
21
+ biz_content: {
22
+ out_trade_no: @order_id,
23
+ product_code: 'QUICK_WAP_WAY',
24
+ total_amount: @total_fee,
25
+ subject: @title
26
+ }.to_json,
27
+ timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S')
28
+ )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ require 'excon'
2
+ require 'active_support/core_ext/hash/conversions'
3
+ require 'active_support/logger'
4
+
5
+ module Onepay
6
+ class SwiftpassRequest
7
+ GATEWAY_URL = 'https://pay.swiftpass.cn/pay/gateway'.freeze
8
+
9
+ attr_accessor :title, :total_fee, :order_id
10
+
11
+ def url
12
+ params = {
13
+ service: 'pay.weixin.wappay',
14
+ mch_id: Onepay.config['swiftpass']['mch_id'],
15
+ out_trade_no: @order_id,
16
+ body: @title,
17
+ total_fee: @total_fee,
18
+ mch_create_ip: Onepay.config['swiftpass']['mch_create_ip'],
19
+ notify_url: Onepay.config['swiftpass']['notify_url'],
20
+ device_info: 'iOS_WAP',
21
+ mch_app_name: '给玩',
22
+ mch_app_id: 'com.geiwan666',
23
+ nonce_str: SecureRandom.uuid.tr('-', '')
24
+ }
25
+
26
+ params = params.merge(sign: md5_sort(params))
27
+
28
+ resp = Excon.post(
29
+ GATEWAY_URL,
30
+ body: xmlify_payload(params),
31
+ headers: { 'Content-Type' => 'application/xml' },
32
+ retry_limit: 6
33
+ )
34
+
35
+ Rails.logger.fatal GATEWAY_URL
36
+ Rails.logger.fatal xmlify_payload(params)
37
+ Rails.logger.fatal resp.body
38
+
39
+ result = Hash.from_xml(resp.body)
40
+ if result['xml']['status'] == '0' && result['xml']['result_code'] == '0'
41
+ return result['xml']['pay_info']
42
+ end
43
+
44
+ nil
45
+ end
46
+
47
+ private
48
+
49
+ def md5_sort(hash)
50
+ keys = hash.keys.sort.map do |k|
51
+ "#{k}=#{hash[k]}"
52
+ end
53
+
54
+ Digest::MD5.hexdigest(keys.join('&') + '&key=' + Onepay.config['swiftpass']['key']).upcase
55
+ end
56
+
57
+ def xmlify_payload(params)
58
+ "<xml>#{params.map { |k, v| "<#{k}>#{v}</#{k}>" }.join}</xml>"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,37 @@
1
+ module Onepay
2
+ class AlipayResponse
3
+ attr_reader :order_id, :total_fee, :extra_param
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ @order_id = params['out_trade_no']
8
+ @total_fee = params['total_fee'].to_f
9
+ @extra_param = params['extra_common_param']
10
+ end
11
+
12
+ def successful?
13
+ sign = md5_sort(filter_params(@params))
14
+ @params['trade_status'] == 'TRADE_SUCCESS' && sign == @params.sign
15
+ end
16
+
17
+ def success_response
18
+ 'success'
19
+ end
20
+
21
+ private
22
+
23
+ def filter_params(params)
24
+ params.dup.delete_if do |k, v|
25
+ k == 'controller' || k == 'action' || k == 'sign' || k == 'sign_type' || v.blank?
26
+ end
27
+ end
28
+
29
+ def md5_sort(hash)
30
+ keys = hash.keys.sort.map do |k|
31
+ "#{k}=#{hash[k]}"
32
+ end
33
+
34
+ Digest::MD5.hexdigest(keys.join('&') + Onepay.config['alipay']['key']).downcase
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ module Onepay
2
+ class SwiftpassResponse
3
+ attr_reader :order_id, :total_fee
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ @order_id = params['out_trade_no']
8
+ @total_fee = params['total_fee']
9
+ end
10
+
11
+ def successful?
12
+ (@params['trade_status'] == 'TRADE_SUCCESS') && (md5_sort(filter_params(@params)) == @params.sign)
13
+ end
14
+
15
+ def success_response
16
+ 'success'
17
+ end
18
+
19
+ private
20
+
21
+ def filter_params(params)
22
+ params.dup.delete_if do |k, v|
23
+ (k == 'controller') || (k == 'action') || (k == 'sign') || (k == 'sign_type') || v.blank?
24
+ end
25
+ end
26
+
27
+ def md5_sort(hash)
28
+ keys = hash.keys.sort.map do |k|
29
+ "#{k}=#{hash[k]}"
30
+ end
31
+
32
+ Digest::MD5.hexdigest(keys.join('&') + Onepay.config['alipay']['key']).downcase
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Onepay
2
+ VERSION = '1.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onepay
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sundbcn@gmail.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: alipay
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0
55
+ description: onepay
56
+ email:
57
+ - sundbcn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/onepay.rb
63
+ - lib/onepay/config.rb
64
+ - lib/onepay/railtie.rb
65
+ - lib/onepay/request/alipay.rb
66
+ - lib/onepay/request/swiftpass.rb
67
+ - lib/onepay/response/alipay.rb
68
+ - lib/onepay/response/swiftpass.rb
69
+ - lib/version.rb
70
+ homepage: http://github.com/sundebing
71
+ licenses: []
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.11
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: onepay
93
+ test_files: []