How can I add in-store credit to my Ruby On Rails app -
i want add store credit app fiverr or other online marketplaces have users can have credit or money in accounts. there gem or recommended steps me build this?
credit little intense because have balance actual money. when world, need make sure app works properly.
nonetheless, you'd have set model , other things working.
the 2 aspects require storage (a model) , payment (mechanics accept money). these permit app accept payments, , have way store them (giving balance work with).
the setup not overly complicated; you'll have payments model literally store money user has sent - you'll able balance through user
model:
storage
#app/models/user.rb class user < activerecord::base #columns id | username | email | etc etc etc has_many :payments def balance payments.sum(:value) #-> @user.balance -> "25" end end #app/models/payment.rb class payment < activerecord::base #columns id | user_id | value | currency | transaction_id | created_at | updated_at belongs_to :user end
this give ability have following setup:
#users id username email 1 joe_bloggs joe_bloggs@yahoo.com #payments id user_id value currency transaction_id 1 1 17.00 usd xb56ytsg3f5f 2 1 7.00 usd yu8953fg5red 3 1 -5.00 usd nil
one of important things note here transaction_id
in payment model. important because allows leave transaction data (ie payment information) provider use (more below).
a typical issue many developers try , store own data. whilst gives them great data sets, means have maintain all. storing transaction_id
payments.
you'll note negative value in payments#3
. demonstrate user-to-user payment (ie spending of in-store credit). has worked on, can come later.
--
payment
the second piece of puzzle use payment processor.
the favourite stripe there others, paypal etc.
in terms of stripe, it's quite involved set up. however, if play cards right, you'll able using following setup:
#config/routes.rb resource :profile resources :payments #-> url.com/profile/payments/new end #app/controllers/payments_controller.rb class paymentscontroller < applicationcontroller before_action :authenticate_user! #-> if using devise, highly recommended def index @payments = current_user.payments end def new @payment = payment.new #-> has work stripe, that's question end def create @payment = payment.new payment_params #-> should take stripe return data & save in model @payment.save end private def payment_params params.require.... end end
an important note.
stripe provides functionality receive payment. payment
model should store transactions in your dataset.
as mentioned, smart thing keep transaction data on file within payment processor, referring transaction_id
need. however, have store payment data in own database working properly.
there number of pre-packed solutions type of functionality:
Comments
Post a Comment