Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Implement Gateway#create_profile #10

Merged
merged 4 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/solidus_paypal_braintree/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class SolidusPaypalBraintree::Customer < ApplicationRecord
belongs_to :user, class_name: "Spree::User"
has_many :sources, class_name: "SolidusPaypalBraintree::Source", inverse_of: :customer
end
15 changes: 14 additions & 1 deletion app/models/solidus_paypal_braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,20 @@ def cancel(response_code)
end
end

def create_profile(_payment)
# Creates a new customer profile in Braintree
#
# @api public
# @param payment [Spree::Payment]
# @return [SolidusPaypalBraintree::Customer]
def create_profile(payment)
source = payment.source

result = Braintree::Customer.create
customer_id = result.customer.id

source.create_customer!(braintree_customer_id: customer_id).tap do
source.save!
end
end

# @return [String]
Expand Down
3 changes: 3 additions & 0 deletions app/models/solidus_paypal_braintree/source.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class SolidusPaypalBraintree::Source < ApplicationRecord
belongs_to :user, class_name: "Spree::User"
belongs_to :payment_method, class_name: 'Spree::PaymentMethod'

belongs_to :customer, class_name: "SolidusPaypalBraintree::Customer"

# we are not currenctly supporting an "imported" flag
def imported
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def change
t.string :nonce
t.string :payment_type
t.integer :user_id, index: true
t.references :customer, index: true
t.references :payment_method, foreign_key: { to_table: :spree_payment_method }, index: { name: 'index_braintree_source_payment_method' }

t.timestamps null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateSolidusPaypalBraintreeCustomers < ActiveRecord::Migration
def change
create_table :solidus_paypal_braintree_customers do |t|
t.references :user
t.string :braintree_customer_id
end

add_index :solidus_paypal_braintree_customers, :user_id, unique: true, name: "index_braintree_customers_on_user_id"
add_index :solidus_paypal_braintree_customers, :braintree_customer_id, unique: true, name: "index_braintree_customers_on_braintree_customer_id"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
RSpec.describe SolidusPaypalBraintree::CheckoutsController, type: :controller do
include_context 'order ready for payment'

describe 'PATCH update' do
cassette_options = { cassette_name: "checkouts_controller/update" }
describe 'PATCH update', vcr: cassette_options do
subject(:patch_update) { patch :update, params }

let(:params) do
Expand Down
141 changes: 141 additions & 0 deletions spec/fixtures/cassettes/braintree/create_profile.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading