티스토리 뷰

카페24 쇼핑몰 솔루션에서 이름이나 휴대폰 번호를 통해 고객 ID를 가져오는 로직을 추가하려면, 카페24 API에서 고객 정보를 조회할 수 있는 기능을 활용해야 합니다. 다음은 구현 가이드입니다.


1. 고객 정보 조회 API 확인

카페24 API 문서에 따르면 고객 정보를 검색하려면 GET /v2/admin/customers 엔드포인트를 사용합니다. 이 API는 이름, 이메일, 휴대폰 번호 등의 파라미터를 통해 고객 목록을 검색할 수 있습니다.

  • API 엔드포인트:
    GET /v2/admin/customers
  • 주요 쿼리 파라미터:
    • name: 고객 이름
    • cellphone: 휴대폰 번호
    • 기타: 이메일 등

2. 서비스 코드 작성

services/cafe24Service.js에 고객 정보를 조회하는 함수 추가:

const axios = require('axios');
const { cafe24 } = require('../config/config');

// 이름 또는 휴대폰 번호로 고객 조회
async function getCustomerByNameOrPhone(name, phone, token) {
  const url = `${cafe24.baseUrl}/v2/admin/customers`;
  const headers = { Authorization: `Bearer ${token}` };

  // 파라미터 구성
  const params = {};
  if (name) params.name = name;
  if (phone) params.cellphone = phone;

  try {
    const response = await axios.get(url, { headers, params });
    return response.data.customers; // 고객 목록 반환
  } catch (error) {
    throw new Error(`Failed to fetch customer: ${error.message}`);
  }
}

module.exports = { getCustomerByNameOrPhone };​

3. 컨트롤러 추가

controllers/pointsController.js에 고객 검색 로직 추가:

const { getCustomerByNameOrPhone } = require('../services/cafe24Service');

async function findCustomer(req, res) {
  const { name, phone } = req.query; // 요청에서 이름과 휴대폰 번호 가져오기
  try {
    const customers = await getCustomerByNameOrPhone(name, phone, req.accessToken);
    if (customers.length === 0) {
      return res.status(404).json({ message: 'No customer found' });
    }
    res.status(200).json(customers);
  } catch (error) {
    res.status(500).json({ error: `Failed to find customer: ${error.message}` });
  }
}

module.exports = { findCustomer };
 

4. 라우트 추가

routes/points.js에 고객 조회 라우트 추가:

const express = require('express');
const { getAccessToken } = require('../middleware/auth');
const { findCustomer } = require('../controllers/pointsController');

const router = express.Router();

// 이름 또는 휴대폰 번호로 고객 조회
router.get('/search', getAccessToken, findCustomer);

module.exports = router;
 

5. 앱에 통합

app.js에 새 라우트 통합:

const express = require('express');
const pointsRoutes = require('./routes/points');

const app = express();
app.use(express.json());

app.use('/points', pointsRoutes);

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
 

6. 요청 테스트

  • 이름으로 검색:
    GET /points/search?name=홍길동
  • 휴대폰 번호로 검색:
    GET /points/search?phone=01012345678
  • 결과 예시:
 
[
  {
    "customer_id": "12345",
    "name": "홍길동",
    "cellphone": "01012345678",
    "email": "hong@example.com"
  }
]
 

7. 주의사항

  • 카페24 API 사용 시, API rate limit를 확인하고 이에 맞게 요청을 최적화하세요.
  • 고객 정보는 민감한 데이터이므로 API 호출 및 응답 처리를 보안에 유의하여 구현하세요.
  • phone 필드의 형식을 카페24와 동일하게 유지하세요 (예: +821012345678 형태).

위 방법을 통해 이름이나 휴대폰 번호로 고객 ID를 가져올 수 있습니다!

댓글