mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-10-15 00:00:44 -04:00
This commit adds `Customer#create_subscription`, which allows a subscription to be created on a customer without first fetching the customer record. Previously: ``` customer = Stripe::Customer.retrieve('cus_abc123def') # GET request customer.subscriptions.create(plan: 'cool-plan-1') # POST request ``` **No alteration has been made to the above method; the preceding implementation still functions as it did previously.** With `#create_subscription`: ``` customer = Stripe::Customer.new('cus_abc123def') # No request customer.create_subscription(plan: 'cool-plan-1') # POST request ``` This method removes the initial `GET` request and instead issues a `POST` directly to create the subscription.
70 lines
1.7 KiB
Ruby
70 lines
1.7 KiB
Ruby
module Stripe
|
|
class Customer < APIResource
|
|
include Stripe::APIOperations::Create
|
|
include Stripe::APIOperations::Delete
|
|
include Stripe::APIOperations::Update
|
|
include Stripe::APIOperations::List
|
|
|
|
def add_invoice_item(params)
|
|
InvoiceItem.create(params.merge(:customer => id), @api_key)
|
|
end
|
|
|
|
def invoices
|
|
Invoice.all({ :customer => id }, @api_key)
|
|
end
|
|
|
|
def invoice_items
|
|
InvoiceItem.all({ :customer => id }, @api_key)
|
|
end
|
|
|
|
def upcoming_invoice
|
|
Invoice.upcoming({ :customer => id }, @api_key)
|
|
end
|
|
|
|
def charges
|
|
Charge.all({ :customer => id }, @api_key)
|
|
end
|
|
|
|
def create_upcoming_invoice(params={})
|
|
Invoice.create(params.merge(:customer => id), @api_key)
|
|
end
|
|
|
|
def cancel_subscription(params={})
|
|
response, api_key = Stripe.request(:delete, subscription_url, @api_key, params)
|
|
refresh_from({ :subscription => response }, api_key, true)
|
|
subscription
|
|
end
|
|
|
|
def update_subscription(params)
|
|
response, api_key = Stripe.request(:post, subscription_url, @api_key, params)
|
|
refresh_from({ :subscription => response }, api_key, true)
|
|
subscription
|
|
end
|
|
|
|
def create_subscription(params)
|
|
response, api_key = Stripe.request(:post, subscriptions_url, @api_key, params)
|
|
refresh_from({ :subscription => response }, api_key, true)
|
|
subscription
|
|
end
|
|
|
|
def delete_discount
|
|
Stripe.request(:delete, discount_url, @api_key)
|
|
refresh_from({ :discount => nil }, api_key, true)
|
|
end
|
|
|
|
private
|
|
|
|
def discount_url
|
|
url + '/discount'
|
|
end
|
|
|
|
def subscription_url
|
|
url + '/subscription'
|
|
end
|
|
|
|
def subscriptions_url
|
|
url + '/subscriptions'
|
|
end
|
|
end
|
|
end
|