Skip to content
Open

V2 #14

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3a31334
permit next_renewal_at
papayaah Nov 8, 2016
b14c434
state machine to handle when a subscription is being renew
papayaah Nov 8, 2016
f582aef
changes to accommodate the subscription state machine
papayaah Nov 8, 2016
490e44f
locale settings for the subscription cancellation reasons
papayaah Nov 8, 2016
715324d
show cancellation data on the admin subscription page
papayaah Nov 8, 2016
4e1688a
migration to add the renewal dates
papayaah Nov 8, 2016
a1cd4f7
subscription renewal active job
papayaah Nov 8, 2016
3c69ff5
send admin renewal to the subscription renewal job
papayaah Nov 8, 2016
7b3e1d6
renew api endpoint
papayaah Nov 10, 2016
ded97c4
cancellation reasons and feedback fields
papayaah Nov 11, 2016
55dbb11
moved all the rest of the possible states into the subscription state…
papayaah Nov 14, 2016
2c3f07a
removing currency, will be depracted on solidus v2
papayaah Nov 14, 2016
0f5d6f7
use Spree during a call to locale setting so it is scoped to the spr…
papayaah Nov 14, 2016
a3963f9
make subscription factory start on the active state
papayaah Nov 14, 2016
b9b29d0
travis setting to fire off Travis CI
papayaah Nov 14, 2016
b5807be
bump up solidus version
papayaah Nov 14, 2016
8ab17d6
refactor some specs, by providing some convenient method to pull next…
papayaah Nov 14, 2016
61e3edb
remove portrait_small_url, this doesn't belong here and should be in …
papayaah Nov 15, 2016
cdde15b
fix subscription state column translation
Nov 15, 2016
9067afb
fix subscription state column translation
Nov 15, 2016
5aa2764
tabify the admin edit subscription page to be consistent with v1.4
papayaah Nov 15, 2016
1e2cc33
make sure adjust sku and failed renewals only show up under the subsc…
papayaah Nov 16, 2016
4f937ec
correct way to add menu to the admin sidebar
papayaah Nov 16, 2016
ace4b28
conflict fix
papayaah Nov 16, 2016
b472cde
rename to solidus_subscriptions
papayaah Nov 16, 2016
1665f8b
install phantom js before travis runs tests
papayaah Nov 18, 2016
044c0f0
no need for npm install
papayaah Nov 18, 2016
9d52949
do not unpause if resume is in the future
papayaah Nov 18, 2016
8be2c31
use poltergeist for capybara
papayaah Nov 18, 2016
04554a5
added capybara screenshot to test
papayaah Nov 18, 2016
b753afc
provide missing en setting that's breaking the spec
papayaah Nov 19, 2016
cb83fdc
using poltergeist as the default driver for capybara seem to be break…
papayaah Nov 19, 2016
e806f4b
subscription renewal spec
papayaah Nov 19, 2016
a7ae3bb
fix product selection
papayaah Dec 1, 2016
a6ba433
show sku of the variant
papayaah Dec 1, 2016
4af7a78
fix editing of item quantity and deleting
papayaah Dec 1, 2016
91a1f41
clean up and fix a few locale settings
papayaah Dec 1, 2016
1891f1f
updates adjust sku service to ignore canceled subscriptions and updat…
sherryson Dec 7, 2016
8dadbc7
Merge branch 'v2' into hotfix/update-adjust-sku
sherryson Dec 7, 2016
d953054
Merge pull request #15 from glossier/hotfix/update-adjust-sku
Dec 7, 2016
aa85ca9
Allow a renewing subscription to be cancelled
Dec 29, 2016
6cfb45b
rubocop fixes
papayaah Dec 5, 2016
a1307c5
added last_completed_order to better reflect what the method really p…
papayaah Jan 19, 2017
e931cea
correct syntax for the controller callbacks
papayaah Apr 3, 2017
bb90730
fix travis script
papayaah Apr 3, 2017
237dbd6
Match local version with Travis' version
hugobast Apr 11, 2017
c571241
Travis is making http requests to stripe
hugobast Apr 11, 2017
695cce7
Merge pull request #16 from glossier/fix/syntax-on-subscription-contr…
hugobast Apr 11, 2017
b970223
Fix/intl renewal (#17)
papayaah Aug 15, 2017
697ddd9
Set order currency in variant autocomplete (#18)
sherryson Aug 15, 2017
27c65cb
Fix/creating subscription from order (#19)
papayaah Aug 30, 2017
e8f3b74
Fix/copying addresses (#20)
papayaah Oct 16, 2017
7093833
filter option and listing stuck renewing subscriptions (#21)
papayaah Feb 2, 2018
797d312
Feature/admin improvements (#22)
papayaah Mar 6, 2018
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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.3
2.3.1
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: ruby

rvm:
- 2.3.1

script:
- bundle exec rake

before_install:
- "phantomjs --version"
- "export PATH=$PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64/bin:$PATH"
- "phantomjs --version"
- "if [ $(phantomjs --version) != '2.1.1' ]; then rm -rf $PWD/travis_phantomjs; mkdir -p $PWD/travis_phantomjs; fi"
- "if [ $(phantomjs --version) != '2.1.1' ]; then wget https://assets.membergetmember.co/software/phantomjs-2.1.1-linux-x86_64.tar.bz2 -O $PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2; fi"
- "if [ $(phantomjs --version) != '2.1.1' ]; then tar -xvf $PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C $PWD/travis_phantomjs; fi"
- "phantomjs --version"
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
source 'http://rubygems.org'

gem 'solidus', github: 'solidusio/solidus', branch: 'master'
gem 'rake', '< 11.0'
gem 'solidus', github: 'solidusio/solidus', branch: 'v1.4'
# Provides basic authentication functionality for testing parts of your engine
gem 'solidus_auth_devise', github: 'solidusio/solidus_auth_devise', branch: 'master'
gem 'active_model_serializers', '~> 0.8.3'
gem 'stripe'
gem 'slim-rails'
gem 'deface'

group :test do
gem 'factory_girl', '4.5.0'
Expand All @@ -17,6 +19,10 @@ group :test do
gem 'guard-rspec', require: false
gem 'simplecov', require: false
gem 'selenium-webdriver'
gem 'poltergeist'
gem 'capybara-screenshot'
gem 'vcr'
gem 'webmock'
end

group :development do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ $(document).ready ->
subscription_item_id = save.data('subscription-item-id')
quantity = parseInt(save.parents('tr').find('input.subscription_item_quantity').val())

toggleItemEdit()
adjustSubscriptionItem(subscription_item_id, quantity)
false

Expand All @@ -21,7 +20,6 @@ $(document).ready ->
del = $(this);
subscription_item_id = del.data('subscription-item-id');

toggleItemEdit()
deleteSubscriptionItem(subscription_item_id)

# handle adding
Expand All @@ -30,16 +28,16 @@ $(document).ready ->
variant = _.find(window.variants, (variant) ->
variant.id == variant_id
)

variantLineItemTemplate = HandlebarsTemplates["variants/line_items_autocomplete_stock"];
$('#stock_details').html variantLineItemTemplate(variant: variant)
$('#stock_details').show()
$('button.add_variant').click addSubscriptionVariant
# Add some tips
$('.with-tip').powerTip
smartPlacement: true
fadeInTime: 50
fadeOutTime: 50
intentPollInterval: 300

# handle the tabs
$('.subscription.tabs li > a').click ->
targetTab = $(this).data('target')
$('.subscription.tab-container > div').hide()
$('div#' + targetTab).show()


toggleSubscriptionItemEdit = ->
Expand All @@ -66,9 +64,11 @@ adjustSubscriptionItem = (subscription_item_id, quantity) ->
quantity: quantity
token: Spree.api_key
).done (msg) ->
show_flash 'success', 'Successfully updated the quantity.'
$('.subscription-item-qty-show').text(quantity)
$('a.edit-subscription-item').trigger 'click'
show_flash 'success', 'Successfully updated the item quantity.'
findSubscriptionItemRow(subscription_item_id)
.find('.subscription-item-qty-show').text(quantity)
findSubscriptionItemRow(subscription_item_id)
.find('a.edit-subscription-item').trigger 'click'

deleteSubscriptionItem = (subscription_item_id) ->
url = Spree.pathFor('api/subscriptions/' + subscription_id + '/subscription_items/' + subscription_item_id)
Expand Down Expand Up @@ -101,4 +101,7 @@ adjustSubscriptionItems = (subscription_id, variant_id, quantity) ->
subscription_item:
variant_id: variant_id
quantity: quantity
token: Spree.api_key).done (msg) ->
token: Spree.api_key).done (msg) ->

findSubscriptionItemRow = (subscription_item_id) ->
$('tr#subscription-item-' + subscription_item_id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* css for spree_subscription gem specific */

.tab-container > div:not(:first-child) {
display: none;
}

This file was deleted.

28 changes: 15 additions & 13 deletions app/controllers/spree/admin/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def new

# build subscription addresses
user = order.user
@subscription.build_ship_address(order.ship_address.dup.attributes.merge({user_id: user.id}))
@subscription.build_bill_address(order.bill_address.dup.attributes.merge({user_id: user.id}))
non_existing_attributes = Spree::Address.attribute_names - Spree::SubscriptionAddress.dup.attribute_names
@subscription.build_ship_address(order.ship_address.dup.attributes.except(*non_existing_attributes).merge({user_id: user.id}))
@subscription.build_bill_address(order.bill_address.dup.attributes.except(*non_existing_attributes).merge({user_id: user.id}))

# build items
build_subscription_items(@subscription, order)
Expand All @@ -38,15 +39,9 @@ def create
end

def renew
before_failure_count = @object.failure_count
::GenerateSubscriptionOrder.new(@object).call
SubscriptionRenewalJob.perform_later @subscription.id
flash[:success] = flash_message_for(@object, :being_renewed)

# check if the failure count has increase, that means we have an error
if @object.failure_count > before_failure_count
flash[:error] = flash_message_for(@object, :error_renew)
else
flash[:success] = flash_message_for(@object, :successfully_renewed)
end
respond_with(@object) do |format|
format.html { redirect_to location_after_save }
end
Expand Down Expand Up @@ -96,7 +91,13 @@ def credit_card
end

def failures
@subscriptions = Spree::Subscription.active.where('failure_count > 0').order('created_at desc')
params[:q] = {
combinator: 'and',
state_in: ['active', 'renewing'],
failure_count_gt: 0,
s: 'last_renewal_at desc'
}
@subscriptions = collection
end

def adjust_sku
Expand Down Expand Up @@ -142,11 +143,12 @@ def require_order_id
def build_subscription_from_order(order)
attrs = {
user_id: order.user.id,
email: order.email,
state: 'active',
interval: order.subscription_interval,
credit_card_id: order.credit_card_id_if_available
credit_card_id: order.credit_card_id_if_available,
}
order.build_subscription(attrs)
order.subscriptions.build(attrs)
end

def build_subscription_items(subscription, order)
Expand Down
20 changes: 12 additions & 8 deletions app/controllers/spree/api/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ module Api
class SubscriptionsController < Spree::Api::BaseController
before_action :find_subscription, except: [:index]

def self.prepended(base)
base.prepend_after_action :deliver_cancellation_email, only: [:cancel]
base.prepend_after_action :deliver_pause_email, only: [:pause]
# need to touch user so the address list is updated
base.prepend_after_action :touch_user, only: [:update_address, :create_address, :select_address]
end
after_action :deliver_cancellation_email, only: [:cancel]
after_action :deliver_pause_email, only: [:pause]
# need to touch user so the address list is updated
after_action :touch_user, only: [:update_address, :create_address, :select_address]

def index
render json: current_api_user.subscriptions,
Expand All @@ -32,6 +30,12 @@ def update
end
end

def renew
SubscriptionRenewalJob.perform_later @subscription.id

render_subscription
end

def skip_next_order
@subscription.skip_next_order

Expand Down Expand Up @@ -100,7 +104,7 @@ def select_address
# create a new credit card
# then assign it to the subscription
def create_credit_card
order = @subscription.last_order
order = @subscription.last_completed_order
credit_card = nil
begin
::Spree::CreditCard.transaction do
Expand Down Expand Up @@ -165,7 +169,7 @@ def permitted_address_params
end

def permitted_subscription_attributes
[:interval, :credit_card_id, :email]
[:interval, :credit_card_id, :email, :next_renewal_at]
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions app/jobs/create_subscription_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ def eligible_line_items(order)
end

def create_subscription_addresses(order, subscription, user)
subscription.create_ship_address!(order.ship_address.dup.attributes.merge({user_id: user.id}))
subscription.create_bill_address!(order.bill_address.dup.attributes.merge({user_id: user.id}))
non_existing_attributes = Spree::Address.attribute_names - Spree::SubscriptionAddress.dup.attribute_names
order_ship_address = order.ship_address.dup.attributes.except(*non_existing_attributes)
order_bill_address = order.bill_address.dup.attributes.except(*non_existing_attributes)

subscription.create_ship_address!(order_ship_address.merge({user_id: user.id}))
subscription.create_bill_address!(order_bill_address.merge({user_id: user.id}))
end

def create_subscription_items(eligible_line_items, subscription, interval)
Expand Down
19 changes: 19 additions & 0 deletions app/jobs/subscription_renewal_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SubscriptionRenewalJob < ActiveJob::Base
queue_as :default

def perform(subscription_id)
subscription = Spree::Subscription.find(subscription_id)
subscription.renew!

before_failure_count = subscription.failure_count
::GenerateSubscriptionOrder.new(subscription).call

# check if the failure count has increase, that means we have an error
if subscription.failure_count > before_failure_count
failed_order = subscription.orders.reorder('created_at desc').first
log = SubscriptionLog.find_by_order_id(failed_order.id)
else
subscription.renewed!
end
end
end
2 changes: 1 addition & 1 deletion app/mailers/spree/subscription_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def pause(subscription)
def set_default_variables(subject, campaign)
@subject = subject
@campaign = campaign
@order = @subscription.last_order
@order = @subscription.last_completed_order
@to_address = @order.email
@from_addess = from_address
end
Expand Down
44 changes: 44 additions & 0 deletions app/models/concerns/subscription_state_machine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module SubscriptionStateMachine
extend ActiveSupport::Concern
included do
class << self
def active
with_states %w(active renewing)
end
end

state_machine initial: :active do
event(:renew) { transition %i(active renewing) => :renewing }
event(:renewed) { transition renewing: :active }

after_transition any => :renewing, do: :mark_last_renewal!
after_transition renewing: :active, do: :adjust_next_renewal!


event :cancel do
transition all => :cancelled
end
event(:pause) { transition active: :paused }
event(:resume) { transition paused: :active }

after_transition on: :cancel do |subscription|
subscription.update_attributes(cancelled_at: Time.now)
end

after_transition on: :pause do |subscription|
subscription.update_attributes(pause_at: Time.now, resume_at: nil)
end

after_transition on: :resume do |subscription, transition|
resume_at = transition.args.first || Time.now
subscription.update_attributes(resume_at: resume_at)
# if resume is set at a future date, do not unpause
if resume_at.to_date > Date.today
subscription.update_attributes(state: 'paused')
else
subscription.update_attributes(pause_at: nil)
end
end
end
end
end
3 changes: 1 addition & 2 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def line_item_interval_match(line_item, options)
line_item.interval == options[:interval]
end


def subscription_interval
subscription ? subscription.interval : 4
subscription ? subscription.interval : 1
end

def subscription_products
Expand Down
7 changes: 7 additions & 0 deletions app/models/spree/order_subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Spree
class OrderSubscription < Spree::Base
self.table_name = :spree_orders_subscriptions
belongs_to :order
belongs_to :subscription
end
end
Loading