Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spree Commerce Custom Attribute Endpoint Tutorial

Dana Jones
September 03, 2013

Spree Commerce Custom Attribute Endpoint Tutorial

Explanation of the Custom Attribute Endpoint tutorial for the Spree Commerce hub.

Dana Jones

September 03, 2013
Tweet

More Decks by Dana Jones

Other Decks in Technology

Transcript

  1. Why? • Customize storefront • Get customizations to the hub

    • Save on support time • Help new hires understand workflow
  2. How? • Step 1: Add custom attribute to storefront –

    Sandbox – Migration – Model Decorator
  3. How? • Step 1: Add custom attribute to storefront –

    Sandbox – Migration – Model Decorator – Deface File
  4. How? <!-- insert_top 'div.inner' --> <p class="field" id=<%="#{address_id}variety" %>> <%=

    form.label :variety, "Address Type" %><br /> <%= form.select :variety, ["Residence", "Business", "Other"] %> </p>
  5. Proof $ curl --header "X-Spree-Token: your_spree_token" http://yourdomain.com/api/orders/R123456789.json {"id":1,"number":"R123456789","item_total":"15.99","total":"22.59","state":"complete","adjustment_total":"6.6","user_id":null,"created_at":"2013-08- 28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z","completed_at":"2013-08- 27T18:20:19Z","payment_total":"0.0","shipment_state":"pending","payment_state":"balance_due","email":"[email protected]","special_instructions":null,"token":"d32f8

    9f869d639a7","bill_address":{"id":2,"firstname":"Grant","lastname":"Christiansen","full_name":"Grant Christiansen","address1":"93314 Elyssa Harbor","address2":"Apt. 338","city":"Krisport","zipcode":"16804","phone":"1-395-505-5100 x6665","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"ship_address": {"id":1,"firstname":"Retha","lastname":"Murray","full_name":"Retha Murray","address1":"8730 Dickens Keys","address2":"Apt. 049","city":"Karenburgh","zipcode":"16804","phone":"425-355- 5233","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"line_items": [{"id":1,"quantity":1,"price":"15.99","variant_id":1,"variant":{"id":1,"name":"Ruby on Rails Tote","sku":"ROR- 00011","price":"15.99","weight":null,"height":null,"width":null,"depth":null,"is_master":true,"cost_price":"17.0","permalink":"ruby-on-rails- tote","options_text":"","option_values":[],"product_id":1,"images": [{"id":21,"position":1,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/21/produ ct/ror_tote.jpeg?1377714005"}, {"id":22,"position":2,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote_back.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/22/produ ct/ror_tote_back.jpeg?1377714005"}]}}],"payments":[{"id":1,"amount":"22.59","state":"pending","payment_method_id":1,"payment_method":{"id":1,"name":"Credit Card","environment":"development"}}],"shipments": [{"id":1,"tracking":null,"number":"H65183147836","cost":"5.0","shipped_at":null,"state":"pending","order_id":"R123456789","stock_location_name":"default","shipping _rates":[{"id":1,"cost":"5.0","selected":true,"shipment_id":1,"shipping_method_id":1}, {"id":2,"cost":"10.0","selected":false,"shipment_id":1,"shipping_method_id":2}, {"id":3,"cost":"15.0","selected":false,"shipment_id":1,"shipping_method_id":3}],"shipping_method":{"name":"UPS Ground (USD)"},"inventory_units": [{"id":1,"state":"on_hand","variant_id":1,"shipment_id":1,"return_authorization_id":null}]}],"adjustments": [{"id":1,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount ":"0.8","label":"North America 5.0%","mandatory":false,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":3,"source_type":"Spree::Shipment","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id": 1,"amount":"5.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":5,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount" :"0.8","label":"Tax","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":7,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id":1," amount":"0.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}],"credit_cards": [{"id":1,"month":"12","year":"2014","cc_type":"visa","last_digits":"1111","first_name":"Sean","last_name":"Schofield","gateway_customer_profile_id":"BGS- 1234","gateway_payment_profile_id":null}]
  6. Proof $ curl --header "X-Spree-Token: your_spree_token" http://yourdomain.com/api/orders/R123456789.json {"id":1,"number":"R123456789","item_total":"15.99","total":"22.59","state":"complete","adjustment_total":"6.6","user_id":null,"created_at":"2013-08- 28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z","completed_at":"2013-08- 27T18:20:19Z","payment_total":"0.0","shipment_state":"pending","payment_state":"balance_due","email":"[email protected]","special_instructions":null,"token":"d32f8

    9f869d639a7","bill_address":{"id":2,"firstname":"Grant","lastname":"Christiansen","full_name":"Grant Christiansen","address1":"93314 Elyssa Harbor","address2":"Apt. 338","city":"Krisport","zipcode":"16804","phone":"1-395-505-5100 x6665","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"ship_address": {"id":1,"firstname":"Retha","lastname":"Murray","full_name":"Retha Murray","address1":"8730 Dickens Keys","address2":"Apt. 049","city":"Karenburgh","zipcode":"16804","phone":"425-355- 5233","company":null,"alternative_phone":null,"country_id":49,"state_id":48,"state_name":null,"variety":null,"country":{"id":49,"iso_name":"UNITED STATES","iso":"US","iso3":"USA","name":"United States","numcode":840},"state":{"id":48,"name":"New York","abbr":"NY","country_id":49}},"line_items": [{"id":1,"quantity":1,"price":"15.99","variant_id":1,"variant":{"id":1,"name":"Ruby on Rails Tote","sku":"ROR- 00011","price":"15.99","weight":null,"height":null,"width":null,"depth":null,"is_master":true,"cost_price":"17.0","permalink":"ruby-on-rails- tote","options_text":"","option_values":[],"product_id":1,"images": [{"id":21,"position":1,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/21/produ ct/ror_tote.jpeg?1377714005"}, {"id":22,"position":2,"attachment_content_type":"image/jpeg","attachment_file_name":"ror_tote_back.jpeg","type":"Spree::Image","attachment_updated_at":"2013-08- 28T18:20:05Z","attachment_width":360,"attachment_height":360,"alt":null,"viewable_type":"Spree::Variant","viewable_id":1,"attachment_url":"/spree/products/22/produ ct/ror_tote_back.jpeg?1377714005"}]}}],"payments":[{"id":1,"amount":"22.59","state":"pending","payment_method_id":1,"payment_method":{"id":1,"name":"Credit Card","environment":"development"}}],"shipments": [{"id":1,"tracking":null,"number":"H65183147836","cost":"5.0","shipped_at":null,"state":"pending","order_id":"R123456789","stock_location_name":"default","shipping _rates":[{"id":1,"cost":"5.0","selected":true,"shipment_id":1,"shipping_method_id":1}, {"id":2,"cost":"10.0","selected":false,"shipment_id":1,"shipping_method_id":2}, {"id":3,"cost":"15.0","selected":false,"shipment_id":1,"shipping_method_id":3}],"shipping_method":{"name":"UPS Ground (USD)"},"inventory_units": [{"id":1,"state":"on_hand","variant_id":1,"shipment_id":1,"return_authorization_id":null}]}],"adjustments": [{"id":1,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount ":"0.8","label":"North America 5.0%","mandatory":false,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":3,"source_type":"Spree::Shipment","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id": 1,"amount":"5.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":5,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::TaxRate","originator_id":1,"amount" :"0.8","label":"Tax","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}, {"id":7,"source_type":"Spree::Order","source_id":1,"adjustable_type":"Spree::Order","adjustable_id":1,"originator_type":"Spree::ShippingMethod","originator_id":1," amount":"0.0","label":"Shipping","mandatory":true,"eligible":true,"created_at":"2013-08-28T18:20:19Z","updated_at":"2013-08-28T18:20:19Z"}],"credit_cards": [{"id":1,"month":"12","year":"2014","cc_type":"visa","last_digits":"1111","first_name":"Sean","last_name":"Schofield","gateway_customer_profile_id":"BGS- 1234","gateway_payment_profile_id":null}]
  7. How? module DummyShip def self.validate_address(address) ## Zipcode must be within

    a given range. unless (20170..20179).to_a.include? (address['zipcode'].to_i) raise "This order is outside our shipping zone." end end end
  8. How? • Step 3: Write the endpoint – Fake API

    – Gemfile – config.ru – custom_attribute_endpoint.rb
  9. post '/get_biz_signer' do get_address begin result = @address['variety'] == "Business"

    ? "do" : "do not" process_result 200, { 'message_id' => @message[:message_id], 'message' => "notification:info", "payload" => { "result" => "You #{result} need to get a signature for this package." } } rescue Exception => e process_result 200, { 'message_id' => @message[:message_id], 'message' => "notification:error", "payload" => { "result" => e.message} } end end
  10. Proof If residential address: $ curl --data @./residential_order.json -i -X

    POST -H 'Content-type:application/json' http://localhost:9292/get_biz_signer {"message_id":"12345","message":"notification:info", "payload":{"result": "You do not need to get a signature for this package."}}
  11. Proof If business address: $ curl --data @./business_order.json -i -X

    POST -H 'Content-type:application/json' http://localhost:9292/get_biz_signer {"message_id":"12345","message":"notification:info", "payload":{"result": "You do need to get a signature for this package."}}
  12. Where? • The tutorial is available in the Integration guides,

    at: guides.spreecommerce.com/integration/custom_attributes_tutorial.html The sample files are available at: github.com/spree/integration_tutorials