Midtrans / midtrans-nodejs-client

Official Midtrans Payment API Client for Node JS | https://midtrans.com
MIT License
181 stars 57 forks source link

vesion 1.4 .* TypeError: midtrans_client__WEBPACK_IMPORTED_MODULE_0___default(...).Snap is not a constructor #75

Open Novianto778 opened 1 month ago

Novianto778 commented 1 month ago

There is a bug when using midtrans-client in version 1.4 above TypeError: midtrans_clientWEBPACK_IMPORTED_MODULE0default(...).Snap is not a constructor

env: Next.js 14.2.5 (app router) midtrans-client 1.4.0 and 1.4.1

code:

import Midtrans from "midtrans-client";

let snap = new Midtrans.Snap({
  isProduction: false,
  serverKey: process.env.NEXT_PUBLIC_MIDTRANS_SERVER_KEY,
  clientKey: process.env.NEXT_PUBLIC_MIDTRANS_CLIENT_KEY,
});

export async function POST(request: Request) {
  //   const body = await request.json();

  let parameter = {
    transaction_details: {
      order_id: "YOUR-ORDERID-123456",
      gross_amount: 10000,
    },

    customer_details: {
      first_name: "budi",
      last_name: "pratama",
      email: "budi.pra@example.com",
      phone: "08111222333",
    },
  };

  const res = await snap.createTransaction(parameter);

  return new Response(JSON.stringify(res), {
    headers: { "Content-Type": "application/json" },
  });
}

image

Solution: downgrade to 1.3.* version

dwicaksono commented 1 month ago

The downgrade version did not work for me.

✓ Compiled /api/payment-gateway in 1216ms (1876 modules)
 ⨯ app/api/payment-gateway/route.ts (12:14) @ eval
 ⨯ TypeError: midtrans_client__WEBPACK_IMPORTED_MODULE_1___default(...).Snap is not a constructor
    at eval (webpack-internal:///(rsc)/./app/api/payment-gateway/route.ts:23:14)
    at (rsc)/./app/api/payment-gateway/route.ts (/Users/dwicaksono/Documents/hirefazz/.next/server/app/api/payment-gateway/route.js:272:1)
    at __webpack_require__ (/Users/dwicaksono/Documents/hirefazz/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fpayment-gateway%2Froute&page=%2Fapi%2Fpayment-gateway%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fpayment-gateway%2Froute.ts&appDir=%2FUsers%2Fdwicaksono%2FDocuments%2Fhirefazz%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Fdwicaksono%2FDocuments%2Fhirefazz&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:15:127)
    at (rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fpayment-gateway%2Froute&page=%2Fapi%2Fpayment-gateway%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fpayment-gateway%2Froute.ts&appDir=%2FUsers%2Fdwicaksono%2FDocuments%2Fhirefazz%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Fdwicaksono%2FDocuments%2Fhirefazz&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (/Users/dwicaksono/Documents/hirefazz/.next/server/app/api/payment-gateway/route.js:262:1)
    at __webpack_require__ (/Users/dwicaksono/Documents/hirefazz/.next/server/webpack-runtime.js:33:43)
    at __webpack_exec__ (/Users/dwicaksono/Documents/hirefazz/.next/server/app/api/payment-gateway/route.js:412:39)
    at /Users/dwicaksono/Documents/hirefazz/.next/server/app/api/payment-gateway/route.js:413:1030
    at __webpack_require__.X (/Users/dwicaksono/Documents/hirefazz/.next/server/webpack-runtime.js:168:21)
    at /Users/dwicaksono/Documents/hirefazz/.next/server/app/api/payment-gateway/route.js:413:47
    at Object.<anonymous> (/Users/dwicaksono/Documents/hirefazz/.next/server/app/api/payment-gateway/route.js:416:3)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
    at mod.require (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/require-hook.js:65:28)
    at require (node:internal/modules/helpers:179:18)
    at requirePage (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/require.js:109:84)
    at /Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/load-components.js:98:84
    at async loadComponentsImpl (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/load-components.js:98:26)
    at async DevServer.findPageComponentsImpl (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/next-server.js:709:36)
    at async DevServer.findPageComponents (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/dev/next-dev-server.js:577:20)
    at async DevServer.renderPageComponent (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/base-server.js:1894:24)
    at async DevServer.renderToResponseImpl (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/base-server.js:1946:32)
    at async DevServer.pipeImpl (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/base-server.js:921:25)
    at async NextNodeServer.handleCatchallRenderRequest (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/base-server.js:817:17)
    at async /Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/lib/router-server.js:173:21)
    at async handleRequest (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/lib/router-server.js:350:24)
    at async requestHandlerImpl (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/lib/router-server.js:374:13)
    at async Server.requestListener (/Users/dwicaksono/Documents/hirefazz/node_modules/next/dist/server/lib/start-server.js:141:13) {
  page: '/api/payment-gateway'
}
  10 | import { clientKey, isProduction, midtransServerKey } from '@/lib/urls';
  11 |
> 12 | const snap = new Midtrans.Snap({
     |              ^
  13 |   isProduction,
  14 |   serverKey: midtransServerKey,
  15 |   clientKey,
 ○ Compiling /_error ...
 ✓ Compiled /_error in 1680ms (3035 modules)
uziwuzzy commented 1 month ago

sorry for the error, please use other version except v1.4, you can downgrade to use v1.3 for now https://www.npmjs.com/package/midtrans-client?activeTab=versions. Will fix the release nextweek.