discourse-subscriptions/app/controllers/payments_controller.rb

41 lines
1.1 KiB
Ruby
Raw Normal View History

# 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 = Customer.where(user_id: current_user.id, product_id: nil).first_or_create do |c|
2019-12-17 00:31:58 -05:00
new_customer = ::Stripe::Customer.create(
email: current_user.email
)
2019-12-17 00:31:58 -05:00
c.customer_id = new_customer[:id]
end
payment = ::Stripe::PaymentIntent.create(
payment_method_types: ['card'],
payment_method: params[:payment_method],
amount: params[:amount],
currency: params[:currency],
2019-12-17 00:31:58 -05:00
customer: customer[:customer_id],
confirm: true
)
render_json_dump payment
rescue ::Stripe::InvalidRequestError => e
render_json_error e.message
rescue ::Stripe::CardError => e
2019-12-17 00:31:58 -05:00
render_json_error I18n.t('discourse_subscriptions.card.declined')
end
end
end
end