forked from solidusio/solidus_stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync Solidus Users and Stripe Customers
Solves solidusio#26. Begins to solve solidusio#74.
- Loading branch information
1 parent
e3f5fa9
commit 32472ab
Showing
13 changed files
with
424 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module CreditCardDecorator | ||
def cc_type=(type) | ||
# See https://stripe.com/docs/api/cards/object#card_object-brand, | ||
# active_merchant/lib/active_merchant/billing/credit_card.rb, | ||
# and active_merchant/lib/active_merchant/billing/credit_card_methods.rb | ||
# (And see also the Solidus docs at core/app/models/spree/credit_card.rb, | ||
# which indicate that Solidus uses ActiveMerchant conventions by default.) | ||
self[:cc_type] = case type | ||
when 'American Express' | ||
'american_express' | ||
when 'Diners Club' | ||
'diners_club' | ||
when 'Discover' | ||
'discover' | ||
when 'JCB' | ||
'jcb' | ||
when 'MasterCard' | ||
'master' | ||
when 'UnionPay' | ||
'unionpay' | ||
when 'Visa' | ||
'visa' | ||
when 'Unknown' | ||
super('') | ||
else | ||
super(type) | ||
end | ||
end | ||
|
||
::Spree::CreditCard.prepend(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module OrderDecorator | ||
include StripeApiMethods | ||
|
||
def stripe_customer_params | ||
stripe_customer_params_from_addresses(bill_address, ship_address, email) | ||
end | ||
|
||
::Spree::Order.prepend(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module UserDecorator | ||
include StripeApiMethods | ||
|
||
def self.prepended(base) | ||
base.after_create :create_stripe_customer | ||
base.after_update :update_stripe_customer | ||
end | ||
|
||
def stripe_customer | ||
Stripe::Customer.retrieve(stripe_customer_id) if stripe_customer_id.present? | ||
end | ||
|
||
def create_stripe_customer | ||
stripe_customer = Stripe::Customer.create(self.stripe_params) | ||
update_column(:stripe_customer_id, stripe_customer.id) | ||
stripe_customer | ||
end | ||
|
||
def update_stripe_customer | ||
Stripe::Customer.update(stripe_customer_id, self.stripe_params) | ||
end | ||
|
||
def delete_stripe_customer | ||
if stripe_customer_id.present? | ||
deleted_user = Stripe::Customer.delete(stripe_customer_id) | ||
update_column(:stripe_customer_id, nil) | ||
deleted_user | ||
end | ||
end | ||
|
||
def stripe_params | ||
stripe_customer_params_from_addresses(bill_address, ship_address, email) | ||
end | ||
|
||
::Spree.user_class.prepend(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module StripeApiMethods | ||
extend ActiveSupport::Concern | ||
|
||
def stripe_customer_params_from_addresses(bill_address, ship_address, email) | ||
{ | ||
address: stripe_address_hash(bill_address), | ||
email: email, | ||
name: bill_address.try(:name) || bill_address&.full_name, | ||
phone: bill_address&.phone, | ||
shipping: { | ||
address: stripe_address_hash(ship_address), | ||
name: ship_address.try(:name) || ship_address&.full_name, | ||
phone: ship_address&.phone | ||
} | ||
} | ||
end | ||
|
||
def stripe_address_hash(address) | ||
{ | ||
city: address&.city, | ||
country: address&.country&.iso, | ||
line1: address&.address1, | ||
line2: address&.address2, | ||
postal_code: address&.zipcode, | ||
state: address&.state_text | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Stripe.api_key = Spree::PaymentMethod::StripeCreditCard.last&.preferences&.dig(:secret_key) |
25 changes: 25 additions & 0 deletions
25
db/migrate/20200726022620_add_stripe_customer_id_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStripeCustomerIdToUsers < SolidusSupport::Migration[5.1] | ||
def up | ||
add_column Spree.user_class.table_name.to_sym, :stripe_customer_id, :string, unique: true | ||
|
||
Spree::User.includes(bill_address: :country, ship_address: :country).find_each do |u| | ||
user_stripe_payment_sources = u&.wallet&.wallet_payment_sources&.select do |wps| | ||
wps.payment_source.payment_method.type == 'Spree::PaymentMethod::StripeCreditCard' | ||
end | ||
payment_customer_id = user_stripe_payment_sources&.map { |ps| ps&.payment_source&.gateway_customer_profile_id }.compact.last | ||
|
||
if payment_customer_id.present? | ||
u.update_column(:stripe_customer_id, payment_customer_id) | ||
u.update_stripe_customer | ||
else | ||
u.create_stripe_customer | ||
end | ||
end | ||
end | ||
|
||
def down | ||
remove_column Spree.user_class.table_name.to_sym, :stripe_customer_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
require "solidus_support" | ||
require "solidus_stripe/engine" | ||
require "solidus_stripe/version" | ||
require "stripe" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.