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

SpreeConf 2012: The 3 P's Preferences, Payments...

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Chris Mar
February 19, 2012

SpreeConf 2012: The 3 P's Preferences, Payments and Promotions

From SpreeConf 2012 NYC. This is a technical training for the inner workings of Spree preferences , Payment Methods, Payments, Default Gateways and Promotions

Demo code is available https://github.com/cmar/spree_conf_pref_demo

Chris Mar

February 19, 2012

Transcript

57 slides
  1. The Three P's => [Preferences, Payments, Promotions] Chris Mar Software

    Engineer Spree Commerce Monday, February 13, 12
  2. Agenda • Preferences • Payments • Payment Methods • Default

    Gateways • Promotions • Activators • Calculators Monday, February 13, 12
  3. Spree 1.0 Preferences • Rewritten for Spree 1.0 • Performance

    • Clustering • Inheritance • ActiveRecord not required Monday, February 13, 12
  4. include Spree::Preferences::Preferable Preferable • Adds methods to define and access

    preferences • Does not require ActiveRecord • included into ActiveRecord::Base Monday, February 13, 12
  5. preferable ActiveRecord • Spree includes in ActiveRecord::Base • preference values

    per model instance • can be mass assigned Monday, February 13, 12
  6. form builder methods Preferences • preference_fields(object, form) • preference_field_for(form, field,

    options) • preference_field_tag(name, value, options) Monday, February 13, 12
  7. storage Preferences AppConfiguration Rails.cache spree_preferences Preferences::Store (singleton) MyModel < ActiveRecord::Base

    include Preferrable MyModel < ActiveRecord::Base include Preferrable Preferences::Configuration include Preferrable extra accessors Monday, February 13, 12
  8. Interaction Preferences MyModel < ActiveRecord::Base include Preferrable Rails.cache spree_preferences Preferences::Store

    (singleton) preferred_flavor get_preference(:flavor) exists? load_preferences Spree::Preferences.all value value or default preferred_flavor = 'vanilla' set_preference(:flavor, 'vanilla') write .save .save write Monday, February 13, 12
  9. pitfalls Preferences • Prefer columns • Only use for values

    that rarely change • Default should be used most of the time • Can’t use in where clause Monday, February 13, 12
  10. Payment Methods • Proxy to ActiveMerchant gateways • Simple interface

    for alternative gateways (skrill, paypal express) • Spree Store can have multiple Payment Methods Monday, February 13, 12
  11. Class Structure Payment Method PaymentMethod payment_source_class Gateway CreditCard (payment_source_class) Gateway::AuthorizeNet

    ActiveMerchant::Billing:: AuthorizeNetGateway (provider_class) provider_class Monday, February 13, 12
  12. gateway Payment Method • ActiveMerchant::Billing:Gateway • Most gateways are thin

    proxy • set provider_class and options • options are preferences Monday, February 13, 12
  13. Spree’s AuthorizeNet Gateway Payment Method • Spree Gateway’s preferences are

    passed to the provider initializer Monday, February 13, 12
  14. Payments • Transaction applied to Order • State based •

    Work with Payment Methods Monday, February 13, 12
  15. order complete Payment PaymentMethod Payment amount = $22.00 CreditCard Order

    process_payments! complete process! process! authorize Activemerchant::Billing:: AuthorizeNet authorize provider complete response amount and CC# response_code and avs_response response from Authorize.net Monday, February 13, 12
  16. payment state machine Payments checkout pending completed processing failed void

    started_processing! started_processing! started_processing! failure! pend! complete! complete! void! void! initial Monday, February 13, 12
  17. Spree 1.0 Changes Spree Gateways • Only Bogus in core

    • payment methods moved to spree_gateway gem • installer adds default gateways Monday, February 13, 12
  18. spree_gateway • payment methods moved to spree_gateway gem • community

    contributed gateways • Authorize.net, Braintree, Stripe, etc. • just include in gem file Monday, February 13, 12
  19. Default Gateways • Spree Commerce Supported • USA ePay for

    U.S. stores • Skrill for international stores • installed with spree_cmd Monday, February 13, 12
  20. USA ePay • USA ePay (BankCard Services) • https://github.com/spree/ spree_usa_epay

    • Gateway uses Soap Web Service • Supports Payment Profiles Monday, February 13, 12
  21. custom gateway USA ePay Client Gateway CreditCard (payment_source_class) Gateway::USAEpay SpreeUsaEpay::Client

    USA ePay Web Service SOAP • ActiveMerchant::Billing::Gateway interface • authorize, capture, credit, void Monday, February 13, 12
  22. USA ePay Client • ActiveMerchant::Billing::Gateway interface (authorize, capture, credit, void)

    • Uses USA ePay WSDL • Uses Savon gem for Soap Monday, February 13, 12
  23. Spree Skrill • supports iFrame interface • Credit Card, Local

    Payment and Moneybookers accounts • stores details in spree_skrill_transactions • details are saved as payment source and viewable in admin Monday, February 13, 12
  24. Skrill Transaction checkout payment Skrill iFrame SkrillStatusController /update confirm order

    1. payment info 2. call back with skrill transaction details 3. payment complete /skrill_return 4. redirect to confirm order Order 2.1 add payment to order SkrillTransaction transaction_id customer_id currency amount 2.2 payment source is skrill_transaction with details Monday, February 13, 12
  25. summary Payments • Default payment methods Skrill and USA ePay

    on install • Community supported gateways • Easy to add your own custom payment methods Monday, February 13, 12
  26. Overview Promotions • event driven activation • rules for eligibility

    • create order adjustments • custom calculators Monday, February 13, 12
  27. Activators • Generic way to handle spree events • ActiveSupport::Notifications.subscribe(/^spree\./)

    • fire_event('spree.order.contents_changed') • Subclass Spree::Activator and save with event name Monday, February 13, 12
  28. interaction Activators order_controller in update fire_event ActiveSupport::Notifications core/lib/engine.rb subscribe(/^spree\./) find

    activators for event spree.order.contents_changed spree.order.contents_changed Spree::Activator listens for event spree.order.contents_changed Spree::Activator listens for event spree.order.contents_changed Spree::Activator listens for event spree.order.contents_changed activate activate activate Monday, February 13, 12
  29. Promotions • Promotion < Activator • adds new events like

    spree.checkout.coupon_code_added • has many rules and actions Monday, February 13, 12
  30. Promotion Rules • determines promotion eligibility • Spree comes with:

    • first order • item total • specific product • specific user • registered user Monday, February 13, 12
  31. Promotion Actions • Action when promotion is activated and eligible

    • Create Adjustment • Flat Percent • Flat Rate per Order or Item • Flexible Rate • Free Shipping • Add Item to Order Monday, February 13, 12
  32. Calculators • Reusable compute Objects • Used to compute amount

    for Adjustments • Easy to build and use your own! Monday, February 13, 12
  33. Activation Promotion Promotion < Activator 1. Activate spree.checkout. coupon_code_added Order

    Promotion Rules Create Adjustment Action Adjustment Create Adjustment Action 2. Perform 3. Create 4. Eligible 5. compute 6. compute Calculator 7. compute Monday, February 13, 12

More Decks by Chris Mar

Other Decks in Programming