Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SoleeChoi committed Oct 17, 2019
0 parents commit b8d4623
Show file tree
Hide file tree
Showing 46 changed files with 7,201 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
/dist
/test
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
/dist
/test
tsconfig.json
tslint.json
jestconfig.json
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# iamport-rest-client-nodejs
[ ![alt text](https://img.shields.io/badge/axios-v0.19.0-orange.svg?longCache=true&style=flat-square) ](https://github.com/axios/axios)
[ ![alt text](https://img.shields.io/badge/lodash-v4.17.15-yellow.svg?longCache=true&style=flat-square) ](https://github.com/lodash/lodash)
[ ![alt text](https://img.shields.io/badge/qs-v6.9.0-green.svg?longCache=true&style=flat-square) ](https://github.com/ljharb/qs)

NodeJS용 아임포트 REST API Client 입니다.

## 설치하기
```
npm install iamport-rest-client-nodejs
```

## 테스트하기
프로젝트를 클론 받은 후 필요한 모듈을 설치합니다. yan example 명령어를 통해 테스트할 타깃, `REST API 키` 그리고 `REST API SECRET 키`를 입력합니다.

```
$ git clone https://github.com/SoleeChoi/iamport-rest-client-nodejs.git
$ cd ./iamport-rest-client-nodejs
$ npm install
$ yarn example [테스트 타깃] [apiKey] [apiSecret]
```

테스트 타깃의 종류는 아래와 같습니다.

| 타깃 | 내용 |
| --------- | -------------------------- |
| payments | 일반결제 테스트 |
| prepare | 결제 예정 금액 등록 및 조회 테스트 |
| escrows | 에스크로 결제 테스트 |
| vbanks | 가상계좌 테스트 |
| naverpay | 네이버페이 주문형 테스트 |

`REST API 키``REST API SECRET 키`는 아임포트 관리자페이지 > 시스템 설정 > 내정보에서 확인하실 수 있습니다. 자세한 정보는 [가맹점 정보 확인하기](https://docs.iamport.kr/implementation/account-info)를 참고해주세요.

테스트 예시는 아래와 같습니다.

```
$ npm install
$ yarn example payments imp_apikey ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f
```
28 changes: 28 additions & 0 deletions example/enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {
Sorting,
Status,
DeliveryMethod,
DeliveryCompany,
ReturnReason,
CancelReason,
} = require('../lib/enum');

module.exports = () => {
console.log(Sorting.getKeys());
console.log(Sorting.getValue('updated'));

console.log(Status.getKeys());
console.log(Status.getValue('paid'));

console.log(DeliveryMethod.getKeys());
console.log(DeliveryMethod.getValue('RETURN_DESIGNATED'));

console.log(DeliveryCompany.getKeys());
console.log(DeliveryCompany.getValue('CHUNIL'));

console.log(ReturnReason.getKeys());
console.log(ReturnReason.getValue('INTENT_CHANGED'));

console.log(CancelReason.getKeys());
console.log(CancelReason.getValue('PRODUCT_UNSATISFIED'));
};
44 changes: 44 additions & 0 deletions example/escrows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { Escrows } = require('../lib/request');

/* 배송정보 등록 */
const logis = {
company: 'EPOST',
invoice: '1110301285808',
sent_at: 1567468800,
};
const sender = {
name: '시옷',
tel: '1670-5176',
addr: '신사동 661-16',
postcode: '06018',
};
const receiver = {
name: '시옷',
tel: '1670-5176',
addr: '신사동 661-16',
postcode: '06018',
};

module.exports = async (iamport) => {
/* 배송정보 등록 */
const postEscrows = Escrows.post({
imp_uid: 'imp_448280090638',
logis,
sender,
receiver,
});
await postEscrows.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));

/* 배송정보 수정 */
const putEscrwos = Escrows.put({
imp_uid: 'imp_448280090638',
logis,
sender,
receiver,
});
await putEscrwos.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));
};
27 changes: 27 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Iamport = require('../lib/Iamport');

const [, , target, apiKey, apiSecret] = process.argv;

const iamport = new Iamport({
apiKey: apiKey || 'imp_apikey',
apiSecret: apiSecret || 'ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f',
});

const TESTS = {
enum: require('./enum'),
escrows: require('./escrows'),
naverpay: require('./naverpay'),
payments: require('./payments'),
prepare: require('./prepare'),
vbanks: require('./vbanks'),
};

if (target) {
if (Object.keys(TESTS).indexOf(target) === -1) {
console.log('테스트 타깃이 올바르지 않습니다.');
} else {
TESTS[target](iamport);
}
} else {
console.log('테스트할 타깃을 입력해주세요. 예) yarn example payments');
}
49 changes: 49 additions & 0 deletions example/naverpay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { NaverPay } = require('../lib/request');

module.exports = async (iamport) => {
/* 결제취소 */
const cancelParams = NaverPay.cancel({
imp_uid: 'imp_474085849392',
product_order_id: ['haha', '2019101461256650'],
reason: 'SOLD_OUT',
});
await cancelParams.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));

/* 발주처리 */
const placeParams = NaverPay.place({
imp_uid: 'imp_474085849392',
product_order_id: ['haha', '2019101461256650'],
});
await placeParams.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));

/* 반품요청 */
const requestReturnParams = NaverPay.requestReturn({
imp_uid: 'imp_909917793094',
product_order_id: ['haha', '2019101460921650'],
reason: 'INTENT_CHANGED ',
delivery_method: 'RETURN_INDIVIDUAL',
});
await requestReturnParams.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));

/* 주문번호 조회 */
const productOrderIdParams = NaverPay.getProductOrderId({
imp_uid: 'imp_829275950993',
});
await productOrderIdParams.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));

/* 주문내역 상세조회 */
const orderDetailParams = NaverPay.getOrderDetail({
product_order_id: '2019101080337810',
});
await orderDetailParams.request(iamport)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));
};
88 changes: 88 additions & 0 deletions example/payments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const _ = require('lodash');
const IamportIterator = require('../lib/IamportIterator');
const { Payments } = require('../lib/request');

module.exports = async (iamport) => {
/* 결제내역 조회 > 아임포트 번호 */
const impUidsParams = Payments.getByImpUids({
imp_uid: ['imp_829275950993', 'imp_202176503865', 'helelelel'],
});
await impUidsParams.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));

/* 결제내역 조회 > 주문 번호 */
const merchantUidsParams = Payments.getByMerchantUids({
merchant_uid: 'nobody_1570682275823',
status: 'cancelled',
sorting: '-updated',
page: 1,
});
await merchantUidsParams.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));


/* 결제내역 조회 > 결제 상태 */
const paymentByStatus = Payments.getByStatus({
status: 'ready',
sorting: '-updated',
page: 1,
limit: 20,
});
let result = [];
await paymentByStatus.request(iamport)
.then(async response => {
result = _.concat(result, response.data.response.list);

const iterator = new IamportIterator(paymentByStatus);
while(iterator.hasNext()) {
await iterator.next(iamport)
.then(response => {
result = _.concat(result, response.data.response.list);
})
.catch(error => Promise.reject(error));
}
console.log('response: ', response.data.code);
})
.catch(error => console.log('error: ', error.response.data));

/* 결제정보 조회 > 아임포트 번호 */
const paymentByImpUid = Payments.getByImpUid({
imp_uid: 'imp_829275950993',
});
await paymentByImpUid.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));


/* 결제정보 조회 > 주문번호 */
const paymentByMerchantUid = Payments.getByMerchantUid({
merchant_uid: 'nobody_1570682275823',
status: 'cancelled',
sorting: '-updated',
});
await paymentByMerchantUid.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));

/* 결제금액 조회 > 아임포트 번호 */
const amountByImpUid = Payments.getAmountByImpUid({
imp_uid: 'imp_448280090638',
});
await amountByImpUid.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));

/* 결제취소 */
const paymentCancel = Payments.postCancel({
imp_uid: 'imp_448280090638',
amount: 0,
tax_free: 0,
checksum: 1000,
reason: 'test by deedee',
});
await paymentCancel.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));
};
20 changes: 20 additions & 0 deletions example/prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { Prepare } = require('../lib/request');

module.exports = async (iamport) => {
/* 결제 예정 금액 등록 */
const postAmount = Prepare.post({
merchant_uid: 'mid_1448280088556',
amount: 1000,
});
await iamport.request(postAmount)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));

/* 결제 예정 금액 조회 */
const getAmount = Prepare.get({
merchant_uid: 'mid_1448280088556',
});
await iamport.request(getAmount)
.then(response => console.log('response: ', response.data))
.catch(error => console.log('error: ', error.response.data));
};
42 changes: 42 additions & 0 deletions example/vbanks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { Vbanks } = require('../lib/request');

module.exports = async (iamport) => {
/* 가상계좌 생성 */
const postVbank = Vbanks.post({
merchant_uid: `mid_1567151116054`,
amount: 1000,
vbank_code: '023',
vbank_due: 1576108800,
vbank_holder: '홍길동',
});
await postVbank.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));

/* 가상계좌 수정 */
const putVbank = Vbanks.put({
imp_uid: `imp_448280090638`,
amount: 1000,
vbank_due: 1576108800,
});
await putVbank.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));

/* 가상계좌 삭제 */
const deleteVbank = Vbanks.delete({
imp_uid: `imp_448280090638`,
});
await deleteVbank.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));

/* 가상계좌 예금주 조회 */
const getVbankHolder = Vbanks.getHolder({
bank_code: '023',
bank_num: '43220240353',
});
await getVbankHolder.request(iamport)
.then(response => console.log('response: ', response.data.code))
.catch(error => console.log('error: ', error.response.data));
};
35 changes: 35 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE'

export interface Headers {
Authorization: string,
}

export interface Spec {
url: string,
method?: Method, // default: GET
data?: object,
params?: object,
responseType?: string, // default: Item
responseClass: any,
}

export interface Config {
url: string,
method: Method,
headers: Headers,
data: object,
params: object,
}

export interface AxiosResponse {
response: {
data: IamportData,
},
}

export interface IamportData {
code: number,
message?: string,
response?: any,
}

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/Iamport');
Loading

0 comments on commit b8d4623

Please sign in to comment.