mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-03-06 09:39:51 +00:00
42 lines
1.0 KiB
Ruby
42 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseSubscriptions
|
|
class PaymentsController < ::ApplicationController
|
|
include DiscourseSubscriptions::Stripe
|
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
|
before_action :set_api_key
|
|
|
|
requires_login
|
|
|
|
def create
|
|
begin
|
|
customer = ::Stripe::Customer.create(
|
|
email: current_user.email,
|
|
)
|
|
|
|
DiscourseSubscriptions::Customer.create(
|
|
user_id: current_user.id,
|
|
customer_id: customer[:id],
|
|
)
|
|
|
|
payment = ::Stripe::PaymentIntent.create(
|
|
payment_method_types: ['card'],
|
|
payment_method: params[:payment_method],
|
|
amount: params[:amount],
|
|
currency: params[:currency],
|
|
confirm: true,
|
|
customer: customer[:id],
|
|
)
|
|
|
|
render_json_dump payment
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
render_json_error e.message
|
|
rescue ::Stripe::CardError => e
|
|
render_json_error 'Card Declined'
|
|
end
|
|
end
|
|
end
|
|
end
|