Skip to content

Commit

Permalink
close #170 revert payout profile payment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
theachoem committed Jul 15, 2024
1 parent bec9cf6 commit e13551b
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 182 deletions.
25 changes: 12 additions & 13 deletions app/models/spree/payout_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ module Spree
class PayoutProfile < Base
acts_as_paranoid

has_many :payout_profile_payments, class_name: 'Spree::PayoutProfilePayment', inverse_of: :payout_profile
has_many :payout_profile_products, class_name: 'Spree::PayoutProfileProduct', inverse_of: :payout_profile
has_many :products, class_name: "Spree::Product", through: :payout_profile_products
has_many :products, class_name: 'Spree::Product', through: :payout_profile_products

belongs_to :vendor, class_name: 'Spree::Vendor', optional: true, inverse_of: :payout_profiles

validates :type, presence: true
validates :name, presence: true
validates :bank_account_number, presence: true, uniqueness: { scope: [:type, :vendor_id] }
validates :bank_account_number, presence: true, uniqueness: { scope: %i[type vendor_id] }

scope :payway, -> { where(type: 'Spree::PayoutProfiles::PaywayV2') }
scope :verified, -> { where.not(verified_at: nil) }
Expand All @@ -22,8 +21,8 @@ class PayoutProfile < Base
self.whitelisted_ransackable_attributes = %w[name vendor_id]

def self.default
Rails.cache.fetch("default_payout_account/#{self.name.underscore}") do
find_by(type: self.name, default: true)
Rails.cache.fetch("default_payout_account/#{name.underscore}") do
find_by(type: name, default: true)
end
end

Expand All @@ -33,10 +32,10 @@ def bank_name

def display_name
display_name = name
bank_info = [bank_name, bank_account_number].compact.join(" - ")

bank_info = [bank_name, bank_account_number].compact.join(' - ')
display_name += " (#{bank_info})" unless bank_info.empty?

display_name
end

Expand Down Expand Up @@ -86,10 +85,10 @@ def ensure_default_exists_and_clear_vendor
end

def confirm_destroyable
unless can_be_deleted?
errors.add(:base, :cannot_destroy_only_payout_profile)
throw(:abort)
end
end
return if can_be_deleted?

errors.add(:base, :cannot_destroy_only_payout_profile)
throw(:abort)
end
end
end
6 changes: 0 additions & 6 deletions app/models/spree/payout_profile_payment.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/spree/payout_profile_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class PayoutProfileProduct < Base
validates :product, presence: true

validates :payout_profile_id, uniqueness: { scope: :product_id }
end
end
end
2 changes: 1 addition & 1 deletion app/models/vpago/line_item_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def required_payway_payout?

def commission_rate
product_commission_rate = product.respond_to?(:commission_rate) ? product.commission_rate : nil
product_commission_rate || vendor&.commission_rate || 0
product_commission_rate || vendor&.commission_rate || 0
end

def commission_amount
Expand Down
26 changes: 11 additions & 15 deletions app/models/vpago/payment_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
module Vpago
module PaymentDecorator
def self.prepended(base)
base.has_many :payout_profile_payments, class_name: 'Spree::PayoutProfilePayment', inverse_of: :payment
end

# On the first call, everything works. The order is transitioned to complete and one Spree::Payment,
# On the first call, everything works. The order is transitioned to complete and one Spree::Payment,
# which redirect the payment. But, after making the same call again,
# for instance because the payment wasn't completed or failed,
# another Spree::Payment is created but without a payment_url. So, if a consumer,
# for whatever reason, failed to complete the first payment, it would not be possible try again.
# for whatever reason, failed to complete the first payment, it would not be possible try again.
# This also meant that any consecutive Spree::Payment would not have a payment_url. The consumer is stuck

def build_source
return unless new_record?

if source_attributes.present? && source.blank? && payment_method.try(:payment_source_class)
self.source = payment_method.payment_source_class.new(source_attributes)
source.payment_method_id = payment_method.id
source.user_id = order.user_id if order
return unless source_attributes.present? && source.blank? && payment_method.try(:payment_source_class)

# Spree will not process payments if order is completed.
# We should call process! for completed orders to create a the gateway payment.
process! if order.completed?
end
self.source = payment_method.payment_source_class.new(source_attributes)
source.payment_method_id = payment_method.id
source.user_id = order.user_id if order

# Spree will not process payments if order is completed.
# We should call process! for completed orders to create a the gateway payment.
process! if order.completed?
end

def request_update
Expand All @@ -41,4 +37,4 @@ def authorized?
end
end

Spree::Payment.prepend(Vpago::PaymentDecorator)
Spree::Payment.prepend(Vpago::PaymentDecorator)
22 changes: 14 additions & 8 deletions app/models/vpago/payment_method_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
module Vpago
module PaymentMethodDecorator
TYPE_PAYWAY = 'Spree::Gateway::Payway'
TYPE_PAYWAY_V2 = 'Spree::Gateway::PaywayV2'
TYPE_WINGSDK = 'Spree::Gateway::WingSdk'
TYPE_ACLEDA = 'Spree::Gateway::Acleda'
TYPE_ACLEDA_MOBILE = 'Spree::Gateway::AcledaMobile'
TYPE_PAYWAY = 'Spree::Gateway::Payway'.freeze
TYPE_PAYWAY_V2 = 'Spree::Gateway::PaywayV2'.freeze
TYPE_WINGSDK = 'Spree::Gateway::WingSdk'.freeze
TYPE_ACLEDA = 'Spree::Gateway::Acleda'.freeze
TYPE_ACLEDA_MOBILE = 'Spree::Gateway::AcledaMobile'.freeze

def self.prepended(base)
base.preference :icon_name, :string, default: 'cheque'

def base.vpago_payments
[Spree::PaymentMethod::TYPE_PAYWAY_V2, Spree::PaymentMethod::TYPE_PAYWAY, Spree::PaymentMethod::TYPE_WINGSDK, Spree::PaymentMethod::TYPE_ACLEDA, Spree::PaymentMethod::TYPE_ACLEDA_MOBILE]
[
Spree::PaymentMethod::TYPE_PAYWAY_V2,
Spree::PaymentMethod::TYPE_PAYWAY,
Spree::PaymentMethod::TYPE_WINGSDK,
Spree::PaymentMethod::TYPE_ACLEDA,
Spree::PaymentMethod::TYPE_ACLEDA_MOBILE
]
end
end

def vpago_payment?
self.class.vpago_payments.include?(self.type)
self.class.vpago_payments.include?(type)
end

def vapgo_checkout_service
Expand All @@ -40,7 +46,7 @@ def payment_request_updater
elsif type_acleda?
::Vpago::Acleda::PaymentRequestUpdater
elsif type_acleda_mobile?
::Vpago::AcledaMobile::PaymentRequestUpdater
::Vpago::AcledaMobile::PaymentRequestUpdater
end
end

Expand Down
12 changes: 0 additions & 12 deletions db/migrate/20240520091500_create_spree_payout_profile_payments.rb

This file was deleted.

36 changes: 13 additions & 23 deletions lib/vpago/payment_status_marker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ class PaymentStatusMarker
attr_accessor :payment, :error_message

# :status, :description, :updated_by_user_id, updated_reason
def initialize(payment, options={})
def initialize(payment, options = {})
@payment = payment
@options = options
@options[:status] = @options[:status] || false

# payouts must be constructed array of Spree::PayoutProfilePayment
@payouts = @options[:payouts]
@options[:status] = @options[:status] || false
end

def call
Expand All @@ -26,8 +23,9 @@ def update_payment_source

payment_status = @options[:status] ? 'success' : 'failed'
source.payment_status = payment_status
source.payment_description = @options[:description]
source.transaction_id = @options[:transaction_id] if @options[:transaction_id].present? ## for acleda, we already update the transaction_id at when checkout
source.payment_description = @options[:description]
## for acleda, we already update the transaction_id at when checkout
source.transaction_id = @options[:transaction_id] if @options[:transaction_id].present?
source.preferred_wing_response = @options[:wing_response]
source.preferred_acleda_response = @options[:acleda_response]
source.preferred_payway_v2_response = @options[:payway_v2_response]
Expand All @@ -37,10 +35,10 @@ def update_payment_source
source.updated_reason = @options[:updated_reason]
source.updated_by_user_at = Time.zone.now
end
if(!source.save)
@error_message = source.errors.full_messages.join('\n')
end

return if source.save

@error_message = source.errors.full_messages.join('\n')
end

def update_payment_and_order
Expand All @@ -49,28 +47,19 @@ def update_payment_and_order
else
transition_to_failed!
end

order_updater
end

def transition_to_paid!
complete_payment!
complete_order!
save_payout_payments!
end

def save_payout_payments!
return unless @payouts.present?

ActiveRecord::Base.transaction do
@payouts.each { |payment| payment.save! }
end
end

def transition_to_failed!
@payment.failure! if !@payment.failed?
@payment.failure! unless @payment.failed?
@payment.order.update(state: 'payment')

notify_failed_payment
end

Expand All @@ -94,6 +83,7 @@ def complete_payment!

def complete_order!
return if @payment.order.completed?

order = @payment.order
order.finalize!
order.update(state: 'complete', completed_at: Time.zone.now)
Expand Down
3 changes: 1 addition & 2 deletions lib/vpago/payway_v2/payment_request_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def call
checker_result = {
status: true,
description: nil,
payouts: checker.build_payout_profile_payments,
payway_v2_response: checker.json_response,
payway_v2_response: checker.json_response
}
marker_options = @options.merge(checker_result)
marker = ::Vpago::PaymentStatusMarker.new(@payment, marker_options)
Expand Down
28 changes: 6 additions & 22 deletions lib/vpago/payway_v2/transaction_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ def success?
# request failed does not mean payment failed.
# but it failed when status is failed.
def pending?
%w(1 2).include?(status)
%w[1 2].include?(status)
end

def failed?
%w(3 4 5).include?(status)
%w[3 4 5].include?(status)
end

def check_remote_status
conn = Faraday::Connection.new do |faraday|
faraday.request :url_encoded
faraday.request :url_encoded
end

data = {
Expand All @@ -45,7 +45,7 @@ def check_remote_status
hash: checker_hmac
}

conn.post(check_transaction_url, data)
conn.post(check_transaction_url, data)
end

def error_message
Expand All @@ -60,22 +60,6 @@ def json_response
end
end

def build_payout_profile_payments
payouts_response = json_response['payout']

return [] if payouts_response.nil? || !payouts_response.is_a?(Array) || payouts_response.empty?

payouts_response.map do |payout|
payout_profile = Spree::PayoutProfiles::PaywayV2.where(bank_account_number: payout['acc']).first_or_create! do |profile|
profile.name = payout['acc_name']&.strip
end

payout_profile_payment = Spree::PayoutProfilePayment.where(payout_profile: payout_profile, payment: @payment).first_or_initialize
payout_profile_payment.amount = payout['amt'].to_f
payout_profile_payment
end
end

def checker_hmac
data = "#{req_time}#{merchant_id}#{transaction_id}"
hash = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), api_key, data))
Expand All @@ -85,7 +69,7 @@ def checker_hmac
end

def check_transaction_url
"#{host}#{ENV['PAYWAY_CHECK_TRANSACTION_PATH']}"
"#{host}#{ENV.fetch('PAYWAY_CHECK_TRANSACTION_PATH', nil)}"
end
end
end
Expand Down
Loading

0 comments on commit e13551b

Please sign in to comment.