Updated stripeclient snippets in Readme.md (#1619)

This commit is contained in:
prathmesh-stripe 2025-06-23 12:14:53 -04:00 committed by GitHub
parent 3d8ec2f326
commit 8ac3b4f3c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -60,13 +60,14 @@ value:
```ruby ```ruby
require 'stripe' require 'stripe'
Stripe.api_key = 'sk_test_...'
client = Stripe::StripeClient.new("sk_test_...")
# list customers # list customers
Stripe::Customer.list() customers = client.v1.customers.list()
# retrieve single customer # retrieve single customer
Stripe::Customer.retrieve('cus_123456789') customer = client.v1.customers.retrieve('cus_123456789')
``` ```
### Per-request Configuration ### Per-request Configuration
@ -78,7 +79,9 @@ per-request key and/or account:
```ruby ```ruby
require "stripe" require "stripe"
Stripe::Customer.list( client = Stripe::StripeClient.new("sk_test_...")
client.v1.customers.list(
{}, {},
{ {
api_key: 'sk_test_...', api_key: 'sk_test_...',
@ -86,47 +89,8 @@ Stripe::Customer.list(
stripe_version: '2018-02-28', stripe_version: '2018-02-28',
} }
) )
Stripe::Customer.retrieve(
'cus_123456789',
{
api_key: 'sk_test_...',
stripe_account: 'acct_...',
stripe_version: '2018-02-28',
}
)
Stripe::Customer.retrieve(
{
id: 'cus_123456789',
expand: %w(balance_transaction)
},
{
stripe_version: '2018-02-28',
api_key: 'sk_test_...',
}
)
Stripe::Customer.capture(
'cus_123456789',
{},
{
stripe_version: '2018-02-28',
api_key: 'sk_test_...',
}
)
``` ```
Keep in mind that there are different method signatures depending on the action:
- When operating on a collection (e.g. `.list`, `.create`) the method signature is
`method(params, opts)`.
- When operating on resource (e.g. `.capture`, `.update`) the method signature is
`method(id, params, opts)`.
- One exception is that `retrieve`, despite being an operation on a resource, has the signature
`retrieve(id, opts)`. In addition, it will accept a Hash for the `id` param but will extract the
`id` key out and use the others as options.
### StripeClient vs legacy pattern ### StripeClient vs legacy pattern
We introduced the `StripeClient` class in v13 of the Ruby SDK. The legacy pattern used prior to that version is still available to use but will be marked as deprecated soon. Review the [migration guide to use StripeClient](https://github.com/stripe/stripe-ruby/wiki/Migration-guide-for-v13) to move from the legacy pattern. We introduced the `StripeClient` class in v13 of the Ruby SDK. The legacy pattern used prior to that version is still available to use but will be marked as deprecated soon. Review the [migration guide to use StripeClient](https://github.com/stripe/stripe-ruby/wiki/Migration-guide-for-v13) to move from the legacy pattern.
@ -138,7 +102,7 @@ Once the legacy pattern is deprecated, new API endpoints will only be accessible
Both indexer and accessors can be used to retrieve values of resource properties. Both indexer and accessors can be used to retrieve values of resource properties.
```ruby ```ruby
customer = Stripe::Customer.retrieve('cus_123456789') customer = client.v1.customers.retrieve('cus_123456789')
puts customer['id'] puts customer['id']
puts customer.id puts customer.id
``` ```
@ -146,7 +110,7 @@ puts customer.id
NOTE: If the resource property is not defined, the accessors will raise an exception, while the indexer will return `nil`. NOTE: If the resource property is not defined, the accessors will raise an exception, while the indexer will return `nil`.
```ruby ```ruby
customer = Stripe::Customer.retrieve('cus_123456789') customer = client.v1.customers.retrieve('cus_123456789')
puts customer['unknown'] # nil puts customer['unknown'] # nil
puts customer.unknown # raises NoMethodError puts customer.unknown # raises NoMethodError
``` ```
@ -156,7 +120,7 @@ puts customer.unknown # raises NoMethodError
Get access to response objects by using the `last_response` property of the returned resource: Get access to response objects by using the `last_response` property of the returned resource:
```ruby ```ruby
customer = Stripe::Customer.retrieve('cus_123456789') customer = client.v1.customers.retrieve('cus_123456789')
print(customer.last_response.http_status) # to retrieve status code print(customer.last_response.http_status) # to retrieve status code
print(customer.last_response.http_headers) # to retrieve headers print(customer.last_response.http_headers) # to retrieve headers
@ -388,7 +352,7 @@ If you:
you can now use the `raw_request` method on `StripeClient`. you can now use the `raw_request` method on `StripeClient`.
```ruby ```ruby
client = Stripe::StripeClient.new(...) client = Stripe::StripeClient.new('sk_test_...')
resp = client.raw_request(:post, "/v1/beta_endpoint", params: {param: 123}, opts: {stripe_version: "2022-11-15; feature_beta=v3"}) resp = client.raw_request(:post, "/v1/beta_endpoint", params: {param: 123}, opts: {stripe_version: "2022-11-15; feature_beta=v3"})
# (Optional) resp is a StripeResponse. You can use `Stripe.deserialize` to get a StripeObject. # (Optional) resp is a StripeResponse. You can use `Stripe.deserialize` to get a StripeObject.