wx_pay 0.0.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c46a29e5c22d6e86f78da2d7c92fba8204ae67c
4
- data.tar.gz: 9246f29160dba30f1f990b8a278a666e1f0d6acb
3
+ metadata.gz: f60e9ab955e8797883c4befead6e4de2a0f65d0e
4
+ data.tar.gz: 7a299e0d07bf2eb5f3b8c899e73d1d93805d3ecf
5
5
  SHA512:
6
- metadata.gz: 0d28f442024679881952cc57e3c1ddc3151eee0344396c3294b71e04e01db659cf1e9427f8f40c03f915917aef66cce2bef52e0e8dac5f3d85dc29f91f9de6c1
7
- data.tar.gz: 75091fb1b3df4e26720c380c988573adf6b6f5e4f1c9839e30bdee3b61fd9b0de9b6a8a060f8f47e6c2670ff8aa7ba598bfe294bbe3f8c944ca88cd2625d14ad
6
+ metadata.gz: 76b692ac8affd84e344736645d44742af351540dac7ac56cb94feed71c939d3c80b997c1d41078756612e9da4ccdf1d8cfc39fb7d83c051a2efb36cd68ba0afa
7
+ data.tar.gz: e435dbdf3d63f7245556d55e8b55443915716289c4f0fa096192ad1fb91c13a5c819149b4fcd2502b5dfdd3702dcedab38c0d6656a361ad678453e2ae73c2b95
data/lib/wx_pay.rb CHANGED
@@ -4,7 +4,7 @@ require 'wx_pay/service'
4
4
 
5
5
  module WxPay
6
6
  class<< self
7
- attr_accessor :appid, :mch_id, :key
7
+ attr_accessor :appid, :mch_id, :key, :apiclient_cert_path
8
8
 
9
9
  def extra_rest_client_options=(options)
10
10
  @rest_client_options = options
@@ -13,5 +13,9 @@ module WxPay
13
13
  def extra_rest_client_options
14
14
  @rest_client_options || {}
15
15
  end
16
+
17
+ def apiclient_cert
18
+ @apiclient_cert ||= OpenSSL::PKCS12.new(WxPay.apiclient_cert_path, WxPay.mch_id)
19
+ end
16
20
  end
17
21
  end
@@ -3,7 +3,7 @@ require 'active_support/core_ext/hash/conversions'
3
3
 
4
4
  module WxPay
5
5
  module Service
6
- GATEWAY_URL = 'https://api.mch.weixin.qq.com/pay'
6
+ GATEWAY_URL = 'https://api.mch.weixin.qq.com'
7
7
 
8
8
  INVOKE_UNIFIEDORDER_REQUIRED_FIELDS = %i(body out_trade_no total_fee spbill_create_ip notify_url trade_type)
9
9
  def self.invoke_unifiedorder(params)
@@ -15,7 +15,7 @@ module WxPay
15
15
 
16
16
  check_required_options(params, INVOKE_UNIFIEDORDER_REQUIRED_FIELDS)
17
17
 
18
- r = invoke_remote("#{GATEWAY_URL}/unifiedorder", make_payload(params))
18
+ r = invoke_remote("#{GATEWAY_URL}/pay/unifiedorder", make_payload(params))
19
19
 
20
20
  yield r if block_given?
21
21
 
@@ -38,6 +38,35 @@ module WxPay
38
38
  params
39
39
  end
40
40
 
41
+ INVOKE_REFUND_REQUIRED_FIELDS = %i(transaction_id out_trade_no out_refund_no total_fee refund_fee)
42
+ def self.invoke_refund(params)
43
+ params = {
44
+ appid: WxPay.appid,
45
+ mch_id: WxPay.mch_id,
46
+ nonce_str: SecureRandom.uuid.tr('-', ''),
47
+ op_user_id: WxPay.mch_id
48
+ }.merge(params)
49
+
50
+ check_required_options(params, INVOKE_REFUND_REQUIRED_FIELDS)
51
+
52
+ # 微信退款需要双向证书
53
+ # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
54
+ # https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
55
+
56
+ WxPay.extra_rest_client_options = {
57
+ ssl_client_cert: WxPay.apiclient_cert.certificate,
58
+ ssl_client_key: WxPay.apiclient_cert.key,
59
+ verify_ssl: OpenSSL::SSL::VERIFY_NONE
60
+ }
61
+
62
+ r = invoke_remote "#{GATEWAY_URL}/secapi/pay/refund", make_payload(params)
63
+
64
+ yield(r) if block_given?
65
+
66
+ r
67
+ end
68
+
69
+
41
70
  private
42
71
 
43
72
  def self.check_required_options(options, names)
@@ -1,3 +1,3 @@
1
1
  module WxPay
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/test/test_helper.rb CHANGED
@@ -6,3 +6,4 @@ require 'fakeweb'
6
6
  WxPay.appid = 'wxd930ea5d5a258f4f'
7
7
  WxPay.key = '8934e7d15453e97507ef794cf7b0519d'
8
8
  WxPay.mch_id = '1900000109'
9
+ WxPay.apiclient_cert_path = '/path/to/your/cert/file.p12'
@@ -0,0 +1,51 @@
1
+
2
+ class ServiceTest < MiniTest::Test
3
+ def setup
4
+ @params = {
5
+ transaction_id: '1217752501201407033233368018',
6
+ op_user_id: '10000100',
7
+ out_refund_no: '1415701182',
8
+ out_trade_no: '1415757673',
9
+ refund_fee: 1,
10
+ total_fee: 1
11
+ }
12
+
13
+ @apiclient_cert = Minitest::Mock.new
14
+ @apiclient_cert.expect(:certificate, 'certificate')
15
+ @apiclient_cert.expect(:key, 'key')
16
+ end
17
+
18
+ def test_invoke_refund
19
+
20
+
21
+ response_body = <<-EOF
22
+ <xml>
23
+ <return_code><![CDATA[SUCCESS]]></return_code>
24
+ <return_msg><![CDATA[OK]]></return_msg>
25
+ <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
26
+ <mch_id><![CDATA[10000100]]></mch_id>
27
+ <nonce_str><![CDATA[NfsMFbUFpdbEhPXP]]></nonce_str>
28
+ <sign><![CDATA[B7274EB9F8925EB93100DD2085FA56C0]]></sign>
29
+ <result_code><![CDATA[SUCCESS]]></result_code>
30
+ <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
31
+ <out_trade_no><![CDATA[1415757673]]></out_trade_no>
32
+ <out_refund_no><![CDATA[1415701182]]></out_refund_no>
33
+ <refund_id><![CDATA[2008450740201411110000174436]]></refund_id>
34
+ <refund_channel><![CDATA[]]></refund_channel>
35
+ <refund_fee>1</refund_fee>
36
+ <coupon_refund_fee>0</coupon_refund_fee>
37
+ </xml>
38
+ EOF
39
+
40
+ FakeWeb.register_uri(
41
+ :post,
42
+ %r|https://api\.mch\.weixin\.qq\.com*|,
43
+ body: response_body
44
+ )
45
+
46
+ WxPay.stub :apiclient_cert, @apiclient_cert do
47
+ r = WxPay::Service.invoke_refund(@params)
48
+ assert_equal r.success?, true
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wx_pay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '4'
33
+ version: '3.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '4'
40
+ version: '3.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +110,7 @@ files:
110
110
  - lib/wx_pay/version.rb
111
111
  - test/test_helper.rb
112
112
  - test/wx_pay/result_test.rb
113
+ - test/wx_pay/service_test.rb
113
114
  - test/wx_pay/sign_test.rb
114
115
  homepage: https://github.com/jasl/wx_pay
115
116
  licenses:
@@ -138,5 +139,6 @@ summary: An unofficial simple wechat pay gem
138
139
  test_files:
139
140
  - test/test_helper.rb
140
141
  - test/wx_pay/result_test.rb
142
+ - test/wx_pay/service_test.rb
141
143
  - test/wx_pay/sign_test.rb
142
144
  has_rdoc: