Module: Effective::Providers::Deluxe

Extended by:
ActiveSupport::Concern
Included in:
OrdersController
Defined in:
app/controllers/effective/providers/deluxe.rb

Instance Method Summary collapse

Instance Method Details

#deluxeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/effective/providers/deluxe.rb', line 6

def deluxe
  raise('deluxe provider is not available') unless EffectiveOrders.deluxe?

  @order = Effective::Order.deep.find(params[:id])
  @order.current_user = current_user unless admin_checkout?(deluxe_params)

  EffectiveResources.authorize!(self, :update, @order)

  ## Process Payment Intent
  api = Effective::DeluxeApi.new

  # The payment_intent is set by the Deluxe HostedPaymentForm
  payment_intent_payload = deluxe_params[:payment_intent]

  if payment_intent_payload.blank?
    flash[:danger] = 'Unable to process deluxe order without payment. please try again.'
    return order_not_processed(declined_url: deluxe_params[:declined_url])
  end

  # Decode the base64 encoded JSON object into a Hash
  payment_intent = api.decode_payment_intent_payload(payment_intent_payload)
  card_info = api.card_info(payment_intent)

  valid = (payment_intent['status'] == 'success')

  if valid == false
    return order_declined(payment: card_info, provider: 'deluxe', card: card_info['card'], declined_url: deluxe_params[:declined_url])
  end

  ## Purchase Order right now
  purchased = api.purchase!(@order, payment_intent)

  payment = api.payment
  raise('expected a payment Hash') unless payment.kind_of?(Hash)

  if purchased == false
    flash[:danger] = "Payment was unsuccessful. The credit card payment failed with message: #{Array(payment['responseMessage']).to_sentence.presence || 'none'}. Please try again."
    return order_declined(
      payment: payment, 
      provider: 'deluxe', 
      card: payment['card'], 
      declined_url: deluxe_params[:declined_url]
    )
  end

  # Valid Authorized and Completed Payment
  order_purchased(
    payment: payment,
    provider: 'deluxe',
    card: payment['card'],
    purchased_url: deluxe_params[:purchased_url]
  )
end