0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

stripe API 使い方 (List all customers)

Last updated at Posted at 2021-12-16

API の仕様はこちら
List all customers

Ubuntu 21.10 で確認しました。
#データの準備#

CLI のインストール

wget https://github.com/stripe/stripe-cli/releases/download/v1.7.9/stripe_1.7.9_linux_amd64.deb
sudo dpkg -i stripe_1.7.9_linux_amd64.deb

インストールの確認

$ stripe --version
stripe version 1.7.9

カスタマーの作成

stripe customers create

Secret Key の取得
https://dashboard.stripe.com/test/dashboard
stripe_aa.png

.env の作成

.env
SECRET_KEY="sk_test_51K7DKjJ*****"

#stripe cli でカスタマーの一覧を取得#

stripe login を行う必要があります。

get_customers_cli.sh
#! /bin/bash
#
#	get_customers_cli.sh
#
#				Dec/19/2021
#
#
stripe customers list --limit=10 > customers.json
#
jq .data[].id customers.json

実行結果

$ ./get_customers_cli.sh 
"cus_KmrIU4hzWqYt7R"
"cus_KmoG4k1wiKxSDc"
"cus_KmnqmJhBAvPGMq"
"cus_KmnpiRFjm8he97"

#Curl でカスタマーの一覧を取得#

get_customers_curl.sh
#! /bin/bash
#
#	get_customers_curl.sh
#
#						Dec/16/2021
#
. .env
#
curl https://api.stripe.com/v1/customers \
  -u $SECRET_KEY":" > customers.json
#
jq .data[].id customers.json
#

実行結果

$ ./get_customers_curl.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2850  100  2850    0     0   4298      0 --:--:-- --:--:-- --:--:--  4298
"cus_KmrIU4hzWqYt7R"
"cus_KmoG4k1wiKxSDc"
"cus_KmnqmJhBAvPGMq"
"cus_KmnpiRFjm8he97"

#Httpie でカスタマーの一覧を取得#

get_customers_http.sh
#! /bin/bash
#
#	get_customers_http.sh
#
#				Dec/16/2021
#
. .env
#
http https://api.stripe.com/v1/customers \
  --auth $SECRET_KEY":" > customers.json
#
jq .data[].id customers.json

実行結果

$ ./get_customers_http.sh 
"cus_KmrIU4hzWqYt7R"
"cus_KmoG4k1wiKxSDc"
"cus_KmnqmJhBAvPGMq"
"cus_KmnpiRFjm8he97"

#Python requests でカスタマーの一覧を取得#

get_customers_requests.py
#! /usr/bin/python
#
#	get_customers_requests.py
#
#						Dec/16/2021
#
# --------------------------------------------------------------------
import os
import json
import requests
from requests.auth import HTTPBasicAuth
from dotenv import load_dotenv
# --------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
SECRET_KEY = os.environ.get("SECRET_KEY")
#
url="https://api.stripe.com/v1/customers"
rr = requests.get(url, auth=HTTPBasicAuth(SECRET_KEY,''))
#
print(rr)
str_json=rr.text
dict_aa = json.loads(str_json)
#
for uu in dict_aa["data"]:
	print(uu["id"])
#
# --------------------------------------------------------------------

実行結果

$ ./get_customers_requests.py 
<Response [200]>
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97

#Python sdk でカスタマーの一覧を取得#

sdk のインストール

udo pip3 install stripe
get_customers_sdk.py
#! /usr/bin/python
#
#	get_customers_sdk.py
#
#					Dec/16/2021
# --------------------------------------------------------------------
import os
import stripe
from dotenv import load_dotenv
# --------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
SECRET_KEY = os.environ.get("SECRET_KEY")

stripe.api_key = SECRET_KEY

aa=stripe.Customer.list(limit=10)
#
print(len(aa["data"]))
#
for uu in aa["data"]:
	print(uu["id"]) 
# --------------------------------------------------------------------

実行結果

$ ./get_customers_sdk.py 
4
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97

#Node.js request でカスタマーの一覧を取得#

ライブラリーのインストール

npm install --save dotenv
npm install --save request
get_customers_request.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_customers_request.js
//
//						Dec/17/2021
// ---------------------------------------------------------------
async function get_customers_proc(secret_key)
{
	var request = require('request')
	var options = {
	url: 'https://api.stripe.com/v1/customers',
	method: 'GET',
	headers: {
		Authorization: 'Bearer ' + secret_key
		}
	}
 
	request(options, function (error, response, body)
		{
 
		const dict_aa = JSON.parse (body) 

		for(var it in dict_aa["data"])
			{
			const unit_aa = dict_aa["data"][it]
			console.log(unit_aa["id"])
			}

		})
}

// ---------------------------------------------------------------
const dotenv = require('dotenv')

dotenv.config()
const secret_key = `${process.env.SECRET_KEY}`

get_customers_proc(secret_key)
// ---------------------------------------------------------------

実行結果

$ ./get_customers_request.js 
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97

#Node.js sdk でカスタマーの一覧を取得#

sdk のインストール

npm install --save stripe
get_customers_sdk.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_customers_sdk.js
//
//						Dec/17/2021
// ---------------------------------------------------------------
async function get_customers_proc(secret_key)
{
const stripe = require('stripe')(secret_key)
const customers = await stripe.customers.list({ limit: 10, })

//	console.log(customers["data"])

	for(var it in customers["data"])
		{
		const unit_aa = customers["data"][it]
		console.log(unit_aa["id"])
		}
}

// ---------------------------------------------------------------
const dotenv = require('dotenv')

dotenv.config()
const secret_key = `${process.env.SECRET_KEY}`

get_customers_proc(secret_key)
// ---------------------------------------------------------------

実行結果

$ ./get_customers_sdk.js 
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97

#関連情報#
Go: stripe API 使い方 (List all customers)
PHP: stripe API 使い方 (List all customers)
Ruby: stripe API 使い方 (List all customers)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?