Class: Spree::Kashflow::CustomerPayload

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/spree/kashflow/customer_payload.rb

Overview

Maps a Spree order's billing details onto the shape KashFlow's Customer complex type expects. Pure object: reads the order and its associations, performs no network calls, and writes nothing back to Spree.

Constant Summary collapse

EU_COUNTRY_CODES =

Returns ISO 3166-1 alpha-2 codes of EU member states, used to derive the EC VAT-treatment flag. The United Kingdom is deliberately absent: post-Brexit it is neither EU nor "outside" for KashFlow's purposes.

Returns:

  • (Array<String>)

    ISO 3166-1 alpha-2 codes of EU member states, used to derive the EC VAT-treatment flag. The United Kingdom is deliberately absent: post-Brexit it is neither EU nor "outside" for KashFlow's purposes.

%w[
  AT BE BG HR CY CZ DK EE FI FR DE GR HU IE IT LV LT LU MT NL PL PT RO SK SI ES SE
].freeze
UNITED_KINGDOM_CODE =

Returns the ISO 3166-1 alpha-2 code KashFlow treats as the United Kingdom.

Returns:

  • (String)

    the ISO 3166-1 alpha-2 code KashFlow treats as the United Kingdom

"GB"
VAT_NUMBER_METADATA_KEY =

Returns the order metadata key a host application must write for VATNumber to be included in the payload. Spree has no dedicated VAT number column, so this gem reads it out of order.metadata under this key.

Returns:

  • (String)

    the order metadata key a host application must write for VATNumber to be included in the payload. Spree has no dedicated VAT number column, so this gem reads it out of order.metadata under this key.

"vat_number"
USER_CODE_FORMAT =

Returns format for a registered user's code, keyed on the Spree user id so it survives the customer changing their email address.

Returns:

  • (String)

    format for a registered user's code, keyed on the Spree user id so it survives the customer changing their email address.

"SPU%d"
GUEST_CODE_FORMAT =

Returns format for a guest's code. Guests have no stable id, so the normalised email is hashed — same email in, same code out, which is what keeps the upsert an update rather than a duplicate.

Returns:

  • (String)

    format for a guest's code. Guests have no stable id, so the normalised email is hashed — same email in, same code out, which is what keeps the upsert an update rather than a duplicate.

"SPG%s"
GUEST_CODE_DIGEST_LENGTH =

Returns hex characters of the email digest kept in a guest code. 12 hex chars is 48 bits: collision-safe at any plausible customer count while leaving the total code short.

Returns:

  • (Integer)

    hex characters of the email digest kept in a guest code. 12 hex chars is 48 bits: collision-safe at any plausible customer count while leaving the total code short.

12
CODE_MAX_LENGTH =

Returns the longest code this presenter will emit. KashFlow does not publish the limit in the WSDL; this is a conservative ceiling that both formats stay well inside.

Returns:

  • (Integer)

    the longest code this presenter will emit. KashFlow does not publish the limit in the WSDL; this is a conservative ceiling that both formats stay well inside.

20

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ CustomerPayload

Returns a new instance of CustomerPayload.

Parameters:

  • order (Spree::Order)

    a completed order with a billing address



65
66
67
# File 'app/presenters/spree/kashflow/customer_payload.rb', line 65

def initialize(order)
  @order = order
end

Instance Method Details

#to_hHash{String => Object}

Keys are emitted in the WSDL Customer <s:sequence>'s relative order — … Address4, CountryName, CountryCode, Postcode, Website, EC, OutsideEC, … ContactFirstName, ContactLastName, … VATNumber. Savon serialises a Hash body in insertion order, and a .NET ASMX endpoint enforcing that sequence drops or mis-binds an out-of-order element rather than raising, so the order of these keys is load-bearing, not cosmetic.

Returns:

  • (Hash{String => Object})

    a KashFlow Customer structure keyed by the WSDL field names, ready to hand to KashFlow's customer-upsert operation



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/presenters/spree/kashflow/customer_payload.rb', line 80

def to_h
  payload = {
    "Code" => customer_code,
    "Name" => customer_name,
    "Email" => order.email,
    "Address1" => bill_address&.address1,
    "Address2" => bill_address&.address2,
    "Address3" => bill_address&.city,
    "Address4" => bill_address&.state_name_text,
    "CountryCode" => billing_country_code,
    "Postcode" => bill_address&.zipcode,
    "EC" => ec_flag,
    "OutsideEC" => outside_ec_flag,
    "ContactFirstName" => bill_address&.firstname,
    "ContactLastName" => bill_address&.lastname
  }
  payload["VATNumber"] = vat_number if vat_number.present?
  payload
end