Skip to content

Commit

Permalink
refine & rewrite test
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhj committed Jul 10, 2015
1 parent 39e31c9 commit bd14db2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/wx_pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module WxPay
class<< self
attr_accessor :appid, :mch_id, :key, :apiclient_cert_file
attr_accessor :appid, :mch_id, :key, :apiclient_cert_path

def extra_rest_client_options=(options)
@rest_client_options = options
Expand All @@ -13,5 +13,9 @@ def extra_rest_client_options=(options)
def extra_rest_client_options
@rest_client_options || {}
end

def apiclient_cert
@apiclient_cert ||= OpenSSL::PKCS12.new(WxPay.apiclient_cert_path, WxPay.mch_id)
end
end
end
7 changes: 2 additions & 5 deletions lib/wx_pay/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ def self.invoke_refund(params)
# 微信退款需要双向证书
# https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
# https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3

p12_file = File.read WxPay.apiclient_cert_file
cert = OpenSSL::PKCS12.new(p12_file, WxPay.mch_id)

WxPay.extra_rest_client_options = {
ssl_client_cert: cert.certificate,
ssl_client_key: cert.key,
ssl_client_cert: WxPay.apiclient_cert.certificate,
ssl_client_key: WxPay.apiclient_cert.key,
verify_ssl: OpenSSL::SSL::VERIFY_NONE
}

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
WxPay.appid = 'wxd930ea5d5a258f4f'
WxPay.key = '8934e7d15453e97507ef794cf7b0519d'
WxPay.mch_id = '1900000109'
WxPay.apiclient_cert_path = '/path/to/your/cert/file.p12'
18 changes: 11 additions & 7 deletions test/wx_pay/service_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@

class ServiceTest < MiniTest::Test
def setup
WxPay.appid = 'xxxxxxxxxxxxxx'
WxPay.key = 'xxxxxxxxxxxxxxx'
WxPay.mch_id = 'xxxxxxxxxxxxxx'
WxPay.apiclient_cert_file = '/Users/jimmy/Workspace/shopshow-ultron/public/apiclient_cert.p12'

@params = {
transaction_id: '1217752501201407033233368018',
op_user_id: '10000100',
Expand All @@ -13,9 +9,15 @@ def setup
refund_fee: 1,
total_fee: 1
}

@apiclient_cert = Minitest::Mock.new
@apiclient_cert.expect(:certificate, 'certificate')
@apiclient_cert.expect(:key, 'key')
end

def test_invoke_refund


response_body = <<-EOF
<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
Expand All @@ -41,7 +43,9 @@ def test_invoke_refund
body: response_body
)

r = WxPay::Service.invoke_refund(@params)
assert_equal r.success?, true
WxPay.stub :apiclient_cert, @apiclient_cert do
r = WxPay::Service.invoke_refund(@params)
assert_equal r.success?, true
end
end
end

0 comments on commit bd14db2

Please sign in to comment.