mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-10-11 00:01:46 -04:00
Support persisted use of Stripe-Account header everywhere
Including implicit use in /v1/accounts/ endpoints
This commit is contained in:
parent
085ad6cfb3
commit
4d611c62f7
@ -14,6 +14,7 @@ require 'stripe/api_operations/create'
|
|||||||
require 'stripe/api_operations/update'
|
require 'stripe/api_operations/update'
|
||||||
require 'stripe/api_operations/delete'
|
require 'stripe/api_operations/delete'
|
||||||
require 'stripe/api_operations/list'
|
require 'stripe/api_operations/list'
|
||||||
|
require 'stripe/api_operations/request'
|
||||||
|
|
||||||
# Resources
|
# Resources
|
||||||
require 'stripe/util'
|
require 'stripe/util'
|
||||||
@ -56,6 +57,7 @@ module Stripe
|
|||||||
DEFAULT_CA_BUNDLE_PATH = File.dirname(__FILE__) + '/data/ca-certificates.crt'
|
DEFAULT_CA_BUNDLE_PATH = File.dirname(__FILE__) + '/data/ca-certificates.crt'
|
||||||
@api_base = 'https://api.stripe.com'
|
@api_base = 'https://api.stripe.com'
|
||||||
@connect_base = 'https://connect.stripe.com'
|
@connect_base = 'https://connect.stripe.com'
|
||||||
|
@uploads_base = 'https://uploads.stripe.com'
|
||||||
|
|
||||||
@ssl_bundle_path = DEFAULT_CA_BUNDLE_PATH
|
@ssl_bundle_path = DEFAULT_CA_BUNDLE_PATH
|
||||||
@verify_ssl_certs = true
|
@verify_ssl_certs = true
|
||||||
@ -63,7 +65,7 @@ module Stripe
|
|||||||
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :api_key, :api_base, :verify_ssl_certs, :api_version, :connect_base
|
attr_accessor :api_key, :api_base, :verify_ssl_certs, :api_version, :connect_base, :uploads_base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.api_url(url='', api_base_url=nil)
|
def self.api_url(url='', api_base_url=nil)
|
||||||
@ -88,6 +90,12 @@ module Stripe
|
|||||||
'email support@stripe.com if you have any questions.)')
|
'email support@stripe.com if you have any questions.)')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if method == :get && headers.key?(:idempotency_key)
|
||||||
|
raise ArgumentError.new(
|
||||||
|
'Idempotency keys cannot be used for GET requests.'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
request_opts = { :verify_ssl => false }
|
request_opts = { :verify_ssl => false }
|
||||||
|
|
||||||
if ssl_preflight_passed?
|
if ssl_preflight_passed?
|
||||||
|
@ -1,9 +1,27 @@
|
|||||||
module Stripe
|
module Stripe
|
||||||
class Account < SingletonAPIResource
|
class Account < APIResource
|
||||||
|
include Stripe::APIOperations::Create
|
||||||
|
include Stripe::APIOperations::List
|
||||||
|
include Stripe::APIOperations::Update
|
||||||
|
|
||||||
|
def url
|
||||||
|
if self['id']
|
||||||
|
super
|
||||||
|
else
|
||||||
|
"/v1/account"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @override To make id optional
|
||||||
|
def self.retrieve(id=nil, opts={})
|
||||||
|
super
|
||||||
|
end
|
||||||
|
|
||||||
def deauthorize(client_id, opts={})
|
def deauthorize(client_id, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
opts = {:api_base => Stripe.connect_base}.merge(Util.normalize_opts(opts))
|
||||||
response, api_key = Stripe.request(:post, '/oauth/deauthorize', api_key, { 'client_id' => client_id, 'stripe_user_id' => self.id }, headers, Stripe.connect_base)
|
response, opts = request(:post, '/oauth/deauthorize', { 'client_id' => client_id, 'stripe_user_id' => self.id }, opts)
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
opts.delete(:api_base) # the api_base here is a one-off, don't persist it
|
||||||
|
Util.convert_to_stripe_object(response, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,9 +3,8 @@ module Stripe
|
|||||||
module Create
|
module Create
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def create(params={}, opts={})
|
def create(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, url, params, opts)
|
||||||
response, api_key = Stripe.request(:post, self.url, api_key, params, headers)
|
Util.convert_to_stripe_object(response, opts)
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
module Stripe
|
module Stripe
|
||||||
module APIOperations
|
module APIOperations
|
||||||
module Delete
|
module Delete
|
||||||
def delete(params = {}, opts={})
|
def delete(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
opts = Util.normalize_opts(opts)
|
||||||
response, api_key = Stripe.request(:delete, url, api_key || @api_key, params, headers)
|
response, opts = request(:delete, url, params, opts)
|
||||||
refresh_from(response, api_key)
|
refresh_from(response, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,9 +3,9 @@ module Stripe
|
|||||||
module List
|
module List
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def all(filters={}, opts={})
|
def all(filters={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
opts = Util.normalize_opts(opts)
|
||||||
response, api_key = Stripe.request(:get, url, api_key, filters, headers)
|
response, opts = request(:get, url, filters, opts)
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
Util.convert_to_stripe_object(response, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
32
lib/stripe/api_operations/request.rb
Normal file
32
lib/stripe/api_operations/request.rb
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
module Stripe
|
||||||
|
module APIOperations
|
||||||
|
module Request
|
||||||
|
module ClassMethods
|
||||||
|
@@opts_to_persist = Set.new([:api_key, :api_base, :stripe_account, :stripe_version])
|
||||||
|
|
||||||
|
def request(method, url, params={}, opts={})
|
||||||
|
opts = Util.normalize_opts(opts)
|
||||||
|
|
||||||
|
headers = opts.clone
|
||||||
|
api_key = headers.delete(:api_key)
|
||||||
|
api_base = headers.delete(:api_base)
|
||||||
|
# Assume all remaining opts must be headers
|
||||||
|
|
||||||
|
response, opts[:api_key] = Stripe.request(method, url, api_key, params, headers, api_base)
|
||||||
|
[response, opts.select {|k, _| @@opts_to_persist.include?(k)}]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend(ClassMethods)
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def request(method, url, params={}, opts={})
|
||||||
|
opts = @opts.merge(Util.normalize_opts(opts))
|
||||||
|
self.class.request(method, url, params, opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,8 +1,8 @@
|
|||||||
module Stripe
|
module Stripe
|
||||||
module APIOperations
|
module APIOperations
|
||||||
module Update
|
module Update
|
||||||
def save(opts={})
|
def save(params={})
|
||||||
values = serialize_params(self).merge(opts)
|
values = serialize_params(self).merge(params)
|
||||||
|
|
||||||
if @values[:metadata]
|
if @values[:metadata]
|
||||||
values[:metadata] = serialize_metadata
|
values[:metadata] = serialize_metadata
|
||||||
@ -11,8 +11,8 @@ module Stripe
|
|||||||
if values.length > 0
|
if values.length > 0
|
||||||
values.delete(:id)
|
values.delete(:id)
|
||||||
|
|
||||||
response, api_key = Stripe.request(:post, url, @api_key, values)
|
response, opts = request(:post, url, values)
|
||||||
refresh_from(response, api_key)
|
refresh_from(response, opts)
|
||||||
end
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
module Stripe
|
module Stripe
|
||||||
class APIResource < StripeObject
|
class APIResource < StripeObject
|
||||||
|
include Stripe::APIOperations::Request
|
||||||
|
|
||||||
def self.class_name
|
def self.class_name
|
||||||
self.name.split('::')[-1]
|
self.name.split('::')[-1]
|
||||||
end
|
end
|
||||||
@ -19,12 +21,13 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def refresh
|
def refresh
|
||||||
response, api_key = Stripe.request(:get, url, @api_key, @retrieve_options)
|
response, opts = request(:get, url)
|
||||||
refresh_from(response, api_key)
|
refresh_from(response, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.retrieve(id, api_key=nil)
|
def self.retrieve(id, opts=nil)
|
||||||
instance = self.new(id, api_key)
|
opts = Util.normalize_opts(opts)
|
||||||
|
instance = self.new(id, opts)
|
||||||
instance.refresh
|
instance.refresh
|
||||||
instance
|
instance
|
||||||
end
|
end
|
||||||
|
@ -7,9 +7,8 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def refund(params={}, opts={})
|
def refund(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, refund_url, params, opts)
|
||||||
response, api_key = Stripe.request(:post, refund_url, api_key || @api_key, params, headers)
|
refresh_from(response, opts)
|
||||||
refresh_from(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -12,7 +12,7 @@ module Stripe
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.retrieve(id, api_key=nil)
|
def self.retrieve(id, opts=nil)
|
||||||
raise NotImplementedError.new("Cards cannot be retrieved without a customer ID. Retrieve a card using customer.cards.retrieve('card_id')")
|
raise NotImplementedError.new("Cards cannot be retrieved without a customer ID. Retrieve a card using customer.cards.retrieve('card_id')")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,48 +5,40 @@ module Stripe
|
|||||||
include Stripe::APIOperations::Update
|
include Stripe::APIOperations::Update
|
||||||
|
|
||||||
def refund(params={}, opts={})
|
def refund(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, refund_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from(response, opts)
|
||||||
:post, refund_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def capture(params={}, opts={})
|
def capture(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, capture_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from(response, opts)
|
||||||
:post, capture_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_dispute(params={}, opts={})
|
def update_dispute(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, dispute_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from({ :dispute => response }, opts, true)
|
||||||
:post, dispute_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from({ :dispute => response }, api_key, true)
|
|
||||||
dispute
|
dispute
|
||||||
end
|
end
|
||||||
|
|
||||||
def close_dispute(params={}, opts={})
|
def close_dispute(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, close_dispute_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from(response, opts)
|
||||||
:post, close_dispute_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_as_fraudulent
|
def mark_as_fraudulent
|
||||||
params = {
|
params = {
|
||||||
:fraud_details => { :user_report => 'fraudulent' }
|
:fraud_details => { :user_report => 'fraudulent' }
|
||||||
}
|
}
|
||||||
response, api_key = Stripe.request(:post, url, @api_key, params)
|
response, opts = request(:post, url, params)
|
||||||
refresh_from(response, api_key)
|
refresh_from(response, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_as_safe
|
def mark_as_safe
|
||||||
params = {
|
params = {
|
||||||
:fraud_details => { :user_report => 'safe' }
|
:fraud_details => { :user_report => 'safe' }
|
||||||
}
|
}
|
||||||
response, api_key = Stripe.request(:post, url, @api_key, params)
|
response, opts = request(:post, url, params)
|
||||||
refresh_from(response, api_key)
|
refresh_from(response, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -6,58 +6,52 @@ module Stripe
|
|||||||
include Stripe::APIOperations::List
|
include Stripe::APIOperations::List
|
||||||
|
|
||||||
def add_invoice_item(params, opts={})
|
def add_invoice_item(params, opts={})
|
||||||
opts[:api_key] = @api_key
|
opts = @opts.merge(Util.normalize_opts(opts))
|
||||||
InvoiceItem.create(params.merge(:customer => id), opts)
|
InvoiceItem.create(params.merge(:customer => id), opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoices
|
def invoices
|
||||||
Invoice.all({ :customer => id }, @api_key)
|
Invoice.all({ :customer => id }, @opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoice_items
|
def invoice_items
|
||||||
InvoiceItem.all({ :customer => id }, @api_key)
|
InvoiceItem.all({ :customer => id }, @opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def upcoming_invoice
|
def upcoming_invoice
|
||||||
Invoice.upcoming({ :customer => id }, @api_key)
|
Invoice.upcoming({ :customer => id }, @opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def charges
|
def charges
|
||||||
Charge.all({ :customer => id }, @api_key)
|
Charge.all({ :customer => id }, @opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_upcoming_invoice(params={}, opts={})
|
def create_upcoming_invoice(params={}, opts={})
|
||||||
opts[:api_key] = @api_key
|
opts = @opts.merge(Util.normalize_opts(opts))
|
||||||
Invoice.create(params.merge(:customer => id), opts)
|
Invoice.create(params.merge(:customer => id), opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cancel_subscription(params={}, opts={})
|
def cancel_subscription(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:delete, subscription_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from({ :subscription => response }, opts, true)
|
||||||
:delete, subscription_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from({ :subscription => response }, api_key, true)
|
|
||||||
subscription
|
subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_subscription(params={}, opts={})
|
def update_subscription(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, subscription_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from({ :subscription => response }, opts, true)
|
||||||
:post, subscription_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from({ :subscription => response }, api_key, true)
|
|
||||||
subscription
|
subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_subscription(params={}, opts={})
|
def create_subscription(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, subscriptions_url, params, opts)
|
||||||
response, api_key = Stripe.request(
|
refresh_from({ :subscription => response }, opts, true)
|
||||||
:post, subscriptions_url, api_key || @api_key, params, headers)
|
|
||||||
refresh_from({ :subscription => response }, api_key, true)
|
|
||||||
subscription
|
subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_discount
|
def delete_discount
|
||||||
Stripe.request(:delete, discount_url, @api_key)
|
_, opts = request(:delete, discount_url)
|
||||||
refresh_from({ :discount => nil }, api_key, true)
|
refresh_from({ :discount => nil }, opts, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -1,34 +1,22 @@
|
|||||||
module Stripe
|
module Stripe
|
||||||
class FileUpload < APIResource
|
class FileUpload < APIResource
|
||||||
UPLOADS_API_BASE = "https://uploads.stripe.com"
|
|
||||||
|
|
||||||
def self.url
|
def self.url
|
||||||
"/v1/files"
|
"/v1/files"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.request_headers
|
def self.create(params={}, opts={})
|
||||||
{
|
opts = {
|
||||||
:content_type => 'multipart/form-data',
|
content_type: 'multipart/form-data',
|
||||||
}
|
api_base: Stripe::uploads_base
|
||||||
end
|
}.merge(opts)
|
||||||
|
response, opts = request(:post, url, params, opts)
|
||||||
def self.create(params={}, api_key=nil)
|
Util.convert_to_stripe_object(response, opts)
|
||||||
response, api_key = Stripe.request(
|
|
||||||
:post, self.url, api_key, params, self.request_headers, UPLOADS_API_BASE)
|
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.all(filters={}, opts={})
|
def self.all(filters={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
opts = {api_base: Stripe::uploads_base}.merge(opts)
|
||||||
response, api_key = Stripe.request(
|
response, opts = request(:get, url, filters, opts)
|
||||||
:get, self.url, api_key, filters, headers, UPLOADS_API_BASE)
|
Util.convert_to_stripe_object(response, opts)
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
def refresh
|
|
||||||
response, api_key = Stripe.request(
|
|
||||||
:get, url, @api_key, @retrieve_options, self.class.request_headers, UPLOADS_API_BASE)
|
|
||||||
refresh_from(response, api_key)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,14 +4,15 @@ module Stripe
|
|||||||
include Stripe::APIOperations::Update
|
include Stripe::APIOperations::Update
|
||||||
include Stripe::APIOperations::Create
|
include Stripe::APIOperations::Create
|
||||||
|
|
||||||
def self.upcoming(params, api_key = nil)
|
def self.upcoming(params, opts={})
|
||||||
response, api_key = Stripe.request(:get, upcoming_url, api_key, params)
|
opts = Util.normalize_opts(opts)
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
response, api_key = Stripe.request(:get, upcoming_url, opts, params)
|
||||||
|
Util.convert_to_stripe_object(response, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def pay
|
def pay
|
||||||
response, api_key = Stripe.request(:post, pay_url, @api_key)
|
response, opts = Stripe.request(:post, pay_url, @opts, {})
|
||||||
refresh_from(response, api_key)
|
refresh_from(response, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
module Stripe
|
module Stripe
|
||||||
class ListObject < StripeObject
|
class ListObject < StripeObject
|
||||||
|
include Stripe::APIOperations::Request
|
||||||
|
|
||||||
def [](k)
|
def [](k)
|
||||||
case k
|
case k
|
||||||
@ -14,24 +15,19 @@ module Stripe
|
|||||||
self.data.each(&blk)
|
self.data.each(&blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
def retrieve(id, api_key=nil)
|
def retrieve(id, opts={})
|
||||||
api_key ||= @api_key
|
response, opts = request(:get,"#{url}/#{CGI.escape(id)}", {}, opts)
|
||||||
response, api_key = Stripe.request(:get,"#{url}/#{CGI.escape(id)}", api_key)
|
Util.convert_to_stripe_object(response, opts)
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(params={}, opts={})
|
def create(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:post, url, params, opts)
|
||||||
api_key ||= @api_key
|
Util.convert_to_stripe_object(response, opts)
|
||||||
response, api_key = Stripe.request(:post, url, api_key, params, headers)
|
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def all(params={}, opts={})
|
def all(params={}, opts={})
|
||||||
api_key, headers = Util.parse_opts(opts)
|
response, opts = request(:get, url, params, opts)
|
||||||
api_key ||= @api_key
|
Util.convert_to_stripe_object(response, opts)
|
||||||
response, api_key = Stripe.request(:get, url, api_key, params, headers)
|
|
||||||
Util.convert_to_stripe_object(response, api_key)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,25 +2,24 @@ module Stripe
|
|||||||
class StripeObject
|
class StripeObject
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
attr_accessor :api_key
|
@@permanent_attributes = Set.new([:id])
|
||||||
@@permanent_attributes = Set.new([:api_key, :id])
|
|
||||||
|
|
||||||
# The default :id method is deprecated and isn't useful to us
|
# The default :id method is deprecated and isn't useful to us
|
||||||
if method_defined?(:id)
|
if method_defined?(:id)
|
||||||
undef :id
|
undef :id
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(id=nil, api_key=nil)
|
def initialize(id=nil, opts={})
|
||||||
# parameter overloading!
|
# parameter overloading!
|
||||||
if id.kind_of?(Hash)
|
if id.kind_of?(Hash)
|
||||||
@retrieve_options = id.dup
|
@retrieve_params = id.dup
|
||||||
@retrieve_options.delete(:id)
|
@retrieve_params.delete(:id)
|
||||||
id = id[:id]
|
id = id[:id]
|
||||||
else
|
else
|
||||||
@retrieve_options = {}
|
@retrieve_params = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@api_key = api_key
|
@opts = opts
|
||||||
@values = {}
|
@values = {}
|
||||||
# This really belongs in APIResource, but not putting it there allows us
|
# This really belongs in APIResource, but not putting it there allows us
|
||||||
# to have a unified inspect method
|
# to have a unified inspect method
|
||||||
@ -29,8 +28,8 @@ module Stripe
|
|||||||
@values[:id] = id if id
|
@values[:id] = id if id
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.construct_from(values, api_key=nil)
|
def self.construct_from(values, opts={})
|
||||||
self.new(values[:id], api_key).refresh_from(values, api_key)
|
self.new(values[:id]).refresh_from(values, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s(*args)
|
def to_s(*args)
|
||||||
@ -42,9 +41,8 @@ module Stripe
|
|||||||
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
|
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh_from(values, api_key, partial=false)
|
def refresh_from(values, opts, partial=false)
|
||||||
@api_key = api_key
|
@opts = opts
|
||||||
|
|
||||||
@previous_metadata = values[:metadata]
|
@previous_metadata = values[:metadata]
|
||||||
removed = partial ? Set.new : Set.new(@values.keys - values.keys)
|
removed = partial ? Set.new : Set.new(@values.keys - values.keys)
|
||||||
added = Set.new(values.keys - @values.keys)
|
added = Set.new(values.keys - @values.keys)
|
||||||
@ -62,7 +60,7 @@ module Stripe
|
|||||||
@unsaved_values.delete(k)
|
@unsaved_values.delete(k)
|
||||||
end
|
end
|
||||||
values.each do |k, v|
|
values.each do |k, v|
|
||||||
@values[k] = Util.convert_to_stripe_object(v, api_key)
|
@values[k] = Util.convert_to_stripe_object(v, @opts)
|
||||||
@transient_values.delete(k)
|
@transient_values.delete(k)
|
||||||
@unsaved_values.delete(k)
|
@unsaved_values.delete(k)
|
||||||
end
|
end
|
||||||
@ -106,12 +104,12 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def _dump(level)
|
def _dump(level)
|
||||||
Marshal.dump([@values, @api_key])
|
Marshal.dump([@values, @opts])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._load(args)
|
def self._load(args)
|
||||||
values, api_key = Marshal.load(args)
|
values, opts = Marshal.load(args)
|
||||||
construct_from(values, api_key)
|
construct_from(values, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION < '1.9.2'
|
if RUBY_VERSION < '1.9.2'
|
||||||
|
@ -7,13 +7,13 @@ module Stripe
|
|||||||
"#{Customer.url}/#{CGI.escape(customer)}/subscriptions/#{CGI.escape(id)}"
|
"#{Customer.url}/#{CGI.escape(customer)}/subscriptions/#{CGI.escape(id)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.retrieve(id, api_key=nil)
|
def self.retrieve(id, opts=nil)
|
||||||
raise NotImplementedError.new("Subscriptions cannot be retrieved without a customer ID. Retrieve a subscription using customer.subscriptions.retrieve('subscription_id')")
|
raise NotImplementedError.new("Subscriptions cannot be retrieved without a customer ID. Retrieve a subscription using customer.subscriptions.retrieve('subscription_id')")
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_discount
|
def delete_discount
|
||||||
Stripe.request(:delete, discount_url, @api_key)
|
response, opts = request(:delete, discount_url)
|
||||||
refresh_from({ :discount => nil }, api_key, true)
|
refresh_from({ :discount => nil }, opts, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -43,13 +43,13 @@ module Stripe
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.convert_to_stripe_object(resp, api_key)
|
def self.convert_to_stripe_object(resp, opts)
|
||||||
case resp
|
case resp
|
||||||
when Array
|
when Array
|
||||||
resp.map { |i| convert_to_stripe_object(i, api_key) }
|
resp.map { |i| convert_to_stripe_object(i, opts) }
|
||||||
when Hash
|
when Hash
|
||||||
# Try converting to a known object class. If none available, fall back to generic StripeObject
|
# Try converting to a known object class. If none available, fall back to generic StripeObject
|
||||||
object_classes.fetch(resp[:object], StripeObject).construct_from(resp, api_key)
|
object_classes.fetch(resp[:object], StripeObject).construct_from(resp, opts)
|
||||||
else
|
else
|
||||||
resp
|
resp
|
||||||
end
|
end
|
||||||
@ -119,23 +119,16 @@ module Stripe
|
|||||||
|
|
||||||
# The secondary opts argument can either be a string or hash
|
# The secondary opts argument can either be a string or hash
|
||||||
# Turn this value into an api_key and a set of headers
|
# Turn this value into an api_key and a set of headers
|
||||||
def self.parse_opts(opts)
|
def self.normalize_opts(opts)
|
||||||
case opts
|
case opts
|
||||||
when NilClass
|
when NilClass
|
||||||
return nil, {}
|
{}
|
||||||
when String
|
when String
|
||||||
return opts, {}
|
{api_key: opts}
|
||||||
when Hash
|
when Hash
|
||||||
headers = {}
|
opts.clone
|
||||||
if opts[:idempotency_key]
|
|
||||||
headers[:idempotency_key] = opts[:idempotency_key]
|
|
||||||
end
|
|
||||||
if opts[:stripe_account]
|
|
||||||
headers[:stripe_account] = opts[:stripe_account]
|
|
||||||
end
|
|
||||||
return opts[:api_key], headers
|
|
||||||
else
|
else
|
||||||
raise TypeError.new("parse_opts expects a string or a hash")
|
raise TypeError.new('normalize_opts expects a string or a hash')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,13 +4,28 @@ module Stripe
|
|||||||
class AccountTest < Test::Unit::TestCase
|
class AccountTest < Test::Unit::TestCase
|
||||||
should "account should be retrievable" do
|
should "account should be retrievable" do
|
||||||
resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
||||||
@mock.expects(:get).once.returns(test_response(resp))
|
@mock.expects(:get).
|
||||||
|
once.
|
||||||
|
with('https://api.stripe.com/v1/account', nil, nil).
|
||||||
|
returns(test_response(resp))
|
||||||
a = Stripe::Account.retrieve
|
a = Stripe::Account.retrieve
|
||||||
assert_equal "test+bindings@stripe.com", a.email
|
assert_equal "test+bindings@stripe.com", a.email
|
||||||
assert !a.charge_enabled
|
assert !a.charge_enabled
|
||||||
assert !a.details_submitted
|
assert !a.details_submitted
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "account should be retrievable via plural endpoint" do
|
||||||
|
resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
||||||
|
@mock.expects(:get).
|
||||||
|
once.
|
||||||
|
with('https://api.stripe.com/v1/accounts/acct_foo', nil, nil).
|
||||||
|
returns(test_response(resp))
|
||||||
|
a = Stripe::Account.retrieve('acct_foo')
|
||||||
|
assert_equal "test+bindings@stripe.com", a.email
|
||||||
|
assert !a.charge_enabled
|
||||||
|
assert !a.details_submitted
|
||||||
|
end
|
||||||
|
|
||||||
should "be able to deauthorize an account" do
|
should "be able to deauthorize an account" do
|
||||||
resp = {:id => 'acct_1234', :email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
resp = {:id => 'acct_1234', :email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
||||||
@mock.expects(:get).once.returns(test_response(resp))
|
@mock.expects(:get).once.returns(test_response(resp))
|
||||||
|
@ -294,7 +294,6 @@ module Stripe
|
|||||||
@mock.expects(:get).never
|
@mock.expects(:get).never
|
||||||
@mock.expects(:post).never
|
@mock.expects(:post).never
|
||||||
@mock.expects(:delete).with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
|
@mock.expects(:delete).with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
|
||||||
|
|
||||||
c = Stripe::Customer.construct_from(test_customer)
|
c = Stripe::Customer.construct_from(test_customer)
|
||||||
c.delete
|
c.delete
|
||||||
assert_equal true, c.deleted
|
assert_equal true, c.deleted
|
||||||
@ -318,6 +317,33 @@ module Stripe
|
|||||||
assert c[0].card.kind_of?(Stripe::StripeObject) && c[0].card.object == 'card'
|
assert c[0].card.kind_of?(Stripe::StripeObject) && c[0].card.object == 'card'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "passing in a stripe_account header should pass it through on call" do
|
||||||
|
Stripe.expects(:execute_request).with do |opts|
|
||||||
|
opts[:method] == :get &&
|
||||||
|
opts[:url] == "#{Stripe.api_base}/v1/customers/c_test_customer" &&
|
||||||
|
opts[:headers][:stripe_account] == 'acct_abc'
|
||||||
|
end.once.returns(test_response(test_customer))
|
||||||
|
c = Stripe::Customer.retrieve("c_test_customer", {:stripe_account => 'acct_abc'})
|
||||||
|
end
|
||||||
|
|
||||||
|
should "passing in a stripe_account header should pass it through on save" do
|
||||||
|
Stripe.expects(:execute_request).with do |opts|
|
||||||
|
opts[:method] == :get &&
|
||||||
|
opts[:url] == "#{Stripe.api_base}/v1/customers/c_test_customer" &&
|
||||||
|
opts[:headers][:stripe_account] == 'acct_abc'
|
||||||
|
end.once.returns(test_response(test_customer))
|
||||||
|
c = Stripe::Customer.retrieve("c_test_customer", {:stripe_account => 'acct_abc'})
|
||||||
|
|
||||||
|
Stripe.expects(:execute_request).with do |opts|
|
||||||
|
opts[:method] == :post &&
|
||||||
|
opts[:url] == "#{Stripe.api_base}/v1/customers/c_test_customer" &&
|
||||||
|
opts[:headers][:stripe_account] == 'acct_abc' &&
|
||||||
|
opts[:payload] == 'description=FOO'
|
||||||
|
end.once.returns(test_response(test_customer))
|
||||||
|
c.description = 'FOO'
|
||||||
|
c.save
|
||||||
|
end
|
||||||
|
|
||||||
context "error checking" do
|
context "error checking" do
|
||||||
|
|
||||||
should "404s should raise an InvalidRequestError" do
|
should "404s should raise an InvalidRequestError" do
|
||||||
|
@ -10,11 +10,12 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "marshal a stripe object correctly" do
|
should "marshal a stripe object correctly" do
|
||||||
obj = Stripe::StripeObject.construct_from({ :id => 1, :name => 'Stripe' }, 'apikey')
|
obj = Stripe::StripeObject.construct_from({ :id => 1, :name => 'Stripe' }, {:api_key => 'apikey'})
|
||||||
m = Marshal.load(Marshal.dump(obj))
|
m = Marshal.load(Marshal.dump(obj))
|
||||||
assert_equal 1, m.id
|
assert_equal 1, m.id
|
||||||
assert_equal 'Stripe', m.name
|
assert_equal 'Stripe', m.name
|
||||||
assert_equal 'apikey', m.api_key
|
expected_hash = {:api_key => 'apikey'}
|
||||||
|
assert_equal expected_hash, m.instance_variable_get('@opts')
|
||||||
end
|
end
|
||||||
|
|
||||||
should "recursively call to_hash on its values" do
|
should "recursively call to_hash on its values" do
|
||||||
|
@ -25,35 +25,5 @@ module Stripe
|
|||||||
symbolized = Stripe::Util.symbolize_names(start)
|
symbolized = Stripe::Util.symbolize_names(start)
|
||||||
assert_equal(finish, symbolized)
|
assert_equal(finish, symbolized)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "parse a nil opts argument" do
|
|
||||||
api_key, headers = Stripe::Util.parse_opts(nil)
|
|
||||||
assert_equal({}, headers)
|
|
||||||
assert_equal(nil, api_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "parse a string opts argument" do
|
|
||||||
api_key, headers = Stripe::Util.parse_opts('foo')
|
|
||||||
assert_equal({}, headers)
|
|
||||||
assert_equal('foo', api_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "parse a hash opts argument with just api_key" do
|
|
||||||
api_key, headers = Stripe::Util.parse_opts({:api_key => 'foo'})
|
|
||||||
assert_equal({}, headers)
|
|
||||||
assert_equal('foo', api_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "parse a hash opts argument with just idempotency_key" do
|
|
||||||
api_key, headers = Stripe::Util.parse_opts({:idempotency_key => 'foo'})
|
|
||||||
assert_equal({:idempotency_key => 'foo'}, headers)
|
|
||||||
assert_equal(nil, api_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "parse a hash opts argument both idempotency_key and api_key" do
|
|
||||||
api_key, headers = Stripe::Util.parse_opts({:api_key => 'bar', :idempotency_key => 'foo'})
|
|
||||||
assert_equal({:idempotency_key => 'foo'}, headers)
|
|
||||||
assert_equal('bar', api_key)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user