A REST API client for I'mport
Iamporter๋ ์์ํฌํธ์์ ์ ๊ณตํ๋ REST API๋ฅผ ์ฝ๊ฒ ํ์ฉํ๊ธฐ ์ํ์ฌ ์์ฑ๋ Node.js ํด๋ผ์ด์ธํธ ์ ๋๋ค.
- ์์ํฌํธ๋ ๊ตญ๋ด PG์ฌ์์๊ฐํธํ ์ฐ๋์ ์ ๊ณตํ๋ ์๋น์ค์ ๋๋ค.
- ์ด์ฉ ์ค ๋ฐ์ํ ๋ฌธ์ ์ ๋ํ์ฌ ์ฑ ์์ง์ง ์์ต๋๋ค.
- ์ต์ด ์์ฑ์ ์๋์ฐจ ๋ ํธ ํ๋ซํผ CARPLAT์์ ์ฌ์ฉํ๊ธฐ ์ํ์ฌ ์์ฑ๋์์ต๋๋ค.
- Written in ES6 Syntax
- Promise Support
- Exception Handling with a custom error class
IamporterError
$ npm install iamporter
- ๋ชจ๋ API ์์ฒญ์ Promise๋ฅผ ๋ฐํํฉ๋๋ค.
- API ์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์ํ๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ํ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํฉ๋๋ค.
{
"status": 200, // HTTP STATUS CODE
"message": "", // ์์ํฌํธ API ์๋ต ๋ฉ์์ง ํน์ Iamporter ์ ์ ๋ฉ์์ง
"data": {}, // ์์ํฌํธ API ์๋ต ๋ฐ์ดํฐ
ย "raw": {} // ์์ํฌํธ API RAW DATA
}
- API ์์ฒญ์ ์ฑ๊ณต์ ์ผ๋ก ์ํํ์ง ๋ชปํ ๊ฒฝ์ฐ ํญ์
IamporterError
์๋ฌ๋ฅผ ๋ฐํํฉ๋๋ค.
iamporter.paySubscription(...)
.catch((err) => {
console.log(err.status); // HTTP STATUS CODE
console.log(err.message); // ์์ํฌํธ API ์๋ต ๋ฉ์์ง ํน์ Iamporter ์ ์ ๋ฉ์์ง
console.log(err.data); // ์์ํฌํธ API ์๋ต ๋ฐ์ดํฐ
console.log(err.raw); // ์์ํฌํธ API RAW DATA
});
iamporter
ํจํค์ง๋Iamporter
์IamporterError
๋ ํด๋์ค๋ฅผ ์ ๊ณตํฉ๋๋ค.
const { Iamporter, IamporterError } = require('iamporter');
// For Testing (ํ
์คํธ์ฉ API KEY์ SECRET ๊ธฐ๋ณธ ์ค์ )
const iamporter = new Iamporter();
// For Production
const iamporter = new Iamporter({
apiKey: 'YOUR_API_KEY',
secret: 'YOUR_SECRET'
});
iamporter
๋ API ์์ฒญ ์ ์ API ํ ํฐ์ ์ ํจ์ฑ์ ํ์ธ ํ ์๋ ๋ฐ๊ธ/๊ฐฑ์ ํ๋ฏ๋ก ์ง์ ํ ํฐ API๋ฅผ ํธ์ถํ ํ์๊ฐ ์์ต๋๋ค.
// ์ธ์คํด์ค ์์ฑ ์ ์ค์ ํ API KEY์ SECRET
iamporter.getToken()
.then(...)
// ํ ํฐ ์์ฑ ์ ์ฌ์ฉ๋ API KEY์ SECRET ์ง์ ์ง์
iamporter.getToken('API_KEY', 'SECRET')
.then(...)
- ์ ๊ธฐ ๊ตฌ๋ (Subscription)ํ ์๋น์ค ๋ฑ์ ์ด์ฉํ ์ ์๋ ๋น๋งํค๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
// ๋น๋งํค ์์ฑ
iamporter.createSubscription({
'customer_uid': 'test_uid',
'card_number': '1234-1234-1234-1234',
'expiry': '2021-11',
'birth': '620201',
'pwd_2digit': '99'
}).then(result => {
console.log(result);
}).catch(err => {
if (err instanceof IamporterError)
// Handle the exception
});
// ๋น๋งํค ์กฐํ
iamporter.getSubscription('test_uid')
.then(...)
// ๋น๋งํค ์ญ์
iamporter.deleteSubscription('test_uid')
.then(...)
// ๋น์ธ์ฆ ๊ฒฐ์ (๋น๋งํค ์ด์ฉ)
iamporter.paySubscription({
'customer_uid': 'test_uid',
'merchant_uid': 'test_billing_key',
'amount': 50000
}).then(result => {
console.log(result);
}).catch(err => {
if (err instanceof IamporterError)
// Handle the exception
});
- ๋น๋งํค๋ฅผ ์์ฑํ์ง ์์๋ ์ ์ฉ์นด๋ ์ ๋ณด๋ง์ผ๋ก ๊ฐํธ ๊ฒฐ์ ๋ฅผ ํ ์ ์์ต๋๋ค.
// Onetime ๋น์ธ์ฆ ๊ฒฐ์
iamporter.payOnetime({
'merchant_uid': 'merchant_1448280088556',
'amount': 5000,
'card_number': '1234-1234-1234-1234',
'expiry': '2021-12',
'birth': '590912',
'pwd_2digit': '11'
}).then(result => {
console.log(result);
}).catch(err => {
if (err instanceof IamporterError)
// Handle the exception
});
// ํด์ธ์นด๋ ๋น์ธ์ฆ ๊ฒฐ์
iamporter.payForeign({
'merchant_uid': 'merchant_1448280088556',
'amount': 5000,
'card_number': '1234-1234-1234-1234',
'expiry': '2021-12',
}).then(result => {
console.log(result);
}).catch(err => {
if (err instanceof IamporterError)
// Handle the exception
});
- ์์ํฌํธ ๊ณ ์ ์์ด๋ ํน์ ์์ ๊ณ ์ ์์ด๋๋ก ๊ฒฐ์ ์ทจ์๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
- ๋ถ๋ถ ๊ฒฐ์ ์ทจ์ ๋ํ ์ง์๋ฉ๋๋ค.
// ์์ํฌํธ ๊ณ ์ ์์ด๋๋ก ๊ฒฐ์ ์ทจ์
iamporter.cancelByImpUid('imp_448280090638')
.then(...)
// ์์ ๊ณ ์ ์์ด๋๋ก ๊ฒฐ์ ์ทจ์
iamporter.cancelByMerchantUid('merchant_1448280088556')
.then(...)
// ์์ ๊ณ ์ ์์ด๋๋ก ๋ถ๋ถ ๊ฒฐ์ ์ทจ์
iamporter.cancelByMerchantUid('merchant_1448280088556', {
'amount': 2500,
'reason': '์์ฝ ๋ณ๊ฒฝ'
}).then(...)
// ๊ฒฐ์ ์ทจ์ ํ ๊ณ์ข ํ๋ถ
iamporter.cancel({
'imp_uid': 'imp_448280090638',
'reason': '์ ํ ์ํ ๋ถ๋',
'refund_holder': 'ํ๊ธธ๋',
'refund_bank': '03',
'refund_account': '056-076923-01-017'
).then(...)
- ์์ํฌํธ์์๋ ์์ํฌํธ ๊ณ ์ ์์ด๋(ImpUid)์ ์์ ๊ณ ์ ์์ด๋(MerchantUid)๋ก ๊ฒฐ์ ์ ๋ณด ์กฐํ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
// ์์ํฌํธ ๊ณ ์ ์์ด๋๋ก ๊ฒฐ์ ์ ๋ณด ์กฐํ
iamporter.findByImpUid('imp_448280090638')
.then(...)
// ์์ ๊ณ ์ ์์ด๋๋ก ๊ฒฐ์ ์ ๋ณด ์กฐํ
iamporter.findByMerchantUid('merchant_1448280088556')
.then(...)
// ์์ ๊ณ ์ ์์ด๋๋ก ๊ฒฐ์ ์ ๋ณด ๋ชฉ๋ก ์กฐํ
iamporter.findAllByMerchantUid('merchant_1448280088556')
.then(...)
// ๊ฒฐ์ ์ํ๋ก ๊ฒฐ์ ์ ๋ณด ๋ชฉ๋ก ์กฐํ(status: ['all', 'ready', 'paid', 'cancelled', 'failed'])
iamporter.findAllByStatus('paid')
.then(...)
- ์์ํฌํธ์์๋ ๊ฒฐ์ ๊ฑด์ ๋ํ ์ฌ์ ์ ๋ณด ๋ฑ๋ก ๋ฐ ๊ฒ์ฆ์ ํ ์ ์์ต๋๋ค.
// ๊ฒฐ์ ์์ ๊ธ์ก ์ฌ์ ๋ฑ๋ก
iamporter.createPreparedPayment({
'merchant_uid': 'merchant_1448280088556',
'amount', '128900'
}).then(...)
// ๊ฒฐ์ ์์ ๊ธ์ก ์กฐํ
iamporter.getPreparedPayment('merchant_1448280088556')
.then(...)
- ์์ํฌํธ์์๋ SMS ๋ณธ์ธ์ธ์ฆ ๊ฒฐ๊ณผ๋ฅผ ์กฐํ/์ญ์ ํ ์ ์์ต๋๋ค.
// ์์ํฌํธ ๊ณ ์ ์์ด๋๋ก SMS ๋ณธ์ธ์ธ์ฆ ๊ฒฐ๊ณผ ์กฐํ
iamporter.getCertification('imp_448280090638')
.then(...)
// ์์ํฌํธ ๊ณ ์ ์์ด๋๋ก SMS ๋ณธ์ธ์ธ์ฆ ๊ฒฐ๊ณผ ์ญ์
iamporter.deleteCertification('imp_448280090638')
.then(...)
- ์์ํฌํธ์์๋ PG ๊ฒฐ์ ํ๋ฉด ์์ด API ๋ง์ผ๋ก ๊ฐ์๊ณ์ข ๋ฐ๊ธ์ด ๊ฐ๋ฅํฉ๋๋ค.
// ๊ฐ์๊ณ์ข ๋ฐ๊ธ
iamporter.createVbank({
'merchant_uid': 'merchant_1448280088556',
'amount': '128900',
'vbank_code': '03',
'vbank_due': 1485697047,
'vbank_holder': 'PLAT Corp'
}).then(...)
If you have any questions, feel free to join me at #posquit0
on Freenode and ask away. Click here to connect.