Guest User

Untitled

a guest
Mar 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. class PaymentSystem
  2. class << self
  3.  
  4. def get_coupon(coupon_id)
  5. Stripe::Coupon.retrieve coupon_id
  6. end
  7.  
  8. def get_plan(plan_id)
  9. Stripe::Plan.retrieve plan_id
  10. end
  11.  
  12. def get_subscription(sub_id)
  13. Stripe::Subscription.retrieve sub_id
  14. end
  15.  
  16. def get_customer(customer_id)
  17. Stripe::Customer.retrieve customer_id
  18. end
  19.  
  20. def update_customer(customer, data)
  21. customer.source = data[:source]
  22. customer.save
  23. end
  24.  
  25. def delete_customer(customer)
  26. customer.delete
  27. end
  28.  
  29. def get_invoices(customer_id)
  30. Stripe::Invoice.list customer: customer_id
  31. end
  32.  
  33. def create_customer(data)
  34. Stripe::Customer.create data
  35. end
  36.  
  37. def create_subscription(customer, data)
  38. customer.subscriptions.create data
  39. end
  40. def end_subscription(subscription)
  41. subscription.delete at_period_end: true
  42. end
  43.  
  44. def validate_plan_and_coupon(plan_id, coupon_id)
  45. plan = get_plan plan_id
  46. subscription_data = {
  47. plan: plan.id
  48. }
  49. if coupon_id.present?
  50. coupon = get_coupon coupon_id
  51. subscription_data[:coupon] = coupon.id unless !coupon.valid
  52. end
  53. subscription_data
  54. end
  55.  
  56. end
  57. end
Add Comment
Please, Sign In to add comment