From 78d93c9b2a0fcd665b3909fded0da79b47f22af3 Mon Sep 17 00:00:00 2001 From: Kei Kubo Date: Fri, 6 Dec 2013 20:49:37 +0900 Subject: [PATCH] Webpay: Add authorize & capture Closes #942. --- CHANGELOG | 1 + .../billing/gateways/webpay.rb | 30 ++++-- test/remote/gateways/remote_webpay_test.rb | 18 ++++ test/unit/gateways/webpay_test.rb | 94 ++++++++++++++++++- 4 files changed, 136 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8298b8c213c..b8d071a6f55 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,7 @@ * USA ePay Advanced: Fix capture amount [nearapogee] * NAB Transact: Fix merchant descriptor with capture/refund requests [nagash] * Braintree Blue: Add custom_fields & device_data parameters [parallel588] +* Webpay: Add authorize & capture [keikubo] == Version 1.42.2 (November 13th, 2013) diff --git a/lib/active_merchant/billing/gateways/webpay.rb b/lib/active_merchant/billing/gateways/webpay.rb index 999a32e5b45..de8d6e484dc 100644 --- a/lib/active_merchant/billing/gateways/webpay.rb +++ b/lib/active_merchant/billing/gateways/webpay.rb @@ -13,12 +13,10 @@ class WebpayGateway < StripeGateway self.homepage_url = 'https://webpay.jp/' self.display_name = 'WebPay' - def authorize(money, credit_card, options = {}) - raise NotImplementedError.new - end - - def capture(money, credit_card, options = {}) - raise NotImplementedError.new + def capture(money, authorization, options = {}) + post = {:amount => localized_amount(money)} + add_application_fee(post, options) + commit(:post, "charges/#{CGI.escape(authorization)}/capture", post) end def refund(money, identification, options = {}) @@ -52,6 +50,26 @@ def add_customer(post, creditcard, options) post[:customer] = options[:customer] if options[:customer] && !creditcard.respond_to?(:number) end + def store(creditcard, options = {}) + post = {} + add_creditcard(post, creditcard, options) + post[:description] = options[:description] + post[:email] = options[:email] + + commit_options = generate_options(options) + if options[:customer] + MultiResponse.run(:first) do |r| + r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/", post, commit_options) } + + return r unless options[:set_default] and r.success? and !r.params["id"].blank? + + r.process { update_customer(options[:customer], :default_card => r.params["id"]) } + end + else + commit(:post, 'customers', post, commit_options) + end + end + def json_error(raw_response) msg = 'Invalid response received from the WebPay API. Please contact support@webpay.jp if you continue to receive this message.' msg += " (The raw response returned by the API was #{raw_response.inspect})" diff --git a/test/remote/gateways/remote_webpay_test.rb b/test/remote/gateways/remote_webpay_test.rb index 3aa0edf27ea..27645565d6a 100644 --- a/test/remote/gateways/remote_webpay_test.rb +++ b/test/remote/gateways/remote_webpay_test.rb @@ -48,6 +48,24 @@ def test_unsuccessful_purchase assert_equal 'Your card number is incorrect', response.message end + def test_authorization_and_capture + assert authorization = @gateway.authorize(@amount, @credit_card, @options) + assert_success authorization + assert !authorization.params["captured"] + + assert capture = @gateway.capture(@amount, authorization.authorization) + assert_success capture + end + + def test_authorization_and_void + assert authorization = @gateway.authorize(@amount, @credit_card, @options) + assert_success authorization + assert !authorization.params["captured"] + + assert void = @gateway.void(authorization.authorization) + assert_success void + end + def test_successful_void assert response = @gateway.purchase(@amount, @credit_card, @options) assert_success response diff --git a/test/unit/gateways/webpay_test.rb b/test/unit/gateways/webpay_test.rb index 98fc8749852..87a08584fe7 100644 --- a/test/unit/gateways/webpay_test.rb +++ b/test/unit/gateways/webpay_test.rb @@ -1,6 +1,8 @@ require 'test_helper' class WebpayTest < Test::Unit::TestCase + include CommStub + def setup @gateway = WebpayGateway.new(:login => 'login') @@ -14,6 +16,25 @@ def setup } end + def test_successful_authorization + @gateway.expects(:ssl_request).returns(successful_authorization_response) + + assert response = @gateway.authorize(@amount, @credit_card, @options) + assert_instance_of Response, response + assert_success response + + assert_equal 'ch_test_charge', response.authorization + assert response.test? + end + + def test_successful_capture + @gateway.expects(:ssl_request).returns(successful_capture_response) + + assert response = @gateway.capture(@amount, "ch_test_charge") + assert_success response + assert response.test? + end + def test_successful_purchase @gateway.expects(:ssl_request).returns(successful_purchase_response) @@ -26,7 +47,7 @@ def test_successful_purchase assert response.test? end - def test_appropiate_purchase_amount + def test_appropriate_purchase_amount @gateway.expects(:ssl_request).returns(successful_purchase_response) response = @gateway.purchase(@amount, @credit_card, @options) @@ -36,6 +57,17 @@ def test_appropiate_purchase_amount assert_equal @amount / 100, response.params["amount"] end + def test_successful_purchase_with_token + response = stub_comms(@gateway, :ssl_request) do + @gateway.purchase(@amount, "tok_xxx") + end.check_request do |method, endpoint, data, headers| + assert_match(/card=tok_xxx/, data) + end.respond_with(successful_purchase_response) + + assert response + assert_instance_of Response, response + assert_success response + end def test_successful_void @gateway.expects(:ssl_request).returns(successful_purchase_response(true)) @@ -134,6 +166,66 @@ def test_metadata_header private + def successful_authorization_response + <<-RESPONSE +{ + "id": "ch_test_charge", + "object": "charge", + "created": 1309131571, + "livemode": false, + "paid": true, + "amount": 40000, + "currency": "jpy", + "refunded": false, + "fee": 0, + "fee_details": [], + "card": { + "country": "JP", + "exp_month": 9, + "exp_year": #{Time.now.year + 1}, + "last4": "4242", + "object": "card", + "type": "Visa" + }, + "captured": false, + "description": "ActiveMerchant Test Purchase", + "dispute": null, + "uncaptured": true, + "disputed": false +} + RESPONSE + end + + def successful_capture_response + <<-RESPONSE +{ + "id": "ch_test_charge", + "object": "charge", + "created": 1309131571, + "livemode": false, + "paid": true, + "amount": 40000, + "currency": "jpy", + "refunded": false, + "fee": 0, + "fee_details": [], + "card": { + "country": "JP", + "exp_month": 9, + "exp_year": #{Time.now.year + 1}, + "last4": "4242", + "object": "card", + "type": "Visa" + }, + "captured": true, + "description": "ActiveMerchant Test Purchase", + "dispute": null, + "uncaptured": false, + "disputed": false +} + RESPONSE + end + # Place raw successful response from gateway here def successful_purchase_response(refunded=false) <<-RESPONSE