8
10

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 5 years have passed since last update.

[nodejs][stripe] stripe を node で使う(単発の支払い)

Last updated at Posted at 2017-03-17

customer が登録されている前提で invoice を作ってみましょう。
customer の登録方法は下記 URL で説明してますので、そちらも合わせて読んでくださいまし。
stripe を node で使う(customer 情報の登録)

参考URL

使い方

流れとしては

  • invoiceItems.create() で invoice の明細行を作って
  • invoices.create() で invoice の明細行をまとめて一つの invoice にして
  • invoices.pay() で実際に請求を行う

感じになります。

invoice 明細行の作成

要はカートの中に突っ込まれた個々の商品っすね。
注意点としては currency が usd の時は amount は $1 = 100 となることっすね。{amount: 1, currency: 'usd'} は $1 ではなく ¢1 です。
jpy の時は気にしないでいいです。

create-invoice-items.js
var stripe = require('stripe')('STRIPE_API_KEY_HERE');
var params = {
    customer: 'cus_xxxxxxxxxxxxxx',
    amount: 1500,
    currency: 'jpy',
    description: "One-time setup fee"
};

stripe.invoiceItems.create(parms, function(err,invoiceItem){
    cosole.log(invoiceItem);
});
example-response
{
  "id": "ii_xxxxxxxxxxxxxxxxxxxxxxxx",
  "object": "invoiceitem",
  "amount": 1500,
  "currency": "jpy",
  "customer": "cus_xxxxxxxxxxxxxx",
  "date": 1489721334,
  "description": "One-time setup fee",
  "discountable": false,
  "invoice": null,
  "livemode": false,
  "metadata": {
  },
  "quantity": 1
}

これを明細分だけ、ひたすら追加していけばいいわけです。

invoice の作成

明細行を全部追加し終わったら、invoices.create() で明細行をまとめて invoice 作成します。
消費税とか請求するときも、ここで指定します。

create-invoice.js
var stripe = require('stripe')('STRIPE_API_KEY_HERE');
var params = {
    customer: 'cus_xxxxxxxxxxxxxx',
    tax_percent: 8.0
};

stripe.invoices.create(parms, function(err,invoice){
    cosole.log(invoice);
});
example-response
{
  "id": "in_xxxxxxxxxxxxxxxxxxxxxxxx",
  "object": "invoice",
  "amount_due": 1609,
  "application_fee": null,
  "attempt_count": 1,
  "attempted": true,
  "closed": true,
  "currency": "jpy",
  "customer": "cus_xxxxxxxxxxxxxx",
  "date": 1489749157,
  "description": null,
  "ending_balance": 0,
  "forgiven": false,
  "lines": {
    "data": [
      {
        "id": "ii_xxxxxxxxxxxxxxxxxxxxxxxx",
        "object": "line_item",
        "amount": 1500,
        "currency": "jpy",
        "description": "One-time setup fee",
        "discountable": false,
        "livemode": false,
        "metadata": {
        },
        "quantity": 1,
        "type": "invoiceitem"
      }
    ],
    "total_count": 1,
    "object": "list",
    "url": "/v1/invoices/in_xxxxxxxxxxxxxxxxxxxxxxxx/lines"
  },
  "livemode": false,
  "metadata": {
  },
  "receipt_number": null,
  "starting_balance": 0,
  "statement_descriptor": null,
  "subtotal": 1500,
  "tax": 119,
  "tax_percent": 8.0,
  "total": 1609
}

response の invoice.id (in_xxxxxxxxxxxxxxxxxxxxxxxx)は覚えておきましょう。実際に請求するときに使います。

実際の請求

作成した invoice を請求するには invoices.pay() を使います。

pay-invoice.js
var stripe = require('stripe')('STRIPE_API_KEY_HERE');
var invoice_id = "in_xxxxxxxxxxxxxxxxxxxxxxxx";

stripe.invoices.pay(invoice_id, function(err,invoice){
    cosole.log(invoice);
});

invoice 生成して即時請求するなら、これでもいいっすね。

var stripe = require('stripe')('STRIPE_API_KEY_HERE');
var params = {
    customer: 'cus_xxxxxxxxxxxxxx',
    tax_percent: 8.0
};

stripe.invoices.create(parms, function(err,invoice){
    if (!err) {
        stripe.invoices.pay(invoice.id, function(err,invoice){
            cosole.log(invoice);
        });
    }
});
8
10
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
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?