Skip to content

Feature/matalan - WIP #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '2.1'

jobs:
build-ruby-27:
docker:
- image: circleci/ruby:2.7.0
steps:
- checkout
- restore_cache:
keys:
- rubygems-27-cache-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "flex-commerce-api.gemspec" }}
- rubygems-27-cache-{{ .Branch }}
- rubygems-27-cache
- run: bundle install --path vendor/bundle
- save_cache:
key: rubygems-27-cache-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "flex-commerce-api.gemspec" }}
paths:
- vendor/bundle
- run: bundle exec rspec

build-ruby-25:
docker:
- image: circleci/ruby:2.5.3
steps:
- checkout
- restore_cache:
keys:
- rubygems-25-cache-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "flex-commerce-api.gemspec" }}
- rubygems-25-cache-{{ .Branch }}
- rubygems-25-cache
- run: gem install bundler -v "~> 1.17"
- run: bundle install --path vendor/bundle
- save_cache:
key: rubygems-25-cache-{{ .Branch }}-{{ checksum "Gemfile" }}-{{ checksum "flex-commerce-api.gemspec" }}
paths:
- vendor/bundle
- run: bundle exec rspec


workflows:
version: 2
build:
jobs:
- build-ruby-27
- build-ruby-25
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0
2.5.3
4 changes: 1 addition & 3 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ def available_shipping_methods
private

def stock_levels
StockLevel.where(skus: line_items.map { |li| li.item.sku }.join(",")).all
StockLevel.with_params(fields: {stock_levels: "stock_available"}).where(skus: line_items.map { |li| li.item.sku }.join(",")).all
end


end
end
12 changes: 6 additions & 6 deletions app/models/customer_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ def self.authenticate(attributes = {})
nil
end

def self.find_by_email(email)
requestor.custom("email:#{URI.encode_www_form_component(email)}", {request_method: :get}, {}).first
def self.find_by_email(email, options = {})
requestor.custom("email:#{URI.encode_www_form_component(email)}", {request_method: :get}, options).first
rescue ::FlexCommerceApi::Error::NotFound
nil
end

def self.find_by_reference(reference)
requestor.custom("reference:#{reference}", {request_method: :get}, {}).first
def self.find_by_reference(reference, options = {})
requestor.custom("reference:#{reference}", {request_method: :get}, options).first
rescue ::FlexCommerceApi::Error::NotFound
nil
end

# Find customer account by password reset token provided in email's link
# Used in reset password scenario
def self.find_by_token(token)
requestor.custom("token:#{token}", {request_method: :get}, {}).first
def self.find_by_token(token, options = {})
requestor.custom("token:#{token}", {request_method: :get}, options).first
rescue ::FlexCommerceApi::Error::NotFound
nil
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/oms/billing_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class BillingAddress < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
9 changes: 9 additions & 0 deletions app/models/oms/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class Customer < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
15 changes: 15 additions & 0 deletions app/models/oms/customer_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "flex_commerce_api/oms/api_base"
require "flex_commerce_api/api_base"
module FlexCommerce
module OMS
class CustomerOrder < FlexCommerceApi::OMS::ApiBase
has_many :payments, class_name: "::FlexCommerce::OMS::Payments"
has_many :line_items, class_name: "::FlexCommerce::OMS::LineItem"
has_many :discounts, class_name: "::FlexCommerce::OMS::Discount"
has_many :shipping_address, class_name: "::FlexCommerce::OMS::Address"
has_one :billing_address, class_name: "::FlexCommerce::OMS::Address"
has_one :customer, class_name: "::FlexCommerce::OMS::Customer"
has_one :shipping_method, class_name: "::FlexCommerce::OMS::ShippingMethod"
end
end
end
9 changes: 9 additions & 0 deletions app/models/oms/discount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class Discount < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
9 changes: 9 additions & 0 deletions app/models/oms/line_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class LineItem < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
9 changes: 9 additions & 0 deletions app/models/oms/payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class Payment < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
9 changes: 9 additions & 0 deletions app/models/oms/shipping_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class ShippingAddress < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
9 changes: 9 additions & 0 deletions app/models/oms/shipping_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "flex_commerce_api/oms/api_base"

module FlexCommerce
module OMS
class ShippingMethod < FlexCommerceApi::OMS::ApiBase
belongs_to :customer_order, class_name: "::FlexCommerce::OMS::CustomerOrder"
end
end
end
2 changes: 1 addition & 1 deletion flex-commerce-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.9"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.3"
spec.add_development_dependency "webmock", "~> 1.21"
Expand Down
12 changes: 12 additions & 0 deletions lib/flex_commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ module V2
autoload :UnallocateOrder, File.join(FlexCommerce.gem_root, "app", "models", "v2", "unallocate_order")
end

# OMS
module OMS
autoload :BillingAddress, File.join(FlexCommerce.gem_root, "app", "models", "oms", "billing_address")
autoload :CustomerOrder, File.join(FlexCommerce.gem_root, "app", "models", "oms", "customer_order")
autoload :Customer, File.join(FlexCommerce.gem_root, "app", "models", "oms", "customer")
autoload :Discount, File.join(FlexCommerce.gem_root, "app", "models", "oms", "discount")
autoload :LineItem, File.join(FlexCommerce.gem_root, "app", "models", "oms", "line_item")
autoload :Payment, File.join(FlexCommerce.gem_root, "app", "models", "oms", "payment")
autoload :ShippingAddress, File.join(FlexCommerce.gem_root, "app", "models", "oms", "shipping_address")
autoload :ShippingMethod, File.join(FlexCommerce.gem_root, "app", "models", "oms", "shipping_method")
end

# V1 Models
autoload :Address, File.join(gem_root, "app", "models", "address")
autoload :AssetFile, File.join(gem_root, "app", "models", "asset_file")
Expand Down
13 changes: 13 additions & 0 deletions lib/flex_commerce_api/oms/api_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "flex_commerce_api/base_resource"

module FlexCommerceApi
module OMS
class ApiBase < FlexCommerceApi::BaseResource
def self.endpoint_version
"v1/oms"
end

reconfigure
end
end
end
14 changes: 7 additions & 7 deletions lib/paypal_express/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
module FlexCommerce
module PaypalExpress
# @class Setup
#
#
# This service authorises the payment via the Paypal gateway
class Auth
include ::Retry
include ::FlexCommerce::PaypalExpress::Api

DEFAULT_CURRENCY = "GBP"

# @initialize
#
# @param {String} token - Paypal token
Expand All @@ -29,9 +29,9 @@ def initialize(cart:, token:, payer_id:, payment_transaction:, gateway_class: ::
def call
process_with_gateway
end

private

attr_accessor :cart, :token, :payer_id, :payment_transaction, :gateway_class

def process_with_gateway
Expand Down Expand Up @@ -63,15 +63,15 @@ def process_with_gateway
def do_express_checkout_payment
Retry.call(no_of_retries: no_of_retires, rescue_errors: ::ActiveMerchant::ConnectionError) {
::NewRelic::Agent.increment_metric('Custom/Paypal/Do_Express_Checkout_Payment') if defined?(NewRelic::Agent)
gateway.order(convert_amount(cart.total), token: token, payer_id: payer_id, currency: DEFAULT_CURRENCY)
gateway.order(convert_amount(payment_transaction.amount), token: token, payer_id: payer_id, currency: DEFAULT_CURRENCY)
}
end


def do_authorization(response)
Retry.call(no_of_retries: no_of_retires, rescue_errors: ::ActiveMerchant::ConnectionError) {
::NewRelic::Agent.increment_metric('Custom/Paypal/Do_Auhtorization') if defined?(NewRelic::Agent)
gateway.authorize_transaction(response.params["transaction_id"], convert_amount(cart.total), transaction_entity: "Order", currency: DEFAULT_CURRENCY, payer_id: payer_id)
gateway.authorize_transaction(response.params["transaction_id"], convert_amount(payment_transaction.amount), transaction_entity: "Order", currency: DEFAULT_CURRENCY, payer_id: payer_id)
}
end

Expand Down
51 changes: 35 additions & 16 deletions lib/paypal_express/generate_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
module FlexCommerce
module PaypalExpress
# @class GenerateSummary
#
#
# This class is used while setting up the paypal for FE
# It deals with line items total, sub total, tax calculations and
# Also deals with discounted line items and discounts inorder to send to paypal
#
#
class GenerateSummary
include ::FlexCommerce::PaypalExpress::Api
def initialize(cart: , use_tax: false)

def initialize(cart: , use_tax: false, gift_card_amount:)
self.cart = cart
self.use_tax = use_tax
self.gift_card_amount = gift_card_amount
raise "use_tax is not yet supported. FlexCommerce::PaypalExpress::GenerateSummary should support it in the future" if use_tax
end

# @method call
#
#
# @returns an object with subtotal, tax, handling, shipping and items keys
def call
{
Expand All @@ -34,35 +35,35 @@ def call
private

# @method subtotal
#
#
# @returns the sum of line items total. This doesnt include any promotions
def subtotal
items.sum {|i| i[:quantity] * (i[:amount])}
end

# @method total
#
#
# @return amount after converting cart total from pence to pounds
def total
convert_amount(cart.total)
end

# @method tax
#
#
# @returns the sum of total line items tax
def tax
items.sum {|i| i[:tax] * i[:quantity]}
end

# @method handling
#
#
# @returns Payment handling charges, which is 0
def handling
0
end

# @method shipping
#
#
# @returns 0 if cart is eligible for free shipping
# @returns cart.shipping_total, if cart is not eligibl for free shipping
def shipping
Expand All @@ -71,14 +72,14 @@ def shipping
end

# @mthod items
#
#
# @returns both line items and discounts
def items
normal_items + discount_items
normal_items + discount_items + gift_cards
end

# @method discounts
#
#
# @returns [] if there are no discounts on cart
# @returns Array containing about the total discount amount, if any applied.
def discount_items
Expand All @@ -95,8 +96,26 @@ def discount_items
]
end

# @method gift_cards
#
# @returns [] if there are no gift cards on cart
# @returns Array containing total gift card amount, if any applied.
def gift_cards
return [] if gift_card_amount.nil? || gift_card_amount.zero?
[
{
name: "Gift card",
number: "NA",
quantity: 1,
amount: convert_amount(BigDecimal(0) - gift_card_amount),
description: "Gift card",
tax: 0
}
]
end

# @method normal_items
#
#
# @returns Object, the normal line items added to cart
# @note these line items unit prices will be without any discounts
def normal_items
Expand All @@ -112,7 +131,7 @@ def normal_items
end
end

attr_accessor :cart, :use_tax
attr_accessor :cart, :use_tax, :gift_card_amount
end
end
end
end
Loading