Class: Stay::Api::V1::PaymentsController
- Inherits:
-
BaseApiController
- Object
- BaseApiController
- Stay::Api::V1::PaymentsController
show all
- Includes:
- StripeConcern
- Defined in:
- app/controllers/stay/api/v1/payments_controller.rb
Instance Method Summary
collapse
#attach_payment_method_to_customer, #confirm_payment_intent, #create_customer, #create_or_retrieve_customer, #create_payment_intent, #create_payment_method_from_token, #destroy_payment_method, #get_payment_intent, #user
Instance Method Details
#confirm ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'app/controllers/stay/api/v1/payments_controller.rb', line 86
def confirm
payment_intent = get_payment_intent
if payment_intent.status == "succeeded"
@booking.update(status: "completed")
render json: { success: true, redirect_url: params[:redirect_url] }
else
render json: { error: payment_intent.last_payment_error.message }, status: 422
end
end
|
#create ⇒ Object
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'app/controllers/stay/api/v1/payments_controller.rb', line 32
def create
begin
customer = create_or_retrieve_customer
payment_intent = if @booking.payment_intent_id && @booking.payment_state == "confirmed"
get_payment_intent
else
create_payment_intent
end
payment_status = case payment_intent.status
when "succeeded"
"confirmed"
when "requires_action"
"pending"
else
"failed"
end
create_payment(payment_intent, payment_status, @booking)
@booking.update(payment_intent_id: payment_intent.id, payment_state:payment_status)
if payment_intent.status == "requires_action"
render json: {
message: "Payment requires further action",
client_secret: payment_intent.client_secret,
payment_intent_id: payment_intent.id
}, status: :ok
elsif payment_intent.status == "succeeded"
render json: {
message: "Payment successful",
payment_intent: payment_intent
}, status: :ok
else
render json: {
message: "Payment failed",
error: payment_intent.last_payment_error
}, status: :unprocessable_entity
end
rescue Stripe::CardError => e
render json: { error: e.message }, status: :unprocessable_entity
rescue => e
render json: { error: e.message }, status: :internal_server_error
end
end
|
#create_payment(payment_intent, status, booking) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'app/controllers/stay/api/v1/payments_controller.rb', line 77
def create_payment(payment_intent, status, booking)
Stay::Payment.create!(
booking: booking,
payment_method: Stay::PaymentMethod.last,
amount: booking.total_amount,
state: status,
)
end
|
#create_payment_method ⇒ Object
17
18
19
20
21
22
23
|
# File 'app/controllers/stay/api/v1/payments_controller.rb', line 17
def create_payment_method
payment_method = create_payment_method(payment_method_params)
attach_payment_method(payment_method.id)
render json: { message: "Payment method created and attached successfully.", payment_method: payment_method }
rescue Stripe::StripeError => e
render json: { error: e.message }, status: :unprocessable_entity
end
|
#list_payment_methods ⇒ Object
25
26
27
28
29
30
|
# File 'app/controllers/stay/api/v1/payments_controller.rb', line 25
def list_payment_methods
payment_methods = list_payment_methods
render json: { payment_methods: payment_methods.data }
rescue Stripe::StripeError => e
render json: { error: e.message }, status: :unprocessable_entity
end
|
#payment_method ⇒ Object
8
9
10
11
12
13
14
15
|
# File 'app/controllers/stay/api/v1/payments_controller.rb', line 8
def payment_method
methods = Stay::PaymentMethod.all
if methods.any?
render json: { message: "Payment Method Found", data: methods, success: true }, status: :ok
else
render json: { error: "no method found", success: false }, status: :unprocessable_entity
end
end
|