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

Commit

Permalink
Extract out helper method. Additional specs
Browse files Browse the repository at this point in the history
  • Loading branch information
osahyoun committed Jun 6, 2016
1 parent 2c0d70d commit 38b9a89
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
23 changes: 23 additions & 0 deletions lib/payment_processor/go_cardless/helper.rb
@@ -0,0 +1,23 @@
module PaymentProcessor
module GoCardless
module Helper
extend self

# Returns the next date for the passed day.
#
# === Attributes
#
# *day+ - The day to set the date too.
def next_available_date(day)
date = Date.today.change(day: day)

if Time.now.day >= day
date += 1.month
end

date
end
end
end
end

14 changes: 2 additions & 12 deletions lib/payment_processor/go_cardless/populator.rb
Expand Up @@ -28,7 +28,7 @@ def subscription_params
interval_unit: "monthly"
}.tap do |params|
if bacs?
params[:start_date] = subscription_start_date
params[:start_date] = Helper.next_available_date(Settings.gocardless.gbp_charge_day.to_i)
end
end
)
Expand Down Expand Up @@ -77,18 +77,8 @@ def client
)
end

def subscription_start_date
date = Date.today.change(day:20)

if Time.now.day >= settings_charge_day
date += 1.month
end

date
end

def charge_date
if Settings.gocardless.gbp_charge_day.blank? || mandate.scheme.downcase != 'bacs'
if Settings.gocardless.gbp_charge_day.blank? || !bacs?
return mandate.next_possible_charge_date
end

Expand Down
39 changes: 39 additions & 0 deletions spec/lib/payment_processor/go_cardless/helper_spec.rb
@@ -0,0 +1,39 @@
require 'rails_helper'

describe PaymentProcessor::GoCardless::Helper do
describe '.next_available_date' do
subject { PaymentProcessor::GoCardless::Helper }

context 'day has not passed' do
it 'returns current month' do
Timecop.freeze('5 May') do
expect(subject.next_available_date(6)).to eq(Date.parse('6 May'))
end
end
end

context 'day is today' do
it 'returns with following month' do
Timecop.freeze('5 May') do
expect(subject.next_available_date(5)).to eq(Date.parse('5 June'))
end
end
end

context 'day has passed' do
it 'returns with following month' do
Timecop.freeze('5 May') do
expect(subject.next_available_date(4)).to eq(Date.parse('4 June'))
end
end
end

context 'invalid day' do
it 'raises argument error' do
expect {
subject.next_available_date(32)
}.to raise_error(ArgumentError, 'invalid date')
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/payment_processor/go_cardless/subscription_spec.rb
Expand Up @@ -32,11 +32,13 @@ module GoCardless
links: double(customer: 'CU00000', mandate: 'MA00000')
)
end

let(:mandate) do
instance_double('GoCardlessPro::Resources::Mandate',
id: 'MA00000', scheme: 'sepa', next_possible_charge_date: 1.day.from_now
)
end

let(:subscription) { instance_double('GoCardlessPro::Resources::Subscription', id: 'SU00000') }

let(:amount_in_dollars){ 12.5 }
Expand All @@ -57,6 +59,28 @@ module GoCardless

include_examples 'transaction and subscription', :make_subscription

describe 'charge date' do
subject { described_class.make_subscription(required_options) }

let(:mandate) do
instance_double('GoCardlessPro::Resources::Mandate',
id: 'MA00000',
scheme: 'bacs',
next_possible_charge_date: 1.day.from_now )
end

it 'sets start date for bacs' do
required_options
expect_any_instance_of(
GoCardlessPro::Services::SubscriptionsService
).to receive(:create).with(
params: hash_including(start_date: instance_of(Date))
)

subject
end
end

describe 'calling the GC SDK' do
it 'creates a subscription with the right params' do
expect_any_instance_of(
Expand Down
8 changes: 2 additions & 6 deletions spec/requests/api/go_cardless/go_cardless_spec.rb
Expand Up @@ -401,19 +401,15 @@

shared_examples 'successful subscription' do
it 'passes the correct data to the GoCardless Payment SDK' do

sdk_params[:params] = sdk_params[:params].merge(
name: "donation",
interval_unit: "monthly",
start_date: Date.parse("2016/02/20")
start_date: instance_of(Date)
)
subscriptions_service = instance_double(GoCardlessPro::Services::SubscriptionsService, create: double(id: 'asdf'))
allow_any_instance_of(GoCardlessPro::Client).to receive(:subscriptions).and_return(subscriptions_service)
expect(subscriptions_service).to receive(:create).with(sdk_params)

Timecop.freeze('2016/01/21') do
subject
end
subject
end

it 'posts donation action to queue with correct data' do
Expand Down

0 comments on commit 38b9a89

Please sign in to comment.