Skip to content

Commit 26a5cd2

Browse files
Updated stripeclient snippets in Readme.md
1 parent 3d8ec2f commit 26a5cd2

File tree

1 file changed

+12
-48
lines changed

1 file changed

+12
-48
lines changed

README.md

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ value:
6060

6161
```ruby
6262
require 'stripe'
63-
Stripe.api_key = 'sk_test_...'
63+
64+
client = Stripe::StripeClient.new("sk_test_...")
6465

6566
# list customers
66-
Stripe::Customer.list()
67+
customers = client.v1.customers.list()
6768

68-
# retrieve single customer
69-
Stripe::Customer.retrieve('cus_123456789')
69+
# retrieve single customer
70+
customer = client.v1.customers.retrieve('cus_123456789')
7071
```
7172

7273
### Per-request Configuration
@@ -78,55 +79,18 @@ per-request key and/or account:
7879
```ruby
7980
require "stripe"
8081

81-
Stripe::Customer.list(
82-
{},
83-
{
84-
api_key: 'sk_test_...',
85-
stripe_account: 'acct_...',
86-
stripe_version: '2018-02-28',
87-
}
88-
)
82+
client = Stripe::StripeClient.new("sk_test_...")
8983

90-
Stripe::Customer.retrieve(
91-
'cus_123456789',
84+
client.v1.customers.list(
85+
{},
9286
{
9387
api_key: 'sk_test_...',
9488
stripe_account: 'acct_...',
9589
stripe_version: '2018-02-28',
9690
}
9791
)
98-
99-
Stripe::Customer.retrieve(
100-
{
101-
id: 'cus_123456789',
102-
expand: %w(balance_transaction)
103-
},
104-
{
105-
stripe_version: '2018-02-28',
106-
api_key: 'sk_test_...',
107-
}
108-
)
109-
110-
Stripe::Customer.capture(
111-
'cus_123456789',
112-
{},
113-
{
114-
stripe_version: '2018-02-28',
115-
api_key: 'sk_test_...',
116-
}
117-
)
11892
```
11993

120-
Keep in mind that there are different method signatures depending on the action:
121-
122-
- When operating on a collection (e.g. `.list`, `.create`) the method signature is
123-
`method(params, opts)`.
124-
- When operating on resource (e.g. `.capture`, `.update`) the method signature is
125-
`method(id, params, opts)`.
126-
- One exception is that `retrieve`, despite being an operation on a resource, has the signature
127-
`retrieve(id, opts)`. In addition, it will accept a Hash for the `id` param but will extract the
128-
`id` key out and use the others as options.
129-
13094
### StripeClient vs legacy pattern
13195

13296
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,15 +102,15 @@ Once the legacy pattern is deprecated, new API endpoints will only be accessible
138102
Both indexer and accessors can be used to retrieve values of resource properties.
139103

140104
```ruby
141-
customer = Stripe::Customer.retrieve('cus_123456789')
105+
customer = client.v1.customers.retrieve('cus_123456789')
142106
puts customer['id']
143107
puts customer.id
144108
```
145109

146110
NOTE: If the resource property is not defined, the accessors will raise an exception, while the indexer will return `nil`.
147111

148112
```ruby
149-
customer = Stripe::Customer.retrieve('cus_123456789')
113+
customer = client.v1.customers.retrieve('cus_123456789')
150114
puts customer['unknown'] # nil
151115
puts customer.unknown # raises NoMethodError
152116
```
@@ -156,7 +120,7 @@ puts customer.unknown # raises NoMethodError
156120
Get access to response objects by using the `last_response` property of the returned resource:
157121

158122
```ruby
159-
customer = Stripe::Customer.retrieve('cus_123456789')
123+
customer = client.v1.customers.retrieve('cus_123456789')
160124

161125
print(customer.last_response.http_status) # to retrieve status code
162126
print(customer.last_response.http_headers) # to retrieve headers
@@ -388,7 +352,7 @@ If you:
388352
you can now use the `raw_request` method on `StripeClient`.
389353

390354
```ruby
391-
client = Stripe::StripeClient.new(...)
355+
client = Stripe::StripeClient.new('sk_test_...')
392356
resp = client.raw_request(:post, "/v1/beta_endpoint", params: {param: 123}, opts: {stripe_version: "2022-11-15; feature_beta=v3"})
393357

394358
# (Optional) resp is a StripeResponse. You can use `Stripe.deserialize` to get a StripeObject.

0 commit comments

Comments
 (0)