Skip to content
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

Support New Beta Version of Affirm with Secondary Financing Options #62

Closed
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 app/assets/javascripts/spree/frontend/affirm_checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
$submit_button.trigger("loading");

// submit the checkout
affirm.checkout.post();
affirm.checkout.open();

e.preventDefault();
return false;
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/spree/affirm_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def confirm
return redirect_to spree.order_path(order), notice: "Order is already in complete state"
end

affirm_checkout = SolidusAffirm::Checkout.new(token: checkout_token)
affirm_transaction = Affirm::Client.new.read_transaction(checkout_token)
provider = SolidusAffirm::Checkout::PROVIDERS[affirm_transaction.provider_id - 1]
affirm_checkout = SolidusAffirm::Checkout.new(token: checkout_token, provider: provider)

affirm_checkout.transaction do
if affirm_checkout.save!
Expand All @@ -24,6 +26,7 @@ def confirm
})
hook = SolidusAffirm::Config.callback_hook.new
hook.authorize!(payment)
hook.remove_tax!(order) if provider == "katapult"
redirect_to hook.after_authorize_url(order)
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/affirm_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def affirm_js_setup(public_api_key, javascript_url)
def affirm_payload_json(order, payment_method, metadata = {})
config = {
confirmation_url: spree.confirm_affirm_url(payment_method_id: payment_method.id, order_id: order.id),
cancel_url: spree.cancel_affirm_url(payment_method_id: payment_method.id, order_id: order.id)
cancel_url: spree.cancel_affirm_url(payment_method_id: payment_method.id, order_id: order.id),
exchange_lease_enabled: SolidusAffirm::Config.exchange_lease_enabled
}
payload = SolidusAffirm::CheckoutPayload.new(order, config, metadata)
SolidusAffirm::Config.checkout_payload_serializer.new(payload, root: false).to_json
Expand Down
3 changes: 3 additions & 0 deletions app/models/solidus_affirm/checkout.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module SolidusAffirm
class Checkout < SolidusSupport.payment_source_parent_class

PROVIDERS = [:affirm, :katapult]

self.table_name = "affirm_checkouts"

def reusable?
Expand Down
5 changes: 5 additions & 0 deletions app/models/solidus_affirm/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ def callback_controller_name
def checkout_payload_serializer
@checkout_payload_serializer ||= SolidusAffirm::CheckoutPayloadSerializer
end

attr_writer :exchange_lease_enabled
def exchange_lease_enabled
@exchange_lease_enabled ||= false
end
end
end
14 changes: 5 additions & 9 deletions app/models/solidus_affirm/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ def cancel(charge_id, try_credit = true)
initialize_gateway

begin
transaction = ::Affirm::Charge.find(charge_id)
# workaround: on 404 responses we get XML data from the API.
# the affirm-ruby gem doesn't handle non-JSON responses at the moment.
rescue NoMethodError
return ActiveMerchant::Billing::Response.new(false, "Affirm charge not found")
end

unless transaction.success?
return ActiveMerchant::Billing::Response.new(false, transaction.error.message)
transaction = ::Affirm::Client.new.read_transaction(charge_id)
# workaround: on 404 responses we get XML data from the API.
# the affirm-ruby gem doesn't handle non-JSON responses at the moment.
rescue Exception => e
return ActiveMerchant::Billing::Response.new(false, e.message)
end

if voidable?(transaction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class CheckoutPayloadSerializer < ActiveModel::Serializer
def merchant
hsh = {
user_confirmation_url: object.config[:confirmation_url],
user_cancel_url: object.config[:cancel_url]
user_cancel_url: object.config[:cancel_url],
exchange_lease_enabled: object.config[:exchange_lease_enabled]
}
hsh[:name] = object.config[:name] if object.config[:name].present?
hsh
Expand Down
6 changes: 5 additions & 1 deletion app/serializers/solidus_affirm/line_item_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

module SolidusAffirm
class LineItemSerializer < ActiveModel::Serializer
attributes :display_name, :sku, :unit_price, :qty, :item_image_url, :item_url
attributes :display_name, :sku, :unit_price, :qty, :item_image_url, :item_url, :leasable

def leasable
true
end

def display_name
object.name
Expand Down
12 changes: 12 additions & 0 deletions app/views/spree/admin/payments/source_views/_affirm.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<fieldset data-hook="affirm_source_view">
<legend align="center">Affirm Source</legend>

<div class="row">
<div class="col-4">
<dl>
<dt>Provider:</dt>
<dd><%= payment.source.provider %></dd>
</dl>
</div>
</div>
</fieldset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAffirmProviderToAffirmCheckouts < ActiveRecord::Migration[6.0]
def change
add_column :affirm_checkouts, :provider, :string, default: :affirm
end
end
36 changes: 18 additions & 18 deletions lib/solidus_affirm/affirm_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ def initialize(options)
end

def authorize(_money, affirm_source, _options = {})
response = ::Affirm::Charge.authorize(affirm_source.token)
if response.success?
ActiveMerchant::Billing::Response.new(true, "Transaction approved", {}, authorization: response.id)
else
ActiveMerchant::Billing::Response.new(false, response.error.message)
begin
response = ::Affirm::Client.new.authorize(affirm_source.token)
return ActiveMerchant::Billing::Response.new(true, "Transaction Approved", {}, authorization: response.id)
rescue Exception => e
return ActiveMerchant::Billing::Response.new(false, e.message)
end
end

def capture(_money, charge_id, _options = {})
response = ::Affirm::Charge.capture(charge_id)
if response.success?
ActiveMerchant::Billing::Response.new(true, "Transaction Captured")
else
ActiveMerchant::Billing::Response.new(false, response.error.message)
begin
response = ::Affirm::Client.new.capture(charge_id)
return ActiveMerchant::Billing::Response.new(true, "Transaction Captured")
rescue Exception => e
return ActiveMerchant::Billing::Response.new(false, e.message)
end
end

def void(charge_id, _money, _options = {})
response = ::Affirm::Charge.void(charge_id)
if response.success?
begin
response = ::Affirm::Client.new.void(charge_id)
return ActiveMerchant::Billing::Response.new(true, "Transaction Voided")
else
return ActiveMerchant::Billing::Response.new(false, response.error.message)
rescue Exception => e
return ActiveMerchant::Billing::Response.new(false, e.message)
end
end

def credit(money, charge_id, _options = {})
response = ::Affirm::Charge.refund(charge_id, amount: money)
if response.success?
begin
response = ::Affirm::Client.new.refund(charge_id, money)
return ActiveMerchant::Billing::Response.new(true, "Transaction Credited with #{money}")
else
return ActiveMerchant::Billing::Response.new(false, response.error.message)
rescue Exception => e
return ActiveMerchant::Billing::Response.new(false, e.message)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/solidus_affirm/callback_hook/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ module CallbackHook
class Base
def authorize!(payment)
payment.process!
authorized_affirm = Affirm::Charge.find(payment.response_code)
authorized_affirm = Affirm::Client.new.read_transaction(payment.response_code)
payment.amount = authorized_affirm.amount / 100.0
payment.save!
payment.order.next! if payment.order.payment?
end

def remove_tax!(order)
# TODO:
end

def after_authorize_url(order)
order_state_checkout_path(order)
end
Expand Down
3 changes: 1 addition & 2 deletions solidus_affirm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency 'active_model_serializers', '~> 0.10'
s.add_dependency 'affirm-ruby', '1.1.0'
s.add_dependency 'affirm-ruby-api', '~> 1.0'
s.add_dependency 'solidus_core', ['>= 2.0', '< 3']
s.add_dependency "solidus_support", '>= 0.2.2'

s.add_development_dependency 'solidus_dev_support'
s.add_development_dependency 'vcr'
s.add_development_dependency 'webmock'
end
133 changes: 0 additions & 133 deletions spec/fixtures/vcr_casettes/callback_hook_authorize_success.yml

This file was deleted.

44 changes: 0 additions & 44 deletions spec/fixtures/vcr_casettes/invalid_authorize.yml

This file was deleted.

Loading