a payment model is created

This commit is contained in:
Rimian Perkins 2019-09-13 22:03:29 +10:00
parent c1f1bc4ee4
commit b34e7ade8c
3 changed files with 33 additions and 11 deletions

View File

@ -29,6 +29,13 @@ module DiscoursePatrons
confirm: true, confirm: true,
) )
Payment.create(
payment_intent_id: response[:id],
receipt_email: response[:receipt_email],
url: response[:charges][:url],
amount: response[:amount]
)
rescue ::Stripe::InvalidRequestError => e rescue ::Stripe::InvalidRequestError => e
response = { error: e } response = { error: e }
rescue ::Stripe::CardError => e rescue ::Stripe::CardError => e

View File

@ -3,9 +3,14 @@
class CreatePaymentsTable < ActiveRecord::Migration[5.2] class CreatePaymentsTable < ActiveRecord::Migration[5.2]
def change def change
create_table :payments do |t| create_table :payments do |t|
t.integer :payment_intent_id, null: false
t.string :receipt_email, null: false
t.string :url, null: false
t.integer :amount, null: false
t.references :user, foreign_key: true
t.timestamps t.timestamps
end end
# add_index :payments, [:payment_intent_id], unique: true add_index :payments, :payment_intent_id, unique: true
end end
end end

View File

@ -28,57 +28,67 @@ module DiscoursePatrons
end end
describe 'create' do describe 'create' do
let(:payment) do
{
id: 'xyz-1234',
charges: { url: '/v1/charges?payment_intent=xyz-1234' },
amount: 9000,
receipt_email: 'hello@example.com'
}
end
before do before do
SiteSetting.stubs(:discourse_patrons_currency).returns('AUD') SiteSetting.stubs(:discourse_patrons_currency).returns('AUD')
SiteSetting.stubs(:discourse_patrons_secret_key).returns('xyz-678') SiteSetting.stubs(:discourse_patrons_secret_key).returns('xyz-678')
end end
it 'responds ok' do it 'responds ok' do
::Stripe::PaymentIntent.expects(:create) ::Stripe::PaymentIntent.expects(:create).returns(payment)
post :create, params: {}, format: :json post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
xit 'creates a payment' do it 'creates a payment' do
::Stripe::PaymentIntent.expects(:create) ::Stripe::PaymentIntent.expects(:create).returns(payment)
expect { expect {
post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json
}.to change { Payment.count } }.to change { Payment.count }
end end
it 'has the correct amount' do it 'has the correct amount' do
::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 2000)) ::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 2000)).returns(payment)
post :create, params: { amount: '20.00' }, format: :json post :create, params: { amount: '20.00' }, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'has no amount' do it 'has no amount' do
::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 0)) ::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 0)).returns(payment)
post :create, params: {}, format: :json post :create, params: {}, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'has curency' do it 'has curency' do
::Stripe::PaymentIntent.expects(:create).with(has_entry(:currency, 'AUD')) ::Stripe::PaymentIntent.expects(:create).with(has_entry(:currency, 'AUD')).returns(payment)
post :create, params: {}, format: :json post :create, params: {}, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'has a receipt email' do it 'has a receipt email' do
::Stripe::PaymentIntent.expects(:create).with(has_entry(:receipt_email, 'hello@example.com')) ::Stripe::PaymentIntent.expects(:create).with(has_entry(:receipt_email, 'hello@example.com')).returns(payment)
post :create, params: { receipt_email: 'hello@example.com' }, format: :json post :create, params: { receipt_email: 'hello@example.com' }, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'has a payment method' do it 'has a payment method' do
::Stripe::PaymentIntent.expects(:create).with(has_entry(:payment_method, 'xyz-1234')) ::Stripe::PaymentIntent.expects(:create).with(has_entry(:payment_method, 'xyz-1234')).returns(payment)
post :create, params: { payment_method_id: 'xyz-1234' }, format: :json post :create, params: { payment_method_id: 'xyz-1234' }, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'has a description' do it 'has a description' do
SiteSetting.stubs(:discourse_patrons_payment_description).returns('hello-world') SiteSetting.stubs(:discourse_patrons_payment_description).returns('hello-world')
::Stripe::PaymentIntent.expects(:create).with(has_entry(:description, 'hello-world')) ::Stripe::PaymentIntent.expects(:create).with(has_entry(:description, 'hello-world')).returns(payment)
post :create, params: {}, format: :json post :create, params: {}, format: :json
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end