split fees between buyer and seller, fix a couple tasks, change timing

pull/3/head
lalanza808 5 years ago
parent bad79cedec
commit 4c97b3b114

@ -107,8 +107,8 @@ def edit_bid(request, bid_id):
@login_required @login_required
def accept_bid(request, bid_id): def accept_bid(request, bid_id):
aw = AuctionWallet() aw = AuctionWallet()
bid = ItemBid.objects.get(id=bid_id) bid = ItemBid.objects.filter(id=bid_id).first()
platform_fee_xmr = bid.bid_price_xmr * (settings.PLATFORM_FEE_PERCENT / 100) platform_fee_xmr = bid.bid_price_xmr * (settings.PLATFORM_FEE_PERCENT / 100) / 2 # split buyer/seller
expected_payment_xmr = bid.bid_price_xmr + platform_fee_xmr expected_payment_xmr = bid.bid_price_xmr + platform_fee_xmr
account_label = f'Sale account for Item #{bid.item.id}, Bid #{bid.id}' account_label = f'Sale account for Item #{bid.item.id}, Bid #{bid.id}'

@ -7,5 +7,5 @@ register = template.Library()
@register.filter(is_safe=True) @register.filter(is_safe=True)
def determine_platform_fee(value): def determine_platform_fee(value):
platform_fee_xmr = float(value * (settings.PLATFORM_FEE_PERCENT / 100)) platform_fee_xmr = float(value * (settings.PLATFORM_FEE_PERCENT / 100) / 2)
return round(platform_fee_xmr, 4) return round(platform_fee_xmr, 4)

@ -113,9 +113,9 @@ def poll_for_buyer_escrow_payments():
for sale in item_sales: for sale in item_sales:
logger.info(f'[INFO] Polling escrow address #{sale.escrow_account_index} for sale #{sale.id} for new funds.') logger.info(f'[INFO] Polling escrow address #{sale.escrow_account_index} for sale #{sale.id} for new funds.')
sale_account = aw.wallet.accounts[sale.escrow_account_index] sale_account = aw.wallet.accounts[sale.escrow_account_index]
balance = sale_account.balances()[1] unlocked = sale_account.balances()[1]
sale.received_payment_xmr = balance sale.received_payment_xmr = unlocked
if balance >= Decimal(sale.expected_payment_xmr): if unlocked >= Decimal(str(sale.expected_payment_xmr)):
logger.info(f'[INFO] Found payment of {sale.received_payment_xmr} XMR for sale #{sale.id}.') logger.info(f'[INFO] Found payment of {sale.received_payment_xmr} XMR for sale #{sale.id}.')
sale.payment_received = True sale.payment_received = True
@ -131,12 +131,14 @@ def pay_sellers_on_sold_items():
item_sales = ItemSale.objects.filter(item_received=True, payment_received=True).filter(seller_paid=False) item_sales = ItemSale.objects.filter(item_received=True, payment_received=True).filter(seller_paid=False)
for sale in item_sales: for sale in item_sales:
logger.info(f'[INFO] Sending {sale.agreed_price_xmr} XMR from wallet account #{sale.escrow_account_index} to item owner\'s payout address for sale #{sale.id}.') # Take platform fees from the sale - the 50:50 split between buyer/seller
sale_total = sale.agreed_price_xmr - sale.platform_fee_xmr
sale_account = aw.wallet.accounts[sale.escrow_account_index] sale_account = aw.wallet.accounts[sale.escrow_account_index]
logger.info(f'[INFO] Sending {sale_total} XMR from wallet account #{sale.escrow_account_index} to item owner\'s payout address for sale #{sale.id}.')
if sale_account.balances()[1] > Decimal(sale.agreed_price_xmr): if sale_account.balances()[1] > Decimal(sale.agreed_price_xmr):
try: try:
aw.wallet.accounts[sale.escrow_account_index].transfer( sale_account.transfer(
sale.item.payout_address, sale.agreed_price_xmr sale.item.payout_address, sale_total
) )
sale.seller_paid = True sale.seller_paid = True
sale.escrow_complete = True sale.escrow_complete = True
@ -156,7 +158,7 @@ def pay_sellers_on_sold_items():
sale.seller_notified_of_payout = True sale.seller_notified_of_payout = True
sale.save() sale.save()
@periodic_task(crontab(minute='0', hour='*')) @periodic_task(crontab(minute='*/30'))
def pay_platform_on_sold_items(): def pay_platform_on_sold_items():
aw = AuctionWallet() aw = AuctionWallet()
if aw.connected is False: if aw.connected is False:
@ -171,9 +173,11 @@ def pay_platform_on_sold_items():
for sale in item_sales: for sale in item_sales:
logger.info(f'[INFO] Paying platform fees for sale #{sale.id} to wallet {aof}.') logger.info(f'[INFO] Paying platform fees for sale #{sale.id} to wallet {aof}.')
sale_account = aw.wallet.accounts[sale.escrow_account_index] sale_account = aw.wallet.accounts[sale.escrow_account_index]
if sale_account.balances()[1] >= Decimal(0.0): bal = sale_account.balances()[1]
if bal >= 0:
logger.info(f'[INFO] Getting platform fees of {bal} XMR')
try: try:
aw.wallet.accounts[sale.escrow_account_index].sweep_all(aof) sale_account.sweep_all(aof)
sale.platform_paid = True sale.platform_paid = True
sale.sale_finalized = True sale.sale_finalized = True
sale.save() sale.save()

@ -33,13 +33,15 @@ def get_sale(request, sale_id):
_address_qr = BytesIO() _address_qr = BytesIO()
address_qr = qrcode_make(qr_uri).save(_address_qr) address_qr = qrcode_make(qr_uri).save(_address_qr)
total_seller_payout = sale.agreed_price_xmr - sale.platform_fee_xmr
context = { context = {
'sale': sale, 'sale': sale,
'qrcode': b64encode(_address_qr.getvalue()).decode(), 'qrcode': b64encode(_address_qr.getvalue()).decode(),
'shipping_address': UserShippingAddress.objects.filter( 'shipping_address': UserShippingAddress.objects.filter(
user=bid.bidder user=bid.bidder
).first() ).first(),
'total_seller_payout': total_seller_payout,
} }
return render(request, 'sales/get_sale.html', context) return render(request, 'sales/get_sale.html', context)

@ -114,9 +114,12 @@
<img src="{% static 'images/monero-symbol-800.png' %}" width=200 class="center"> <img src="{% static 'images/monero-symbol-800.png' %}" width=200 class="center">
{% elif sale.item_received %} {% elif sale.item_received %}
<p class="sale-info">Hey {{ sale.item.owner.username }},</p> <p class="sale-info">Hey {{ sale.item.owner.username }},</p>
<p class="sale-info">The buyer confirmed receipt of their shipment which means things worked out. It's time for you to get paid!<p> <p class="sale-info">The buyer confirmed receipt of their shipment which means things worked out. It's time for you to get paid!</p>
<p class="sale-info">Your payout address provided during item creation will be paid the accepted bid amount out of the escrow wallet.<p> <p class="sale-info">Your payout address provided during item creation will be paid the accepted bid amount out of the escrow wallet.</p>
<p class="sale-info"><strong>Payout Address</strong>: {{ sale.item.payout_address }}<p> <p class="sale-info"><strong>Accepted Bid (XMR)</strong>: {{ sale.bid.bid_price_xmr }}</p>
<p class="sale-info"><strong>Platform Fee (XMR)</strong>: {{ sale.platform_fee_xmr }}</p>
<p class="sale-info"><strong>Total Payout (XMR)</strong>: {{ total_seller_payout }}</p>
<p class="sale-info"><strong>Payout Address</strong>: {{ sale.item.payout_address }}</p>
<p class="sale-info">Thanks for using {{ site_meta.name }}!</p> <p class="sale-info">Thanks for using {{ site_meta.name }}!</p>
<hr> <hr>
<p class="sale-info">Please provide me with any feedback so I can make the process better in the future.</p> <p class="sale-info">Please provide me with any feedback so I can make the process better in the future.</p>