Kaynağa Gözat

Merge pull request #221 from Shopify/usage_charge_resource

adding usage charge resource
Jason Zhao 9 yıl önce
ebeveyn
işleme
5f93f6adea

+ 8 - 0
lib/shopify_api/resources/recurring_application_charge.rb

@@ -12,6 +12,10 @@ module ShopifyAPI
       end
     end
 
+    def usage_charges
+      UsageCharge.find(:all, params: { recurring_application_charge_id: id })
+    end
+
     def cancel
       load_attributes_from_response(self.destroy)
     end
@@ -19,5 +23,9 @@ module ShopifyAPI
     def activate
       load_attributes_from_response(post(:activate))
     end
+
+    def customize(customize_recurring_app_charge_params = {})
+      load_attributes_from_response(put(:customize, recurring_application_charge: customize_recurring_app_charge_params ))
+    end
   end
 end

+ 5 - 0
lib/shopify_api/resources/usage_charge.rb

@@ -0,0 +1,5 @@
+module ShopifyAPI
+  class UsageCharge < Base
+    init_prefix :recurring_application_charge
+  end
+end

+ 2 - 0
test/fixtures/recurring_application_charge.json

@@ -13,6 +13,8 @@
     "test": null,
     "trial_days": 0,
     "trial_ends_on": null,
+    "terms": "$1 per 1000 emails",
+    "capped_amount": "100.00",
     "updated_at": "2014-01-20T13:42:19-05:00",
     "decorated_return_url": "http://super-duper.shopifyapps.com?charge_id=654381177",
     "confirmation_url": "http://apple.myshopify.com/admin/charges/654381177/confirm_recurring_application_charge?signature=BAhpBHkQASc%3D--419fc7424f8c290ac2b21b9004ed223e35b52164"

+ 5 - 0
test/fixtures/recurring_application_charge_adjustment.json

@@ -0,0 +1,5 @@
+{
+  "recurring_application_charge": {
+    "update_capped_amount_url": "http://apple.myshopify.com/admin/charges/654381177/confirm_update_capped_amount?signature=BAhpBHkQASc%3D--419fc7424f8c290ac2b21b9004ed223e35b52164"
+  }
+}

+ 11 - 0
test/fixtures/usage_charge.json

@@ -0,0 +1,11 @@
+{
+  "usage_charge": {
+    "id": 359376002,
+    "description": "1000 emails",
+    "price": "9.00",
+    "recurring_application_charge_id": 654381177,
+    "billing_on": "2015-07-08",
+    "balance_remaining": "91.00",
+    "balance_used": "9.00"
+  }
+}

+ 23 - 0
test/fixtures/usage_charges.json

@@ -0,0 +1,23 @@
+{
+  "usage_charges": [
+    {
+      "id": 359376005,
+      "description": "1000 emails",
+      "price": "9.00",
+      "recurring_application_charge_id": 654381177,
+      "billing_on": "2015-07-08",
+      "balance_remaining": "82.00",
+      "balance_used": "18.00"
+
+    },
+    {
+      "id": 359376006,
+      "description": "1000 emails",
+      "price": "9.00",
+      "recurring_application_charge_id": 654381177,
+      "billing_on": "2015-07-08",
+      "balance_remaining": "82.00",
+      "balance_used": "18.00"
+    }
+  ]
+}

+ 45 - 17
test/recurring_application_charge_test.rb

@@ -3,7 +3,7 @@ require 'test_helper'
 class RecurringApplicationChargeTest < Test::Unit::TestCase
 
   def test_recurring_application_charges_create
-    fake "recurring_application_charges", :method => :post, :status => 201, :body => load_fixture('recurring_application_charge')
+    fake "recurring_application_charges", method: :post, status: 201, body: load_fixture('recurring_application_charge')
 
     charge = ShopifyAPI::RecurringApplicationCharge.create(
       name: "Default Plan",
@@ -15,7 +15,7 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_get_recurring_application_charges
-    fake "recurring_application_charges/654381177", :method => :get, :status => 201, :body => load_fixture('recurring_application_charge')
+    fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
 
     charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
 
@@ -23,7 +23,7 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_list_recurring_application_charges
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
     charge = ShopifyAPI::RecurringApplicationCharge.find(:all)
 
@@ -31,23 +31,23 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_list_since_recurring_application_charges
-    fake "recurring_application_charges.json?since_id=64512345",:extension => false, :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges.json?since_id=64512345",extension: false, method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
-    charge = ShopifyAPI::RecurringApplicationCharge.find(:all, :params => {:since_id => '64512345'})
+    charge = ShopifyAPI::RecurringApplicationCharge.find(:all, params: { since_id: '64512345' })
 
     assert_equal "Super Mega Plan", charge.last.name
   end
 
   def test_list_fields_recurring_application_charges
-    fake "recurring_application_charges.json?fields=name",:extension => false, :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges.json?fields=name",extension: false, method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
-    charge = ShopifyAPI::RecurringApplicationCharge.find(:all, :params => {:fields => 'name'})
+    charge = ShopifyAPI::RecurringApplicationCharge.find(:all, params: { fields: 'name' })
 
     assert_equal "Super Mega Plan", charge.last.name
   end
 
   def test_pending_recurring_application_charge
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
     charge = ShopifyAPI::RecurringApplicationCharge.pending
 
@@ -55,7 +55,7 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_cancelled_recurring_application_charge
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
     charge = ShopifyAPI::RecurringApplicationCharge.cancelled
 
@@ -63,7 +63,7 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_accepted_recurring_application_charge
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
     charge = ShopifyAPI::RecurringApplicationCharge.accepted
 
@@ -72,7 +72,7 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_declined_recurring_application_charge
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
 
     charge = ShopifyAPI::RecurringApplicationCharge.declined
 
@@ -80,22 +80,51 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_activate_recurring_application_charge
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
-    fake "recurring_application_charges/455696199/activate", :method => :post, :status => 200, :body => "{}"
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
+    fake "recurring_application_charges/455696199/activate", method: :post, status: 200, body: "{}"
 
     charge = ShopifyAPI::RecurringApplicationCharge.accepted
 
     assert charge.last.activate
   end
 
+  def test_adjust_recurring_application_charge
+    fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
+    fake "recurring_application_charges/654381177/customize.json?recurring_application_charge%5Bcapped_amount%5D=200", method: :put, body: load_fixture('recurring_application_charge_adjustment'), extension: false
+
+    charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
+
+    assert charge.customize(capped_amount: 200)
+  end
+
   def test_cancel_recurring_application_charge
-    fake "recurring_application_charges", :method => :get, :status => 201, :body => load_fixture('recurring_application_charges')
-    fake "recurring_application_charges/455696194", :method => :delete, :status => 200, :body => "{}"
+    fake "recurring_application_charges", method: :get, status: 201, body: load_fixture('recurring_application_charges')
+    fake "recurring_application_charges/455696194", method: :delete, status: 200, body: "{}"
 
     charge = ShopifyAPI::RecurringApplicationCharge.current
     assert charge.cancel
   end
 
+  def test_usage_charges_recurring_application_charge_found
+    fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
+    fake "recurring_application_charges/654381177/usage_charges", method: :get, status: 201, body: load_fixture('usage_charges')
+
+    charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
+    usage_charges = charge.usage_charges
+
+    assert_equal 2, usage_charges.length
+  end
+
+  def test_usage_charges_recurring_application_charge_not_found
+    fake "recurring_application_charges/654381177", method: :get, status: 201, body: load_fixture('recurring_application_charge')
+    fake "recurring_application_charges/654381177/usage_charges", method: :get, status: 201, body: "[]"
+
+    charge = ShopifyAPI::RecurringApplicationCharge.find(654381177)
+    usage_charges = charge.usage_charges
+
+    assert_equal 0, usage_charges.length
+  end
+
   def test_no_recurring_application_charge_found
     fake "recurring_application_charges", body: {recurring_application_charges: []}.to_json
 
@@ -105,7 +134,7 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
   end
 
   def test_recurring_application_charge_not_found_error
-    fake "recurring_application_charges", :body => '{"errors":"Not Found"}', :status => 404
+    fake "recurring_application_charges", body: '{"errors":"Not Found"}', status: 404
 
     all_application_charges = ShopifyAPI::RecurringApplicationCharge.all
     if ActiveResource::VERSION::MAJOR >= 4 && ActiveResource::VERSION::MINOR >= 2
@@ -117,5 +146,4 @@ class RecurringApplicationChargeTest < Test::Unit::TestCase
     assert_equal nil, ShopifyAPI::RecurringApplicationCharge.current
     assert_equal [],  ShopifyAPI::RecurringApplicationCharge.pending
   end
-
 end

+ 21 - 0
test/usage_charge_test.rb

@@ -0,0 +1,21 @@
+require 'test_helper'
+
+class UsageChargeTest < Test::Unit::TestCase
+
+  def test_create_usage_charges
+    fake "recurring_application_charges/654381177/usage_charges", method: :post, body: load_fixture('usage_charge')
+
+    usage_charge = ShopifyAPI::UsageCharge.new(description: '1000 emails', price: 1.0)
+    usage_charge.prefix_options = {recurring_application_charge_id: 654381177}
+    assert usage_charge.save
+  end
+
+  def test_get_usage_charges
+    fake "recurring_application_charges/654381177/usage_charges/359376002", method: :get, status: 201, body: load_fixture('usage_charge')
+
+    usage_charge = ShopifyAPI::UsageCharge.find(359376002, params: {recurring_application_charge_id: 654381177})
+
+    assert_equal "1000 emails", usage_charge.description
+  end
+
+end