こちらのページの続きです。
stripe API 使い方 (List all customers)
#Ruby Faraday でカスタマーの一覧を取得#
get_customers_faraday.rb
#! /usr/bin/ruby
#
# get_customers_faraday.rb
#
# Dec/19/2021
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
require "dotenv"
Dotenv.load
secret_key = ENV['SECRET_KEY']
STDERR.puts "*** 開始 ***"
#
url="https://api.stripe.com/v1/customers"
#
headers = {"Authorization": "Bearer " + secret_key}
res = Faraday.get(url, params = nil, headers = headers)
puts res.status
json_str = res.body
dict_aa=JSON.parse(json_str)
puts dict_aa["data"].length
#
dict_aa["data"].each do |unit|
puts unit["id"]
end
#
STDERR.puts "*** 終了 ***"
# ---------------------------------------------------------------------
実行結果
$ ./get_customers_faraday.rb
*** 開始 ***
200
4
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97
*** 終了 ***
#Ruby sdk でカスタマーの一覧を取得#
sdk のインストール
sudo gem install stripe
get_customers_sdk.rb
#! /usr/bin/ruby
#
# get_customers_sdk.rb
#
# Dec/19/2021
# ---------------------------------------------------------------------
require 'stripe'
require "dotenv"
Dotenv.load
Stripe.api_key = ENV['SECRET_KEY']
cc = Stripe::Customer.list({limit: 10})
puts cc["data"].length
#
cc["data"].each do |unit|
puts unit["id"]
end
# ---------------------------------------------------------------------
実行結果
$ ./get_customers_sdk.rb
4
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97