Skip to content

Commit

Permalink
add external payment phases
Browse files Browse the repository at this point in the history
  • Loading branch information
amandamfielding committed Nov 20, 2023
1 parent 355552e commit e731340
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/recurly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Recurly
require 'recurly/external_invoice'
require 'recurly/external_product'
require 'recurly/external_product_reference'
require 'recurly/external_payment_phase'
require 'recurly/external_subscription'
require 'recurly/helper'
require 'recurly/invoice'
Expand Down
1 change: 1 addition & 0 deletions lib/recurly/external_invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ExternalInvoice < Resource
belongs_to :site
belongs_to :account
belongs_to :external_subscription
belongs_to :external_payment_phase

# @return [[ExternalCharge], nil]
has_many :line_items, class_name: :ExternalCharge
Expand Down
27 changes: 27 additions & 0 deletions lib/recurly/external_payment_phase.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Recurly
class ExternalPaymentPhase < Resource

# @return [ExternalSubscription]
belongs_to :external_subscription

define_attribute_methods %w(
id
started_at
ends_at
starting_billing_period_index
ending_billing_period_index
offer_type
offer_name
period_count
period_length
amount
currency
created_at
updated_at
)

# We do not expose POST or PUT via the v2 API
protected(*%w(save save!))
private_class_method(*%w(create! create))
end
end
15 changes: 15 additions & 0 deletions lib/recurly/external_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class ExternalSubscription < Resource
# @return [ExternalInvoice]
has_many :external_invoices

# @return [ExternalPaymentPhase]
has_many :external_payment_phases

define_attribute_methods %w(
account
external_id
Expand All @@ -32,5 +35,17 @@ class ExternalSubscription < Resource
# We do not expose PUT or POST in the v2 API.
protected(*%w(save save!))
private_class_method(*%w(create! create))

def get_external_payment_phases
Pager.new(Recurly::ExternalPaymentPhase, uri: "#{path}/external_payment_phases", parent: self)
rescue Recurly::API::UnprocessableEntity => e
raise Invalid, e.message
end

def get_external_payment_phase(external_payment_phase_uuid)
ExternalPaymentPhase.from_response API.get("#{path}/external_payment_phases/#{external_payment_phase_uuid}")
rescue Recurly::API::UnprocessableEntity => e
raise Invalid, e.message
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/accounts/external_invoices/show-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Content-Type: application/xml; charset=utf-8
<external_invoice href="https://pcc.recurly.dev:3000/v2/external_invoices/sk0bmpw0wbby">
<account href="https://pcc.recurly.dev:3000/v2/accounts/AWE-348dds"/>
<external_subscription href="https://pcc.recurly.dev:3000/v2/external_subscriptions/sdam2lfeop3e"/>
<external_payment_phase href="https://pcc.recurly.dev:3000/v2/external_payment_phases/sdam2lfeop3e"/>
<external_id>abc123</external_id>
<state>paid</state>
<currency>USD</currency>
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/accounts/external_subscriptions/show-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Content-Type: application/xml; charset=utf-8
<external_subscription href="https://api.recurly.com/v2/external_subscriptions/ru8rz20qmmju">
<account href="https://api.recurly.com/v2/accounts/AWE-76"/>
<external_invoices href="https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e/external_invoices"/>
<external_payment_phases href="https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e/external_payment_phases"/>
<external_id>1234abcd</external_id>
<external_product_reference nil="nil"></external_product_reference>
<last_purchased nil="nil"></last_purchased>
Expand Down
21 changes: 21 additions & 0 deletions spec/fixtures/external_payment_phases/show-200.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<external_payment_phases type="array">
<external_payment_phase href="https://pcc.recurly.dev:3000/v2/external_payment_phases/sk0bmpw0wbby">
<id>sk0bmpw0wbby</id>
<started_at type="datetime">2023-03-14T19:55:07Z</started_at>
<ends_at type="datetime">2023-04-14T19:55:07Z</ends_at>
<starting_billing_period_index></starting_billing_period_index>
<ending_billing_period_index>4</ending_billing_period_index>
<offer_type></offer_type>
<offer_name></offer_name>
<period_count>1</period_count>
<period_length>2 MONTHS</period_length>
<amount>0.00</amount>
<currency>USD</currency>
<created_at type="datetime">2023-03-14T19:55:07Z</created_at>
<updated_at type="datetime">2023-03-14T19:55:07Z</updated_at>
</external_payment_phase>
</external_payment_phases>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Content-Type: application/xml; charset=utf-8
<external_invoice href="https://pcc.recurly.dev:3000/v2/external_invoices/sk0bmpw0wbby">
<account href="https://pcc.recurly.dev:3000/v2/accounts/AWE-348dds"/>
<external_subscription href="https://pcc.recurly.dev:3000/v2/external_subscriptions/sdam2lfeop3e"/>
<external_payment_phase href="https://pcc.recurly.dev:3000/v2/external_payment_phases/pexv3leeop4w"/>
<external_id>abc123</external_id>
<state>paid</state>
<currency>USD</currency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<external_payment_phases type="array">
<external_payment_phase href="https://pcc.recurly.dev:3000/v2/external_payment_phases/sk0bmpw0wbby">
<id>sk0bmpw0wbby</id>
<started_at type="datetime">2023-03-14T19:55:07Z</started_at>
<ends_at type="datetime">2023-04-14T19:55:07Z</ends_at>
<starting_billing_period_index></starting_billing_period_index>
<ending_billing_period_index>4</ending_billing_period_index>
<offer_type></offer_type>
<offer_name></offer_name>
<period_count>1</period_count>
<period_length>2 MONTHS</period_length>
<amount>0.00</amount>
<currency>USD</currency>
<created_at type="datetime">2023-03-14T19:55:07Z</created_at>
<updated_at type="datetime">2023-04-14T19:55:07Z</updated_at>
</external_payment_phase>
</external_payment_phases>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<external_payment_phase href="https://pcc.recurly.dev:3000/v2/external_payment_phases/sk0bmpw0wbby">
<id>sk0bmpw0wbby</id>
<started_at type="datetime">2023-03-14T19:55:07Z</started_at>
<ends_at type="datetime">2023-04-14T19:55:07Z</ends_at>
<starting_billing_period_index></starting_billing_period_index>
<ending_billing_period_index>4</ending_billing_period_index>
<offer_type></offer_type>
<offer_name></offer_name>
<period_count>1</period_count>
<period_length>2 MONTHS</period_length>
<amount>0.00</amount>
<currency>USD</currency>
<created_at type="datetime">2023-03-14T19:55:07Z</created_at>
<updated_at type="datetime">2023-04-14T19:55:07Z</updated_at>
</external_payment_phase>
1 change: 1 addition & 0 deletions spec/fixtures/external_subscriptions/show-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Content-Type: application/xml; charset=utf-8
<external_subscription href="https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e">
<account href="https://api.recurly.com/v2/accounts/AWE-348dds"/>
<external_invoices href="https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e/external_invoices"/>
<external_payment_phases href="https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e/external_payment_phases"/>
<external_id>external-id</external_id>
<external_product_reference>
<id>sd1eheqcn5a2</id>
Expand Down
75 changes: 75 additions & 0 deletions spec/recurly/external_payment_phase_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'spec_helper'

describe ExternalPaymentPhase do
let(:external_payment_phase) {
ExternalPaymentPhase.new(
id: 'sd28t3zdm59p',
external_subscription: ExternalSubscription.new(id: '123abc'),
started_at: '2023-01-23T19:02:40Z',
ends_at: '2023-02-23T19:02:40Z',
starting_billing_period_index: 1,
ending_billing_period_index: 4,
period_count: 1,
period_length: '2 MONTHS',
amount: 0.00,
currency: 'USD',
created_at: '2023-01-23T19:02:40Z',
updated_at: '2023-02-23T19:02:40Z'
)
}
let(:external_subscription) {
stub_api_request :get, 'external_subscriptions/sdam2lfeop3e', 'external_subscriptions/show-200'
ExternalSubscription.find('sdam2lfeop3e')
}

describe "#associations" do
it "has correct associations" do
expect(external_payment_phase.external_subscription).must_be_instance_of ExternalSubscription
end
end

describe "#methods" do
it "has correct attributes" do
expect(external_payment_phase.id).must_equal('sd28t3zdm59p')
expect(external_payment_phase.starting_billing_period_index).must_equal(1)
expect(external_payment_phase.created_at).must_equal('2023-01-23T19:02:40Z')
expect(external_payment_phase.updated_at).must_equal('2023-02-23T19:02:40Z')
end
end

describe ".get_external_payment_phases" do
before do
stub_api_request(
:get, "https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e/external_payment_phases",
"external_subscriptions/external_payment_phases/index-200"
)
end

it "returns external_payment_phases" do
external_payment_phase = external_subscription.get_external_payment_phases[0]

external_payment_phase.must_be_instance_of(ExternalPaymentPhase)
external_payment_phase.id.must_equal('sk0bmpw0wbby')
external_payment_phase.created_at.must_equal(DateTime.new(2023, 3, 14, 19, 55, 7))
external_payment_phase.updated_at.must_equal(DateTime.new(2023, 4, 14, 19, 55, 7))
end
end

describe ".get_external_payment_phase" do
before do
stub_api_request(
:get, "https://api.recurly.com/v2/external_subscriptions/sdam2lfeop3e/external_payment_phases/sk0bmpw0wbby",
"external_subscriptions/external_payment_phases/show-200"
)
end

it "returns an external payment phase" do
external_payment_phase = external_subscription.get_external_payment_phase('sk0bmpw0wbby')

external_payment_phase.must_be_instance_of(ExternalPaymentPhase)
external_payment_phase.id.must_equal('sk0bmpw0wbby')
external_payment_phase.created_at.must_equal(DateTime.new(2023, 3, 14, 19, 55, 7))
external_payment_phase.updated_at.must_equal(DateTime.new(2023, 4, 14, 19, 55, 7))
end
end
end
6 changes: 2 additions & 4 deletions spec/recurly/external_subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@
<last_purchased>2022-11-07 17:08:18</last_purchased>\
<quantity>1</quantity>\
<state>active</state>\
<trial_started_at>2022-11-07 17:08:18</trial_started_at>
<trial_ends_at>2022-11-14 17:08:18</trial_ends_at>
<canceled_at>2022-11-15 17:08:18</canceled_at>
<in_grace_period>false</in_grace_period>
<trial_ends_at>2022-11-14 17:08:18</trial_ends_at>\
<trial_started_at>2022-11-07 17:08:18</trial_started_at>\
<updated_at>2022-11-07 17:08:18</updated_at>\
</external_subscription>
XML
Expand Down

0 comments on commit e731340

Please sign in to comment.