Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Webpay: Add authorize & capture
Browse files Browse the repository at this point in the history
  • Loading branch information
keikubo authored and ntalbott committed Dec 11, 2013
1 parent 8dba9d3 commit 78d93c9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 24 additions & 6 deletions lib/active_merchant/billing/gateways/webpay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -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 [email protected] if you continue to receive this message.'
msg += " (The raw response returned by the API was #{raw_response.inspect})"
Expand Down
18 changes: 18 additions & 0 deletions test/remote/gateways/remote_webpay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 93 additions & 1 deletion test/unit/gateways/webpay_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'test_helper'

class WebpayTest < Test::Unit::TestCase
include CommStub

def setup
@gateway = WebpayGateway.new(:login => 'login')

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 78d93c9

Please sign in to comment.