diff --git a/app/controllers/patrons_controller.rb b/app/controllers/patrons_controller.rb index 5bd56e7..e00f9d0 100644 --- a/app/controllers/patrons_controller.rb +++ b/app/controllers/patrons_controller.rb @@ -2,13 +2,33 @@ module DiscoursePatrons class PatronsController < ApplicationController + skip_before_action :verify_authenticity_token, only: [:create] + def index result = {} render json: result end def create - render json: {} + ::Stripe.api_key = SiteSetting.discourse_patrons_secret_key + + begin + + response = ::Stripe::PaymentIntent.create( + amount: params[:amount], + currency: SiteSetting.discourse_patrons_currency, + payment_method_types: ['card'], + payment_method: params[:paymentMethodId], + confirm: true, + ) + + rescue ::Stripe::InvalidRequestError => e + response = { error: e } + rescue ::Stripe::CardError => e + response = { error: 'Card Declined' } + end + + render json: response end end end diff --git a/plugin.rb b/plugin.rb index 8a07a09..3a951e9 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true. +# frozen_string_literal: true # name: Discourse Patrons # about: Integrates Stripe into Discourse to allow visitors to make payments diff --git a/spec/controllers/discourse_patrons/patrons_controller_spec.rb b/spec/controllers/discourse_patrons/patrons_controller_spec.rb index 9c62ec3..e753de9 100644 --- a/spec/controllers/discourse_patrons/patrons_controller_spec.rb +++ b/spec/controllers/discourse_patrons/patrons_controller_spec.rb @@ -14,7 +14,13 @@ module DiscoursePatrons end describe 'create' do + before do + SiteSetting.stubs(:discourse_patrons_currency).returns('AUD') + SiteSetting.stubs(:discourse_patrons_secret_key).returns('xyz-678') + end + it 'responds ok' do + ::Stripe::PaymentIntent.expects(:create) post :create, params: {}, format: :json expect(response).to have_http_status(200) end