Module: Stay::StripeConcern

Extended by:
ActiveSupport::Concern
Included in:
Api::V1::CreditCardsController, Api::V1::PaymentsController, PaymentsController
Defined in:
app/controllers/concerns/stay/stripe_concern.rb

Instance Method Summary collapse

Instance Method Details

#attach_payment_method_to_customer(payment_method_token, customer_id) ⇒ Object



73
74
75
76
77
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 73

def attach_payment_method_to_customer(payment_method_token, customer_id)
  Stripe::PaymentMethod.attach(
    payment_method_token,{ customer: customer_id }
  )
end

#confirm_payment_intent(payment_intent_id, payment_method_id) ⇒ Object



89
90
91
92
93
94
95
96
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 89

def confirm_payment_intent(payment_intent_id, payment_method_id)
  Stripe::PaymentIntent.confirm(
    payment_intent_id,
  {
      payment_method: payment_method_id,
      # return_url: params[redirect_url]
  })
end

#create_customerObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 28

def create_customer
 customer =  Stripe::Customer.create({
    email: user.email,
    name: "#{user.first_name} #{user.last_name}",
    address: {
      city: user.addresses.last&.city,
      state: user.addresses.last&.state&.name,
      country: user.addresses.last&.country&.name,
      line1: user.addresses.last&.address1,
      line2: user.addresses.last&.address2,
      postal_code: user.addresses.last&.zipcode
    },
    metadata: { order_id: user.id }
  })
  user.update(stripe_customer_id: customer.id)
end

#create_or_retrieve_customerObject



45
46
47
48
49
50
51
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 45

def create_or_retrieve_customer
  if user.stripe_customer_id.present?
    @customer  = Stripe::Customer.retrieve(user.stripe_customer_id)
  else
    @customer =  create_customer
  end
end

#create_payment_intentObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 53

def create_payment_intent
  payment_method_id = params[:payment_method_id]
  payment_intent = Stripe::PaymentIntent.create(
    amount: (@booking.total_amount * 100).to_i,
    description: @booking.user.email,
    currency: params[:currency] || "usd",
    payment_method: payment_method_id,
    receipt_email: user.email,
    customer: @customer,
    confirm: false
  )
  if payment_intent.status == "requires_confirmation"
    payment_intent = confirm_payment_intent(payment_intent.id, payment_method_id)
  end      
end

#create_payment_methodObject



16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 16

def create_payment_method
  Stripe::PaymentMethod.create({
    type: params[:payment_method_type],
    card: {
      number: params[:number],
      exp_month: params[:exp_month],
      exp_year: params[:exp_year],
      cvc: params[:cvc]
    }
  })
end

#create_payment_method_from_token(token) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 79

def create_payment_method_from_token(token)
  payment_method = Stripe::PaymentMethod.create({
      type: "card",
      card: { token: token }  
  })
  
  attach_payment_method_to_customer( payment_method.id, current_devise_api_user.stripe_customer_id)
  payment_method.id
end

#destroy_payment_method(payment_method_token) ⇒ Object



98
99
100
101
102
103
104
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 98

def destroy_payment_method(payment_method_token)
  begin
    Stripe::PaymentMethod.detach(payment_method_token)
  rescue Stripe::InvalidRequestError => e
    raise "Failed to detach payment method: #{e.message}"
  end
end

#get_payment_intentObject



69
70
71
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 69

def get_payment_intent
  Stripe::PaymentIntent.retrieve(@booking.payment_intent_id)
end

#userObject



12
13
14
# File 'app/controllers/concerns/stay/stripe_concern.rb', line 12

def user
  current_devise_api_user || current_user
end