alipay-easysdk-ruby 1.0.1 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2b04a16730b90788a7d0c75911eb49750f253f1a40cf6efab4a81696a31cf9b
4
- data.tar.gz: c66dfd5f038def8e6462b1cf2faa61f2f5fd42c42b2eea9b9426977b123a3eb9
3
+ metadata.gz: e0448e76a0838f021e25e4181ca71d3f28586426a636eda913b93ba9ef221740
4
+ data.tar.gz: de78cb3ee1ba2e4643e1ee6c7b804f38d5c99fff93b92b59ea5aaced4a9a2702
5
5
  SHA512:
6
- metadata.gz: a97a34c94daf4056fe0b7d102db422bc3080c16f74f19b1e438f1e8e5993dc3288b5a552854d6e0898185f9f40609f529956250e0a363f1622a5a2cbd40593dd
7
- data.tar.gz: 28dc7cda0b59867844a27ca7871ee27cbcbf5bfc3b5908ec17c522b683e56300bd79a38f916ef87b3b275772264b715133ff344644a550e166831dc657ea2362
6
+ metadata.gz: 1f6efbf67c297f1bb264c388095e715f6774e3a6bbf555d38d1ddf4a1d28d3d3580c6843b1ec721bb8dc61644261e16c39a816695c5cb06b49026aef82ed6235
7
+ data.tar.gz: 0d8aec51df7f3f588bf58aa3255840785fbe3357e1fb36870195abe7e308098f69d8f5e50c385cfcec959a876ee71445f61efb8d257ba2043b4a29878b69edc3
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'alipay/easysdk'
4
+
5
+ # 配置 SDK
6
+ Alipay::EasySDK.configure(
7
+ protocol: 'https',
8
+ gateway_host: 'openapi-sandbox.dl.alipaydev.com',
9
+ sign_type: 'RSA2',
10
+ app_id: '9021000156667919',
11
+ merchant_private_key: 'MIIEogIBAAKCAQEAmy8gfxY5RFBKixIWoflLqULF1gZ43QdlEYln+1f0k+tbiWbhi3DBOKp9gjgGmOJmBzVuaDQ+V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J/7w7cgM16EZzBIkA3ZavdvivXWSQcPvpDGdTGgNVyZe1JKpNOI2ef19qq4b74+yjBlcoTwKBgA/vUkbAR1rgi2OMFqPQWGYArFLE03JFERgiKasm2pjzJST6vNtKnd0jnBlGigskpPUKuzsFDkBOitZaiILDpBIohv391LsLwqrGrKY5cwrm60kEshw5rnTewyDBZalWgMwc0XzE6K4mmrsYj8nGI2H9yiLRk8+iFA4Th1BafyH/AoGAcQHDXuNR0inKvsoEX1hYAoYmE7GwYcxl88AdrAnfqJHgdxHaWCKb/gYiXqRR0RFhiAEZCuVm9IvJ6Dh2eR2psC9lyixgQiyiJYLSA1sLzVzQS/OkSpC3w5srcwxOWzRTBreZbGzV+zJqh9NBCQt0S3Bq2Mi1qYxzxUAytlYSdrQ=',
12
+ alipay_public_key: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmy8gfxY5RFBKixIWoflLqULF1gZ43QdlEYln+1f0k+tbiWbhi3DBOKp9gjgGmOJmBzVuaDQ+VMDmrolIc8tOz6urQU2PSjMSSqLub3xwyxiZO2qjfkn5riO68ITkr3RN7zVyZcheoaTfexygtg7xH8eqbqhghnVs97KwKnkIt7mzhIvbs60qrV8YAkbm6Hw85s4BVBoBleFZPQCBDXffVTz72GGu5iFHcbVoZdtT2GKPWWiaYb9BEuzzmUo41pPdHrMYP0VnR9UdGnLZyC70YXQ6ZshRux0/rkIrxLGVlk5UBTqdxtP8r2oqoVLl4L5tBcTw4gRPqL4+UiDQ7YE3ZQIDAQAB',
13
+ encrypt_key: 'rEoolKE9DfJIQHMMelZapw=='
14
+ )
15
+
16
+ # 直接获取支付链接(GET 方式)
17
+ response = Alipay::EasySDK.page
18
+ .pay(
19
+ '小程序会员充值',
20
+ 'ORDER1234567890',
21
+ '19.80',
22
+ 'https://merchant.test/return'
23
+ )
24
+
25
+ if response.success?
26
+ puts '支付表单:'
27
+ puts response.form
28
+ puts
29
+ puts '支付链接:'
30
+ puts response.payment_url
31
+ else
32
+ warn "调用失败: #{response.error_message}"
33
+ end
@@ -4,23 +4,48 @@ module Alipay
4
4
  module EasySDK
5
5
  module Kernel
6
6
  class Config
7
- attr_accessor :protocol, :gateway_host, :app_id, :merchant_private_key,
8
- :alipay_public_key, :sign_type, :charset, :format, :version,
9
- :merchant_cert_sn, :alipay_root_cert_sn, :notify_url
7
+ attr_accessor :protocol,
8
+ :gateway_host,
9
+ :app_id,
10
+ :sign_type,
11
+ :alipay_public_key,
12
+ :merchant_private_key,
13
+ :merchant_cert_path,
14
+ :alipay_cert_path,
15
+ :alipay_root_cert_path,
16
+ :merchant_cert_sn,
17
+ :alipay_cert_sn,
18
+ :alipay_root_cert_sn,
19
+ :notify_url,
20
+ :encrypt_key,
21
+ :http_proxy,
22
+ :ignore_ssl,
23
+ :charset,
24
+ :format,
25
+ :version
10
26
 
11
27
  def initialize(options = {})
12
- @protocol = options[:protocol] || 'https'
13
- @gateway_host = options[:gateway_host] || 'openapi.alipay.com/gateway.do'
14
- @app_id = options[:app_id]
15
- @merchant_private_key = options[:merchant_private_key]
16
- @alipay_public_key = options[:alipay_public_key]
17
- @sign_type = options[:sign_type] || AlipayConstants::DEFAULT_SIGN_TYPE
18
- @charset = options[:charset] || AlipayConstants::DEFAULT_CHARSET
19
- @format = options[:format] || AlipayConstants::DEFAULT_FORMAT
20
- @version = options[:version] || AlipayConstants::DEFAULT_VERSION
21
- @merchant_cert_sn = options[:merchant_cert_sn]
22
- @alipay_root_cert_sn = options[:alipay_root_cert_sn]
23
- @notify_url = options[:notify_url]
28
+ opts = options || {}
29
+
30
+ @protocol = fetch_option(opts, :protocol, 'protocol') || 'https'
31
+ @gateway_host = fetch_option(opts, :gateway_host, 'gateway_host', :gatewayHost, 'gatewayHost') || 'openapi.alipay.com/gateway.do'
32
+ @app_id = fetch_option(opts, :app_id, 'app_id', :appId, 'appId')
33
+ @sign_type = fetch_option(opts, :sign_type, 'sign_type', :signType, 'signType') || AlipayConstants::DEFAULT_SIGN_TYPE
34
+ @alipay_public_key = fetch_option(opts, :alipay_public_key, 'alipay_public_key', :alipayPublicKey, 'alipayPublicKey')
35
+ @merchant_private_key = fetch_option(opts, :merchant_private_key, 'merchant_private_key', :merchantPrivateKey, 'merchantPrivateKey')
36
+ @merchant_cert_path = fetch_option(opts, :merchant_cert_path, 'merchant_cert_path', :merchantCertPath, 'merchantCertPath')
37
+ @alipay_cert_path = fetch_option(opts, :alipay_cert_path, 'alipay_cert_path', :alipayCertPath, 'alipayCertPath')
38
+ @alipay_root_cert_path = fetch_option(opts, :alipay_root_cert_path, 'alipay_root_cert_path', :alipayRootCertPath, 'alipayRootCertPath')
39
+ @merchant_cert_sn = fetch_option(opts, :merchant_cert_sn, 'merchant_cert_sn', :merchantCertSN, 'merchantCertSN')
40
+ @alipay_cert_sn = fetch_option(opts, :alipay_cert_sn, 'alipay_cert_sn', :alipayCertSN, 'alipayCertSN')
41
+ @alipay_root_cert_sn = fetch_option(opts, :alipay_root_cert_sn, 'alipay_root_cert_sn', :alipayRootCertSN, 'alipayRootCertSN')
42
+ @notify_url = fetch_option(opts, :notify_url, 'notify_url', :notifyUrl, 'notifyUrl')
43
+ @encrypt_key = fetch_option(opts, :encrypt_key, 'encrypt_key', :encryptKey, 'encryptKey')
44
+ @http_proxy = fetch_option(opts, :http_proxy, 'http_proxy', :httpProxy, 'httpProxy')
45
+ @ignore_ssl = fetch_option(opts, :ignore_ssl, 'ignore_ssl', :ignoreSSL, 'ignoreSSL')
46
+ @charset = fetch_option(opts, :charset, 'charset') || AlipayConstants::DEFAULT_CHARSET
47
+ @format = fetch_option(opts, :format, 'format') || AlipayConstants::DEFAULT_FORMAT
48
+ @version = fetch_option(opts, :version, 'version') || AlipayConstants::DEFAULT_VERSION
24
49
  end
25
50
 
26
51
  def gateway_url
@@ -32,6 +57,25 @@ module Alipay
32
57
  raise "merchant_private_key is required" if @merchant_private_key.nil? || @merchant_private_key.empty?
33
58
  raise "alipay_public_key is required" if @alipay_public_key.nil? || @alipay_public_key.empty?
34
59
  end
60
+
61
+ private
62
+
63
+ def fetch_option(options, *keys)
64
+ keys.each do |key|
65
+ if key.is_a?(String)
66
+ symbol_key = key.tr('-', '_').to_sym
67
+ return options[key] if options.key?(key)
68
+ return options[symbol_key] if options.key?(symbol_key)
69
+ elsif key.is_a?(Symbol)
70
+ string_key = key.to_s
71
+ camel_case_key = string_key.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
72
+ return options[key] if options.key?(key)
73
+ return options[string_key] if options.key?(string_key)
74
+ return options[camel_case_key] if options.key?(camel_case_key)
75
+ end
76
+ end
77
+ nil
78
+ end
35
79
  end
36
80
  end
37
81
  end
@@ -6,6 +6,7 @@ require_relative 'util/sign_content_extractor'
6
6
  require 'net/http'
7
7
  require 'uri'
8
8
  require 'cgi'
9
+ require 'json'
9
10
 
10
11
  module Alipay
11
12
  module EasySDK
@@ -38,28 +39,47 @@ module Alipay
38
39
  end
39
40
 
40
41
  def get_config(key)
41
- case key
42
- when 'protocol'
43
- @config.protocol
44
- when 'gatewayHost'
45
- @config.gateway_host
46
- when 'appId'
47
- @config.app_id
48
- when 'merchantPrivateKey'
49
- @config.merchant_private_key
50
- when 'alipayPublicKey'
51
- @config.alipay_public_key
52
- when 'signType'
53
- @config.sign_type
54
- when 'charset'
55
- @config.charset
56
- when 'format'
57
- @config.format
58
- when 'version'
59
- @config.version
60
- else
61
- nil
62
- end
42
+ lookup = {
43
+ 'protocol' => :protocol,
44
+ 'gatewayHost' => :gateway_host,
45
+ 'gateway_host' => :gateway_host,
46
+ 'appId' => :app_id,
47
+ 'app_id' => :app_id,
48
+ 'merchantPrivateKey' => :merchant_private_key,
49
+ 'merchant_private_key' => :merchant_private_key,
50
+ 'alipayPublicKey' => :alipay_public_key,
51
+ 'alipay_public_key' => :alipay_public_key,
52
+ 'signType' => :sign_type,
53
+ 'sign_type' => :sign_type,
54
+ 'charset' => :charset,
55
+ 'format' => :format,
56
+ 'version' => :version,
57
+ 'notifyUrl' => :notify_url,
58
+ 'notify_url' => :notify_url,
59
+ 'encryptKey' => :encrypt_key,
60
+ 'encrypt_key' => :encrypt_key,
61
+ 'httpProxy' => :http_proxy,
62
+ 'http_proxy' => :http_proxy,
63
+ 'ignoreSSL' => :ignore_ssl,
64
+ 'ignore_ssl' => :ignore_ssl,
65
+ 'merchantCertPath' => :merchant_cert_path,
66
+ 'merchant_cert_path' => :merchant_cert_path,
67
+ 'alipayCertPath' => :alipay_cert_path,
68
+ 'alipay_cert_path' => :alipay_cert_path,
69
+ 'alipayRootCertPath' => :alipay_root_cert_path,
70
+ 'alipay_root_cert_path' => :alipay_root_cert_path,
71
+ 'merchantCertSN' => :merchant_cert_sn,
72
+ 'merchant_cert_sn' => :merchant_cert_sn,
73
+ 'alipayCertSN' => :alipay_cert_sn,
74
+ 'alipay_cert_sn' => :alipay_cert_sn,
75
+ 'alipayRootCertSN' => :alipay_root_cert_sn,
76
+ 'alipay_root_cert_sn' => :alipay_root_cert_sn
77
+ }
78
+
79
+ method = lookup[key]
80
+ return unless method
81
+
82
+ @config.public_send(method)
63
83
  end
64
84
 
65
85
  def get_sdk_version
@@ -220,7 +240,7 @@ module Alipay
220
240
  if @biz_params != nil
221
241
  biz_params = json.to_json_string(@biz_params)
222
242
  end
223
- sorted_map = system_params || {}
243
+ sorted_map = (system_params || {}).dup
224
244
  if !biz_params.nil? && !biz_params.empty?
225
245
  # 模拟PHP json_encode($bizParams, JSON_UNESCAPED_UNICODE)
226
246
  json_string = JSON.generate(JSON.parse(biz_params)).gsub('/', "\\/")
@@ -1,5 +1,6 @@
1
1
  require_relative 'alipay_constants'
2
2
  require_relative 'config'
3
+ require_relative 'cert_environment'
3
4
  require_relative 'easy_sdk_kernel'
4
5
  require_relative '../payment/wap/client'
5
6
  require_relative '../payment/page/client'
@@ -56,11 +57,12 @@ module Alipay
56
57
 
57
58
  def normalize_config(options)
58
59
  raise ArgumentError, '配置参数不能为空' if options.nil?
59
- return options if options.is_a?(Config)
60
- Config.new(options)
60
+ options.is_a?(Config) ? options : Config.new(options)
61
61
  end
62
62
 
63
63
  def initialize_context(config)
64
+ apply_cert_environment(config)
65
+ config.validate
64
66
  @config = config
65
67
  @kernel = EasySDKKernel.new(config)
66
68
  @payment = Payment.new(@kernel)
@@ -70,6 +72,16 @@ module Alipay
70
72
  def ensure_context_set!
71
73
  raise ConfigurationNotSetError, '请先调用Factory.set_options(config)设置SDK配置' unless @config
72
74
  end
75
+
76
+ def apply_cert_environment(config)
77
+ return if config.alipay_cert_path.nil? || config.alipay_cert_path.to_s.strip.empty?
78
+
79
+ cert_env = CertEnvironment.new
80
+ cert_env.setup(config.merchant_cert_path, config.alipay_cert_path, config.alipay_root_cert_path)
81
+ config.merchant_cert_sn = cert_env.merchant_cert_sn
82
+ config.alipay_root_cert_sn = cert_env.root_cert_sn
83
+ config.alipay_public_key ||= cert_env.cached_alipay_public_key
84
+ end
73
85
  end
74
86
 
75
87
  class Payment
@@ -1,6 +1,7 @@
1
1
  require 'net/http'
2
2
  require 'uri'
3
3
  require 'cgi'
4
+ require 'openssl'
4
5
 
5
6
  require_relative '../../kernel/easy_sdk_kernel'
6
7
  require_relative 'models/alipay_trade_create_response'
@@ -163,9 +164,9 @@ module Alipay
163
164
  end
164
165
 
165
166
  def perform_http_request(uri, request)
166
- http = Net::HTTP.new(uri.host, uri.port)
167
+ http = build_http_client(uri)
167
168
  http.use_ssl = uri.scheme == 'https'
168
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
169
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl? && ignore_ssl?
169
170
  http.open_timeout = 15
170
171
  http.read_timeout = 15
171
172
  http.request(request)
@@ -183,6 +184,32 @@ module Alipay
183
184
  @kernel.optional_text_params.clear if @kernel.optional_text_params
184
185
  @kernel.optional_biz_params.clear if @kernel.optional_biz_params
185
186
  end
187
+
188
+ def build_http_client(uri)
189
+ proxy_settings = http_proxy_settings
190
+ if proxy_settings
191
+ Net::HTTP.new(uri.host, uri.port, *proxy_settings)
192
+ else
193
+ Net::HTTP.new(uri.host, uri.port)
194
+ end
195
+ end
196
+
197
+ def http_proxy_settings
198
+ raw_proxy = @kernel.get_config('httpProxy')
199
+ return nil if raw_proxy.nil? || raw_proxy.to_s.strip.empty?
200
+
201
+ proxy_uri = raw_proxy =~ %r{^https?://} ? URI(raw_proxy) : URI("http://#{raw_proxy}")
202
+ [proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password]
203
+ rescue URI::InvalidURIError
204
+ nil
205
+ end
206
+
207
+ def ignore_ssl?
208
+ value = @kernel.get_config('ignoreSSL')
209
+ return false if value.nil?
210
+
211
+ value == true || value.to_s.strip.downcase == 'true'
212
+ end
186
213
  end
187
214
  end
188
215
  end
@@ -1,5 +1,5 @@
1
1
  module Alipay
2
2
  module EasySDK
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alipay-easysdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alipay EasySDK Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-10-27 00:00:00.000000000 Z
11
+ date: 2025-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ files:
76
76
  - README.md
77
77
  - Rakefile
78
78
  - examples/demo.rb
79
+ - examples/demo_with_get.rb
79
80
  - examples/demo_with_post.rb
80
81
  - lib/alipay/easysdk.rb
81
82
  - lib/alipay/easysdk/kernel/alipay_constants.rb