Skip to content
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
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
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
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
22 changes: 12 additions & 10 deletions lib/paypal_express/process/paypal_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
end
25 changes: 15 additions & 10 deletions lib/paypal_express/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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(
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,4 +27,4 @@
send(key, value)
end
end
end
end
6 changes: 3 additions & 3 deletions spec/factories/asset_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
2 changes: 1 addition & 1 deletion spec/factories/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
end

factory :bundle_list, parent: :json_api_resource_list do
type "bundle"
type { "bundle" }
end
end
4 changes: 2 additions & 2 deletions spec/factories/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -17,4 +17,4 @@
send(key, value)
end
end
end
end
Loading