mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-03-06 09:39:51 +00:00
customer model
This commit is contained in:
parent
1f7549060d
commit
dcb4b82dec
@ -7,12 +7,22 @@ module DiscoursePatrons
|
|||||||
before_action :set_api_key
|
before_action :set_api_key
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
begin
|
||||||
customer = ::Stripe::Customer.create(
|
customer = ::Stripe::Customer.create(
|
||||||
email: current_user.email,
|
email: current_user.email,
|
||||||
source: params[:source]
|
source: params[:source]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DiscoursePatrons::Customer.create(
|
||||||
|
customer_id: customer.id
|
||||||
|
user_id: current_user.id
|
||||||
|
)
|
||||||
|
|
||||||
render_json_dump customer
|
render_json_dump customer
|
||||||
|
|
||||||
|
rescue ::Stripe::InvalidRequestError => e
|
||||||
|
return render_json_error e.message
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
7
app/models/customer.rb
Normal file
7
app/models/customer.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module DiscoursePatrons
|
||||||
|
class Customer < ActiveRecord::Base
|
||||||
|
self.table_name = "discourse_patrons_customers"
|
||||||
|
end
|
||||||
|
end
|
11
db/migrate/20191025031631_create_customers.rb
Normal file
11
db/migrate/20191025031631_create_customers.rb
Normal 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
|
@ -55,6 +55,7 @@ after_initialize do
|
|||||||
"../app/controllers/products_controller",
|
"../app/controllers/products_controller",
|
||||||
"../app/controllers/subscriptions_controller",
|
"../app/controllers/subscriptions_controller",
|
||||||
"../app/models/payment",
|
"../app/models/payment",
|
||||||
|
"../app/models/customer",
|
||||||
"../app/serializers/payment_serializer",
|
"../app/serializers/payment_serializer",
|
||||||
].each { |path| require File.expand_path(path, __FILE__) }
|
].each { |path| require File.expand_path(path, __FILE__) }
|
||||||
|
|
||||||
|
@ -5,13 +5,14 @@ require 'rails_helper'
|
|||||||
module DiscoursePatrons
|
module DiscoursePatrons
|
||||||
RSpec.describe CustomersController do
|
RSpec.describe CustomersController do
|
||||||
describe "create" do
|
describe "create" do
|
||||||
|
describe "authenticated" do
|
||||||
let(:user) { Fabricate(:user, email: 'hello.2@example.com') }
|
let(:user) { Fabricate(:user, email: 'hello.2@example.com') }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
sign_in(user)
|
sign_in(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a customer" do
|
it "creates a stripe customer" do
|
||||||
::Stripe::Customer.expects(:create).with(
|
::Stripe::Customer.expects(:create).with(
|
||||||
email: 'hello.2@example.com',
|
email: 'hello.2@example.com',
|
||||||
source: 'tok_interesting'
|
source: 'tok_interesting'
|
||||||
@ -19,6 +20,15 @@ module DiscoursePatrons
|
|||||||
|
|
||||||
post "/patrons/customers.json", params: { source: 'tok_interesting' }
|
post "/patrons/customers.json", params: { source: 'tok_interesting' }
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user