Skip to content

Instantly share code, notes, and snippets.

@scottchiang
Created July 10, 2013 06:32
Show Gist options
  • Save scottchiang/5963889 to your computer and use it in GitHub Desktop.
Save scottchiang/5963889 to your computer and use it in GitHub Desktop.
Gumroad balance design question
data model:
seller:
has_many :products
attributes: balance
product:
has_many :purchases
has_many :buyers, :through => :purchase
belongs_to :seller
attributes: price, seller_id
add index to seller_id
purchase:
belongs_to :product
belongs_to :buyer
attributes: payment_accepted(boolean), paid_seller(boolean), refund_requested(boolean), refund_processed(boolean), purchase_date, product_id, buyer_id
add index to product_id and buyer_id
buyer:
has_many :purchases
attribute: balance
I added an index on seller_id on the product model because you will costantly be looking up who's selling the product.
I added an index on buyer_id and product_id on the purchase model because those will be frequent queries.
There is a relationship between all 4 models. You can query all of a seller's products. A product can be purchased many times. Each purchase has the product information as well as the buyer's information.
To pay a seller, take the sellers products and query all new purchases up to a week before the pay day if that's the pay schedule. You can set a different schedule by specifying the pay schedule. The purchases' product_id will need to match with one of the products that belong to the seller. The amount will be totalled and added to the seller's balance.
To refund a purchase, the relationship on purchase model allows you to find both the buyer and seller. Deduct and add the appropriate amount of money from the buyer and seller to complete the refund.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment