21
21

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 で使う(customer 情報の登録)

Posted at

オンライン決済サービスの Stripe を Lambda から使用する必要があったので、メモ的に

参考URL

使い方

サンプルコードはエラー処理入れてないので、適宜修正してくださいまし。

Customer の作成

請求先のお客さんですね。
APIキーは Stripe のダッシュボードから取得できます。
Test Secret Key or Live Secret Key を使用、APIキー変えるだけで本番とテスト環境を切り替えられるのは便利すね。

create-stripe-customer.js
var stripe = require('stripe')('STRIPE_API_KEY_HERE');
var userData = {ID: '', email: 'customer@example.com'};

// stripe customer の存在チェック
stripe.customers.retrieve(userData.id, function(err, customer) {
    if (!customer || customer.deleted) {
        // stripe customer が存在しない時は stripe にcustomerを登録
        var params = {
            email: userData.email
        };
        stripe.customers.create(params, function(err,customer){
            userData.ID = customer.id;
            console.log(customer);
        });
    }
});
example_response
{
  "id": "cus_xxxxxxxxxxxxxx",
  "object": "customer",
  "account_balance": 0,
  "created": 1480587682,
  "currency": "jpy",
  "default_source": null,
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": "customer@example.com",
  "livemode": false,
  "metadata": {
  },
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_xxxxxxxxxxxxxx/sources"
  },
  "subscriptions": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_xxxxxxxxxxxxxx/subscriptions"
  }
}

Customer が使用するクレジットカードを登録

npm パッケージの説明 には stripe.customers.createSource() にカード情報渡せばいいみたいに書いてあるけど、これではダメです。
まず、stripe.tokens.create() で token を作った後で、その token を stripe.customers.createSource() に渡します。

create-stripe-customer.source.js
var stripe = require('stripe')('STRIPE_API_KEY_HERE');
var userData = {ID: 'cus_xxxxxxxxxxxxxx', card: {ID: '', last4: ''});
var cardParams = {
    card: {
        exp_month: 10,
        exp_year: 2018,
        number: '4242424242424242',
        cvc: 100
    }
};

// カードの存在チェック
stripe.customers.retrieveCard(userData.ID, userData.card.ID, function(err, card){
    if (!card || card.deleted) {
        // カードが登録されていなければ token を作ってから、customers.createSource() で登録
        stripe.tokens.create(cardParams, function(err,token){
            userData.card.ID = token.card.id;
            userData.card.last4 = token.card.last4;

            var params = {
                source: token.id
            };
            stripe.customers.createSource(userData.ID, params, function(err, card){
                console.log(card);
            });
        });
    } else {
        // カードが登録されていたら有効期限を更新
        userData.card.ID = card.id;
        userData.card.last4 = card.last4;

        var params = {
            exp_month: cardParams.card.exp_month,
            exp_year: cardParams.card.exp_year
        }
        stripe.customers.updateCard(userData.ID, card.id, params, function(err, card){
            console.log(card);
        });
    }
});
example_response
{
  "id": "card_xxxxxxxxxxxxxxxxxxxxxxxx",
  "object": "card",
  "address_city": null,
  "address_country": null,
  "address_line1": null,
  "address_line1_check": null,
  "address_line2": null,
  "address_state": null,
  "address_zip": null,
  "address_zip_check": null,
  "brand": "Visa",
  "country": "US",
  "customer": "cus_xxxxxxxxxxxxxx",
  "cvc_check": null,
  "dynamic_last4": null,
  "exp_month": 8,
  "exp_year": 2017,
  "funding": "credit",
  "last4": "4242",
  "metadata": {
  },
  "name": null,
  "tokenization_method": null
}
21
21
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
21
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?