diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 4839e31..bdbba96 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -7,12 +7,22 @@ module DiscoursePatrons before_action :set_api_key def create - customer = ::Stripe::Customer.create( - email: current_user.email, - source: params[:source] - ) + begin + customer = ::Stripe::Customer.create( + email: current_user.email, + source: params[:source] + ) - render_json_dump customer + 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 diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 0000000..65f071b --- /dev/null +++ b/app/models/customer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module DiscoursePatrons + class Customer < ActiveRecord::Base + self.table_name = "discourse_patrons_customers" + end +end diff --git a/db/migrate/20191025031631_create_customers.rb b/db/migrate/20191025031631_create_customers.rb new file mode 100644 index 0000000..3a2a579 --- /dev/null +++ b/db/migrate/20191025031631_create_customers.rb @@ -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 diff --git a/plugin.rb b/plugin.rb index 838436d..7e0be0c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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__) } diff --git a/spec/requests/customers_controller_spec.rb b/spec/requests/customers_controller_spec.rb index 0d8f0f7..0bf91d7 100644 --- a/spec/requests/customers_controller_spec.rb +++ b/spec/requests/customers_controller_spec.rb @@ -5,19 +5,29 @@ require 'rails_helper' module DiscoursePatrons RSpec.describe CustomersController do describe "create" do - let(:user) { Fabricate(:user, email: 'hello.2@example.com') } + describe "authenticated" do + let(:user) { Fabricate(:user, email: 'hello.2@example.com') } - before do - sign_in(user) - end + before do + sign_in(user) + end - it "creates a customer" do - ::Stripe::Customer.expects(:create).with( - email: 'hello.2@example.com', - source: 'tok_interesting' - ) + it "creates a stripe customer" do + ::Stripe::Customer.expects(:create).with( + email: 'hello.2@example.com', + source: 'tok_interesting' + ) - post "/patrons/customers.json", params: { source: 'tok_interesting' } + 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