Payment/iamport

์žฅ์—ฌ์ง„ยท2022๋…„ 4์›” 17์ผ
0
post-thumbnail

iamport๐Ÿ’ธ

  • ๊ฒฐ์ œ ์†”๋ฃจ์…˜
  • ๊ฐœ๋ฐœ ํ™˜๊ฒฝ๊ณผ ์ƒ๊ด€์—†์ด ์›ํ•˜๋Š” PG์‚ฌ์™€์˜ ๊ฒฐ์ œ์‹œ์Šคํ…œ์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ๊ฒฐ์ œ API
  • iamport๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ณต์žกํ•œ ๊ฒฐ์ œํ™˜๊ฒฝ์„ ์ง์ ‘์ ์œผ๋กœ ๊ตฌํ˜„ํ•  ํ•„์š”์—†์Œ!!

[imp_uid]

  • iamport ์‚ฌ์šฉํ•˜๊ธฐ
    1) iamport ์ดˆ๊ธฐ ์„ค์ •
    iamport์—์„œ ๋กœ๊ทธ์ธ ํ›„ ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ์„ค์ • ํ•„์š”
    ์‹œ์Šคํ…œ ์„ค์ • - PG์„ค์ • (์ผ๋ฐ˜๊ฒฐ์ œ ๋ฐ ์ •๊ธฐ๊ฒฐ์ œ) => ๊ฒฐ์ œ์— ํ•„์š”ํ•œ PG์‚ฌ ์„ค์ •
    ์ด๋•Œ ์‹œ๋ฒ”์šฉ์ด๋ผ๋ฉด ํ…Œ์ŠคํŠธ ๋ชจ๋“œ ์ฒดํฌ ํ•„์ˆ˜!!
  • ๊ฒฐ์ œ ์ฝ”๋“œ ๊ตฌํ˜„
<!-- ํ”„๋ก ํŠธ ์—”๋“œ --> 

  <script
      type="text/javascript"
      src="https://code.jquery.com/jquery-1.12.4.min.js"
    ></script>
    <script
      type="text/javascript"
      src="https://cdn.iamport.kr/js/iamport.payment-1.2.0.js"
    ></script>
    <script
      type="text/javascript"
      src="https://unpkg.com/axios/dist/axios.min.js"
    ></script>
    <script>
      function mypayment() {
        const myAmount = Number(document.getElementById("amount").value);
        const merchant_uid = document.getElementById("merchant_uid").value;

        const IMP = window.IMP; // ์ƒ๋žต ๊ฐ€๋Šฅ
        IMP.init("๊ฐ€๋งน์  ์‹๋ณ„ ์ฝ”๋“œ"); // Example: imp00000000
        IMP.request_pay(
          {
            // param
            pg: "html5_inicis",
            pay_method: "card",
            //merchant_uid: merchant_uid, //๊ตฌ๋งคํ•œ carId
            name: "์ž๋™์ฐจ",
            amount: myAmount,
            buyer_email: "gildong@gmail.com",
            buyer_name: "ํ™๊ธธ๋™",
            buyer_tel: "010-4242-4242",
            buyer_addr: "์„œ์šธํŠน๋ณ„์‹œ ๊ฐ•๋‚จ๊ตฌ ์‹ ์‚ฌ๋™",
            buyer_postcode: "01181",
            m_redirect_url: "", // ๋ชจ๋ฐ”์ผ ๊ฒฐ์ œํ›„ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋  ์ฃผ์†Œ!!
          },
          async (rsp) => {
            // callback
            if (rsp.success) {

              console.log(data);
            }
            // );
            else {
              // ๊ฒฐ์ œ ์‹คํŒจ์‹œ
              console.log("์‹คํŒจ");
            }
          }
        );
      }
    </script>
//ํ™˜๋ถˆ ๊ตฌํ˜„ ์ฝ”๋“œ
  // ์•„์ž„ํฌํŠธ์— ๊ฒฐ์ œ ์ทจ์†Œ ์š”์ฒญ
  getRefundData = async ({
    impUid,
    refundAmount,
    reason,
    access_token,
    sum,
  }) => {
    const refundData = await axios({
      url: 'https://api.iamport.kr/payments/cancel',
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${access_token}`, // ์•„์ž„ํฌํŠธ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐœ๊ธ‰๋ฐ›์€ ์—‘์„ธ์Šค ํ† ํฐ
      },
      data: {
        reason: reason, // ๊ฐ€๋งน์  ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํ™˜๋ถˆ์‚ฌ์œ 
        imp_uid: impUid, // imp_uid๋ฅผ ํ™˜๋ถˆ `unique key`๋กœ ์ž…๋ ฅ
        amount: Math.abs(refundAmount), // ๊ฐ€๋งน์  ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํ™˜๋ถˆ๊ธˆ์•ก
        checksum: sum, // [๊ถŒ์žฅ] ํ™˜๋ถˆ ๊ฐ€๋Šฅ ๊ธˆ์•ก ์ž…๋ ฅ
      },
    });

    const { response } = refundData.data;

** ๊ฒฐ์ œ์™€ ํ™˜๋ถˆ๊ณผ์ •์€ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๊ธฐ๋•Œ๋ฌธ์— ๊ฒ€์ฆํ•ด์•ผํ•  ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ๊ต‰์žฅํžˆ ๋ณต์žกํ•˜๊ณ  ์ฝ”๋“œ ์ž‘์„ฑ ์‹œ ์œ„์˜ ๊ธฐ๋ณธ์ ์ธ ์ฝ”๋“œ ์™ธ์—๋„ ์‹ ๊ฒฝ์จ์•ผํ•  ๋ถ€๋ถ„์ด ๋งŽ์Œ!!!


๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ˜น์‹œ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Š

0๊ฐœ์˜ ๋Œ“๊ธ€