Class: Spree::PaymentResponse

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/payment_response.rb

Overview

Lightweight value object representing a payment gateway response.

This is Spree’s native replacement for ActiveMerchant::Billing::Response. It carries the same interface so existing payment method implementations (Bogus, Check, StoreCredit, and third-party gateways) can return it without any callers needing to change.

Examples:

Successful authorization

Spree::PaymentResponse.new(true, 'Transaction approved', {},
  authorization: 'ch_abc123',
  avs_result:    { code: 'D' },
  cvv_result:    { code: 'M', message: 'Match' },
  test:          true)

Failed charge

Spree::PaymentResponse.new(false, 'Card declined', { message: 'Insufficient funds' })

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success, message, params = {}, options = {}) ⇒ PaymentResponse

Returns a new instance of PaymentResponse.

Parameters:

  • success (Boolean)

    whether the gateway action succeeded

  • message (String)

    human-readable result message

  • params (Hash) (defaults to: {})

    raw key/value pairs returned by the gateway

  • options (Hash) (defaults to: {})

    additional metadata

Options Hash (options):

  • :authorization (String)

    gateway transaction reference

  • :avs_result (Hash)

    must contain :code

  • :cvv_result (Hash)

    may contain :code and :message

  • :test (Boolean)

    sandbox/test flag



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/spree/payment_response.rb', line 46

def initialize(success, message, params = {}, options = {})
  @success = success
  @message = message
  @params = params.with_indifferent_access
  @test = options[:test] || false
  @authorization = options[:authorization]
  @avs_result = options[:avs_result] ? { 'code' => options[:avs_result][:code] } : { 'code' => nil }
  @cvv_result = if options[:cvv_result]
                  { 'code' => options[:cvv_result][:code], 'message' => options[:cvv_result][:message] }
                else
                  { 'code' => nil, 'message' => nil }
                end
end

Instance Attribute Details

#authorizationString? (readonly)

Returns gateway authorization / transaction reference.

Returns:

  • (String, nil)

    gateway authorization / transaction reference



30
31
32
# File 'app/models/spree/payment_response.rb', line 30

def authorization
  @authorization
end

#avs_resultHash (readonly)

Returns AVS (Address Verification System) result with “code” key.

Returns:

  • (Hash)

    AVS (Address Verification System) result with “code” key



33
34
35
# File 'app/models/spree/payment_response.rb', line 33

def avs_result
  @avs_result
end

#cvv_resultHash (readonly)

Returns CVV result with “code” and “message” keys.

Returns:

  • (Hash)

    CVV result with “code” and “message” keys



36
37
38
# File 'app/models/spree/payment_response.rb', line 36

def cvv_result
  @cvv_result
end

#messageString (readonly)

Returns human-readable result message.

Returns:

  • (String)

    human-readable result message



24
25
26
# File 'app/models/spree/payment_response.rb', line 24

def message
  @message
end

#paramsHashWithIndifferentAccess (readonly)

Returns raw gateway params.

Returns:

  • (HashWithIndifferentAccess)

    raw gateway params



21
22
23
# File 'app/models/spree/payment_response.rb', line 21

def params
  @params
end

#testBoolean (readonly)

Returns whether the response came from a test/sandbox environment.

Returns:

  • (Boolean)

    whether the response came from a test/sandbox environment



27
28
29
# File 'app/models/spree/payment_response.rb', line 27

def test
  @test
end

Instance Method Details

#success?Boolean

Returns whether the gateway action succeeded.

Returns:

  • (Boolean)

    whether the gateway action succeeded



61
62
63
# File 'app/models/spree/payment_response.rb', line 61

def success?
  @success
end

#test?Boolean

Returns whether the response came from a test/sandbox environment.

Returns:

  • (Boolean)

    whether the response came from a test/sandbox environment



66
67
68
# File 'app/models/spree/payment_response.rb', line 66

def test?
  @test
end