class ActiveMerchant::Billing::CheckoutV2

Constants

STANDARD_ERROR_CODE_MAPPING

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/active_merchant/billing/checkout_v2.rb, line 22
def initialize(options={})
  requires!(options, :secret_key)
  super
end

Public Instance Methods

authorize(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 34
def authorize(amount, payment_method, options={})
  post = {}
  post[:autoCapture] = "n"
  add_invoice(post, amount, options)

  if payment_method.is_a?(String) && payment_method.include?('card_tok_')
    post[:cardToken] = payment_method
    post[:email] = options[:email]

    commit(:authorize_token, post)
  else
    add_payment_method(post, payment_method)
    add_customer_data(post, options)

    commit(:authorize, post)
  end
end
capture(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 52
def capture(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)
  add_customer_data(post, options)

  commit(:capture, post, authorization)
end
purchase(amount, payment_method, options={}) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 27
def purchase(amount, payment_method, options={})
  MultiResponse.run do |r|
    r.process { authorize(amount, payment_method, options) }
    r.process { capture(amount, r.authorization, options) }
  end
end
refund(amount, authorization, options={}) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 65
def refund(amount, authorization, options={})
  post = {}
  add_invoice(post, amount, options)
  add_customer_data(post, options)

  commit(:refund, post, authorization)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 84
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: )[^\\]*)i, '\1[FILTERED]').
    gsub(%r(("number\\":\\")\d+), '\1[FILTERED]').
    gsub(%r(("cvv\\":\\")\d+), '\1[FILTERED]')
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 80
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 73
def verify(credit_card, options={})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(100, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options={}) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 60
def void(authorization, options={})
  post = {}
  commit(:void, post, authorization)
end

Private Instance Methods

add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 108
def add_customer_data(post, options)
  post[:email] = options[:email] || "unspecified@example.com"
  address = options[:billing_address]
  if(address && post[:card])
    post[:card][:billingDetails] = {}
    post[:card][:billingDetails][:address1] = address[:address1]
    post[:card][:billingDetails][:address2] = address[:address2]
    post[:card][:billingDetails][:city] = address[:city]
    post[:card][:billingDetails][:state] = address[:state]
    post[:card][:billingDetails][:country] = address[:country]
    post[:card][:billingDetails][:postcode] = address[:zip]
    post[:card][:billingDetails][:phone] = { number: address[:phone] } unless address[:phone].blank?
  end
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 93
def add_invoice(post, money, options)
  post[:value] = amount(money)
  post[:trackId] = options[:order_id]
  post[:currency] = options[:currency] || currency(money)
end
add_payment_method(post, payment_method) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 99
def add_payment_method(post, payment_method)
  post[:card] = {}
  post[:card][:name] = payment_method.name
  post[:card][:number] = payment_method.number
  post[:card][:cvv] = payment_method.verification_value
  post[:card][:expiryYear] = format(payment_method.year, :four_digits)
  post[:card][:expiryMonth] = format(payment_method.month, :two_digits)
end
authorization_from(raw) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 209
def authorization_from(raw)
  raw["id"]
end
avs_result(action, response) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 165
def avs_result(action, response)
  action == :purchase ? AVSResult.new(code: response["card"]["avsCheck"]) : nil
end
base_url() click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 161
def base_url
  test? ? test_url : live_url
end
commit(action, post, authorization = nil) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 123
def commit(action, post, authorization = nil)
  begin
    raw_response = ssl_post(url(post, action, authorization), post.to_json, headers)
    response = parse(raw_response)
  rescue ResponseError => e
    raise unless(e.response.code.to_s =~ /4\d\d/)
    response = parse(e.response.body)
  end

  succeeded = success_from(response)
  Response.new(
    succeeded,
    message_from(succeeded, response),
    response,
    authorization: authorization_from(response),
    error_code: error_code_from(succeeded, response),
    test: test?,
    avs_result: avs_result(action, response),
    cvv_result: cvv_result(action, response))
end
cvv_result(action, response) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 169
def cvv_result(action, response)
  action == :purchase ? CVVResult.new(response["card"]["cvvCheck"]) : nil
end
error_code_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 213
def error_code_from(succeeded, response)
  succeeded ? nil : STANDARD_ERROR_CODE_MAPPING[response["responseCode"]]
end
headers() click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 144
def headers
  {
    "Authorization" => @options[:secret_key],
    "Content-Type"  => "application/json;charset=UTF-8"
  }
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 186
def message_from(succeeded, response)
  if succeeded
    "Succeeded"
  elsif response["errors"]
    response["message"] + ": " + response["errors"].first
  else
    response["responseMessage"] || response["message"] || "Unable to read error message"
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 173
def parse(body)
  JSON.parse(body)
  rescue JSON::ParserError
    {
      "message" => "Invalid JSON response received from CheckoutV2Gateway. Please contact CheckoutV2Gateway if you continue to receive this message.",
      "raw_response" => scrub(body)
    }
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 182
def success_from(response)
  response["responseCode"] == ("10000" || "10100")
end
url(post, action, authorization) click to toggle source
# File lib/active_merchant/billing/checkout_v2.rb, line 151
def url(post, action, authorization)
  if action == :authorize
    "#{base_url}/charges/card"
  elsif action == :authorize_token
    "#{base_url}/charges/token"
  else
    "#{base_url}/charges/#{authorization}/#{action}"
  end
end