Class: Workarea::GiftCards::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/gift_cards/gateway.rb

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Instance Method Details

#authorize(amount, tender) ⇒ Workarea::GiftCards::Gateway::Response

See purchase. This gateway performs a purchase on authorization.

Parameters:

  • amount (Integer)

    amount to authorize, in cents

  • tender (Workarea::Payment::Tender::GiftCard)

    for transaction

Returns:



26
27
28
# File 'lib/workarea/gift_cards/gateway.rb', line 26

def authorize(amount, tender)
  purchase(amount, tender)
end

#balance(token) ⇒ Money

Lookup the balance of one or many Workarea::Payment::GiftCard records. It will always return a single sum. If multiple numbers are provided, it returns the total of all balances. Invalid numbers return a balance of zero.

Parameters:

  • token (String)

    the gift card number/token

Returns:

  • (Money)

    the balance gift cards matching the numbers provided



106
107
108
# File 'lib/workarea/gift_cards/gateway.rb', line 106

def balance(token)
  Array.wrap(token).sum(0.to_m) { |t| Payment::GiftCard.find_balance(t) }
end

#cancel(amount, tender) ⇒ Workarea::GiftCards::Gateway::Response

See refund.

Parameters:

  • amount (Integer)

    amount to cancel, in cents

  • tender (Workarea::Payment::Tender::GiftCard)

    for transaction

Returns:



37
38
39
# File 'lib/workarea/gift_cards/gateway.rb', line 37

def cancel(amount, tender)
  refund(amount, tender)
end

#capture(amount, tender) ⇒ Workarea::GiftCards::Gateway::Response

This gateway does a purchase on authorization so no capture is required.

Parameters:

  • amount (Integer)

    amount to capture, in cents

  • tender (Workarea::Payment::Tender::GiftCard)

    for transaction

Returns:



48
49
50
51
# File 'lib/workarea/gift_cards/gateway.rb', line 48

def capture(amount, tender)
  # no op, captures on authorization
  Response.new(true, I18n.t('workarea.gift_cards.capture'))
end

#lookup(params) ⇒ Workarea::Payment::GiftCard?

Lookup a gift card.

Parameters:

  • params (Hash)

Options Hash (params):

  • :email (String)

    address associated to the gift card

  • :token (String)

    gift card number/token to lookup

Returns:



118
119
120
121
122
123
124
125
# File 'lib/workarea/gift_cards/gateway.rb', line 118

def lookup(params)
  email = params.fetch(:email, nil)
  token = params.fetch(:token, nil)

  if email.present? && token.present?
    Workarea::Payment::GiftCard.find_by_token_and_email(token, email)
  end
end

#purchase(amount, tender) ⇒ Workarea::GiftCards::Gateway::Response

Perform a purchase on a Workarea::Payment::GiftCard. This will increment the ‘used` field, and decrement the `balance` field.

Parameters:

  • amount (Integer)

    amount to purchase, in cents

  • tender (Workarea::Payment::Tender::GiftCard)

    for transaction

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/workarea/gift_cards/gateway.rb', line 61

def purchase(amount, tender)
  Payment::GiftCard.purchase(tender.number, amount)

  Response.new(
    true,
    I18n.t(
      'workarea.gift_cards.debit',
      amount: amount,
      number: tender.number
    )
  )
rescue Payment::InsufficientFunds
  Response.new(false, I18n.t('workarea.gift_cards.insufficient_funds'))
end

#refund(amount, tender) ⇒ Workarea::GiftCards::Gateway::Response

Undo a purchase on a Workarea::Payment::GiftCard. This will increment the ‘balance` field, and decrement the `used` field.

Parameters:

  • amount (Integer)

    amount to refund, in cents

  • tender (Workarea::Payment::Tender::GiftCard)

    for transaction

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/workarea/gift_cards/gateway.rb', line 84

def refund(amount, tender)
  Payment::GiftCard.refund(tender.number, amount)

  Response.new(
    true,
    I18n.t(
      'workarea.gift_cards.credit',
      amount: amount,
      number: tender.number
    )
  )
end

#uses_system_cards?Boolean

Whether or not this gateway uses Workarea::Payment::GiftCard This controls the display of the admin menu, triggering the creation of cards from orders, and tracking redemptions.

Returns:

  • (Boolean)


15
16
17
# File 'lib/workarea/gift_cards/gateway.rb', line 15

def uses_system_cards?
  true
end