Julio Napurí 9 anni fa
parent
commit
e3886bb50a
3 ha cambiato i file con 157 aggiunte e 0 eliminazioni
  1. 13 0
      lib/shopify_api/resources/refund.rb
  2. 112 0
      test/fixtures/refund.json
  3. 32 0
      test/refund_test.rb

+ 13 - 0
lib/shopify_api/resources/refund.rb

@@ -0,0 +1,13 @@
+module ShopifyAPI
+  class Refund < Base
+    init_prefix :order
+
+    def self.calculate(*args)
+      options = { :refund => args[0] }
+      params = options.merge(args[1][:params]) if args[1] && args[1][:params]
+      self.prefix = "/admin/orders/#{params[:order_id]}/"
+      resource = post(:calculate, {}, options.to_json)
+      instantiate_record(format.decode(resource.body), {})
+    end
+  end
+end

+ 112 - 0
test/fixtures/refund.json

@@ -0,0 +1,112 @@
+{
+  "refund": {
+    "id": 509562969,
+    "order_id": 450789469,
+    "created_at": "2016-03-22T11:15:00-04:00",
+    "note": "it broke during shipping",
+    "restock": true,
+    "user_id": 799407056,
+    "refund_line_items": [
+      {
+        "id": 104689539,
+        "quantity": 1,
+        "line_item_id": 703073504,
+        "line_item": {
+          "id": 703073504,
+          "variant_id": 457924702,
+          "title": "IPod Nano - 8gb",
+          "quantity": 1,
+          "price": "199.00",
+          "grams": 200,
+          "sku": "IPOD2008BLACK",
+          "variant_title": "black",
+          "vendor": null,
+          "fulfillment_service": "manual",
+          "product_id": 632910392,
+          "requires_shipping": true,
+          "taxable": true,
+          "gift_card": false,
+          "name": "IPod Nano - 8gb - black",
+          "variant_inventory_management": "shopify",
+          "properties": [
+          ],
+          "product_exists": true,
+          "fulfillable_quantity": 1,
+          "total_discount": "0.00",
+          "fulfillment_status": null,
+          "tax_lines": [
+            {
+              "title": "State Tax",
+              "price": "3.98",
+              "rate": 0.06
+            }
+          ]
+        }
+      },
+      {
+        "id": 709875399,
+        "quantity": 1,
+        "line_item_id": 466157049,
+        "line_item": {
+          "id": 466157049,
+          "variant_id": 39072856,
+          "title": "IPod Nano - 8gb",
+          "quantity": 1,
+          "price": "199.00",
+          "grams": 200,
+          "sku": "IPOD2008GREEN",
+          "variant_title": "green",
+          "vendor": null,
+          "fulfillment_service": "manual",
+          "product_id": 632910392,
+          "requires_shipping": true,
+          "taxable": true,
+          "gift_card": false,
+          "name": "IPod Nano - 8gb - green",
+          "variant_inventory_management": "shopify",
+          "properties": [
+            {
+              "name": "Custom Engraving Front",
+              "value": "Happy Birthday"
+            }
+          ],
+          "product_exists": true,
+          "fulfillable_quantity": 1,
+          "total_discount": "0.00",
+          "fulfillment_status": null,
+          "tax_lines": [
+            {
+              "title": "State Tax",
+              "price": "3.98",
+              "rate": 0.06
+            }
+          ]
+        }
+      }
+    ],
+    "transactions": [
+      {
+        "id": 179259969,
+        "order_id": 450789469,
+        "amount": "209.00",
+        "kind": "refund",
+        "gateway": "bogus",
+        "status": "success",
+        "message": null,
+        "created_at": "2005-08-05T12:59:12-04:00",
+        "test": false,
+        "authorization": "authorization-key",
+        "currency": "USD",
+        "location_id": null,
+        "user_id": null,
+        "parent_id": null,
+        "device_id": null,
+        "receipt": {},
+        "error_code": null,
+        "source_name": "web"
+      }
+    ],
+    "order_adjustments": [
+    ]
+  }
+}

+ 32 - 0
test/refund_test.rb

@@ -0,0 +1,32 @@
+require 'test_helper'
+
+class RefundTest < Test::Unit::TestCase
+
+  test '#create should create a refund' do
+    fake "orders/450789469/refunds", :method => :post, :status => 201, :body => load_fixture('refund')
+    refund = ShopifyAPI::Refund.create(
+      :order_id => 450789469,
+      :restock => true,
+      :note => "wrong size",
+      :shipping => { :full_refund => true },
+      :refund_line_items => [{ :line_item => 518995019, :quantity => 1 }]
+    )
+    assert_equal 703073504, refund.refund_line_items.first.line_item_id
+  end
+
+  test '#find should return a refund' do
+    fake "orders/450789469/refunds/509562969.json?order_id=450789469", :extension => false, :method => :get, :body => load_fixture('refund')
+    fake "orders/450789469/refunds/509562969", :method => :get, :body => load_fixture('refund')
+    refund = ShopifyAPI::Refund.find(509562969, :params => {:order_id => 450789469})
+    assert_equal 509562969, refund.id
+  end
+
+  test '#calculate a refund' do
+    fake "orders/450789469/refunds/calculate", :method => :post, :body => load_fixture('refund')
+    data = { :shipping => { :amount => 0 } }
+
+    refund = ShopifyAPI::Refund.calculate(data, :params => {:order_id => 450789469})
+    assert_equal 2, refund.refund_line_items.count
+    assert_equal 703073504, refund.refund_line_items.first.line_item_id
+  end
+end