Instantly share code, notes, and snippets.

Embed
What would you like to do?
A payment service wrapper for stripe and ruby on rails
class PaymentSystem
class << self
def get_coupon(coupon_id)
Stripe::Coupon.retrieve coupon_id
end
def get_plan(plan_id)
Stripe::Plan.retrieve plan_id
end
def get_subscription(sub_id)
Stripe::Subscription.retrieve sub_id
end
def get_customer(customer_id)
Stripe::Customer.retrieve customer_id
end
def update_customer(customer, data)
customer.source = data[:source]
customer.save
end
def delete_customer(customer)
customer.delete
end
def get_invoices(customer_id)
Stripe::Invoice.list customer: customer_id
end
def create_customer(data)
Stripe::Customer.create data
end
def create_subscription(customer, data)
customer.subscriptions.create data
end
def end_subscription(subscription)
subscription.delete at_period_end: true
end
def validate_plan_and_coupon(plan_id, coupon_id)
plan = get_plan plan_id
subscription_data = {
plan: plan.id
}
if coupon_id.present?
coupon = get_coupon coupon_id
subscription_data[:coupon] = coupon.id unless !coupon.valid
end
subscription_data
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment