Skip to content

Commit

Permalink
Merge pull request #396 from sharetribe/seller-commission
Browse files Browse the repository at this point in the history
Checkout Finland seller commission
  • Loading branch information
rap1ds committed May 13, 2014
2 parents b7725f2 + cfa8fac commit 48d14ae
Show file tree
Hide file tree
Showing 61 changed files with 396 additions and 363 deletions.
70 changes: 43 additions & 27 deletions app/assets/javascripts/kassi.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ function initialize_listing_view(locale) {
});
}

function initialize_accept_transaction_form(commission_percentage, service_fee_vat, form_type, form_id, minimum_price, minimum_price_message) {
function initialize_accept_transaction_form(commission_percentage, gatewayCommissionPercentage, gatewayCommissionFixed, service_fee_vat, form_type, form_id, minimum_price, minimum_price_message) {
auto_resize_text_areas("text_area");
style_action_selectors();

Expand All @@ -726,26 +726,27 @@ function initialize_accept_transaction_form(commission_percentage, service_fee_v
},
});
} else {
update_complex_form_price_fields(commission_percentage, service_fee_vat);
$(".trigger-focusout").focusout(function(value) {
update_complex_form_price_fields(commission_percentage, service_fee_vat);
});
function update() {
update_complex_form_price_fields(commission_percentage, gatewayCommissionPercentage, gatewayCommissionFixed, service_fee_vat);
}

$(".trigger-focusout").focusout(update);
update();
}

}
}

function updateSellerGetsValue(priceInputSelector, youWillGetSelector, currencySelector, commissionPercentage) {
function updateSellerGetsValue(priceInputSelector, youWillGetSelector, currencySelector, communityCommissionPercentage, gatewayCommissionPercentage, gatewayCommissionFixed) {
$display = $(youWillGetSelector);
$input = $(priceInputSelector);
$currency = $(currencySelector);

function updateYouWillGet() {
var sum = ST.paymentMath.parseFloatFromFieldValue($input.val());
var serviceFee = ST.paymentMath.serviceFee(sum, commissionPercentage);
var sellerGets = sum - serviceFee;
var sellerGets = sum - ST.paymentMath.totalCommission(sum, communityCommissionPercentage, gatewayCommissionPercentage, gatewayCommissionFixed);
var currency = $currency.val();

sellerGets = sellerGets < 0 ? 0 : sellerGets;
$display.text([ST.paymentMath.displayMoney(sellerGets), currency].join(" "));
}

Expand All @@ -758,33 +759,48 @@ function updateSellerGetsValue(priceInputSelector, youWillGetSelector, currencyS

function update_simple_form_price_fields(commission_percentage) {
var sum = ST.paymentMath.parseFloatFromFieldValue($(".invoice-sum-field").val());
var service_fee_sum = ST.paymentMath.serviceFee(sum, commission_percentage);
var service_fee_sum = ST.paymentMath.totalCommission(sum, commission_percentage, 0, 0);
var seller_sum = sum - service_fee_sum;
$("#service-fee").text(ST.paymentMath.displayMoney(service_fee_sum));
$("#payment-to-seller").text(ST.paymentMath.displayMoney(seller_sum));
}

function update_complex_form_price_fields(commission_percentage, service_fee_vat) {
var total_sum = 0;
var total_sum_with_vat = 0;
for (var i = 0; i < $(".field-row").length; i++) {
var sum = parseInt($(".payment-row-sum-field.row" + i).val());
function update_complex_form_price_fields(commissionPercentage, gatewayCommissionPercentage, gatewasCommissionFixed, serviceFeeVat) {
var euro = '\u20AC'

var vat = parseInt($(".payment-row-vat-field.row" + i).val());
if (! vat > 0) { vat = 0;}
var rows = $(".field-row").toArray().map(function(row) {
var row = $(row);
var sumEl = row.find(".payment-row-sum-field");
var vatEl = row.find(".payment-row-vat-field");
var totalEl = row.find(".total-label");
var sum = ST.paymentMath.parseFloatFromFieldValue(sumEl.val());
var vat = ST.paymentMath.parseFloatFromFieldValue(vatEl.val());

row_sum = sum + (sum * vat / 100);
$(".total-label.row" + i).text(row_sum.toFixed(2) + '\u20AC');
total_sum += sum;
total_sum_with_vat += row_sum;
}
vat = Math.min(Math.max(vat, 0), 100);
var sumWithVat = sum + (sum * vat / 100);

return {
totalEl: totalEl,
sumWithVat: sumWithVat
};
});

var total = rows.reduce(function(total, rowObj) {
return total + rowObj.sumWithVat;
}, 0);

var totalFee = ST.paymentMath.totalCommission(total, commissionPercentage, gatewayCommissionPercentage, gatewasCommissionFixed);
var totalFeeWithoutVat = totalFee / (1 + serviceFeeVat / 100);
var youWillGet = total - totalFee;

rows.forEach(function(rowObj) {
rowObj.totalEl.text(rowObj.sumWithVat.toFixed(2) + euro);
});

var service_fee_sum = total_sum*commission_percentage/100;
$("#service-fee-sum").text(service_fee_sum.toFixed(2) + '\u20AC');
$("#service-fee-sum").text(totalFeeWithoutVat.toFixed(2) + euro);
$("#service-fee-total").text(totalFee.toFixed(2) + euro);

service_fee_sum_with_vat = service_fee_sum + (service_fee_sum * service_fee_vat / 100);
$("#service-fee-total").text(service_fee_sum_with_vat.toFixed(2) + '\u20AC');
$("#total").text((total_sum_with_vat + service_fee_sum_with_vat).toFixed(2) + '\u20AC');
$("#total").text(youWillGet.toFixed(2) + euro);
}

function initialize_confirm_transaction_form() {
Expand Down
16 changes: 10 additions & 6 deletions app/assets/javascripts/payment_math.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ window.ST.paymentMath = (function() {
return parseFloat(value.replace(',', '.'));
}

function serviceFee(sum, commissionPercentage) {
return Math.ceil(sum * commissionPercentage / 100);
}

function displayMoney(sum) {
return typeof sum === "number" && !isNaN(sum) ? sum.toFixed(2) : "-";
}

function totalCommission(totalSum, communityCommissionPercentage, gatewayCommissionPercentage, gatewayCommissionFixed) {
var communityCommission = totalSum * communityCommissionPercentage / 100;
var gatewayCommission = totalSum * gatewayCommissionPercentage / 100;
var commission = communityCommission + gatewayCommission + gatewayCommissionFixed;

return Math.ceil(commission);
}

return {
parseFloatFromFieldValue: parseFloatFromFieldValue,
serviceFee: serviceFee,
displayMoney: displayMoney
displayMoney: displayMoney,
totalCommission: totalCommission
};
})();
3 changes: 1 addition & 2 deletions app/controllers/braintree_payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ def update
recipient = @braintree_payment.recipient
listing = @conversation.listing

commission = @current_community.commission_from_seller
price = @braintree_payment.sum_cents

amount = price.to_f / 100 # Braintree want's whole dollars
service_fee = PaymentMath.service_fee(price, commission).to_f / 100
service_fee = @braintree_payment.total_commission.cents.to_f / 100

BTLog.warn("Sending sale transaction from #{payer.id} to #{recipient.id}. Amount: #{amount}, fee: #{service_fee}")

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def ensure_authorized_to_cancel
def prepare_accept_or_reject_form
if @current_community.payments_in_use?
@payment = @current_community.payment_gateway.new_payment
@payment.community = @current_community
@payment.default_sum(@conversation.listing, Maybe(@current_community).vat.or_else(0))
end

if @current_community.requires_payout_registration? && @current_community.payment_possible_for?(@conversation.listing) && ! @current_user.can_receive_payments_at?(@current_community)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/listings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def show
end

def new
@seller_commission = @current_community.payment_gateway.seller_pays_commission? if @current_community.payments_in_use?
@seller_commission_in_use = @current_community.commission_from_seller && @current_community.payments_in_use?
@selected_tribe_navi_tab = "new_listing"
@listing = Listing.new

Expand Down Expand Up @@ -160,11 +160,11 @@ def create
end

def edit
@seller_commission = @current_community.payment_gateway.seller_pays_commission? if @current_community.payments_in_use?
@seller_commission_in_use = @current_community.commission_from_seller && @current_community.payments_in_use?
@selected_tribe_navi_tab = "home"
if !@listing.origin_loc
@listing.build_origin_loc(:location_type => "origin_loc")
end
if !@listing.origin_loc
@listing.build_origin_loc(:location_type => "origin_loc")
end

@custom_field_questions = @listing.category.custom_fields.find_all_by_community_id(@current_community.id)
@numeric_field_ids = numeric_field_ids(@custom_field_questions)
Expand Down
17 changes: 6 additions & 11 deletions app/jobs/payment_created_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ def before(job)
end

def perform
begin
payment = Payment.find(payment_id)
community = Community.find(community_id)
payment_mail_creator = PaymentMailCreator.new(payment, community)
payment = Payment.find(payment_id)
community = Community.find(community_id)
payment_mail_creator = PaymentMailCreator.new(payment, community)

if payment.recipient.should_receive?("email_about_new_payments")
payment_mail_creator.new_payment.deliver
end
payment_mail_creator.receipt_to_payer.deliver
rescue => ex
puts ex.message
puts ex.backtrace.join("\n")
if payment.recipient.should_receive?("email_about_new_payments")
payment_mail_creator.new_payment.deliver
end
payment_mail_creator.receipt_to_payer.deliver
end

end
4 changes: 2 additions & 2 deletions app/mailers/person_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def braintree_new_payment(payment, community)
@email_type = "email_about_new_payments"
@payment = payment

@service_fee = PaymentMath.service_fee(@payment.sum_cents, community.commission_from_seller).to_f / 100
@you_get = PaymentMath::SellerCommission.seller_gets(@payment.sum_cents, community.commission_from_seller).to_f / 100
@service_fee = payment.total_commission.cents.to_f / 100
@you_get = payment.seller_gets.cents.to_f / 100

set_up_urls(@payment.recipient, community, @email_type)
mail(:to => @recipient.confirmed_notification_emails_to,
Expand Down
15 changes: 14 additions & 1 deletion app/models/braintree_payment.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
class BraintreePayment < Payment
attr_accessor :credit_card_number, :credit_card_expiration_date, :cardholder_name, :cvv
attr_accessible :braintree_transaction_id

monetize :sum_cents, :allow_nil => true

def sum_exists?
!sum_cents.nil?
end

def total_sum
sum_cents.to_f / 100
sum
end

# Build default payment sum by listing
# Note: Consider removing this :(
def default_sum(listing, vat=0)
self.sum = listing.price
end
end
30 changes: 29 additions & 1 deletion app/models/checkout_payment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
class CheckoutPayment < Payment
# Currently this model is empty, but because of table inheritance we need this

has_many :rows, :class_name => "PaymentRow", :foreign_key => "payment_id"

def initialize_rows(community)
if community.vat
self.rows = [PaymentRow.new, PaymentRow.new, PaymentRow.new]
else
self.rows = [PaymentRow.new]
end
end

def sum_exists?
!rows.empty?
end

def summary_string
rows.collect(&:title).join(", ")
end

# Total payment that will be charged from the payer's account
def total_sum
rows.collect(&:sum_with_vat).sum
end

# Build default payment sum by listing
# Note: Consider removing this :(
def default_sum(listing, vat=0)
rows.build(title: listing.title, currency: listing.currency, sum: listing.price, vat: vat)
end
end
14 changes: 0 additions & 14 deletions app/models/community.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,6 @@ def mangopay_in_use?
payment_gateway.present? && payment_gateway.type == "Mangopay"
end

# Returns the total service fee for a certain listing
# in the current community (including gateway fee, platform
# fee and marketplace fee)
def service_fee_for(listing)
service_fee = PaymentMath.service_fee(listing.price_cents, commission_from_seller)
Money.new(service_fee, listing.currency)
end

# Price that the seller gets after the service fee is deducted
def price_seller_gets_for(listing)
seller_gets = PaymentMath::SellerCommission.seller_gets(listing.price_cents, commission_from_seller)
Money.new(seller_gets, listing.currency)
end

# Return either minimum price defined by this community or the absolute
# platform default minimum price.
def absolute_minimum_price(currency)
Expand Down
1 change: 1 addition & 0 deletions app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def message_attributes=(attributes)

def payment_attributes=(attributes)
payment ||= community.payment_gateway.new_payment
payment.payment_gateway ||= community.payment_gateway
payment.conversation = self
payment.status = "pending"
payment.payer = requester
Expand Down

0 comments on commit 48d14ae

Please sign in to comment.