Koras02
Koras02코딩웹
Koras02
전체 방문자
오늘
어제
  • 분류 전체보기 (299)
    • 백엔드 (59)
      • nestjs (2)
      • Ruby (3)
      • PostgresQL (11)
      • MySQL (5)
      • Haskell (7)
      • Koa (3)
      • Java (3)
      • Python (5)
      • Rust (5)
      • MongoDB (2)
      • PHP (3)
      • Spring Boot (1)
      • redis (5)
      • deno (2)
    • 웹서버 (3)
      • nginx (1)
      • Apache (2)
      • Google Web Server (0)
    • 모바일개발 (5)
      • Figma (0)
      • React Native (2)
      • swift (0)
      • Flutter (3)
      • Kotlin (0)
    • 프론트 엔드 (158)
      • HTML (34)
      • CSS (7)
      • Javascript (35)
      • Angular (0)
      • Typescript (2)
      • React (58)
      • Vue (2)
      • GIT (6)
      • GraphQL (1)
      • Doker (4)
      • Go (8)
      • svelte (1)
      • gatsby (0)
    • etc. (47)
      • Notion (0)
      • TIL (24)
      • Algorithm (17)
      • Algorithm 개념 정리 (2)
      • Wiki (3)
      • Official document (1)
    • 웹개념 (12)
    • 변수정리 (1)
    • VSCode (2)
    • 포트폴리오 분석 (2)
      • React (2)
    • os (5)
      • 윈도우 (4)
      • Mac (0)
      • 가상머신 (0)
      • linux (1)
    • 응용프로그램언어 (2)
      • C (2)
      • C++ (0)
      • C# (0)
    • 블로그 운영관련 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
글쓰기

공지사항

  • [공지사항] 개발 이슈나 공식문서 업데이트 업로드 예정입니⋯

인기 글

태그

  • javascript
  • CSS
  • html5
  • React
  • go
  • Java
  • 프로그래머스
  • koa
  • redis
  • Rust
  • mysql
  • Haskell
  • 데이터 타입
  • 알고리즘
  • 문자열
  • 하스켈
  • html
  • Flutter
  • Til
  • PostgreSQL

티스토리

최근 댓글

최근 글

250x250
hELLO · Designed By 정상우.
Koras02

Koras02코딩웹

[React] 간단한 결제시스템 만들어보기
프론트 엔드/React

[React] 간단한 결제시스템 만들어보기

2022. 2. 23. 00:01
728x90

프로젝트 개발시 간단하게 결제시스템을 이용해고 싶다면 결제를 대행해주는 담당사의 API를 가지고 만드실 수 있습니다. 이런 간단한 Pay를 추가하는 방법을 알아보겠습니다.

 

React에서 App.js, Home/index.js 와 Payment/index.js를 다음과 같이 작성해보세요.


App.js

import React from 'react';
import { Switch } from 'react-router-dom';
import { Route } from 'react-router-dom';
import Home from './Home';
import Payment from './Payment';

function App() {
    return (
        <Switch>
            <Route exact path="/" component={Home} />
            <Route path="/payment" component={Payment} />
        </Switch>
    );
}

export default App;

Home/index.js

import React from 'react';
import * as S from './HomeStyle';
import girl from '../Image/minion.png';
import chopa from '../Image/chopa.png';
import { useHistory, withRouter } from 'react-router-dom';

const Home = () => {
    const history = useHistory();

    const handleChopa = () => {
        history.push('/payment');
    };
    const handleMinion = () => {
        history.push('/payment');
    };

    return (
        <S.Wrapper>
            <div>
                <h2> Voice Story AI</h2>
                <h4>음성을 보여주는 음성변환 웹서비스 입니다.</h4>
                <h4>아래 캐릭터 버튼을 눌러 테스트 또는 결제를 진행해주세요</h4>
            </div>
            <div></div>
            <S.ButtonContainer>
                <S.Button onClick={handleMinion}>
                    <img src={girl} width="60px" height="60px" alt="girl" />
                    미니언즈
                </S.Button>
                <S.Button onClick={handleChopa}>
                    <img src={chopa} width="60px" height="60px" alt="girl" />
                    쵸파
                </S.Button>
            </S.ButtonContainer>
        </S.Wrapper>
    );
};

export default withRouter(Home);

Payment/index.js

import React, { useEffect } from 'react';

const Payment = (effect, deps) => {
    useEffect(() => {
        const jquery = document.createElement('script');
        jquery.src = 'https://code.jquery.com/jquery-1.12.4.min.js';
        const iamport = document.createElement('script');
        iamport.src = 'https://cdn.iamport.kr/js/iamport.payment-1.1.7.js';
        document.head.appendChild(jquery);
        document.head.appendChild(iamport);
        return () => {
            document.head.removeChild(jquery);
            document.head.removeChild(iamport);
        };
    }, []);

    const onClickPayment = () => {
        const { IMP } = window;
        IMP.init('imp77220765');

        const data = {
            pg: 'html5_inicis',
            pay_method: 'card',
            merchant_uid: `mid_${new Date().getTime()}`,
            name: '결제 테스트',
            amount: '1000',
            custom_data: {
                name: '부가정보',
                desc: '세부 부가정보',
            },
            buyer_name: '홍길동',
            buyer_tel: '01012345678',
            buyer_email: '14279625@gmail.com',
            buyer_addr: '구천면로 000-00',
            buyer_postalcode: '01234',
        };

        IMP.request_pay(data, callback);
    };

    const callback = response => {
        const { success, error_msg, imp_uid, merchant_uid, pay_method, paid_amount, status } = response;

        if (success) {
            alert('결제 성공');
        } else {
            alert(`결제 실패: ${error_msg}`);
        }
    };

    return <>{onClickPayment()};</>;
};
export default Payment;

위처럼 작성하면 다음과 같은 결과를 얻을 수 있습니다.

참고 자료

 

결제 시스템 만들기

간단한 결제 시스템 창을 호출하는 방법을 배워보겠습니다.

velog.io

 

'프론트 엔드 > React' 카테고리의 다른 글

[React] StroyBook 도입기  (0) 2022.03.01
[React] React 상태관리에 대해 알아보자 -context api, redux, mobx, swr  (0) 2022.02.09
[React] useRef 란?  (0) 2022.02.07
    '프론트 엔드/React' 카테고리의 다른 글
    • [React]Supabase를 사용한 Todo앱 빌드하기
    • [React] StroyBook 도입기
    • [React] React 상태관리에 대해 알아보자 -context api, redux, mobx, swr
    • [React] useRef 란?
    Koras02
    Koras02
    현재 사용중인 언어 - next-js,react,vue, typescript

    티스토리툴바