diff --git a/app/controllers/patrons_controller.rb b/app/controllers/patrons_controller.rb index d40c1cc..92e31fb 100644 --- a/app/controllers/patrons_controller.rb +++ b/app/controllers/patrons_controller.rb @@ -30,10 +30,12 @@ module DiscoursePatrons ) Payment.create( + user_id: user_id, payment_intent_id: response[:id], receipt_email: response[:receipt_email], url: response[:charges][:url], - amount: response[:amount] + amount: response[:amount], + currency: response[:currency] ) rescue ::Stripe::InvalidRequestError => e @@ -50,5 +52,11 @@ module DiscoursePatrons def param_currency_to_number params[:amount].to_s.sub('.', '').to_i end + + def user_id + if current_user + current_user.id + end + end end end diff --git a/app/serializers/payment_serializer.rb b/app/serializers/payment_serializer.rb index eb4d4b6..b553cf1 100644 --- a/app/serializers/payment_serializer.rb +++ b/app/serializers/payment_serializer.rb @@ -1,9 +1,30 @@ # frozen_string_literal: true class PaymentSerializer < ApplicationSerializer - attributes :payment_intent_id, :receipt_email, :url, :created_at_age, :amount + attributes :payment_intent_id, :receipt_email, :url, :created_at_age, :amount, :amount_currency def created_at_age Time.now - object.created_at end + + def amount_currency + ActiveSupport::NumberHelper.number_to_currency( + object.amount/100, + precision: 2, + unit: currency_unit + ) + end + + private + + def currency_unit + case object.currency + when "eur" + "€" + when "gbp" + "£" + else + "$" + end + end end diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons.hbs index 229ccaa..a1701c9 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons.hbs @@ -17,7 +17,7 @@ {{{stripe-payment-link payment}}} {{payment.receipt_email}} {{{format-duration payment.created_at_age}}} - {{payment.amount}} + {{payment.amount_currency}} {{/each}} diff --git a/db/migrate/20190913010928_create_payments_table.rb b/db/migrate/20190913010928_create_payments_table.rb index 13c58d8..bcc5a52 100644 --- a/db/migrate/20190913010928_create_payments_table.rb +++ b/db/migrate/20190913010928_create_payments_table.rb @@ -5,6 +5,7 @@ class CreatePaymentsTable < ActiveRecord::Migration[5.2] create_table :payments do |t| t.string :payment_intent_id, null: false t.string :receipt_email, null: false + t.string :currency, null: false t.string :url, null: false t.integer :amount, null: false t.references :user, foreign_key: true diff --git a/spec/controllers/discourse_patrons/admin_controller_spec.rb b/spec/controllers/discourse_patrons/admin_controller_spec.rb index e69de29..8eea86b 100644 --- a/spec/controllers/discourse_patrons/admin_controller_spec.rb +++ b/spec/controllers/discourse_patrons/admin_controller_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'rails_helper' + +module DiscoursePatrons + RSpec.describe AdminController, type: :controller do + routes { DiscoursePatrons::Engine.routes } + + it 'is a subclass of AdminController' do + expect(DiscoursePatrons::AdminController < Admin::AdminController).to eq(true) + end + + # TODO authenticate + it 'has payments' + end +end diff --git a/spec/controllers/discourse_patrons/patrons_controller_spec.rb b/spec/controllers/discourse_patrons/patrons_controller_spec.rb index e156848..9469209 100644 --- a/spec/controllers/discourse_patrons/patrons_controller_spec.rb +++ b/spec/controllers/discourse_patrons/patrons_controller_spec.rb @@ -33,13 +33,15 @@ module DiscoursePatrons id: 'xyz-1234', charges: { url: '/v1/charges?payment_intent=xyz-1234' }, amount: 9000, - receipt_email: 'hello@example.com' + receipt_email: 'hello@example.com', + currency: 'aud' } end before do SiteSetting.stubs(:discourse_patrons_currency).returns('AUD') SiteSetting.stubs(:discourse_patrons_secret_key).returns('xyz-678') + controller.stubs(:current_user).returns(Fabricate(:user)) end it 'responds ok' do @@ -56,6 +58,13 @@ module DiscoursePatrons }.to change { Payment.count } end + it 'has no user' do + controller.stubs(:current_user).returns(nil) + ::Stripe::PaymentIntent.expects(:create).returns(payment) + post :create, format: :json + expect(response).to have_http_status(200) + end + it 'has the correct amount' do ::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 2000)).returns(payment) post :create, params: { amount: '20.00' }, format: :json