Class: Tang::ApplyCustomerDiscount

Inherits:
Object
  • Object
show all
Defined in:
app/services/tang/apply_customer_discount.rb

Class Method Summary collapse

Class Method Details

.call(customer, coupon) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/tang/apply_customer_discount.rb', line 3

def self.call(customer, coupon)
  if customer.stripe_id.present?
    begin
      cu = Stripe::Customer.retrieve(customer.stripe_id)
      cu.coupon = coupon.stripe_id
      cu.save

      start = cu.discount.start.to_s
      start_timestamp = DateTime.strptime(start, '%s')
      customer.update(coupon: coupon, coupon_start: start_timestamp)
    rescue Stripe::StripeError => e
      customer.errors.add(:base, :invalid, message: e.message)
    end
  end

  return customer
end