format currency and user

This commit is contained in:
Rimian Perkins 2019-09-14 15:55:25 +10:00
parent 5c33268477
commit 9631ddc7e5
6 changed files with 59 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -17,7 +17,7 @@
<td>{{{stripe-payment-link payment}}}</td>
<td>{{payment.receipt_email}}</td>
<td>{{{format-duration payment.created_at_age}}}</td>
<td class="amount">{{payment.amount}}</td>
<td class="amount">{{payment.amount_currency}}</td>
</tr>
{{/each}}
</table>

View File

@ -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

View File

@ -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

View File

@ -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