From cc096c231e1b8e84eb801204136393a33fec794d Mon Sep 17 00:00:00 2001 From: Zeeshan Ishtiaq Date: Tue, 10 Sep 2019 12:08:03 +0100 Subject: [PATCH 1/9] add gift cards to generate summary --- .ruby-version | 2 +- flex-commerce-api.gemspec | 2 +- lib/paypal_express/generate_summary.rb | 51 ++++++++++++------ lib/paypal_express/process/paypal_params.rb | 22 ++++---- spec/factories/addresses.rb | 6 +-- spec/factories/asset_files.rb | 6 +-- spec/factories/bundle.rb | 2 +- spec/factories/categories.rb | 4 +- spec/factories/variants.rb | 2 +- spec/factories/webhook.rb | 4 +- .../paypal_express/generate_summary_spec.rb | 53 ++++++++++++++++--- 11 files changed, 108 insertions(+), 46 deletions(-) diff --git a/.ruby-version b/.ruby-version index 276cbf9..aedc15b 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.3.0 +2.5.3 diff --git a/flex-commerce-api.gemspec b/flex-commerce-api.gemspec index 19298a1..4b40334 100644 --- a/flex-commerce-api.gemspec +++ b/flex-commerce-api.gemspec @@ -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" diff --git a/lib/paypal_express/generate_summary.rb b/lib/paypal_express/generate_summary.rb index 62f6735..ca07250 100644 --- a/lib/paypal_express/generate_summary.rb +++ b/lib/paypal_express/generate_summary.rb @@ -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 { @@ -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 @@ -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 @@ -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 @@ -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 \ No newline at end of file +end diff --git a/lib/paypal_express/process/paypal_params.rb b/lib/paypal_express/process/paypal_params.rb index 5e7923d..3b3d617 100644 --- a/lib/paypal_express/process/paypal_params.rb +++ b/lib/paypal_express/process/paypal_params.rb @@ -9,9 +9,9 @@ class PaypalParams include ::FlexCommerce::PaypalExpress::Api DEFAULT_DESCRIPTION = "Shift Commerce Order".freeze - + # @initialize - # + # # @param {FlexCommerce::PaymentProviderSetup} payment_provider_setup # @param {FlexCommerce::Cart} cart # @param {Paypal Gateway} [gateway_class = ::ActiveMerchant::Billing::PaypalExpressGateway] @@ -23,8 +23,9 @@ class PaypalParams # @param {FlexCommerce::ShippingMethod} shipping_method_model = FlexCommerce::ShippingMethod # @param {boolean} [use_mobile_payments = false] # @param {String} [description] - # - def initialize(cart:,success_url:, cancel_url:, ip_address:, allow_shipping_change: true, callback_url:, shipping_method_model: FlexCommerce::ShippingMethod, use_mobile_payments: false, description:) + # @param {BigDecimal} [gift_card_amount] + # + def initialize(cart:,success_url:, cancel_url:, ip_address:, allow_shipping_change: true, callback_url:, shipping_method_model: FlexCommerce::ShippingMethod, use_mobile_payments: false, description:, gift_card_amount: nil) self.cart = cart self.allow_shipping_change = allow_shipping_change self.success_url = success_url @@ -34,6 +35,7 @@ def initialize(cart:,success_url:, cancel_url:, ip_address:, allow_shipping_chan self.shipping_method_model = shipping_method_model self.use_mobile_payments = use_mobile_payments self.description = description + self.gift_card_amount = gift_card_amount end def call @@ -44,7 +46,7 @@ def call .merge(shipping_options_params) end - attr_accessor :description, :cart, :success_url, :cancel_url, :ip_address, :allow_shipping_change, :callback_url, :shipping_method_model, :use_mobile_payments + attr_accessor :description, :cart, :success_url, :cancel_url, :ip_address, :allow_shipping_change, :callback_url, :shipping_method_model, :use_mobile_payments, :gift_card_amount private @@ -87,16 +89,16 @@ def paypal_items def ui_callback_params return {} unless allow_shipping_change && shipping_methods.count > 0 - { + { callback_url: callback_url, callback_timeout: 6, callback_version: 95, - max_amount: convert_amount((cart.total * 1.2) + shipping_methods.last.total + shipping_methods.last.tax) + max_amount: convert_amount((cart.total * 1.2) + shipping_methods.last.total + shipping_methods.last.tax) } end # @method shipping_methods - # + # # @returns shipping methods with promotions applied def shipping_methods @shipping_methods ||= ShippingMethodsForCart.new(cart: cart, shipping_methods: shipping_method_model.all).call.sort_by(&:total) @@ -115,9 +117,9 @@ def shipping_options_params end def summary - @summary ||= GenerateSummary.new(cart: cart).call + @summary ||= GenerateSummary.new(cart: cart, gift_card_amount: gift_card_amount).call end end end end -end \ No newline at end of file +end diff --git a/spec/factories/addresses.rb b/spec/factories/addresses.rb index dfd8114..8713d1b 100644 --- a/spec/factories/addresses.rb +++ b/spec/factories/addresses.rb @@ -11,8 +11,8 @@ state { Faker::Address.state } postcode { Faker::Address.postcode } country { Faker::Address.country } - preferred_billing false - preferred_shipping false + preferred_billing { false } + preferred_shipping { false } end factory :addresses_from_fixture, class: JsonStruct do @@ -27,4 +27,4 @@ send(key, value) end end -end \ No newline at end of file +end diff --git a/spec/factories/asset_files.rb b/spec/factories/asset_files.rb index 8cdff27..029d4b9 100644 --- a/spec/factories/asset_files.rb +++ b/spec/factories/asset_files.rb @@ -4,10 +4,10 @@ sequence(:name) { |n| "asset file name-#{n}" } asset_folder_id { Faker::Number.number(2) } sequence(:file_content_filename) { |n| "asset_file_name_#{n}.png" } - file_content_size 106 - file_content_content_type "image/png" + file_content_size { 106 } + file_content_content_type { "image/png" } image_width { Faker::Number.number(4) } image_height { Faker::Number.number(4) } reference { "#{Faker::Code.isbn}-#{Time.now.nsec}" } end -end \ No newline at end of file +end diff --git a/spec/factories/bundle.rb b/spec/factories/bundle.rb index bd8d7b1..5656680 100644 --- a/spec/factories/bundle.rb +++ b/spec/factories/bundle.rb @@ -8,6 +8,6 @@ end factory :bundle_list, parent: :json_api_resource_list do - type "bundle" + type { "bundle" } end end diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb index 69555dd..8b74a07 100644 --- a/spec/factories/categories.rb +++ b/spec/factories/categories.rb @@ -3,7 +3,7 @@ factory :category, class: klass do title { Faker::Lorem.sentence } reference { rand(1000000000).to_s } - category_tree_id "reference:web" + category_tree_id { "reference:web" } end factory :categories_from_fixture, class: JsonStruct do obj = JsonStruct.new(JSON.parse(File.read("spec/fixtures/categories/multiple.json"))) @@ -17,4 +17,4 @@ send(key, value) end end -end \ No newline at end of file +end diff --git a/spec/factories/variants.rb b/spec/factories/variants.rb index 04fa9b3..92970be 100644 --- a/spec/factories/variants.rb +++ b/spec/factories/variants.rb @@ -5,6 +5,6 @@ description { Faker::Lorem.sentence } sku { Faker::Code.ean } reference { Faker::Lorem.words(3).join("-") } - stock_level 100 + stock_level { 100 } end end diff --git a/spec/factories/webhook.rb b/spec/factories/webhook.rb index aefa67f..be0e385 100644 --- a/spec/factories/webhook.rb +++ b/spec/factories/webhook.rb @@ -3,7 +3,7 @@ factory :webhook, class: klass do title { Faker::Lorem.words(3).join(" ") } event { "order_created" } - request_url "https://example.com/bla" - request_headers ({ "X-Token" => "test" }) + request_url { "https://example.com/bla" } + request_headers {{ "X-Token" => "test" }} end end diff --git a/spec/lib/paypal_express/generate_summary_spec.rb b/spec/lib/paypal_express/generate_summary_spec.rb index 09e06a8..b857646 100644 --- a/spec/lib/paypal_express/generate_summary_spec.rb +++ b/spec/lib/paypal_express/generate_summary_spec.rb @@ -5,10 +5,11 @@ def convert_amount(amt) (amt * 100).to_i end context "without tax" do - subject { described_class.new(cart: cart) } shared_examples_for "a summary from a cart with tax ignored" do it "should have the correct subtotal" do - expect(subject.call).to include subtotal: convert_amount(line_items.sum(&:total)) + # gift card amount is deducted from subtotal if gift card used + subtotal = line_items.sum(&:total) - gift_card_amount + expect(subject.call).to include subtotal: convert_amount(subtotal) end it "should have zero tax" do expect(subject.call).to include tax: 0 @@ -20,6 +21,7 @@ def convert_amount(amt) expect(subject.call).to include shipping: convert_amount(shipping_total) end end + shared_examples_for "paypal items" do it "should have the correct items - amount and quantity" do expect(paypal_items).to match_array(line_items.map {|li| hash_including(amount: convert_amount(li.unit_price), quantity: li.unit_quantity)}) @@ -35,17 +37,34 @@ def convert_amount(amt) end end + + shared_examples_for "non discounted line items" do + let(:gift_card_amount) { BigDecimal.new(0) } + subject { described_class.new(cart: cart, gift_card_amount: gift_card_amount) } + let(:paypal_items) { subject.call[:items] } + include_examples "paypal items" + end + shared_examples_for "discounted line items" do + let(:gift_card_amount) { BigDecimal.new(0) } + subject { described_class.new(cart: cart, gift_card_amount: gift_card_amount) } let(:paypal_items) { subject.call[:items][0...-1] } include_examples "paypal items" it "should have the last item as special discount item" do expect(subject.call).to include(items: array_including(hash_including(amount: convert_amount(BigDecimal.new(0) - cart.total_discount)))) end end - shared_examples_for "non discounted line items" do - let(:paypal_items) { subject.call[:items] } + + shared_examples_for "line items partially paid with gift cards" do + let(:gift_card_amount) { BigDecimal.new(10) } + subject { described_class.new(cart: cart, gift_card_amount: gift_card_amount) } + let(:paypal_items) { subject.call[:items][0...-1] } include_examples "paypal items" + it "should have the last item as gift card item" do + expect(subject.call).to include(items: array_including(hash_including(amount: convert_amount(BigDecimal.new(0) - gift_card_amount)))) + end end + context "with no shipping" do let(:shipping_total) { BigDecimal.new(0) } context "with no discounts" do @@ -53,10 +72,21 @@ def convert_amount(amt) let(:line_item_discount) { BigDecimal.new(0) } let(:item) { 5.times.map { |n| double(:variant, sku: "sku_#{n}") } } let(:line_items) { 5.times.map { |n| double(:line_item, unit_price: BigDecimal.new(1.67, 12), item: item[n], unit_quantity: 3, total: BigDecimal.new(5.01, 12), title: "Line item #{n}", tax: BigDecimal.new(0)) } } - + include_examples "a summary from a cart with tax ignored" include_examples "non discounted line items" end + + context "with gift card" do + let(:cart) { double(:cart, free_shipping: false, line_items: line_items, shipping_total: shipping_total, total: line_items.sum(&:total) + shipping_total, total_discount: line_item_discount * line_items.length) } + let(:line_item_discount) { BigDecimal.new(0) } + let(:item) { 5.times.map { |n| double(:variant, sku: "sku_#{n}") } } + let(:line_items) { 5.times.map { |n| double(:line_item, unit_price: BigDecimal.new(1.67, 12), item: item[n], unit_quantity: 3, total: BigDecimal.new(5.01, 12), title: "Line item #{n}", tax: BigDecimal.new(0)) } } + + include_examples "a summary from a cart with tax ignored" + include_examples "line items partially paid with gift cards" + end + context "with line item discounts" do let(:cart) { double(:cart, free_shipping: false, line_items: line_items, shipping_total: shipping_total, total: line_items.sum(&:total) + shipping_total, total_discount: line_item_discount * line_items.length) } let(:line_item_discount) { BigDecimal.new(0.79, 12) } @@ -66,6 +96,7 @@ def convert_amount(amt) include_examples "discounted line items" end end + context "with shipping" do let(:shipping_total) { BigDecimal.new(3.99, 12) } context "with no discounts" do @@ -76,6 +107,16 @@ def convert_amount(amt) include_examples "a summary from a cart with tax ignored" include_examples "non discounted line items" end + + context "with gift card" do + let(:cart) { double(:cart, free_shipping: false, line_items: line_items, shipping_total: shipping_total, total: line_items.sum(&:total) + shipping_total, total_discount: line_item_discount * line_items.length) } + let(:line_item_discount) { BigDecimal.new(0) } + let(:item) { 5.times.map { |n| double(:variant, sku: "sku_#{n}") } } + let(:line_items) { 5.times.map { |n| double(:line_ttem, unit_price: BigDecimal.new(1.67, 12), item: item[n], unit_quantity: 3, total: BigDecimal.new(5.01, 12), title: "Line item #{n}", tax: BigDecimal.new(0)) } } + include_examples "a summary from a cart with tax ignored" + include_examples "line items partially paid with gift cards" + end + context "with line item discounts" do let(:cart) { double(:cart, free_shipping: false, line_items: line_items, shipping_total: shipping_total, total: line_items.sum(&:total) + shipping_total, total_discount: line_item_discount * line_items.length) } let(:line_item_discount) { BigDecimal.new(0.79, 12) } @@ -86,4 +127,4 @@ def convert_amount(amt) end end end -end \ No newline at end of file +end From 4df4ba71473ad21501f5e2f314fa94c08613bd1b Mon Sep 17 00:00:00 2001 From: Zeeshan Ishtiaq Date: Tue, 10 Sep 2019 16:25:20 +0100 Subject: [PATCH 2/9] Add changes for gift cards --- lib/paypal_express/auth.rb | 14 +++++++------- lib/paypal_express/setup.rb | 25 +++++++++++++++---------- spec/lib/paypal_express/auth_spec.rb | 10 +++++----- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/paypal_express/auth.rb b/lib/paypal_express/auth.rb index 9786745..6005edf 100644 --- a/lib/paypal_express/auth.rb +++ b/lib/paypal_express/auth.rb @@ -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 @@ -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 @@ -63,7 +63,7 @@ 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 @@ -71,7 +71,7 @@ def do_express_checkout_payment 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 diff --git a/lib/paypal_express/setup.rb b/lib/paypal_express/setup.rb index 726e040..c183297 100644 --- a/lib/paypal_express/setup.rb +++ b/lib/paypal_express/setup.rb @@ -4,14 +4,14 @@ module FlexCommerce module PaypalExpress # @class Setup - # + # # This is the main class, which talks to ActiveMerchant gem to initiate a transaction using Paypal class Setup include ::FlexCommerce::PaypalExpress::Api - + # @initialize - # + # # @param {FlexCommerce::PaymentProviderSetup} payment_provider_setup # @param {FlexCommerce::Cart} cart # @param {Paypal Gateway} [gateway_class = ::ActiveMerchant::Billing::PaypalExpressGateway] @@ -23,12 +23,12 @@ class Setup # @param {FlexCommerce::ShippingMethod} shipping_method_model = FlexCommerce::ShippingMethod # @param {boolean} [use_mobile_payments = false] # @param {String} [description = nil] - # + # # @note: # For `::ActiveMerchant::Billing::PaypalExpressGateway` to work # rails-site should include active merchant gem. Ideally this gem should be included in the gemspec. # But as we are using custom gem, which is not published to ruby gems, there is no way of including it within this gem dependency - def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway, success_url:, cancel_url:, ip_address:, allow_shipping_change: true, callback_url:, shipping_method_model: FlexCommerce::ShippingMethod, use_mobile_payments: false, description: nil) + def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway, success_url:, cancel_url:, ip_address:, allow_shipping_change: true, callback_url:, shipping_method_model: FlexCommerce::ShippingMethod, use_mobile_payments: false, description: nil, gift_card_amount: BigDecimal(0)) self.gateway_class = gateway_class self.cart = cart self.allow_shipping_change = allow_shipping_change @@ -39,12 +39,16 @@ def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGat self.shipping_method_model = shipping_method_model self.use_mobile_payments = use_mobile_payments self.description = description + self.gift_card_amount = gift_card_amount end def call validate_shipping_method - - response = gateway.setup_order(convert_amount(cart.total), paypal_params) + puts '#' * 80 + puts "Gift card amount: #{gift_card_amount}" + puts '#' * 80 + total_amount = cart.total - gift_card_amount + response = gateway.setup_order(convert_amount(total_amount), paypal_params) # If paypal setup went fine, redirect to the paypal page if response.success? PaypalSetup.new(setup_type: "redirect", redirect_url: gateway.redirect_url_for(response.token, mobile: use_mobile_payments)) @@ -59,7 +63,7 @@ def call private - attr_accessor :description, :cart, :gateway_class, :success_url, :cancel_url, :ip_address, :allow_shipping_change, :callback_url, :shipping_method_model, :use_mobile_payments + attr_accessor :description, :cart, :gateway_class, :success_url, :cancel_url, :ip_address, :allow_shipping_change, :callback_url, :shipping_method_model, :use_mobile_payments, :gift_card_amount def paypal_params Process::PaypalParams.new( @@ -71,12 +75,13 @@ def paypal_params callback_url: callback_url, shipping_method_model: shipping_method_model, use_mobile_payments: use_mobile_payments, - description: description + description: description, + gift_card_amount: gift_card_amount ).call end # @method shipping_methods - # + # # @returns shipping methods with promotions applied def shipping_methods @shipping_methods ||= ShippingMethodsForCart.new(cart: cart, shipping_methods: shipping_method_model.all).call.sort_by(&:total) diff --git a/spec/lib/paypal_express/auth_spec.rb b/spec/lib/paypal_express/auth_spec.rb index 500361e..f25943b 100644 --- a/spec/lib/paypal_express/auth_spec.rb +++ b/spec/lib/paypal_express/auth_spec.rb @@ -4,11 +4,11 @@ include ActiveSupport::NumberHelper include_context "context store" include_context "housekeeping" - + let(:token) { "fake-token" } let(:payer_id) { "fake-payer-id" } let(:cart) { build_stubbed(:cart, total: 100) } - let(:transaction) { + let(:transaction) { to_clean.transaction = FlexCommerce::PaymentTransaction.create( cart_id: cart.id, gateway_response: { @@ -28,7 +28,7 @@ cart.billing_address.freeze cart.freeze end - + subject { described_class.new(cart: cart, token: token, payer_id: payer_id, payment_transaction: transaction) } shared_context "mocked active merchant" do |expect_login: true, test_mode: true| @@ -85,7 +85,7 @@ context "happy path in production" do include_context "mocked active merchant", test_mode: false - + before(:each) do expect(active_merchant_gateway).to receive(:order).with(convert_amount(cart.total), token: token, payer_id: payer_id, currency: "GBP").and_return order_response expect(active_merchant_gateway).to receive(:authorize_transaction).with(transaction_id, convert_amount(cart.total), transaction_entity: "Order", payer_id: payer_id, currency: "GBP").and_return authorize_order_response @@ -99,7 +99,7 @@ context "with error scenarios" do include_context "mocked active merchant" - + it "should mark the transactions gateway_response as invalid when failure is recoverable in order stage" do order_response = instance_double "ActiveMerchant::Billing::PaypalExpressGateway", "order_response", params: {"error_codes" => "10410", "message" => "Invalid token", "ack" => "Failure", "Ack" => "Failure"}, success?: false expect(active_merchant_gateway).to receive(:order).with(convert_amount(cart.total), token: token, payer_id: payer_id, currency: "GBP").and_return order_response From ba24f7330172ac81dd40fa734f9c89fd2b1b24a7 Mon Sep 17 00:00:00 2001 From: praWeb Date: Mon, 23 Mar 2020 20:14:33 +0000 Subject: [PATCH 3/9] add the disabled ability --- lib/paypal_express/setup.rb | 3 --- lib/paypal_express/shipping_methods_for_cart.rb | 4 +++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/paypal_express/setup.rb b/lib/paypal_express/setup.rb index c183297..0340cb2 100644 --- a/lib/paypal_express/setup.rb +++ b/lib/paypal_express/setup.rb @@ -44,9 +44,6 @@ def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGat def call validate_shipping_method - puts '#' * 80 - puts "Gift card amount: #{gift_card_amount}" - puts '#' * 80 total_amount = cart.total - gift_card_amount response = gateway.setup_order(convert_amount(total_amount), paypal_params) # If paypal setup went fine, redirect to the paypal page diff --git a/lib/paypal_express/shipping_methods_for_cart.rb b/lib/paypal_express/shipping_methods_for_cart.rb index f5e4e76..5997348 100644 --- a/lib/paypal_express/shipping_methods_for_cart.rb +++ b/lib/paypal_express/shipping_methods_for_cart.rb @@ -26,7 +26,9 @@ def call free_shipping_method_ids.flatten! updated_shipping_methods = [] - shipping_methods.each do |shipping_method| + filtered_shipping_methods = shipping_methods.reject { |sm| sm.meta_attribute(:disabled) == 'true' } + + filtered_shipping_methods.each do |shipping_method| shipping_method_free = free_shipping_method_ids.include?(shipping_method.id) updated_shipping_methods << CartShippingMethod.new(shipping_method, shipping_method_free) end From 6c331d275ebb1c12f63c049d59d090ed351068f7 Mon Sep 17 00:00:00 2001 From: praWeb Date: Mon, 6 Apr 2020 18:20:22 +0100 Subject: [PATCH 4/9] integrate with OMS Customer Order --- app/models/oms/billing_address.rb | 9 +++++++++ app/models/oms/customer.rb | 9 +++++++++ app/models/oms/customer_order.rb | 15 +++++++++++++++ app/models/oms/discount.rb | 9 +++++++++ app/models/oms/line_item.rb | 9 +++++++++ app/models/oms/payment.rb | 9 +++++++++ app/models/oms/shipping_address.rb | 9 +++++++++ app/models/oms/shipping_method.rb | 9 +++++++++ lib/flex_commerce.rb | 12 ++++++++++++ .../json_api_client_extension/remote_builder.rb | 1 + lib/flex_commerce_api/oms/api_base.rb | 13 +++++++++++++ spec/integration/oms/customer_order.rb | 14 ++++++++++++++ 12 files changed, 118 insertions(+) create mode 100644 app/models/oms/billing_address.rb create mode 100644 app/models/oms/customer.rb create mode 100644 app/models/oms/customer_order.rb create mode 100644 app/models/oms/discount.rb create mode 100644 app/models/oms/line_item.rb create mode 100644 app/models/oms/payment.rb create mode 100644 app/models/oms/shipping_address.rb create mode 100644 app/models/oms/shipping_method.rb create mode 100644 lib/flex_commerce_api/oms/api_base.rb create mode 100644 spec/integration/oms/customer_order.rb diff --git a/app/models/oms/billing_address.rb b/app/models/oms/billing_address.rb new file mode 100644 index 0000000..c66e880 --- /dev/null +++ b/app/models/oms/billing_address.rb @@ -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 diff --git a/app/models/oms/customer.rb b/app/models/oms/customer.rb new file mode 100644 index 0000000..49ee4dc --- /dev/null +++ b/app/models/oms/customer.rb @@ -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 diff --git a/app/models/oms/customer_order.rb b/app/models/oms/customer_order.rb new file mode 100644 index 0000000..d8c7096 --- /dev/null +++ b/app/models/oms/customer_order.rb @@ -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 diff --git a/app/models/oms/discount.rb b/app/models/oms/discount.rb new file mode 100644 index 0000000..9591254 --- /dev/null +++ b/app/models/oms/discount.rb @@ -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 diff --git a/app/models/oms/line_item.rb b/app/models/oms/line_item.rb new file mode 100644 index 0000000..e3dfe7c --- /dev/null +++ b/app/models/oms/line_item.rb @@ -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 diff --git a/app/models/oms/payment.rb b/app/models/oms/payment.rb new file mode 100644 index 0000000..a97a7d9 --- /dev/null +++ b/app/models/oms/payment.rb @@ -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 diff --git a/app/models/oms/shipping_address.rb b/app/models/oms/shipping_address.rb new file mode 100644 index 0000000..f842598 --- /dev/null +++ b/app/models/oms/shipping_address.rb @@ -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 diff --git a/app/models/oms/shipping_method.rb b/app/models/oms/shipping_method.rb new file mode 100644 index 0000000..c52383b --- /dev/null +++ b/app/models/oms/shipping_method.rb @@ -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 diff --git a/lib/flex_commerce.rb b/lib/flex_commerce.rb index 52a0611..e68288a 100644 --- a/lib/flex_commerce.rb +++ b/lib/flex_commerce.rb @@ -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") diff --git a/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb b/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb index 3cb5599..2153fa7 100644 --- a/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb +++ b/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb @@ -21,6 +21,7 @@ def find(args = {}) private def get_request(params) + p path klass.parser.parse(klass, connection.run(:get, path, params, klass.custom_headers)) end attr_accessor :path, :connection diff --git a/lib/flex_commerce_api/oms/api_base.rb b/lib/flex_commerce_api/oms/api_base.rb new file mode 100644 index 0000000..54ade02 --- /dev/null +++ b/lib/flex_commerce_api/oms/api_base.rb @@ -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 diff --git a/spec/integration/oms/customer_order.rb b/spec/integration/oms/customer_order.rb new file mode 100644 index 0000000..84cbd7e --- /dev/null +++ b/spec/integration/oms/customer_order.rb @@ -0,0 +1,14 @@ +require "spec_helper" +require "flex_commerce_api" +require "uri" + + +RSpec.describe "OMS Order History" do + include_context "global context" + + let(:subject_class) { ::FlexCommerce::CustomerOrder } + + context "Fetching Order history via OMS" do + subject_class.fetch_orders(customer_reference: "12345") + end +end \ No newline at end of file From 8e1fce67ade8374d856212c3c0ccf984c8710372 Mon Sep 17 00:00:00 2001 From: praWeb Date: Wed, 8 Apr 2020 20:41:37 +0100 Subject: [PATCH 5/9] remove debug statement --- .../json_api_client_extension/remote_builder.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb b/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb index 2153fa7..3cb5599 100644 --- a/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb +++ b/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb @@ -21,7 +21,6 @@ def find(args = {}) private def get_request(params) - p path klass.parser.parse(klass, connection.run(:get, path, params, klass.custom_headers)) end attr_accessor :path, :connection From b85451e6de9868131f9fd568699bc24533ac1f24 Mon Sep 17 00:00:00 2001 From: praWeb Date: Mon, 15 Jun 2020 10:24:54 +0100 Subject: [PATCH 6/9] Add auth amount in the additional info api call --- lib/paypal_express/process/response_parser.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/paypal_express/process/response_parser.rb b/lib/paypal_express/process/response_parser.rb index c2129c1..338f96d 100644 --- a/lib/paypal_express/process/response_parser.rb +++ b/lib/paypal_express/process/response_parser.rb @@ -19,7 +19,8 @@ def call shipping_method_id: get_shipping_method_details, email: get_email_address, shipping_address_attributes: get_shipping_address_attributes, - billing_address_attributes: get_billing_address_attributes + billing_address_attributes: get_billing_address_attributes, + payment_details: response.params["PaymentDetails"] } end From 8bcb145a682545e70d2b2aa51c7bf1aac7480bb8 Mon Sep 17 00:00:00 2001 From: praWeb Date: Fri, 9 Oct 2020 11:22:16 +0100 Subject: [PATCH 7/9] Add optional param --- app/models/customer_account.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/customer_account.rb b/app/models/customer_account.rb index fe8c76e..a148288 100644 --- a/app/models/customer_account.rb +++ b/app/models/customer_account.rb @@ -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 From e24390569cce183d9ea189bbb2b8100cec5dda42 Mon Sep 17 00:00:00 2001 From: praWeb Date: Mon, 26 Oct 2020 12:15:40 +0000 Subject: [PATCH 8/9] Update the stock check query --- app/models/cart.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/cart.rb b/app/models/cart.rb index c7b88d0..4928c2c 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -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 From f1c15f1dd5454cce1861906e94d41c5dc1191ab3 Mon Sep 17 00:00:00 2001 From: Zeeshan Ishtiaq Date: Mon, 1 Feb 2021 13:27:09 +0000 Subject: [PATCH 9/9] add circleci config --- .circleci/config.yml | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..a3e9229 --- /dev/null +++ b/.circleci/config.yml @@ -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