2019-09-11 05:11:02 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscoursePatrons
|
2019-09-12 22:35:38 -04:00
|
|
|
class PatronsController < ::ApplicationController
|
2019-09-24 06:44:51 -04:00
|
|
|
include DiscoursePatrons::Stripe
|
|
|
|
|
2019-09-11 05:55:35 -04:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
2019-09-14 06:54:13 -04:00
|
|
|
before_action :set_api_key
|
2019-09-11 05:55:35 -04:00
|
|
|
|
2019-09-11 05:11:02 -04:00
|
|
|
def index
|
2019-09-12 22:58:17 -04:00
|
|
|
result = { email: '' }
|
2019-09-12 22:35:38 -04:00
|
|
|
|
|
|
|
if current_user
|
|
|
|
result[:email] = current_user.email
|
|
|
|
end
|
|
|
|
|
2019-09-11 05:11:02 -04:00
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-09-11 05:55:35 -04:00
|
|
|
begin
|
|
|
|
|
|
|
|
response = ::Stripe::PaymentIntent.create(
|
2019-09-11 21:49:52 -04:00
|
|
|
amount: param_currency_to_number,
|
2019-09-11 05:55:35 -04:00
|
|
|
currency: SiteSetting.discourse_patrons_currency,
|
|
|
|
payment_method_types: ['card'],
|
2019-09-13 00:34:06 -04:00
|
|
|
payment_method: params[:payment_method_id],
|
2019-09-12 05:17:08 -04:00
|
|
|
description: SiteSetting.discourse_patrons_payment_description,
|
2019-09-13 00:34:06 -04:00
|
|
|
receipt_email: params[:receipt_email],
|
2019-09-11 05:55:35 -04:00
|
|
|
confirm: true,
|
2019-09-15 07:44:10 -04:00
|
|
|
metadata: { user_id: user_id }
|
2019-09-11 05:55:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
rescue ::Stripe::InvalidRequestError => e
|
|
|
|
response = { error: e }
|
|
|
|
rescue ::Stripe::CardError => e
|
|
|
|
response = { error: 'Card Declined' }
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: response
|
2019-09-11 05:11:02 -04:00
|
|
|
end
|
2019-09-11 21:49:52 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def param_currency_to_number
|
|
|
|
params[:amount].to_s.sub('.', '').to_i
|
|
|
|
end
|
2019-09-14 01:55:25 -04:00
|
|
|
|
|
|
|
def user_id
|
|
|
|
if current_user
|
|
|
|
current_user.id
|
|
|
|
end
|
|
|
|
end
|
2019-09-11 03:13:12 -04:00
|
|
|
end
|
|
|
|
end
|