# 請求書を作成して送信する
サンプルの Invoicing システムを構築します。
# 請求書を作成して送信する
Stripe Invoicing を使用して、有効で最適な Invoicing の組み込みを構築します。顧客を検索して、データベースに相当するオブジェクトから価格を取得し、請求書を作成して送信する方法をご紹介します。
1. Build the server
~~~
npm install
~~~
2. Run the server
~~~
npm start
~~~
1. Run the server
~~~
go run server.go
~~~
1. Build the server
~~~
pip3 install -r requirements.txt
~~~
2. Run the server
~~~
export FLASK_APP=server.py
python3 -m flask run --port=4242
~~~
1. Build the server
~~~
bundle install
~~~
2. Run the server
~~~
ruby server.rb -o 0.0.0.0
~~~
1. Build the server
~~~
composer install
~~~
2. Run the server
~~~
php -S 127.0.0.1:4242 --docroot=public
~~~
1. Build the server
~~~
dotnet build
~~~
2. Run the server
~~~
dotnet run
~~~
1. Build the server
~~~
mvn package
~~~
2. Run the server
~~~
java -cp target/sample-jar-with-dependencies.jar com.stripe.sample.Server
~~~
~~~
stripe listen --forward-to localhost:4242/webhook.php
~~~
~~~
stripe listen --forward-to localhost:4242/webhook
~~~
### Stripe Node ライブラリーをインストールする
パッケージをインストールし、それをコードにインポートします。また、まったくゼロから開始していて package.json ファイルが必要な場合には、コードエディターのダウンロードリンクを使用してプロジェクトファイルをダウンロードします。
#### npm
ライブラリーをインストールします。
```bash
npm install --save stripe
```
#### GitHub
または、stripe-node ライブラリーのソースコードを直接 [GitHub から](https://github.com/stripe/stripe-node)ダウンロードします。
### Stripe Ruby ライブラリーをインストールする
Stripe ruby gem をインストールし、require を指定してコードに読み込みます。または、まったくゼロから開始していて Gemfile が必要な場合には、コードエディターのリンクを使用してプロジェクトファイルをダウンロードします。
#### Terminal
gem をインストールします。
```bash
gem install stripe
```
#### Bundler
この行を Gemfile に追加します。
```bash
gem 'stripe'
```
#### GitHub
または、stripe-ruby gem のソースコードを直接 [GitHub から](https://github.com/stripe/stripe-ruby)ダウンロードします。
### Stripe Java ライブラリーをインストールする
ビルドに依存関係を追加し、ライブラリーをインポートします。まったくゼロから開始していてサンプルの pom.xml ファイル (Maven 用) が必要な場合は、コードエディターのリンクを使用してプロジェクトファイルをダウンロードします。
#### Maven
POM に以下の依存関係を追加し、{VERSION} を使用するバージョン番号に置き換えます。
```bash
\ncom.stripe\nstripe-java\n{VERSION}\n
```
#### Gradle
build.gradle ファイルに依存関係を追加し、{VERSION} を使用するバージョン番号に置き換えます。
```bash
implementation "com.stripe:stripe-java:{VERSION}"
```
#### GitHub
JAR を直接 [GitHub から](https://github.com/stripe/stripe-java/releases/latest)ダウンロードします。
### Stripe Python パッケージをインストールする
Stripe パッケージをインストールし、コードにインポートします。まったくゼロから開始していて requirements.txt が必要な場合には、コードエディターのリンクを使用してプロジェクトファイルをダウンロードします。
#### pip
pip を使用してパッケージをインストールします。
```bash
pip3 install stripe
```
#### GitHub
stripe-python ライブラリのソースコードを [GitHub から](https://github.com/stripe/stripe-python)直接ダウンロードします。
### Stripe PHP ライブラリーをインストールする
Composer を使用してライブラリーをインストールし、シークレット API キーで初期化します。まったくゼロから開始していて composer.json ファイルが必要な場合には、コードエディターのリンクを使用してファイルをダウンロードします。
#### Composer
ライブラリーをインストールします。
```bash
composer require stripe/stripe-php
```
#### GitHub
または、stripe-php ライブラリーのソースコードを直接 [GitHub から](https://github.com/stripe/stripe-php)ダウンロードします。
### サーバーを設定する
ビルドに依存関係を追加し、ライブラリーをインポートします。まったくゼロから開始していて go.mod ファイルが必要な場合には、コードエディターのリンクを使用してプロジェクトファイルをダウンロードします。
#### Go
必ず Go モジュールを使用してを初期化してください。
```bash
go get -u github.com/stripe/stripe-go/v84
```
#### GitHub
または、stripe-go モジュールのソースコードを直接 [GitHub から](https://github.com/stripe/stripe-go)ダウンロードします。
### Stripe.net ライブラリーをインストールする
.NET または NuGet でパッケージをインストールします。まったくゼロから開始する場合には、設定済みの .csproj ファイルが含まれるファイルをダウンロードします。
#### dotnet
ライブラリーをインストールします。
```bash
dotnet add package Stripe.net
```
#### NuGet
ライブラリーをインストールします。
```bash
Install-Package Stripe.net
```
#### GitHub
または、Stripe.net ライブラリーのソースコードを直接 [GitHub から](https://github.com/stripe/stripe-dotnet)ダウンロードします。
### Stripe ライブラリーをインストールする
パッケージをインストールし、コードにインポートします。まったくゼロから開始していて `package.json` が必要な場合には、コードエディターのリンクを使用してプロジェクトファイルをダウンロードします。
ライブラリーをインストールします。
```bash
npm install --save stripe @stripe/stripe-js next
```
### 商品、価格、顧客を管理する
最初に、[ダッシュボード](https://docs.stripe.com/invoicing/products-prices.md)または [API](https://docs.stripe.com/api/prices/create.md) を使用して、商品の価格を作成します。価格を作成して、商品に関連付けた後、データベースにその ID を保管します。
次に、データベースで [email (メールアドレス)](https://docs.stripe.com/api/customers/object.md?lang=dotnet#customer_object-email) を基準にして [Customer (顧客)](https://docs.stripe.com/api/customers.md) を検索します。その顧客が存在しない場合は、`Customer` を作成して、将来の購入に備えてその ID を保管します。次のステップでは、例として [Customer オブジェクト](https://docs.stripe.com/api/customers/object.md) の [id](https://docs.stripe.com/api/customers/object.md#customer_object-id) を使用して請求書を作成します。
> `Customer` オブジェクトは、お客様の商品を購入する顧客を表し、請求書の作成に必須です。
### 商品、価格、顧客設定の Account の管理
最初に、[ダッシュボード](https://docs.stripe.com/invoicing/products-prices.md)または [API](https://docs.stripe.com/api/prices/create.md) を使用して、商品の価格を作成します。価格を作成して、商品に関連付けた後、データベースにその ID を保管します。
次に、データベースで顧客をメールで検索します。その顧客が存在しない場合は、[顧客](https://docs.stripe.com/connect/account-capabilities.md?accounts-namespace=v2#customer)の [Account](https://docs.stripe.com/api/v2/core/accounts.md) を作成し、今後の購入に備えて ID を保存します。次のステップでは、たとえば、[Account オブジェクト](https://docs.stripe.com/api/v2/core/accounts/object.md)の [id](https://docs.stripe.com/api/v2/core/accounts/object.md#v2_account_object-id) を使用して請求書を作成します。
> 顧客設定の `Account` オブジェクトは、商品を購入する顧客を表し、請求書の作成に必須です。
### 空の請求書を作成する
[collection_method](https://docs.stripe.com/api/invoices/object.md#invoice_object-collection_method) 属性を `send_invoice` に設定します。Stripe が請求書を期日経過とマークできるようにするために、[days_until_due](https://docs.stripe.com/api/invoices/create.md#create_invoice-days_until_due) パラメーターを追加する必要があります。請求書を送るときに、請求書および支払い手順がメールで顧客に送信されます。
> もう 1 つ利用可能な収集方法として、`charge_automatically` があります。自動請求の場合、Stripe は顧客に関連付けられデフォルトのソースを使用して、直ちに請求書の支払いを試みます。ここでは、`send_invoice` を使用して、望ましくない即時の顧客請求が発生しないようにします。
### 請求書アイテムを作成する
顧客 `id`、商品 `price`、請求書 ID `invoice` を指定して請求書アイテムを作成します。
請求書アイテムの最大数は 250 です。
> 請求書を作成する前に請求書アイテムを作成する場合は、請求書を作成する際に [pending_invoice_items_behavior](https://docs.stripe.com/api/invoices/create.md#create_invoice-pending_invoice_items_behavior) を `include` に設定して、すべての保留中の請求書アイテムが自動的に請求書に追加されるようにします。この場合、誤った顧客に請求書アイテムを追加しないように、請求書アイテムを追加するのは、一度に 1 名の顧客のみとしてください。
>
> 請求書を作成すると、最大 250 の保留中の請求書アイテムが追加され、残りは次の請求書に追加されます。顧客の保留中の請求書アイテムを確認するには、**顧客詳細ページ**をご覧になるか、API を使用して請求書アイテムのすべてを一覧表示する際に [pending](https://docs.stripe.com/api/invoiceitems/list.md#list_invoiceitems-pending) 属性を `true` に設定してください。
顧客に関連付けられているメールアドレスに請求書を送信します。請求書が送信されるとすぐに、Stripe は請求書を確定します。多くの管轄区域では、確定済みの請求書は法的文書と見なされ、一部のフィールドは変更不可能になります。支払い済みの請求書を送信する場合、メールに支払いの参照情報は記載されません。
確定された請求書では、[PDF](https://docs.stripe.com/api/invoices/object.md#invoice_object-invoice_pdf) をダウンロードして送るか、関連付けられた[オンライン請求書ページ](https://docs.stripe.com/invoicing/hosted-invoice-page.md)の[リンクを作成](https://docs.stripe.com/api/invoices/object.md#invoice_object-hosted_invoice_url)できます。
## これで完了です。
これで、最初の請求書を作成して、送信しました。さらに連携を進め、API を使用して税金の徴収を素早く自動化する方法をご確認ください。
### 税金の徴収を自動化する
Stripe 取引の適切な税額を計算して徴収します。[Stripe Tax](https://docs.stripe.com/tax.md) と、導入前にダッシュボードで[有効にする](https://dashboard.stripe.com/tax)方法の詳細をご確認ください。
### 税金の自動計算パラメーターを追加する
`automatic_tax` パラメーターを `enabled: true` に設定します。
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
const stripe = require('stripe')('<>');
// You probably have a database to keep track of preexisting customers.
// But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
const CUSTOMERS = [{stripeId: "cus_123456789", email: "jenny.rosen@example.com"}];
// Prices on Stripe model the pricing scheme of your business.
// Create Prices in the Dashboard or with the API before accepting payments
// and store the IDs in your database.
const PRICES = {basic: "price_123456789", professional: "price_987654321"};
const sendInvoice = async function (email) {
// Look up a customer in your database
let customer = CUSTOMERS.find(c => c.email === email);
let customerId;
if (!customer) {
// Create a new Customer
customer = await stripe.customers.create({
email,
description: 'Customer to invoice',
});
// Store the Customer ID in your database to use for future purchases
CUSTOMERS.push({stripeId: customer.id, email: email});
customerId = customer.id;
} else {
// Read the Customer ID from your database
customerId = customer.stripeId;
}
// You probably have a database to keep track of preexisting customers.
// But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
const CUSTOMERS = [{stripeId: "acct_123456789", email: "jenny.rosen@example.com"}];
// Prices on Stripe model the pricing scheme of your business.
// Create Prices in the Dashboard or with the API before accepting payments
// and store the IDs in your database.
const PRICES = {basic: "price_123456789", professional: "price_987654321"};
const sendInvoice = async function (email) {
// Look up a customer in your database
let customer = CUSTOMERS.find(c => c.email === email);
let customerId;
if (!customer) {
// Create a new customer-configured Account
customer = await stripe.v2.core.accounts.create({
contact_email: email,
display_name: 'Customer to invoice',
configuration: {
customer: {
capabilities: {
automatic_indirect_tax: {requested: true}
},
},
},
});
// Store the Customer ID in your database to use for future purchases
CUSTOMERS.push({stripeId: customer.id, email: email});
customerId = customer.id;
} else {
// Read the Customer ID from your database
customerId = customer.stripeId;
}
// Create an Invoice
const invoice = await stripe.invoices.create({
customer: customerId,
collection_method: 'send_invoice',
days_until_due: 30,
automatic_tax: {enabled: true},
});
// Create an Invoice
const invoice = await stripe.invoices.create({
customer_account: customerId,
collection_method: 'send_invoice',
days_until_due: 30,
automatic_tax: {enabled: true},
});
// Create an Invoice Item with the Price, and Customer you want to charge
const invoiceItem = await stripe.invoiceItems.create({
customer: customerId,
pricing: {
price: PRICES.basic,
},
invoice: invoice.id
});
// Create an Invoice Item with the Price and the customer-configured Account you want to charge
const invoiceItem = await stripe.invoiceItems.create({
customer_account: customerId,
pricing: {
price: PRICES.basic,
},
invoice: invoice.id
});
// Send the Invoice
await stripe.invoices.sendInvoice(invoice.id);
};
{
"name": "stripe-sample",
"version": "1.0.0",
"description": "A sample Stripe implementation",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "stripe-samples",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"stripe": "^20.4.0"
}
}
{
"name": "stripe-sample",
"version": "0.1.0",
"dependencies": {
"@stripe/react-stripe-js": "^3.7.0",
"@stripe/stripe-js": "^7.3.0",
"express": "^4.17.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "^3.4.0",
"stripe": "20.4.0"
},
"devDependencies": {
"concurrently": "4.1.2"
},
"homepage": "http://localhost:3000/checkout",
"proxy": "http://localhost:4242",
"scripts": {
"start-client": "react-scripts start",
"start-server": "node server.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"start": "concurrently \"yarn start-client\" \"yarn start-server\""
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
const stripe = require('stripe')('<>');
\# This is a public sample test API key.
# Don’t submit any personally identifiable information in requests made with this key.
# Sign in to see your own test API key embedded in code samples.
Stripe.api_key = '<>'
\# This is a public sample test API key.
# Don’t submit any personally identifiable information in requests made with this key.
# Sign in to see your own test API key embedded in code samples.
$CLIENT = Stripe::StripeClient.new('<>')
\# You probably have a database to keep track of preexisting customers.
# But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
$CUSTOMERS = [{ stripeId: 'cus_123456789', email: 'jenny.rosen@example.com' }]
# Prices on Stripe model the pricing scheme of your business.
# Create Prices in the Dashboard or with the API before accepting payments
# and store the IDs in your database.
$PRICES = { basic: 'price_123456', professional: 'price_456789' }
def send_invoice(email)
# Look up a customer in your database
customer = $CUSTOMERS.find { |customer_obj| customer_obj[:email] == email }
customer_id = nil
if !customer
# Create a new Customer
customer = Stripe::Customer.create({
email: email,
description: 'Customer to invoice'
})
# Store the Customer ID in your database to use for future purchases
$CUSTOMERS << { stripeId: customer.id, email: email }
customer_id = customer.id
else
# Read the Customer ID from your database
customer_id = customer[:stripeId]
end
\# You probably have a database to keep track of preexisting customers.
# But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
$CUSTOMERS = [{ stripeId: 'acct_123456789', email: 'jenny.rosen@example.com' }]
# Prices on Stripe model the pricing scheme of your business.
# Create Prices in the Dashboard or with the API before accepting payments
# and store the IDs in your database.
$PRICES = { basic: 'price_123456', professional: 'price_456789' }
def send_invoice(email)
# Look up a customer in your database
customer = $CUSTOMERS.find { |customer_obj| customer_obj[:email] == email }
customer_id = nil
if !customer
# Create a new customer-configured Account
customer = $CLIENT.v2.core.accounts.create({
contact_email: email,
display_name: 'Customer to invoice',
configuration: {
customer: {
capabilities: {automatic_indirect_tax: {requested: true}}
}
}
})
\# Store the Customer ID in your database to use for future purchases
$CUSTOMERS << { stripeId: customer.id, email: email }
customer_id = customer.id
else
# Read the Customer ID from your database
customer_id = customer[:stripeId]
end
\# Create an Invoice
invoice = Stripe::Invoice.create({
customer: customer_id,
collection_method: 'send_invoice',
days_until_due: 30,
automatic_tax: {
enabled: true
},
})
\# Create an Invoice
invoice = $CLIENT.v1.invoices.create({
customer_account: customer_id,
collection_method: 'send_invoice',
days_until_due: 30,
automatic_tax: {
enabled: true
},
})
\# Create an Invoice Item
invoice_item = Stripe::InvoiceItem.create({
customer: customer_id,
pricing: {price: $PRICES[:basic]},
invoice: invoice.id
})
\# Create an Invoice Item
invoice_item = $CLIENT.v1.invoice_items.create({
customer_account: customer_id,
pricing: {price: $PRICES[:basic]},
invoice: invoice.id
})
\# Send the Invoice
Stripe::Invoice.send_invoice(invoice[:id])
\# Send the Invoice
$CLIENT.v1.invoices.send_invoice(invoice[:id])
Stripe.api_key = '<>'
import stripe
\# This is a public sample test API key.
# Don’t submit any personally identifiable information in requests made with this key.
# Sign in to see your own test API key embedded in code samples.
stripe.api_key = '<>'
import stripe
\# This is a public sample test API key.
# Don’t submit any personally identifiable information in requests made with this key.
# Sign in to see your own test API key embedded in code samples.
client = stripe.StripeClient('<>')
\# You probably have a database to keep track of preexisting customers
# But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
CUSTOMERS = [{"stripe_id": "cus_123456789", "email": "jenny.rosen@example.com"}]
# Prices in Stripe model the pricing scheme of your business.
# Create Prices in the Dashboard or with the API before accepting payments
# and store the IDs in your database.
PRICES = {"basic": "price_123456789", "professional": "price_987654321"}
def send_invoice(email):
# Look up a customer in your database
customers = [c for c in CUSTOMERS if c["email"] == email]
if customers:
customer_id=customers[0]["stripe_id"]
else:
# Create a new Customer
customer = stripe.Customer.create(
email=email, # Use your email address for testing purposes
description="Customer to invoice",
)
# Store the customer ID in your database for future purchases
CUSTOMERS.append({"stripe_id": customer.id, "email": email})
# Read the Customer ID from your database
customer_id = customer.id
\# You probably have a database to keep track of preexisting customers
# But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
CUSTOMERS = [{"stripe_id": "acct_123456789", "email": "jenny.rosen@example.com"}]
# Prices in Stripe model the pricing scheme of your business.
# Create Prices in the Dashboard or with the API before accepting payments
# and store the IDs in your database.
PRICES = {"basic": "price_123456789", "professional": "price_987654321"}
def send_invoice(email):
# Look up a customer in your database
customers = [c for c in CUSTOMERS if c["email"] == email]
if customers:
customer_id=customers[0]["stripe_id"]
else:
# Create a new customer-configured Account
customer = client.v2.core.accounts.create({
"contact_email": email, # Use your email address for testing purposes
"display_name": "Customer to invoice",
"configuration": {
"customer": {
"capabilities": {"automatic_indirect_tax": {"requested": True}}
},
},
})
\# Store the customer ID in your database for future purchases
CUSTOMERS.append({"stripe_id": customer.id, "email": email})
# Read the Customer ID from your database
customer_id = customer.id
\# Create an Invoice
invoice = stripe.Invoice.create(
customer=customer_id,
collection_method='send_invoice',
days_until_due=30,
\# Create an Invoice
invoice = client.v1.invoices.create({
"customer_account": customer_id,
"collection_method": 'send_invoice',
"days_until_due": 30,
"automatic_tax": { 'enabled': True },
\# Create an Invoice Item with the Price and Customer you want to charge
stripe.InvoiceItem.create(
customer=customer_id,
pricing={"price": PRICES["basic"]},
invoice=invoice.id
)
\# Create an Invoice Item with the Price and the customer-configured Account you want to charge
client.v1.invoice_items.create({
"customer_account": customer_id,
"pricing": {"price": PRICES["basic"]},
"invoice": invoice.id
})
\# Send the Invoice
stripe.Invoice.send_invoice(invoice.id)
return
\# Send the Invoice
client.v1.invoices.send_invoice(invoice.id)
return
certifi==2026.1.4
chardet==5.2.0
click==8.3.1
Flask==3.1.2
idna==3.11
itsdangerous==2.2.0
Jinja2==3.1.6
MarkupSafe==3.0.3
requests==2.32.5
stripe==14.4.0
toml==0.10.2
Werkzeug==3.1.5
stripe.api_key = '<>'
\Stripe\Stripe::setApiKey($stripeSecretKey);
$CLIENT = new \Stripe\StripeClient($stripeSecretKey);
// You probably have a database to keep track of preexisting customers.
// But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
$CUSTOMERS = [
[
'stripeId' => 'cus_123456789',
'email' => 'jenny.rosen@example.com'
],
];
// Prices on Stripe model the pricing scheme of your business.
// Create Prices in the Dashboard or with the API before accepting payments
// and store the IDs in your database.
$PRICES = [
'basic' => 'price_123456789',
'professional' => 'price_987654321',
];
function sendInvoice($email) {
// Look up a customer in your database
global $CUSTOMERS;
global $PRICES;
$customerId = null;
$customers = array_filter($CUSTOMERS, function ($customer) use ($email) {
return $customer['email'] === $email;
});
if (!$customers) {
// Create a new Customer
$customer = \Stripe\Customer::create([
'email' => $email,
'description' => 'Customer to invoice',
]);
// Store the Customer ID in your database to use for future purchases
$CUSTOMERS[] = [
'stripeId' => $customer->id,
'email' => $email
];
$customerId = $customer->id;
}
else {
// Read the Customer ID from your database
$customerId = $customers[0]['stripeId'];
}
// You probably have a database to keep track of preexisting customers.
// But to keep things simple, we'll use an Object to store Stripe object IDs in this example.
$CUSTOMERS = [
[
'stripeId' => 'acct_123456789',
'email' => 'jenny.rosen@example.com'
],
];
// Prices on Stripe model the pricing scheme of your business.
// Create Prices in the Dashboard or with the API before accepting payments
// and store the IDs in your database.
$PRICES = [
'basic' => 'price_123456789',
'professional' => 'price_987654321',
];
function sendInvoice($email) {
// Look up a customer in your database
global $CUSTOMERS;
global $PRICES;
global $CLIENT;
$customerId = null;
$customers = array_filter($CUSTOMERS, function ($customer) use ($email) {
return $customer['email'] === $email;
});
if (!$customers) {
// Create a new customer-configured Account
$customer = $CLIENT->v2->core->accounts->create([
'contact_email' => $email,
'display_name' => 'Customer to invoice',
'configuration' => [
'customer' => [
'capabilities' => [
'automatic_indirect_tax' => [
'requested' => true,
],
],
],
]
]);
// Store the Customer ID in your database to use for future purchases
$CUSTOMERS[] = [
'stripeId' => $customer->id,
'email' => $email
];
$customerId = $customer->id;
}
else {
// Read the Customer ID from your database
$customerId = $customers[0]['stripeId'];
}
// Create an Invoice
$invoice = \Stripe\Invoice::create([
'customer' => $customerId,
'collection_method' => 'send_invoice',
'days_until_due' => 30,
'automatic_tax' => [
'enabled' => true,
],
]);
// Create an Invoice
$invoice = $CLIENT->v1->invoices->create([
'customer_account' => $customerId,
'collection_method' => 'send_invoice',
'days_until_due' => 30,
'automatic_tax' => [
'enabled' => true,
],
]);
// Create an Invoice Item with the Price, and Customer you want to charge
$invoiceItem = \Stripe\InvoiceItem::create([
'customer' => $customerId,
'pricing' => ['price' => $PRICES['basic']],
'invoice' => $invoice->id
]);
// Create an Invoice Item with the Price and the customer-configured Account you want to charge
$invoiceItem = $CLIENT->v1->invoiceItems->create([
'customer_account' => $customerId,
'pricing' => ['price' => $PRICES['basic']],
'invoice' => $invoice->id
]);
// Send the Invoice
$invoice->sendInvoice();
}
// Send the Invoice
$CLIENT->v1->invoices->sendInvoice();
}
$stripeSecretKey = '<>';
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
private static StripeClient Client { get; set; } = new StripeClient("<>");
public static void Main(string[] args)
{
// Add a Customer
Customers.Add(new MyCustomer()
{
StripeId = "acct_123456789",
Email = "jenny.rosen@example.com"
});
public static void sendInvoice(String email)
{
// Look up a customer in your database
var customer = Customers.Find(customer => customer.Email == email);
String customerId;
if(customer == null) {
// Create a new Customer
var customerOptions = new CustomerCreateOptions
{
Email = email,
Description = "Customer to invoice",
};
var stripeCustomer = Client.V1.Customers.Create(customerOptions);
customerId = stripeCustomer.Id;
// Store the Customer ID in your database to use for future purchases
Customers.Add(new MyCustomer()
{
StripeId = stripeCustomer.Id,
Email = stripeCustomer.Email,
});
} else {
// Read the Customer ID from your database
customerId = customer.StripeId;
}
public static void sendInvoice(String email)
{
// Look up a customer in your database
var customer = Customers.Find(customer => customer.Email == email);
String customerId;
if(customer == null) {
// Create a new customer-configured Account
var customerOptions = new Stripe.V2.Core.AccountCreateOptions
{
ContactEmail = email,
DisplayName = "Customer to invoice",
Configuration = new Stripe.V2.Core.AccountCreateConfigurationOptions
{
Customer = new Stripe.V2.Core.AccountCreateConfigurationCustomerOptions
{
Capabilities = new Stripe.V2.Core.AccountCreateConfigurationCustomerCapabilitiesOptions
{
AutomaticIndirectTax = new Stripe.V2.Core.AccountCreateConfigurationCustomerCapabilitiesAutomaticIndirectTaxOptions
{
Requested = true,
},
},
}
},
};
var stripeCustomer = Client.V2.Core.Accounts.Create(customerOptions);
customerId = stripeCustomer.Id;
// Store the Customer ID in your database to use for future purchases
Customers.Add(new MyCustomer()
{
StripeId = stripeCustomer.Id,
Email = stripeCustomer.CustomerEmail,
});
} else {
// Read the Customer ID from your database
customerId = customer.StripeId;
}
// Create an Invoice
var invoiceOptions = new InvoiceCreateOptions
{
Customer = customerId,
CollectionMethod = "send_invoice",
DaysUntilDue = 30,
AutomaticTax = new InvoiceAutomaticTaxOptions
{
Enabled = true,
},
};
var invoice = Client.V1.Invoices.Create(invoiceOptions);
// Create an Invoice
var invoiceOptions = new InvoiceCreateOptions
{
CustomerAccount = customerId,
CollectionMethod = "send_invoice",
DaysUntilDue = 30,
AutomaticTax = new InvoiceAutomaticTaxOptions
{
Enabled = true,
},
};
var invoice = Client.V1.Invoices.Create(invoiceOptions);
// Create an Invoice Item with the Price, and Customer you want to charge
var invoiceItemOptions = new InvoiceItemCreateOptions
{
Customer = customerId,
Pricing = new InvoiceItemPricingOptions
{
Price = Prices["basic"],
},
Invoice = invoice.Id
};
Client.V1.InvoiceItems.Create(invoiceItemOptions);
// Create an Invoice Item with the Price and the customer-configured Account you want to charge
var invoiceItemOptions = new InvoiceItemCreateOptions
{
CustomerAccount = customerId,
Pricing = new InvoiceItemPricingOptions
{
Price = Prices["basic"],
},
Invoice = invoice.Id
};
Client.V1.InvoiceItems.Create(invoiceItemOptions);
// Send the Invoice
Client.V1.Invoices.SendInvoice(invoice.Id);
}
}
}
services.AddSingleton(new StripeClient("<>"));
"github.com/stripe/stripe-go/v84"
"github.com/stripe/stripe-go/v84/customer"
"github.com/stripe/stripe-go/v84/invoice"
"github.com/stripe/stripe-go/v84/invoiceitem"
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
stripe.Key = "<>"
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
client := stripe.NewClient("<>")
// You probably have a database to keep track of preexisting customers.
// But to keep things simple, we'll hardcode a list of fake customers.
customers := []MyCustomer{
{
StripeId: "cus_123456789",
Email: "jenny.rosen@example.com",
},
}
// Prices on Stripe model the pricing scheme of your business.
// Create Prices in the Dashboard or with the API before accepting payments
// and store the IDs in your database.
prices := []MyPrice{
{
Tier: "basic",
StripeId: "price_123456789",
},
{
Tier: "professional",
StripeId: "price_987654321",
},
}
customerID := ""
// Look up a customer in your database
for _, x := range customers {
if x.Email == email {
customerID = x.StripeId
break
}
}
if customerID == "" {
params := &stripe.CustomerParams{
Email: stripe.String("jenny.rosen@example.com"),
Description: stripe.String("Customer to invoice"),
}
cus, _ := customer.New(params)
newCus := MyCustomer{
Email: email,
StripeId: cus.ID,
}
customers = append(customers, newCus)
customerID = cus.ID
}
// Look up the Price ID from your database
var priceID string
for _, y := range prices {
if y.Tier == "basic" {
priceID = y.StripeId
break
}
}
// You probably have a database to keep track of preexisting customers.
// But to keep things simple, we'll hardcode a list of fake customers.
customers := []MyCustomer{
{
StripeId: "acct_123456789",
Email: "jenny.rosen@example.com",
},
}
// Prices on Stripe model the pricing scheme of your business.
// Create Prices in the Dashboard or with the API before accepting payments
// and store the IDs in your database.
prices := []MyPrice{
{
Tier: "basic",
StripeId: "price_123456789",
},
{
Tier: "professional",
StripeId: "price_987654321",
},
}
customerID := ""
// Look up a customer in your database
for _, x := range customers {
if x.Email == email {
customerID = x.StripeId
break
}
}
if customerID == "" {
params := &stripe.V2CoreAccountCreateParams{
ContactEmail: stripe.String("jenny.rosen@example.com"),
DisplayName: stripe.String("Customer to invoice"),
Configuration: &stripe.V2CoreAccountCreateConfigurationParams{
Customer: &stripe.V2CoreAccountCreateConfigurationCustomerParams{
Capabilities: &stripe.V2CoreAccountCreateConfigurationCustomerCapabilitiesParams{
AutomaticIndirectTax: &stripe.V2CoreAccountCreateConfigurationCustomerCapabilitiesAutomaticIndirectTaxParams{
Requested: stripe.Bool(true),
},
},
},
},
}
cus, _ := client.V2CoreAccounts.Create(context.TODO(), params)
newCus := MyCustomer{
Email: email,
StripeId: cus.ID,
}
customers = append(customers, newCus)
customerID = cus.ID
}
// Look up the Price ID from your database
var priceID string
for _, y := range prices {
if y.Tier == "basic" {
priceID = y.StripeId
break
}
}
// Create an Invoice
inParams := &stripe.InvoiceParams{
Customer: stripe.String(customerID),
CollectionMethod: stripe.String("send_invoice"),
DaysUntilDue: stripe.Int64(30),
AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
}
in, _ := invoice.New(inParams)
// Create an Invoice
inParams := &stripe.InvoiceCreateParams{
CustomerAccount: stripe.String(customerID),
CollectionMethod: stripe.String("send_invoice"),
DaysUntilDue: stripe.Int64(30),
AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
}
in, _ := client.V1Invoices.Create(context.TODO(), inParams)
// Create an Invoice Item with the Price, and Customer you want to charge
iiParams := &stripe.InvoiceItemParams{
Customer: stripe.String(customerID),
Pricing: &stripe.InvoiceItemPricingParams{
Price: sstripe.String(priceID),
},
Invoice: stripe.String(in.ID)
}
invoiceitem.New(iiParams)
// Create an Invoice Item with the Price and the customer-configured Account you want to charge
iiParams := &stripe.InvoiceItemCreateParams{
CustomerAccount: stripe.String(customerID),
Pricing: &stripe.InvoiceItemCreatePricingParams{
Price: sstripe.String(priceID),
},
Invoice: stripe.String(in.ID)
}
client.V1InvoiceItems.Create(context.TODO(), iiParams)
// Send the Invoice
params := &stripe.InvoiceSendInvoiceParams{}
invoice.SendInvoice(in.ID, params)
// Send the Invoice
params := &stripe.InvoiceSendInvoiceParams{}
client.V1Invoices.SendInvoice(context.TODO(), in.ID, params)
require github.com/stripe/stripe-go/v84 v84.4.0
"github.com/stripe/stripe-go/v84"
"github.com/stripe/stripe-go/v84/webhook"
stripe.Key = "<>"
import com.stripe.Stripe;
import com.stripe.model.Customer;
import com.stripe.param.CustomerCreateParams;
import com.stripe.StripeClient;
import com.stripe.model.v2.core.Account;
import com.stripe.param.v2.core.AccountCreateParams;
public static void main(String[] args) {
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
Stripe.apiKey = "<>";
// Simulate data in the DB
customers.add(new MyCustomer("cus_123456789", "jenny.rosen@example.com"));
prices.add(new MyPrice("price_123456", "basic"));
prices.add(new MyPrice("price_789123", "professional"));
}
// This is a public sample test API key.
// Don’t submit any personally identifiable information in requests made with this key.
// Sign in to see your own test API key embedded in code samples.
public static StripeClient client = new StripeClient("<>");
public static void main(String[] args) {
// Simulate data in the DB
customers.add(new MyCustomer("acct_123456789", "jenny.rosen@example.com"));
prices.add(new MyPrice("price_123456", "basic"));
prices.add(new MyPrice("price_789123", "professional"));
}
static void sendInvoice(String email) {
Customer stripeCustomer;
// Look up a customer in your database
String customerId = null;
for (MyCustomer c : customers) {
if (c.getEmail().equals(email)) {
customerId = c.getStripeId();
}
}
// Create a new Customer
if(customerId == null) {
CustomerCreateParams params =
CustomerCreateParams
.builder()
.setEmail(email)
.setDescription("Customer to invoice")
.build();
try {
// Store the Customer ID in your database to use for future purchases
stripeCustomer = Customer.create(params);
customers.add(new MyCustomer(stripeCustomer.getId(), email));
customerId = stripeCustomer.getId();
} catch(StripeException e) {
System.out.println(e.getMessage());
}
}
static void sendInvoice(String email) {
Account stripeCustomer;
// Look up a customer in your database
String customerId = null;
for (MyCustomer c : customers) {
if (c.getEmail().equals(email)) {
customerId = c.getStripeId();
}
}
// Create a new customer-configured Account
if(customerId == null) {
AccountCreateParams params =
AccountCreateParams
.builder()
.setContactEmail(email)
.setDisplayName("Customer to invoice")
.setConfiguration(
AccountCreateParams.Configuration.builder()
.setCustomer(
AccountCreateParams.Configuration.Customer.builder()
.setCapabilities(
AccountCreateParams.Configuration.Customer.Capabilities.builder()
.setAutomaticIndirectTax(
AccountCreateParams.Configuration.Customer.Capabilities.AutomaticIndirectTax.builder()
.setRequested(true)
.build()
)
.build()
)
.build()
)
.build()
)
.build();
try {
// Store the Customer ID in your database to use for future purchases
stripeCustomer = client.v2().core().accounts().create(params);
customers.add(new MyCustomer(stripeCustomer.getId(), email));
customerId = stripeCustomer.getId();
} catch(StripeException e) {
System.out.println(e.getMessage());
}
}
try {
// Create an Invoice
InvoiceCreateParams invoiceParams =
InvoiceCreateParams
.builder()
.setCustomer(customerId)
.setCollectionMethod(InvoiceCreateParams.CollectionMethod.SEND_INVOICE)
.setDaysUntilDue(30L)
.setAutomaticTax(
InvoiceCreateParams.AutomaticTax.builder().setEnabled(true).build()
)
.build();
Invoice invoice = Invoice.create(invoiceParams);
try {
// Create an Invoice
InvoiceCreateParams invoiceParams =
InvoiceCreateParams
.builder()
.setCustomerAccount(customerId)
.setCollectionMethod(InvoiceCreateParams.CollectionMethod.SEND_INVOICE)
.setDaysUntilDue(30L)
.setAutomaticTax(
InvoiceCreateParams.AutomaticTax.builder().setEnabled(true).build()
)
.build();
Invoice invoice = client.v1().invoices().create(invoiceParams);
// Look up the Price ID from your database
String priceId = null;
for (MyPrice p : prices) {
if (p.getTier().equals("basic")) {
priceId = p.getStripeId();
}
}
// Create an Invoice Item with the Price and Customer you want to charge
InvoiceItemCreateParams invoiceItemParams =
InvoiceItemCreateParams.builder()
.setCustomer(customerId)
.setPricing(
InvoiceItemCreateParams.Pricing.builder()
.setPrice(priceId)
.build()
)
.setInvoice(invoice.getId())
.build();
try {
InvoiceItem.create(invoiceItemParams);
} catch(StripeException e) {
System.out.println(e.getMessage());
}
// Look up the Price ID from your database
String priceId = null;
for (MyPrice p : prices) {
if (p.getTier().equals("basic")) {
priceId = p.getStripeId();
}
}
// Create an Invoice Item with the Price and the customer-configured Account you want to charge
InvoiceItemCreateParams invoiceItemParams =
InvoiceItemCreateParams.builder()
.setCustomerAccount(customerId)
.setPricing(
InvoiceItemCreateParams.Pricing.builder()
.setPrice(priceId)
.build()
)
.setInvoice(invoice.getId())
.build();
try {
client.v1().invoiceItems().create(invoiceItemParams);
} catch(StripeException e) {
System.out.println(e.getMessage());
}
// Send an Invoice
InvoiceSendInvoiceParams params = InvoiceSendInvoiceParams.builder().build();
invoice.sendInvoice(params);
// Send an Invoice
InvoiceSendInvoiceParams params = InvoiceSendInvoiceParams.builder().build();
client.v1().invoices().sendInvoice(params);
Stripe.apiKey = "<>";
## 次のステップ
#### [着信する Webhook を使用してリアルタイムで更新を取得する](https://docs.stripe.com/webhooks.md)
Stripe アカウントでイベントをリッスンし、組み込みで自動的にリアクションをトリガーできるようにします。
#### [請求書をカスタマイズする](https://docs.stripe.com/invoicing/customize.md)
[請求書テンプレート](https://dashboard.stripe.com/account/billing/invoice)を使用して、請求書の内容をカスタマイズできます。また、[アカウント詳細](https://dashboard.stripe.com/settings/account/?support_details=true)で、顧客の優先言語を設定したり、公開情報を設定することもできます。
#### [Invoicing API](https://docs.stripe.com/api/invoices.md)
Invoicing API の詳細をご覧ください。
#### [Stripe CLI](https://docs.stripe.com/stripe-cli.md)
Stripe CLI には、Invoicing 以外の Stripe アプリケーションのテストに役立つコマンドがいくつか用意されています。