-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds current shop, recurring application charges
Adds utility methods to find current shop and current recurring application charges, functionality that had been available in v9 of API when it used ActiveResource. Fixes 923
- Loading branch information
1 parent
158aca9
commit 6b4ffdc
Showing
6 changed files
with
280 additions
and
0 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
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,22 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ShopifyAPI | ||
module Utils | ||
extend T::Sig | ||
|
||
sig do | ||
params( | ||
fields: T.untyped | ||
).returns(T.nilable(ShopifyAPI::Shop)) | ||
end | ||
def self.current_shop(fields: nil) | ||
ShopifyAPI::Shop.all(fields: fields).first | ||
end | ||
|
||
sig { returns(T.nilable(ShopifyAPI::RecurringApplicationCharge)) } | ||
def self.current_recurring_application_charge | ||
ShopifyAPI::RecurringApplicationCharge.all.find { |c| c.status == "active" } | ||
end | ||
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,106 @@ | ||
{ | ||
"recurring_application_charges": [ | ||
{ | ||
"activated_on": null, | ||
"api_client_id": 755357713, | ||
"billing_on": "2014-01-19T00:00:00+00:00", | ||
"cancelled_on": null, | ||
"created_at": "2014-01-20T13:41:37-05:00", | ||
"id": 455696194, | ||
"name": "Super Mega Plan2", | ||
"price": "15.00", | ||
"return_url": "http://yourapp.com", | ||
"status": "active", | ||
"test": null, | ||
"trial_days": 0, | ||
"trial_ends_on": null, | ||
"updated_at": "2014-01-20T13:42:20-05:00", | ||
"decorated_return_url": "http://yourapp.com?charge_id=455696194" | ||
}, | ||
{ | ||
"activated_on": null, | ||
"api_client_id": 755357713, | ||
"billing_on": "2014-01-19T00:00:00+00:00", | ||
"cancelled_on": null, | ||
"created_at": "2014-01-20T13:41:37-05:00", | ||
"id": 455696195, | ||
"name": "Super Mega Plan2", | ||
"price": "15.00", | ||
"return_url": "http://yourapp.com", | ||
"status": "pending", | ||
"test": null, | ||
"trial_days": 0, | ||
"trial_ends_on": null, | ||
"updated_at": "2014-01-20T13:42:20-05:00", | ||
"decorated_return_url": "http://yourapp.com?charge_id=455696195" | ||
}, | ||
{ | ||
"activated_on": null, | ||
"api_client_id": 755357713, | ||
"billing_on": "2014-01-19T00:00:00+00:00", | ||
"cancelled_on": null, | ||
"created_at": "2014-01-20T13:41:37-05:00", | ||
"id": 455696196, | ||
"name": "Super Mega Plan3", | ||
"price": "15.00", | ||
"return_url": "http://yourapp.com", | ||
"status": "cancelled", | ||
"test": null, | ||
"trial_days": 0, | ||
"trial_ends_on": null, | ||
"updated_at": "2014-01-20T13:42:20-05:00", | ||
"decorated_return_url": "http://yourapp.com?charge_id=455696196" | ||
}, | ||
{ | ||
"activated_on": null, | ||
"api_client_id": 755357713, | ||
"billing_on": "2014-01-19T00:00:00+00:00", | ||
"cancelled_on": null, | ||
"created_at": "2014-01-20T13:41:37-05:00", | ||
"id": 455696197, | ||
"name": "Super Mega Plan4", | ||
"price": "15.00", | ||
"return_url": "http://yourapp.com", | ||
"status": "accepted", | ||
"test": null, | ||
"trial_days": 0, | ||
"trial_ends_on": null, | ||
"updated_at": "2014-01-20T13:42:20-05:00", | ||
"decorated_return_url": "http://yourapp.com?charge_id=455696197" | ||
}, | ||
{ | ||
"activated_on": null, | ||
"api_client_id": 755357713, | ||
"billing_on": "2014-01-19T00:00:00+00:00", | ||
"cancelled_on": null, | ||
"created_at": "2014-01-20T13:41:37-05:00", | ||
"id": 455696198, | ||
"name": "Super Mega Plan5", | ||
"price": "15.00", | ||
"return_url": "http://yourapp.com", | ||
"status": "declined", | ||
"test": null, | ||
"trial_days": 0, | ||
"trial_ends_on": null, | ||
"updated_at": "2014-01-20T13:42:20-05:00", | ||
"decorated_return_url": "http://yourapp.com?charge_id=455696198" | ||
}, | ||
{ | ||
"activated_on": null, | ||
"api_client_id": 755357713, | ||
"billing_on": "2014-01-19T00:00:00+00:00", | ||
"cancelled_on": null, | ||
"created_at": "2014-01-20T13:41:37-05:00", | ||
"id": 455696199, | ||
"name": "Super Mega Plan", | ||
"price": "15.00", | ||
"return_url": "http://yourapp.com", | ||
"status": "accepted", | ||
"test": null, | ||
"trial_days": 0, | ||
"trial_ends_on": null, | ||
"updated_at": "2014-01-20T13:42:20-05:00", | ||
"decorated_return_url": "http://yourapp.com?charge_id=455696199" | ||
} | ||
] | ||
} |
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,23 @@ | ||
{ | ||
"shop": { | ||
"id": 548380009, | ||
"name": "John Smith Test Store", | ||
"email": "[email protected]", | ||
"domain": "shop.apple.com", | ||
"province": "California", | ||
"country": "US", | ||
"address1": "1 Infinite Loop", | ||
"zip": "95014", | ||
"city": "Cupertino", | ||
"address2": "Suite 100", | ||
"created_at": "2007-12-31T19:00:00-05:00", | ||
"updated_at": "2022-04-05T12:52:43-04:00", | ||
"country_code": "US", | ||
"country_name": "United States", | ||
"currency": "USD", | ||
"customer_email": "[email protected]", | ||
"timezone": "(GMT-05:00) Eastern Time (US & Canada)", | ||
"iana_timezone": "America/New_York", | ||
"shop_owner": "John Smith" | ||
} | ||
} |
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,118 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require_relative "./test_helper" | ||
|
||
module ShopifyAPITest | ||
class UtilsTest < Test::Unit::TestCase | ||
extend T::Sig | ||
|
||
def setup | ||
super | ||
|
||
test_session = ShopifyAPI::Auth::Session.new( | ||
id: "id", | ||
shop: "test-shop.myshopify.io", | ||
access_token: "this_is_a_test_token" | ||
) | ||
ShopifyAPI::Context.activate_session(test_session) | ||
modify_context(api_version: "2022-04") | ||
end | ||
|
||
def teardown | ||
super | ||
|
||
ShopifyAPI::Context.deactivate_session | ||
end | ||
|
||
sig { void } | ||
def test_current_shop | ||
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json") | ||
.with( | ||
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" }, | ||
body: {} | ||
) | ||
.to_return(status: 200, body: load_fixture("shop"), headers: {}) | ||
|
||
current_shop = ShopifyAPI::Utils.current_shop() | ||
|
||
assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json") | ||
assert_equal(548380009, current_shop.id) | ||
assert_equal("John Smith Test Store", current_shop.name) | ||
assert_equal("[email protected]", current_shop.email) | ||
assert_equal("shop.apple.com", current_shop.domain) | ||
assert_equal("1 Infinite Loop", current_shop.address1) | ||
assert_equal("Suite 100", current_shop.address2) | ||
assert_equal("Cupertino", current_shop.city) | ||
assert_equal("California", current_shop.province) | ||
assert_equal("US", current_shop.country) | ||
assert_equal("95014", current_shop.zip) | ||
end | ||
|
||
sig { void } | ||
def test_current_shop_with_fields | ||
fields = "address1,address2,city,province,country" | ||
test_shop = JSON.parse(load_fixture("shop")) | ||
shop_with_fields_only = { | ||
"shop" => test_shop["shop"].select { |k, _v| fields.split(",").include?(k) }, | ||
} | ||
|
||
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json?fields=address1%2Caddress2%2Ccity%2Cprovince%2Ccountry") | ||
.with( | ||
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" }, | ||
body: {} | ||
) | ||
.to_return(status: 200, body: JSON.generate(shop_with_fields_only), headers: {}) | ||
|
||
# current_shop = ShopifyAPI::Utils.current_shop(fields: fields) | ||
current_shop = ShopifyAPI::Shop.all(fields: fields).first | ||
|
||
assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/shop.json?fields=address1%2Caddress2%2Ccity%2Cprovince%2Ccountry") | ||
assert_equal("1 Infinite Loop", current_shop.address1) | ||
assert_equal("Suite 100", current_shop.address2) | ||
assert_equal("Cupertino", current_shop.city) | ||
assert_equal("California", current_shop.province) | ||
assert_equal("US", current_shop.country) | ||
end | ||
|
||
sig { void } | ||
def test_current_recurring_application_charge | ||
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json") | ||
.with( | ||
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" }, | ||
body: {} | ||
) | ||
.to_return(status: 200, body: load_fixture("recurring_application_charges"), headers: {}) | ||
|
||
current_recurring_application_charge = ShopifyAPI::Utils.current_recurring_application_charge() | ||
|
||
assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json") | ||
assert_equal(455696194, current_recurring_application_charge.id) | ||
end | ||
|
||
sig { void } | ||
def test_current_recurring_application_charge_no_active | ||
recurring_application_charges = JSON.parse(load_fixture("recurring_application_charges")) | ||
|
||
no_active_recurring_application_charges = { | ||
"recurring_application_charges" => | ||
recurring_application_charges["recurring_application_charges"].select { |c| c["status"] != "active" }, | ||
} | ||
|
||
stub_request(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json") | ||
.with( | ||
headers: { "X-Shopify-Access-Token" => "this_is_a_test_token", "Accept" => "application/json" }, | ||
body: {} | ||
) | ||
.to_return(status: 200, body: JSON.generate(no_active_recurring_application_charges), headers: {}) | ||
|
||
# current_recurring_application_charge = ShopifyAPI::Utils.current_recurring_application_charge() | ||
current_recurring_application_charge = ShopifyAPI::RecurringApplicationCharge.all.find do |c| | ||
c.status == "active" | ||
end | ||
|
||
assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2022-04/recurring_application_charges.json") | ||
assert_nil(current_recurring_application_charge) | ||
end | ||
end | ||
end |