Created
December 2, 2016 01:28
-
-
Save wokamoto/1cf5f789e46728a3d3cf23b90aa3a12d to your computer and use it in GitHub Desktop.
[nodejs][stripe] stripe を node で使う(customer 情報の登録) ref: http://qiita.com/wokamoto/items/0be8471d38044ada9209
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment