Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def perform(stripe_event, *args)
- if stripe_event.event_type != "transfer.paid"
- logger.error "Event was not of type transfer.paid!"
- return
- end
- if stripe_event.data['object']['reversed']
- logger.error "Event shows the transfer was reversed!!"
- return
- end
- if stripe_event.data['object']['status'] != 'paid'
- logger.error "Event does not have a paid status!"
- return
- end
- # Figure out the seller from the event's bank account
- seller = Seller.where('bank_accounts ? :bank_id', { bank_id: stripe_event.data['object']['bank_account']['id'] }).first
- if seller.present?
- seller.transaction do
- # Make sure recent orders have the most recent references
- seller.orders.where('created_at > ?', Time.zone.now - 15.days).each do |o|
- o.update_stripe_references
- end
- # Grab the orders that are part of the transfer
- orders = []
- tnxs = Stripe::BalanceTransaction.all({
- transfer: stripe_event.stripe_object_id,
- limit:100
- },
- {
- api_key: seller.stripe_account_secret,
- })
- tnxs.data.each do |txn|
- Order.with_payment_id(txn.source).each do |o|
- if o.present?
- # Store the transfer id with the order
- o.stripe_references[:transfers] ||= []
- o.stripe_references[:transfers] = (o.stripe_references[:transfers] + [stripe_event.stripe_object_id]).uniq
- o.save!
- orders << o.id
- end
- end
- end
- s = SellerTransaction.new
- s.seller_id = seller.id
- s.ref = stripe_event.stripe_object_id
- #These lines are not appearing on the invoice for certain customers.
- s.description = "Transfer to #{stripe_event.data['object']['bank_account']['bank_name']} (***#{stripe_event.data['object']['bank_account']['last4']}) Orders #{orders}"
- s.debit = Money.new(stripe_event.data['object']['amount'], stripe_event.data['object']['currency'])
- s.save!
- end
- else
- logger.error "ERROR: Unable to find Seller!"
- return
- end
- end
- def update_stripe_references
- self.payments.each do |p|
- if p.has_key?('charge_id')
- ch = Stripe::Charge.retrieve(p['charge_id'])
- next if ch['transfer'].nil?
- tr = Stripe::Transfer.retrieve(ch.transfer)
- payments = self.stripe_references[:payments] || []
- payments.push(tr.destination_payment)
- p['payment_id'] = tr.destination_payment
- self.stripe_references[:payments] = payments.uniq
- transfers = self.stripe_references[:transfers] || []
- transfers.push(tr.id)
- self.stripe_references[:transfers] = transfers.uniq
- end
- end
- self.save!
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement