2019-12-12 18:41:14 -05:00
|
|
|
# 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
|
2020-05-22 12:20:05 -04:00
|
|
|
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-16 18:03:00 -05:00
|
|
|
|
2019-12-17 00:31:58 -05:00
|
|
|
c.customer_id = new_customer[:id]
|
|
|
|
end
|
2019-12-16 18:03:00 -05:00
|
|
|
|
2019-12-12 18:41:14 -05:00
|
|
|
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
|
2019-12-12 18:41:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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')
|
2019-12-12 18:41:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|