Class: Stay::Api::V1::CreditCardsController

Inherits:
BaseApiController
  • Object
show all
Includes:
StripeConcern
Defined in:
app/controllers/stay/api/v1/credit_cards_controller.rb

Instance Method Summary collapse

Methods included from StripeConcern

#attach_payment_method_to_customer, #confirm_payment_intent, #create_customer, #create_or_retrieve_customer, #create_payment_intent, #create_payment_method, #create_payment_method_from_token, #destroy_payment_method, #get_payment_intent, #user

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 28

def create
  begin
    token = create_payment_method_from_token(credit_card_params[:card_token])
    @credit_card = current_devise_api_user.credit_cards.build(credit_card_params.merge(payment_method_token: token))

    if @credit_card.save
      render json: { message: "Credit card saved successfully", data: CreditCardSerializer.new(@credit_card), success: true }, status: :ok
    else
      render json: { error: "Credit card not saved", success: false, errors: @credit_card.errors.full_messages }, status: :unprocessable_entity
    end
  rescue Stripe::InvalidRequestError => e
    render json: { error: e.message, success: false }, status: :unprocessable_entity
  end
end

#destroyObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 55

def destroy
  begin
    destroy_payment_method(@credit_card.payment_method_token)

    if @credit_card.destroy
      render json: { message: "Credit card successfully deleted", success: true }, status: :ok
    else
      render json: { error: "Failed to delete credit card", success: false, errors: @credit_card.errors.full_messages }, status: :unprocessable_entity
    end
  rescue Stripe::InvalidRequestError => e
    render json: { error: "Stripe error: #{e.message}", success: false }, status: :unprocessable_entity
  rescue ActiveRecord::RecordNotFound
    render json: { error: "Credit card not found", success: false }, status: :not_found
  rescue StandardError => e
    render json: { error: "An unexpected error occurred: #{e.message}", success: false }, status: :internal_server_error
  end
end

#editObject



43
44
45
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 43

def edit
  render json: { data: @credit_card, success: true }, status: :ok
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 6

def index
  @credit_cards = current_devise_api_user.credit_cards
  if @credit_cards.any?
    render json: {
      message: "Credit cards found",
      data: CreditCardSerializer.new(@credit_cards.last),
      success: true },
    status: :ok
  else
    render json: { error: "No credit cards found", success: false }, status: :unprocessable_entity
  end
end

#newObject



23
24
25
26
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 23

def new
  @credit_card = current_devise_api_user.credit_cards.build
  render json: { data: @credit_card, success: true }, status: :ok
end

#showObject



19
20
21
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 19

def show
  render json: { data: CreditCardSerializer.new(@credit_card), success: true }, status: :ok
end

#updateObject



47
48
49
50
51
52
53
# File 'app/controllers/stay/api/v1/credit_cards_controller.rb', line 47

def update
  if @credit_card.update(credit_card_params.except(:card_token))
    render json: { message: "Credit card updated successfully", data: CreditCardSerializer.new(@credit_card), success: true }, status: :ok
  else
    render json: { error: "Credit card not updated", success: false, errors: @credit_card.errors.full_messages }, status: :unprocessable_entity
  end
end