customer model

This commit is contained in:
Rimian Perkins 2019-10-26 11:31:19 +11:00
parent 1f7549060d
commit dcb4b82dec
5 changed files with 54 additions and 15 deletions

View File

@ -7,12 +7,22 @@ module DiscoursePatrons
before_action :set_api_key
def create
begin
customer = ::Stripe::Customer.create(
email: current_user.email,
source: params[:source]
)
DiscoursePatrons::Customer.create(
customer_id: customer.id
user_id: current_user.id
)
render_json_dump customer
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message
end
end
end
end

7
app/models/customer.rb Normal file
View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
module DiscoursePatrons
class Customer < ActiveRecord::Base
self.table_name = "discourse_patrons_customers"
end
end

View File

@ -0,0 +1,11 @@
class CreateCustomers < ActiveRecord::Migration[5.2]
def change
create_table :discourse_patrons_customers do |t|
t.string :customer_id, null: false
t.references :user, foreign_key: true
t.timestamps
end
add_index :discourse_patrons_customers, :customer_id, unique: true
end
end

View File

@ -55,6 +55,7 @@ after_initialize do
"../app/controllers/products_controller",
"../app/controllers/subscriptions_controller",
"../app/models/payment",
"../app/models/customer",
"../app/serializers/payment_serializer",
].each { |path| require File.expand_path(path, __FILE__) }

View File

@ -5,13 +5,14 @@ require 'rails_helper'
module DiscoursePatrons
RSpec.describe CustomersController do
describe "create" do
describe "authenticated" do
let(:user) { Fabricate(:user, email: 'hello.2@example.com') }
before do
sign_in(user)
end
it "creates a customer" do
it "creates a stripe customer" do
::Stripe::Customer.expects(:create).with(
email: 'hello.2@example.com',
source: 'tok_interesting'
@ -19,6 +20,15 @@ module DiscoursePatrons
post "/patrons/customers.json", params: { source: 'tok_interesting' }
end
it "saves the customer" do
::Stripe::Customer.expects(:create).returns(id: 'cus_id23456')
expect {
post "/patrons/customers.json", params: { source: 'tok_interesting' }
}.to change { DiscoursePatrons::Customer.count }
end
end
end
end
end